Skip to content

Commit 7257c6c

Browse files
committed
vlow with unregister
1 parent 191b16a commit 7257c6c

File tree

8 files changed

+200
-156
lines changed

8 files changed

+200
-156
lines changed

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ var _withVlow = _interopRequireDefault(require("./withVlow"));
5656
*/
5757

5858
var Vlow = {
59-
version: '1.1.17'
59+
version: '1.1.18'
6060
};
6161
Vlow.Store = _store["default"];
6262
Vlow.Component = _component["default"];

lib/store.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,27 @@ var Store = /*#__PURE__*/function () {
5757
return _this2._vlowSetState(listener, newState, counter);
5858
});
5959
}
60+
}, {
61+
key: "unregisterStore",
62+
value: function unregisterStore() {
63+
var _this3 = this;
64+
if (Object.keys(this._vlowListeners_).length === 0) {
65+
var index = _stores.findIndex(function (m) {
66+
return m.store === _this3;
67+
});
68+
if (index !== -1) {
69+
_stores.splice(index, 1);
70+
}
71+
} else {
72+
console.error('failed to unregister store, the store still has listeners');
73+
}
74+
}
75+
}, {
76+
key: "listenersEmpty",
77+
value: function listenersEmpty() {
78+
// This will be called when there are no more listeners bound to the store.
79+
// It is possible to do something here, for example call this.unregisterStore(); to remove the store from memory.
80+
}
6081
}, {
6182
key: "_vlowFilterState",
6283
value: function _vlowFilterState(state, keys) {
@@ -124,6 +145,9 @@ var Store = /*#__PURE__*/function () {
124145
key: "_vlowRemoveListener",
125146
value: function _vlowRemoveListener(id) {
126147
delete this._vlowListeners_[id];
148+
if (Object.keys(this._vlowListeners_).length === 0) {
149+
this.listenersEmpty();
150+
}
127151
}
128152
}], [{
129153
key: "_vlowGetOrCreateStore",

lib/withVlow.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ var _react = _interopRequireDefault(require("react"));
1515
var _component = _interopRequireDefault(require("./component"));
1616
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2["default"])(o), (0, _possibleConstructorReturn2["default"])(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2["default"])(t).constructor) : o.apply(t, e)); }
1717
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
18-
var withVlow = function withVlow(stores, WrappedComponent) {
18+
var _withVlow = function withVlow(stores, WrappedComponent) {
1919
if (WrappedComponent === undefined) {
2020
return function (WrappedComponent) {
21-
return withVlow(stores, WrappedComponent);
21+
return _withVlow(stores, WrappedComponent);
2222
};
2323
}
2424
var WithVlow = /*#__PURE__*/function (_Component) {
@@ -40,4 +40,4 @@ var withVlow = function withVlow(stores, WrappedComponent) {
4040
WithVlow.displayName = "WithVlow()(".concat(WrappedComponent.displayName || WrappedComponent.name || 'Component', ")");
4141
return WithVlow;
4242
};
43-
var _default = exports["default"] = withVlow;
43+
var _default = exports["default"] = _withVlow;

0 commit comments

Comments
 (0)