Skip to content

Commit 08f812f

Browse files
committed
fix(cleanup): add cleanup logic, necessary because of long living global stores
1 parent fd4c9cd commit 08f812f

File tree

7 files changed

+65
-24
lines changed

7 files changed

+65
-24
lines changed

dist/easyState.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ const rawToProxy = new WeakMap()
258258
/* harmony reexport (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return __WEBPACK_IMPORTED_MODULE_0__observer__["a"]; });
259259
/* unused harmony reexport isObservable */
260260
/* harmony reexport (binding) */ __webpack_require__.d(__webpack_exports__, "b", function() { return __WEBPACK_IMPORTED_MODULE_0__observer__["d"]; });
261-
/* unused harmony reexport unobserve */
261+
/* harmony reexport (binding) */ __webpack_require__.d(__webpack_exports__, "c", function() { return __WEBPACK_IMPORTED_MODULE_0__observer__["e"]; });
262262
/* unused harmony reexport unqueue */
263263
/* unused harmony reexport exec */
264264

@@ -274,7 +274,7 @@ const rawToProxy = new WeakMap()
274274
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_2__store__ = __webpack_require__(13);
275275
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_3__internals__ = __webpack_require__(1);
276276
/* harmony export (immutable) */ __webpack_exports__["d"] = observe;
277-
/* unused harmony export unobserve */
277+
/* harmony export (immutable) */ __webpack_exports__["e"] = unobserve;
278278
/* unused harmony export unqueue */
279279
/* unused harmony export exec */
280280
/* unused harmony export isObservable */
@@ -498,7 +498,7 @@ function easyStateHOC (WrappedComp) {
498498
}
499499

500500
this[IS_DIRECT_RENDER] = true
501-
__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__nx_js_observer_util__["b" /* observe */])(this[OBSERVED_RENDER])
501+
this[OBSERVED_RENDER] = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__nx_js_observer_util__["b" /* observe */])(this[OBSERVED_RENDER])
502502
this[IS_DIRECT_RENDER] = false
503503

504504
return this[RENDER_RESULT]
@@ -524,6 +524,13 @@ function easyStateHOC (WrappedComp) {
524524
}
525525
return false
526526
}
527+
528+
componentWillUnmount () {
529+
if (super.componentWillUnmount) {
530+
return super.componentWillUnmount()
531+
}
532+
__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__nx_js_observer_util__["c" /* unobserve */])(this[OBSERVED_RENDER])
533+
}
527534
}
528535
}
529536

examples/helloWorld/bundle.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6904,7 +6904,7 @@ module.exports = __webpack_require__(126);
69046904
/* harmony reexport (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return __WEBPACK_IMPORTED_MODULE_0__observer__["a"]; });
69056905
/* unused harmony reexport isObservable */
69066906
/* harmony reexport (binding) */ __webpack_require__.d(__webpack_exports__, "b", function() { return __WEBPACK_IMPORTED_MODULE_0__observer__["d"]; });
6907-
/* unused harmony reexport unobserve */
6907+
/* harmony reexport (binding) */ __webpack_require__.d(__webpack_exports__, "c", function() { return __WEBPACK_IMPORTED_MODULE_0__observer__["e"]; });
69086908
/* unused harmony reexport unqueue */
69096909
/* unused harmony reexport exec */
69106910

@@ -6920,7 +6920,7 @@ module.exports = __webpack_require__(126);
69206920
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_2__store__ = __webpack_require__(97);
69216921
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_3__internals__ = __webpack_require__(35);
69226922
/* harmony export (immutable) */ __webpack_exports__["d"] = observe;
6923-
/* unused harmony export unobserve */
6923+
/* harmony export (immutable) */ __webpack_exports__["e"] = unobserve;
69246924
/* unused harmony export unqueue */
69256925
/* unused harmony export exec */
69266926
/* unused harmony export isObservable */
@@ -22641,7 +22641,7 @@ function easyStateHOC (WrappedComp) {
2264122641
}
2264222642

2264322643
this[IS_DIRECT_RENDER] = true
22644-
__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__nx_js_observer_util__["b" /* observe */])(this[OBSERVED_RENDER])
22644+
this[OBSERVED_RENDER] = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__nx_js_observer_util__["b" /* observe */])(this[OBSERVED_RENDER])
2264522645
this[IS_DIRECT_RENDER] = false
2264622646

2264722647
return this[RENDER_RESULT]
@@ -22667,6 +22667,13 @@ function easyStateHOC (WrappedComp) {
2266722667
}
2266822668
return false
2266922669
}
22670+
22671+
componentWillUnmount () {
22672+
if (super.componentWillUnmount) {
22673+
return super.componentWillUnmount()
22674+
}
22675+
__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__nx_js_observer_util__["c" /* unobserve */])(this[OBSERVED_RENDER])
22676+
}
2267022677
}
2267122678
}
2267222679

