Skip to content

Instantly share code, notes, and snippets.

@Andarist
Last active January 23, 2018 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Andarist/83d1c29d9fd565d91d2c3feffbf8b3b1 to your computer and use it in GitHub Desktop.
Save Andarist/83d1c29d9fd565d91d2c3feffbf8b3b1 to your computer and use it in GitHub Desktop.
types relations problem
// { type: 'A', payload: { timestamp: number } }
// { type: 'B', payload: { user_id: string } }
const parseA = data => ({ lastTimestamp: data * 1000 })
const parseB = data => ({ userId: data })
const serverMessageParser = (msg: SocketMessage): ParsedMessage => {
switch (msg.action) {
case 'A':
return parseA(msg.data)
case 'B':
return parseB(msg.data)
}
}
socket.on('message', (msg: SocketMessage) => {
store.dispatch({
type: 'push_received',
payload: {
action: msg.action,
data: serverMessageParser(msg)
}
})
})
const sideEffectsMiddleware = (action: Action, store: Store) => {
switch (action.type) {
case 'push_received':
emitter.emit(action.payload.action, action.payload.data)
return
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment