Skip to content

Commit dcb85e2

Browse files
committed
Make it possible to use our own initialize and some other improvements
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.
1 parent 23f5bf4 commit dcb85e2

File tree

7 files changed

+62
-27
lines changed

7 files changed

+62
-27
lines changed

javascript/stimulus-websocket/index.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import ReconnectingWebSocket from "reconnecting-websocket"
2+
import { Controller } from 'stimulus'
3+
import StimulusReflex from "stimulus_reflex"
4+
import actionCable from 'stimulus_reflex/javascript/transports/action_cable'
5+
import { reflexControllerMethods } from "stimulus_reflex/javascript/reflexes"
6+
27

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

@@ -106,6 +111,43 @@ class Subscriptions {
106111
}
107112
}
108113

114+
class StimulusReflexController extends Controller {
115+
constructor (...args) {
116+
super(...args)
117+
register(this)
118+
}
119+
}
120+
121+
122+
const register = (controller, options = {}) => {
123+
const channel = 'StimulusReflex::Channel'
124+
controller.StimulusReflex = { ...options, channel }
125+
actionCable.createSubscription(controller)
126+
Object.assign(controller, reflexControllerMethods)
127+
}
128+
129+
const initialize = (application, {
130+
controller = StimulusReflexController,
131+
consumer,
132+
debug,
133+
params,
134+
isolate,
135+
deprecate
136+
} = {}) => {
137+
let options = {consumer, controller, debug, params, isolate, deprecate}
138+
StimulusReflex.initialize(application, options)
139+
}
140+
141+
142+
const Sockpuppet = {
143+
initialize: initialize,
144+
register: register,
145+
}
146+
147+
export {
148+
Sockpuppet
149+
}
150+
109151
export default class WebsocketConsumer {
110152
constructor(url, options = {}) {
111153
this._url = url

package-lock.json

Lines changed: 7 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,17 @@
3232
"rollup-plugin-babel": "^4.4.0",
3333
"rollup-plugin-terser": "^7.0.2",
3434
"stimulus": ">=1.1",
35-
"stimulus_reflex": ">=3.4.1",
3635
"webpack": "^4.43.0",
3736
"webpack-bundle-analyzer": "^3.7.0",
3837
"webpack-cli": "^3.3.11",
3938
"cable_ready": ">= 4.4"
4039
},
40+
"peerDependencies": {
41+
"stimulus": ">= 1.1"
42+
},
4143
"dependencies": {
42-
"reconnecting-websocket": "^4.4.0"
44+
"reconnecting-websocket": "^4.4.0",
45+
"stimulus_reflex": "jonathan-s/stimulus_reflex#sockpuppet"
4346
},
4447
"files": [
4548
"javascript/stimulus-websocket/index.js"
@@ -54,4 +57,4 @@
5457
"cypress:open": "cypress open",
5558
"cypress:run": "cypress run"
5659
}
57-
}
60+
}

sockpuppet/js/sockpuppet.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Application } from 'stimulus'
2-
import StimulusReflex from 'stimulus_reflex'
32
// because travis had issues with 'sockpuppet-js' we had to do this.
43
import WebsocketConsumer from '../../javascript/stimulus-websocket/index'
4+
import { Sockpuppet } from '../../javascript/stimulus-websocket/index'
55

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

11-
StimulusReflex.initialize(application, { consumer })
11+
Sockpuppet.initialize(application, { consumer })

sockpuppet/static/sockpuppet/sockpuppet.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sockpuppet/static/sockpuppet/sockpuppet.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/example/javascript/example.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import StimulusReflex from 'stimulus_reflex'
33
import CableReady from 'cable_ready'
44
// because travis had issues with 'sockpuppet-js' we had to do this.
55
import WebsocketConsumer from '../../../javascript/stimulus-websocket/index'
6+
import { Sockpuppet } from '../../../javascript/stimulus-websocket/index'
67
import ExampleController from './controllers/example_controller'
78

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

0 commit comments

Comments
 (0)