examples/simpleTodos/bundle.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6904,7 +6904,7 @@ module.exports = __webpack_require__(126);
69046904
/* harmony reexport (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return __WEBPACK_IMPORTED_MODULE_0__observer__["a"]; });
69056905
/* unused harmony reexport isObservable */
69066906
/* harmony reexport (binding) */ __webpack_require__.d(__webpack_exports__, "b", function() { return __WEBPACK_IMPORTED_MODULE_0__observer__["d"]; });
6907-
/* unused harmony reexport unobserve */
6907+
/* harmony reexport (binding) */ __webpack_require__.d(__webpack_exports__, "c", function() { return __WEBPACK_IMPORTED_MODULE_0__observer__["e"]; });
69086908
/* unused harmony reexport unqueue */
69096909
/* unused harmony reexport exec */
69106910

@@ -6920,7 +6920,7 @@ module.exports = __webpack_require__(126);
69206920
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_2__store__ = __webpack_require__(97);
69216921
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_3__internals__ = __webpack_require__(35);
69226922
/* harmony export (immutable) */ __webpack_exports__["d"] = observe;
6923-
/* unused harmony export unobserve */
6923+
/* harmony export (immutable) */ __webpack_exports__["e"] = unobserve;
69246924
/* unused harmony export unqueue */
69256925
/* unused harmony export exec */
69266926
/* unused harmony export isObservable */
@@ -22641,7 +22641,7 @@ function easyStateHOC (WrappedComp) {
2264122641
}
2264222642

2264322643
this[IS_DIRECT_RENDER] = true
22644-
__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__nx_js_observer_util__["b" /* observe */])(this[OBSERVED_RENDER])
22644+
this[OBSERVED_RENDER] = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__nx_js_observer_util__["b" /* observe */])(this[OBSERVED_RENDER])
2264522645
this[IS_DIRECT_RENDER] = false
2264622646

2264722647
return this[RENDER_RESULT]
@@ -22667,6 +22667,13 @@ function easyStateHOC (WrappedComp) {
2266722667
}
2266822668
return false
2266922669
}
22670+
22671+
componentWillUnmount () {
22672+
if (super.componentWillUnmount) {
22673+
return super.componentWillUnmount()
22674+
}
22675+
__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__nx_js_observer_util__["c" /* unobserve */])(this[OBSERVED_RENDER])
22676+
}
2267022677
}
2267122678
}
2267222679

examples/todoMVC/bundle.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6904,7 +6904,7 @@ module.exports = __webpack_require__(126);
69046904
/* harmony reexport (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return __WEBPACK_IMPORTED_MODULE_0__observer__["a"]; });
69056905
/* unused harmony reexport isObservable */
69066906
/* harmony reexport (binding) */ __webpack_require__.d(__webpack_exports__, "b", function() { return __WEBPACK_IMPORTED_MODULE_0__observer__["d"]; });
6907-
/* unused harmony reexport unobserve */
6907+
/* harmony reexport (binding) */ __webpack_require__.d(__webpack_exports__, "c", function() { return __WEBPACK_IMPORTED_MODULE_0__observer__["e"]; });
69086908
/* unused harmony reexport unqueue */
69096909
/* unused harmony reexport exec */
69106910

@@ -6920,7 +6920,7 @@ module.exports = __webpack_require__(126);
69206920
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_2__store__ = __webpack_require__(97);
69216921
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_3__internals__ = __webpack_require__(35);
69226922
/* harmony export (immutable) */ __webpack_exports__["d"] = observe;
6923-
/* unused harmony export unobserve */
6923+
/* harmony export (immutable) */ __webpack_exports__["e"] = unobserve;
69246924
/* unused harmony export unqueue */
69256925
/* unused harmony export exec */
69266926
/* unused harmony export isObservable */
@@ -22641,7 +22641,7 @@ function easyStateHOC (WrappedComp) {
2264122641
}
2264222642

2264322643
this[IS_DIRECT_RENDER] = true
22644-
__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__nx_js_observer_util__["b" /* observe */])(this[OBSERVED_RENDER])
22644+
this[OBSERVED_RENDER] = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__nx_js_observer_util__["b" /* observe */])(this[OBSERVED_RENDER])
2264522645
this[IS_DIRECT_RENDER] = false
2264622646

2264722647
return this[RENDER_RESULT]
@@ -22667,6 +22667,13 @@ function easyStateHOC (WrappedComp) {
2266722667
}
2266822668
return false
2266922669
}
22670+
22671+
componentWillUnmount () {
22672+
if (super.componentWillUnmount) {
22673+
return super.componentWillUnmount()
22674+
}
22675+
__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__nx_js_observer_util__["c" /* unobserve */])(this[OBSERVED_RENDER])
22676+
}
2267022677
}
2267122678
}
2267222679

examples/userPage/Profile.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import React, { Component } from 'react'
22
import { easyComp } from 'react-easy-state'
33
import store from './store'
44

