Created
January 14, 2020 12:59
-
-
Save atoko/5d185849693000508f5e6590d586b94c to your computer and use it in GitHub Desktop.
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
function* forwardEventsToReducer(channel) { | |
while (true) { | |
const action = yield take(channel); | |
yield put(action); | |
} | |
} | |
function* watchLogins() { | |
while (true) { | |
let {token} = yield take(AUTH_LOGIN); | |
let socket = io("/", { | |
path: "/mo/v2/online/socket", | |
query: { | |
token | |
} | |
}); | |
const channel = eventChannel(emitter => { | |
socket.on('connect', () => { | |
emitter(presenceConnectedAction(socket)); | |
}); | |
socket.on('error', (error) => { | |
emitter(presenceErrorAction(error)); | |
emitter(END); | |
}); | |
socket.on('disconnect', () => { | |
emitter(presenceDisconnectedAction()); | |
emitter(END); | |
}); | |
socket.on('v1/ticket/ack', (data) => { | |
emitter(presenceTicketAck()); | |
}); | |
socket.on('v1/match/assign', (data) => { | |
emitter(presenceMatchAssign(data.gameId)) | |
}); | |
socket.on('v1/match/error', (error) => { | |
emitter(presenceMatchError(error)) | |
}); | |
return () => { | |
socket.close(); | |
} | |
}); | |
yield race({ | |
eventloop: forwardEventsToReducer(channel), | |
error: take(PRESENCE_ERROR), | |
logout: take(AUTH_LOGOUT), | |
verify: take(AUTH_VERIFY_REQUEST) | |
}); | |
channel.close(); | |
yield put(presenceDisconnectedAction()); | |
} | |
} | |
function* watchTicketPost() { | |
yield takeLatest(PRESENCE_TICKET_REQUEST, function*(action) { | |
let state = yield select(); | |
let {socket} = getPresence(state); | |
if (socket !== null) { | |
socket.binary(false).emit(`v1/ticket/${action.verb.toLowerCase()}`); | |
} | |
}); | |
} | |
export default function* rootPresenceSaga() { | |
yield all([ | |
watchLogins(), | |
watchTicketPost(), | |
]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment