Skip to content

Commit

Permalink
Make it possible to use our own initialize and some other improvements
Browse files Browse the repository at this point in the history
A couple of improvements are introduced here.
- Be able to pin what version of StimulusReflex we are using. The
tradeoff is that we need to maintain a fork that is lightly touched
to allow for that functionality.
- Be able to decouple from how rails expect the identifier would look
like
- The javascript code for actioncable is no longer a dependency.
  • Loading branch information
jonathan-s committed Jul 25, 2021
1 parent 23f5bf4 commit dcb85e2
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 27 deletions.
42 changes: 42 additions & 0 deletions javascript/stimulus-websocket/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import ReconnectingWebSocket from "reconnecting-websocket"
import { Controller } from 'stimulus'
import StimulusReflex from "stimulus_reflex"
import actionCable from 'stimulus_reflex/javascript/transports/action_cable'
import { reflexControllerMethods } from "stimulus_reflex/javascript/reflexes"


// read up on the default options that actioncable has for websockets.

Expand Down Expand Up @@ -106,6 +111,43 @@ class Subscriptions {
}
}

class StimulusReflexController extends Controller {
constructor (...args) {
super(...args)
register(this)
}
}


const register = (controller, options = {}) => {
const channel = 'StimulusReflex::Channel'
controller.StimulusReflex = { ...options, channel }
actionCable.createSubscription(controller)
Object.assign(controller, reflexControllerMethods)
}

const initialize = (application, {
controller = StimulusReflexController,
consumer,
debug,
params,
isolate,
deprecate
} = {}) => {
let options = {consumer, controller, debug, params, isolate, deprecate}
StimulusReflex.initialize(application, options)
}


const Sockpuppet = {
initialize: initialize,
register: register,
}

export {
Sockpuppet
}

export default class WebsocketConsumer {
constructor(url, options = {}) {
this._url = url
Expand Down
25 changes: 7 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,17 @@
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-terser": "^7.0.2",
"stimulus": ">=1.1",
"stimulus_reflex": ">=3.4.1",
"webpack": "^4.43.0",
"webpack-bundle-analyzer": "^3.7.0",
"webpack-cli": "^3.3.11",
"cable_ready": ">= 4.4"
},
"peerDependencies": {
"stimulus": ">= 1.1"
},
"dependencies": {
"reconnecting-websocket": "^4.4.0"
"reconnecting-websocket": "^4.4.0",
"stimulus_reflex": "jonathan-s/stimulus_reflex#sockpuppet"
},
"files": [
"javascript/stimulus-websocket/index.js"
Expand All @@ -54,4 +57,4 @@
"cypress:open": "cypress open",
"cypress:run": "cypress run"
}
}
}
4 changes: 2 additions & 2 deletions sockpuppet/js/sockpuppet.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Application } from 'stimulus'
import StimulusReflex from 'stimulus_reflex'
// because travis had issues with 'sockpuppet-js' we had to do this.
import WebsocketConsumer from '../../javascript/stimulus-websocket/index'
import { Sockpuppet } from '../../javascript/stimulus-websocket/index'

const application = Application.start()
const consumer = new WebsocketConsumer(
`${location.protocol=='https:'?'wss':'ws'}://${window.location.host}/ws/sockpuppet-sync`, {debug: false}
)

StimulusReflex.initialize(application, { consumer })
Sockpuppet.initialize(application, { consumer })
4 changes: 2 additions & 2 deletions sockpuppet/static/sockpuppet/sockpuppet.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sockpuppet/static/sockpuppet/sockpuppet.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion tests/example/javascript/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import StimulusReflex from 'stimulus_reflex'
import CableReady from 'cable_ready'
// because travis had issues with 'sockpuppet-js' we had to do this.
import WebsocketConsumer from '../../../javascript/stimulus-websocket/index'
import { Sockpuppet } from '../../../javascript/stimulus-websocket/index'
import ExampleController from './controllers/example_controller'

const application = Application.start()
Expand All @@ -16,4 +17,4 @@ consumer.subscriptions.create('progress', {
}
})
application.register("example", ExampleController)
StimulusReflex.initialize(application, { consumer, debug: true})
Sockpuppet.initialize(application, { consumer, debug: true})

0 comments on commit dcb85e2

Please sign in to comment.