5-
@easyComp
6-
export default class Profile extends Component {
5+
class Profile extends Component {
76
state = {
87
editing: false,
98
newProfile: Object.assign({ name: '' }, store.user)
@@ -54,3 +53,5 @@ export default class Profile extends Component {
5453
this.state.editing = false
5554
}
5655
}
56+
57+
export default easyComp(Profile)

examples/userPage/bundle.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6981,7 +6981,7 @@ module.exports = __webpack_require__(126);
69816981
/* harmony reexport (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return __WEBPACK_IMPORTED_MODULE_0__observer__["a"]; });
69826982
/* unused harmony reexport isObservable */
69836983
/* harmony reexport (binding) */ __webpack_require__.d(__webpack_exports__, "b", function() { return __WEBPACK_IMPORTED_MODULE_0__observer__["d"]; });
6984-
/* unused harmony reexport unobserve */
6984+
/* harmony reexport (binding) */ __webpack_require__.d(__webpack_exports__, "c", function() { return __WEBPACK_IMPORTED_MODULE_0__observer__["e"]; });
69856985
/* unused harmony reexport unqueue */
69866986
/* unused harmony reexport exec */
69876987

@@ -6997,7 +6997,7 @@ module.exports = __webpack_require__(126);
69976997
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_2__store__ = __webpack_require__(97);
69986998
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_3__internals__ = __webpack_require__(35);
69996999
/* harmony export (immutable) */ __webpack_exports__["d"] = observe;
7000-
/* unused harmony export unobserve */
7000+
/* harmony export (immutable) */ __webpack_exports__["e"] = unobserve;
70017001
/* unused harmony export unqueue */
70027002
/* unused harmony export exec */
70037003
/* unused harmony export isObservable */
@@ -22785,7 +22785,7 @@ function easyStateHOC (WrappedComp) {
2278522785
}
2278622786

2278722787
this[IS_DIRECT_RENDER] = true
22788-
__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__nx_js_observer_util__["b" /* observe */])(this[OBSERVED_RENDER])
22788+
this[OBSERVED_RENDER] = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__nx_js_observer_util__["b" /* observe */])(this[OBSERVED_RENDER])
2278922789
this[IS_DIRECT_RENDER] = false
2279022790

2279122791
return this[RENDER_RESULT]
@@ -22811,6 +22811,13 @@ function easyStateHOC (WrappedComp) {
2281122811
}
2281222812
return false
2281322813
}
22814+
22815+
componentWillUnmount () {
22816+
if (super.componentWillUnmount) {
22817+
return super.componentWillUnmount()
22818+
}
22819+
__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__nx_js_observer_util__["c" /* unobserve */])(this[OBSERVED_RENDER])
22820+
}
2281422821
}
2281522822
}
2281622823

@@ -23796,14 +23803,11 @@ let ContactList = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_1_react_easy_s
2379623803
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0_react___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0_react__);
2379723804
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1_react_easy_state__ = __webpack_require__(34);
2379823805
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_2__store__ = __webpack_require__(210);
23799-
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return Profile; });
23800-
var _class;
2380123806

2380223807

2380323808

2380423809

23805-
23806-
let Profile = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_1_react_easy_state__["a" /* easyComp */])(_class = class Profile extends __WEBPACK_IMPORTED_MODULE_0_react__["Component"] {
23810+
let Profile = class Profile extends __WEBPACK_IMPORTED_MODULE_0_react__["Component"] {
2380723811
constructor(...args) {
2380823812
var _temp;
2380923813

@@ -23902,9 +23906,10 @@ let Profile = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_1_react_easy_state
2390223906
Object.assign(__WEBPACK_IMPORTED_MODULE_2__store__["a" /* default */].user, this.state.newProfile);
2390323907
this.state.editing = false;
2390423908
}
23905-
}) || _class;
23909+
};
2390623910

2390723911

23912+
/* harmony default export */ __webpack_exports__["a"] = (__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_1_react_easy_state__["a" /* easyComp */])(Profile));
2390823913

2390923914
/***/ }),
2391023915
/* 215 */

src/easyComp.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { observable, observe} from '@nx-js/observer-util'
1+
import { observable, unobserve, observe } from '@nx-js/observer-util'
22
import autoBind from './autoBind'
33

44
const OBSERVED_RENDER = Symbol('observed render')
@@ -29,7 +29,7 @@ export default function easyStateHOC (WrappedComp) {
2929
}
3030

3131
this[IS_DIRECT_RENDER] = true
32-
observe(this[OBSERVED_RENDER])
32+
this[OBSERVED_RENDER] = observe(this[OBSERVED_RENDER])
3333
this[IS_DIRECT_RENDER] = false
3434

3535
return this[RENDER_RESULT]
@@ -55,5 +55,12 @@ export default function easyStateHOC (WrappedComp) {
5555
}
5656
return false
5757
}
58+
59+
componentWillUnmount () {
60+
if (super.componentWillUnmount) {
61+
return super.componentWillUnmount()
62+
}
63+
unobserve(this[OBSERVED_RENDER])
64+
}
5865
}
5966
}

0 commit comments

Comments
 (0)