Last active
January 23, 2018 17:41
-
-
Save Andarist/83d1c29d9fd565d91d2c3feffbf8b3b1 to your computer and use it in GitHub Desktop.
types relations problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// { 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