103 lines
2.7 KiB
JavaScript
103 lines
2.7 KiB
JavaScript
"use strict";
|
|
|
|
exports.__esModule = true;
|
|
exports["default"] = void 0;
|
|
|
|
var _batch = require("./batch");
|
|
|
|
// encapsulates the subscription logic for connecting a component to the redux store, as
|
|
// well as nesting subscriptions of descendant components, so that we can ensure the
|
|
// ancestor components re-render before descendants
|
|
var CLEARED = null;
|
|
var nullListeners = {
|
|
notify: function notify() {}
|
|
};
|
|
|
|
function createListenerCollection() {
|
|
var batch = (0, _batch.getBatch)(); // the current/next pattern is copied from redux's createStore code.
|
|
// TODO: refactor+expose that code to be reusable here?
|
|
|
|
var current = [];
|
|
var next = [];
|
|
return {
|
|
clear: function clear() {
|
|
next = CLEARED;
|
|
current = CLEARED;
|
|
},
|
|
notify: function notify() {
|
|
var listeners = current = next;
|
|
batch(function () {
|
|
for (var i = 0; i < listeners.length; i++) {
|
|
listeners[i]();
|
|
}
|
|
});
|
|
},
|
|
get: function get() {
|
|
return next;
|
|
},
|
|
subscribe: function subscribe(listener) {
|
|
var isSubscribed = true;
|
|
if (next === current) next = current.slice();
|
|
next.push(listener);
|
|
return function unsubscribe() {
|
|
if (!isSubscribed || current === CLEARED) return;
|
|
isSubscribed = false;
|
|
if (next === current) next = current.slice();
|
|
next.splice(next.indexOf(listener), 1);
|
|
};
|
|
}
|
|
};
|
|
}
|
|
|
|
var Subscription =
|
|
/*#__PURE__*/
|
|
function () {
|
|
function Subscription(store, parentSub) {
|
|
this.store = store;
|
|
this.parentSub = parentSub;
|
|
this.unsubscribe = null;
|
|
this.listeners = nullListeners;
|
|
this.handleChangeWrapper = this.handleChangeWrapper.bind(this);
|
|
}
|
|
|
|
var _proto = Subscription.prototype;
|
|
|
|
_proto.addNestedSub = function addNestedSub(listener) {
|
|
this.trySubscribe();
|
|
return this.listeners.subscribe(listener);
|
|
};
|
|
|
|
_proto.notifyNestedSubs = function notifyNestedSubs() {
|
|
this.listeners.notify();
|
|
};
|
|
|
|
_proto.handleChangeWrapper = function handleChangeWrapper() {
|
|
if (this.onStateChange) {
|
|
this.onStateChange();
|
|
}
|
|
};
|
|
|
|
_proto.isSubscribed = function isSubscribed() {
|
|
return Boolean(this.unsubscribe);
|
|
};
|
|
|
|
_proto.trySubscribe = function trySubscribe() {
|
|
if (!this.unsubscribe) {
|
|
this.unsubscribe = this.parentSub ? this.parentSub.addNestedSub(this.handleChangeWrapper) : this.store.subscribe(this.handleChangeWrapper);
|
|
this.listeners = createListenerCollection();
|
|
}
|
|
};
|
|
|
|
_proto.tryUnsubscribe = function tryUnsubscribe() {
|
|
if (this.unsubscribe) {
|
|
this.unsubscribe();
|
|
this.unsubscribe = null;
|
|
this.listeners.clear();
|
|
this.listeners = nullListeners;
|
|
}
|
|
};
|
|
|
|
return Subscription;
|
|
}();
|
|
|
|
exports["default"] = Subscription; |