Advertisement
Guest User

Untitled

a guest
Nov 28th, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. import { DEFAULT_ADAPTER } from './constants';
  2.  
  3. /*
  4.  
  5. Remove a token from an array of subscriptions
  6.  
  7. @param subscriptions Array (of tokens)
  8. @param token Array ([action, callback])
  9.  
  10. */
  11.  
  12. const removeSubscription = (subscriptions, token, adapter) => {
  13. const idx = subscriptions.indexOf(token);
  14. if (idx < 0) {
  15. console.log('PubSub cannot unregister an unrecognized token');
  16. } else {
  17. subscriptions.splice(idx, 1);
  18. adapter.unsubscribe(token);
  19. }
  20. };
  21.  
  22. /*
  23.  
  24. Creates the subscription object
  25.  
  26. @param component function the React component
  27. @param autoUnmount boolean defaults to true, if it has to remove all subscriptions on cwunmount
  28. @param adapter object the pubsub adapter
  29. @return the subscription object
  30.  
  31. */
  32.  
  33. const createSubscription = function(component, autoUnmount = true, adapter) {
  34. let sub = {
  35. componentWillUnmount: component.componentWillUnmount,
  36. subscriptions: [],
  37. add(action, cb) {
  38. const token = adapter.subscribe(action, cb);
  39. sub.subscriptions.push(token);
  40. return () => removeSubscription(sub.subscriptions, token);
  41. },
  42. removeAll() {
  43. sub.subscriptions.forEach( token => adapter.unsubscribe(token) );
  44. sub.subscriptions = [];
  45. },
  46. publish(action, params) {
  47. adapter.publish(action, params);
  48. }
  49. };
  50.  
  51. if (autoUnmount) {
  52. component.componentWillUnmount = () => {
  53. const { componentWillUnmount, removeAll } = sub;
  54. componentWillUnmount && componentWillUnmount.apply(component, arguments);
  55. removeAll();
  56. sub = void 0;
  57. };
  58. }
  59.  
  60. return sub;
  61. };
  62.  
  63.  
  64. /*
  65.  
  66. PubSubAdapter function
  67.  
  68. @param Object adapter being used
  69. @returns an object wrapping publish/subscribe/unsubscribe from given adapter
  70.  
  71. */
  72.  
  73. const PubSubAdapter = (adapter) => {
  74. const { publish, subscribe, unsubscribe } = adapter;
  75. return {
  76. publish,
  77. subsribe,
  78. unsubscribe
  79. }
  80. };
  81.  
  82.  
  83. /*
  84.  
  85. createPubSub function
  86.  
  87. @param subscribersMap Object, default to empty object, subscribers that will be populated
  88. like so:
  89. subscribersMap[MyReactComponent] = {...}
  90. @param adapter Object, default to default adapter specified
  91. @returns an object wrapping register/unregister for a particular component
  92.  
  93. */
  94.  
  95. const createPubSub = (subscribersMap = {}, adapter = DEFAULT_ADAPTER) => {
  96. return {
  97. register(component, autoUnmount = true) {
  98. let registeredComponent = subscribersMap[component];
  99. registeredComponent = registeredComponent || createSubscription(component, autoUnmount, PubSubAdapter(adapter));
  100. return registeredComponent;
  101. },
  102. unregister(component) {
  103. if (subscribersMap[component]) {
  104. subscribersMap[component].removeAll() && delete subscribersMap[component];
  105. } else {
  106. console.log(`${component.displayName} is NOT registerd to PubSub`);
  107. }
  108. },
  109. };
  110. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement