Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. import { actionChannel, all, put, race, take } from 'redux-saga/effects';
  2.  
  3. export function createAsyncListener(
  4. listenerId: string,
  5. closeListenerId: string,
  6. hookId: string | string[] = listenerId
  7. ) {
  8. return function*() {
  9. while (true) {
  10. const callbackAction = yield take(listenerId);
  11. if (callbackAction && typeof callbackAction.payload === 'function') {
  12. const channel = yield actionChannel(hookId);
  13. try {
  14. while (true) {
  15. const [result, cancel] = yield race([
  16. take(channel),
  17. take(closeListenerId)
  18. ]);
  19. if (cancel) {
  20. break;
  21. }
  22. callbackAction.payload(result.payload);
  23. }
  24. } catch (e) {
  25. console.log(e);
  26. } finally {
  27. channel.close();
  28. }
  29. }
  30. }
  31. };
  32. }
  33.  
  34. export function createWaitHooksListener(
  35. listenerId: string,
  36. closeListenerId: string,
  37. hookIds: string[]
  38. ) {
  39. return function*() {
  40. while (true) {
  41. const callbackAction = yield take(listenerId);
  42. if (typeof callbackAction.payload === 'function') {
  43. while (true) {
  44. const [results, unsubscribe] = yield race([
  45. all(hookIds.map(_hookId => take(_hookId))),
  46. take(closeListenerId)
  47. ]);
  48. if (unsubscribe) {
  49. break;
  50. }
  51. callbackAction.payload(
  52. results.map(result => {
  53. return result.payload;
  54. })
  55. );
  56. }
  57. }
  58. }
  59. };
  60. }
  61.  
  62. export function createListenerActionCreators<T extends any = any>(
  63. listenerId: string,
  64. closeListenerId: string,
  65. hookId: string = listenerId
  66. ) {
  67. const onListener = (callback: (result?: T) => void) => ({
  68. type: listenerId,
  69. payload: callback
  70. });
  71. const closeListener = () => ({
  72. type: closeListenerId
  73. });
  74. const emit = (result?: T, type?: string) => {
  75. return put({
  76. type: type || hookId,
  77. payload: result
  78. });
  79. };
  80. return [onListener, closeListener, emit] as [
  81. typeof onListener,
  82. typeof closeListener,
  83. typeof emit
  84. ];
  85. }
  86.  
  87. export function generateListenerSagaAndActionCreators<T extends any = any>(
  88. listenerId: string,
  89. closeListenerId: string,
  90. hookId: string | string[] = listenerId,
  91. option?: {
  92. waitAll?: true;
  93. }
  94. ) {
  95. const [onListener, closeListener, emit] = createListenerActionCreators<T>(
  96. listenerId,
  97. closeListenerId,
  98. listenerId
  99. );
  100. const listenerSaga =
  101. option && option.waitAll && Array.isArray(hookId)
  102. ? createWaitHooksListener(listenerId, closeListenerId, hookId)
  103. : createAsyncListener(listenerId, closeListenerId, hookId);
  104. return [listenerSaga, onListener, closeListener, emit] as [
  105. typeof listenerSaga,
  106. typeof onListener,
  107. typeof closeListener,
  108. typeof emit
  109. ];
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement