Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ***************************** Subroutines ************************************/
- /**
- * Creating channel event and subscribing chat service
- */
- function subscribeChatMessage(objectInstanceID: string, objectInstanceValue: string) {
- return eventChannel<Map<string, Enote>>((emmiter) => {
- const unsubscribe = enoteService.subscribeEnotesById(objectInstanceID, objectInstanceValue,
- (enotes: Map<string, Enote>) => {
- emmiter(enotes)
- })
- return () => {
- // unsubscribe()
- }
- })
- }
- /**
- * On database fetch
- */
- function* dbFetchChatMessages(objectInstanceID: string, objectInstanceValue: string) {
- try {
- while (true) {
- const channelSubscription: Channel<Map<string, Enote>> = yield call(subscribeChatMessage, objectInstanceID, objectInstanceValue)
- let messages: Map<string, Enote> = yield take(channelSubscription)
- yield put({ type: EnoteActionType.ENOTE_POLL_MESSAGES, payload: { messages: messages, objectInstanceId: objectInstanceValue} })
- yield delay(4000)
- }
- } catch (e) {
- console.error(e)
- }
- }
- /******************************************************************************/
- /******************************* WATCHERS *************************************/
- /******************************************************************************/
- export default function* enoteSaga() {
- while (true) {
- const pollingAction = yield take(EnoteActionType.DB_SUBSCRIBE_ENOTE_MESSAGE)
- const {objectInstanceID, objectInstanceValue} = pollingAction.payload
- yield race([
- call(dbFetchChatMessages, objectInstanceID, objectInstanceValue),
- take(EnoteActionType.DB_UNSUBSCRIBE_ENOTE_MESSAGE)
- ])
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement