Skip to content

Commit f7bfc23

Browse files
christianalfonigitbook-bot
authored andcommitted
GitBook: [master] one page modified
1 parent 4eafc5c commit f7bfc23

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

core/running-side-effects.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ It can be a good idea to not allow your side effects to initialize when they are
9191
```typescript
9292
import * as firebase from 'firebase'
9393

94+
// We use IIFE to hide the private "app" variable
9495
export const api = (() => {
9596
let app
9697

@@ -132,6 +133,35 @@ export const onInitialize = async ({ effects }) => {
132133
Typically you explicitly communicate with effects from actions, by calling methods. But sometimes you need effects to know about the state of the application, or maybe some internal state in the effect should be exposed on your application state. Again we can take advantage of an **initialize** method on the effect:
133134

134135
{% tabs %}
136+
{% tab title="overmind/effects.js" %}
137+
```javascript
138+
// We use an IIFE to isolate some variables
139+
export const socket = (() => {
140+
_options
141+
_ws
142+
return {
143+
initialize(options) {
144+
_options = options
145+
_ws = new WebSocket('ws://...')
146+
_ws.onclose = () => options.onStatusChange('close')
147+
_ws.onopen = () => options.onStatusChange('open')
148+
_ws.addEventListener(
149+
'message',
150+
(event) => options.onMessage(event.data)
151+
)
152+
},
153+
send(type, data) {
154+
_ws.postMessage({
155+
type,
156+
data,
157+
token: _options.getToken()
158+
})
159+
}
160+
}
161+
})()
162+
```
163+
{% endtab %}
164+
135165
{% tab title="overmind/onInitialize.js" %}
136166
```typescript
137167
export const onInitialize = async ({ state, effects, actions }) => {

0 commit comments

Comments
 (0)