Subscription.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. "use strict";
  2. exports.__esModule = true;
  3. exports["default"] = void 0;
  4. var _batch = require("./batch");
  5. // encapsulates the subscription logic for connecting a component to the redux store, as
  6. // well as nesting subscriptions of descendant components, so that we can ensure the
  7. // ancestor components re-render before descendants
  8. var nullListeners = {
  9. notify: function notify() {}
  10. };
  11. function createListenerCollection() {
  12. var batch = (0, _batch.getBatch)();
  13. var first = null;
  14. var last = null;
  15. return {
  16. clear: function clear() {
  17. first = null;
  18. last = null;
  19. },
  20. notify: function notify() {
  21. batch(function () {
  22. var listener = first;
  23. while (listener) {
  24. listener.callback();
  25. listener = listener.next;
  26. }
  27. });
  28. },
  29. get: function get() {
  30. var listeners = [];
  31. var listener = first;
  32. while (listener) {
  33. listeners.push(listener);
  34. listener = listener.next;
  35. }
  36. return listeners;
  37. },
  38. subscribe: function subscribe(callback) {
  39. var isSubscribed = true;
  40. var listener = last = {
  41. callback: callback,
  42. next: null,
  43. prev: last
  44. };
  45. if (listener.prev) {
  46. listener.prev.next = listener;
  47. } else {
  48. first = listener;
  49. }
  50. return function unsubscribe() {
  51. if (!isSubscribed || first === null) return;
  52. isSubscribed = false;
  53. if (listener.next) {
  54. listener.next.prev = listener.prev;
  55. } else {
  56. last = listener.prev;
  57. }
  58. if (listener.prev) {
  59. listener.prev.next = listener.next;
  60. } else {
  61. first = listener.next;
  62. }
  63. };
  64. }
  65. };
  66. }
  67. var Subscription =
  68. /*#__PURE__*/
  69. function () {
  70. function Subscription(store, parentSub) {
  71. this.store = store;
  72. this.parentSub = parentSub;
  73. this.unsubscribe = null;
  74. this.listeners = nullListeners;
  75. this.handleChangeWrapper = this.handleChangeWrapper.bind(this);
  76. }
  77. var _proto = Subscription.prototype;
  78. _proto.addNestedSub = function addNestedSub(listener) {
  79. this.trySubscribe();
  80. return this.listeners.subscribe(listener);
  81. };
  82. _proto.notifyNestedSubs = function notifyNestedSubs() {
  83. this.listeners.notify();
  84. };
  85. _proto.handleChangeWrapper = function handleChangeWrapper() {
  86. if (this.onStateChange) {
  87. this.onStateChange();
  88. }
  89. };
  90. _proto.isSubscribed = function isSubscribed() {
  91. return Boolean(this.unsubscribe);
  92. };
  93. _proto.trySubscribe = function trySubscribe() {
  94. if (!this.unsubscribe) {
  95. this.unsubscribe = this.parentSub ? this.parentSub.addNestedSub(this.handleChangeWrapper) : this.store.subscribe(this.handleChangeWrapper);
  96. this.listeners = createListenerCollection();
  97. }
  98. };
  99. _proto.tryUnsubscribe = function tryUnsubscribe() {
  100. if (this.unsubscribe) {
  101. this.unsubscribe();
  102. this.unsubscribe = null;
  103. this.listeners.clear();
  104. this.listeners = nullListeners;
  105. }
  106. };
  107. return Subscription;
  108. }();
  109. exports["default"] = Subscription;