Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. ***************************** Subroutines ************************************/
  2. /**
  3. * Creating channel event and subscribing chat service
  4. */
  5. function subscribeChatMessage(objectInstanceID: string, objectInstanceValue: string) {
  6. return eventChannel<Map<string, Enote>>((emmiter) => {
  7. const unsubscribe = enoteService.subscribeEnotesById(objectInstanceID, objectInstanceValue,
  8. (enotes: Map<string, Enote>) => {
  9. emmiter(enotes)
  10. })
  11. return () => {
  12. // unsubscribe()
  13. }
  14. })
  15. }
  16.  
  17. /**
  18. * On database fetch
  19. */
  20. function* dbFetchChatMessages(objectInstanceID: string, objectInstanceValue: string) {
  21. try {
  22. while (true) {
  23. const channelSubscription: Channel<Map<string, Enote>> = yield call(subscribeChatMessage, objectInstanceID, objectInstanceValue)
  24. let messages: Map<string, Enote> = yield take(channelSubscription)
  25. yield put({ type: EnoteActionType.ENOTE_POLL_MESSAGES, payload: { messages: messages, objectInstanceId: objectInstanceValue} })
  26. yield delay(4000)
  27. }
  28. } catch (e) {
  29. console.error(e)
  30. }
  31. }
  32.  
  33. /******************************************************************************/
  34. /******************************* WATCHERS *************************************/
  35. /******************************************************************************/
  36. export default function* enoteSaga() {
  37. while (true) {
  38. const pollingAction = yield take(EnoteActionType.DB_SUBSCRIBE_ENOTE_MESSAGE)
  39. const {objectInstanceID, objectInstanceValue} = pollingAction.payload
  40. yield race([
  41. call(dbFetchChatMessages, objectInstanceID, objectInstanceValue),
  42. take(EnoteActionType.DB_UNSUBSCRIBE_ENOTE_MESSAGE)
  43. ])
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement