mobx-route 0.6.0 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -30,6 +30,8 @@ export declare class VirtualRoute<TParams extends AnyObject | EmptyObject = Empt
30
30
  * [**Documentation**](https://js2me.github.io/mobx-route/core/VirtualRoute.html#close-void)
31
31
  */
32
32
  close(): void;
33
+ private firstOpenedStateCheck;
34
+ private processOpenedState;
33
35
  destroy(): void;
34
36
  }
35
37
  //# sourceMappingURL=virtual-route.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"virtual-route.d.ts","sourceRoot":"","sources":["../../../src/core/virtual-route/virtual-route.ts"],"names":[],"mappings":"AAYA,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAErD,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAI/E,OAAO,EACL,oBAAoB,EACpB,sBAAsB,EACtB,yBAAyB,EAC1B,MAAM,0BAA0B,CAAC;AAElC;;;;GAIG;AACH,qBAAa,YAAY,CAAC,OAAO,SAAS,SAAS,GAAG,WAAW,GAAG,WAAW,CAC7E,YAAW,oBAAoB,CAAC,OAAO,CAAC;IAU5B,SAAS,CAAC,MAAM,EAAE,yBAAyB,CAAC,OAAO,CAAC;IARhE,SAAS,CAAC,eAAe,EAAE,eAAe,CAAC;IAC3C,KAAK,EAAE,YAAY,CAAC;IACpB,MAAM,EAAE,OAAO,GAAG,IAAI,CAAC;IAEvB,OAAO,CAAC,aAAa,CAA4B;IAEjD,OAAO,CAAC,WAAW,CAA2D;gBAExD,MAAM,GAAE,yBAAyB,CAAC,OAAO,CAAM;IAmDrE;;OAEG;IACH,IAAI,QAAQ,YAGX;IAED;;OAEG;IACH,cAAc,CACZ,WAAW,EAAE,KAAK,CAAC,yBAAyB,CAAC,OAAO,CAAC,CAAC,aAAa,CAAC,CAAC;IAKvE;;OAEG;IACH,IA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S,IAAI,GACpC,CAAC,MAAM,CAAC,EAAE,KAAK,CAAC,OAAO,CAAC,EAAE,WAAW,CAAC,EAAE,sBAAsB,CAAC,GAC/D,CAAC,MAAM,EAAE,OAAO,EAAE,WAAW,CAAC,EAAE,sBAAsB,CAAC,GAC1D,OAAO,CAAC,IAAI,CAAC;IAyChB;;OAEG;IACH,KAAK;IAYL,OAAO,CAAC,qBAAqB,CAAQ;IACrC,OAAO,CAAC,kBAAkB,CAcxB;IAEF,OAAO;CAGR"}
@@ -20,8 +20,9 @@ export class VirtualRoute {
20
20
  this.query = config.queryParams ?? routeConfig.get().queryParams;
21
21
  this.params = callFunction(config.initialParams, this) ?? null;
22
22
  this.openChecker = config.checkOpened;
23
- this.isLocalOpened = observable.box(this.openChecker?.(this) ?? false);
23
+ this.isLocalOpened = this.openChecker?.(this) ?? false;
24
24
  observable(this, 'params');
25
+ observable.ref(this, 'isLocalOpened');
25
26
  observable.ref(this, '_isOpened');
26
27
  computed.struct(this, 'isOpened');
27
28
  action(this, 'setOpenChecker');
@@ -29,26 +30,11 @@ export class VirtualRoute {
29
30
  action(this, 'close');
30
31
  makeObservable(this);
31
32
  let dispose;
32
- let firstReactionCall = true;
33
33
  onBecomeObserved(this, 'isOpened', () => {
34
34
  if (!config.afterOpen && !config.afterClose) {
35
35
  return;
36
36
  }
37
- dispose = reaction(() => this.isOpened, (isOpened) => {
38
- if (firstReactionCall) {
39
- firstReactionCall = false;
40
- // ignore first 'afterClose' callback call
41
- if (!isOpened) {
42
- return;
43
- }
44
- }
45
- if (isOpened) {
46
- config.afterOpen?.(this.params, this);
47
- }
48
- else {
49
- config.afterClose?.();
50
- }
51
- }, {
37
+ dispose = reaction(() => this.isOpened, this.processOpenedState, {
52
38
  signal: this.abortController.signal,
53
39
  fireImmediately: true,
54
40
  });
@@ -63,7 +49,7 @@ export class VirtualRoute {
63
49
  */
64
50
  get isOpened() {
65
51
  const isOuterOpened = this.openChecker == null || this.openChecker(this);
66
- return this.isLocalOpened.get() && isOuterOpened;
52
+ return this.isLocalOpened && isOuterOpened;
67
53
  }
68
54
  /**
69
55
  * [**Documentation**](https://js2me.github.io/mobx-route/core/VirtualRoute.html#setopenchecker-openchecker-void)
@@ -81,16 +67,23 @@ export class VirtualRoute {
81
67
  }
82
68
  }
83
69
  if (this.config.open == null) {
84
- this.isLocalOpened.set(true);
70
+ runInAction(() => {
71
+ this.isLocalOpened = true;
72
+ });
85
73
  }
86
74
  else {
87
75
  const result = await this.config.open(params, this);
88
76
  // because result can return void so this is truthy for opening state
89
- this.isLocalOpened.set(result !== false);
77
+ runInAction(() => {
78
+ this.isLocalOpened = result !== false;
79
+ });
90
80
  }
91
81
  if (!this.isLocalOpened) {
92
82
  return;
93
83
  }
84
+ if (this.isOpened) {
85
+ this.config.afterOpen?.(this.params, this);
86
+ }
94
87
  if (extraParams?.query) {
95
88
  this.query.update(extraParams.query, extraParams.replace);
96
89
  }
@@ -103,17 +96,31 @@ export class VirtualRoute {
103
96
  */
104
97
  close() {
105
98
  if (this.config.close == null) {
106
- this.isLocalOpened.set(false);
99
+ this.isLocalOpened = false;
107
100
  }
108
101
  else {
109
102
  const result = this.config.close(this);
110
103
  // because result can return void so this is truthy for opening state
111
- this.isLocalOpened.set(result !== false);
104
+ this.isLocalOpened = result !== false;
112
105
  }
113
- runInAction(() => {
114
- this.params = null;
115
- });
106
+ this.params = null;
116
107
  }
108
+ firstOpenedStateCheck = true;
109
+ processOpenedState = (isOpened) => {
110
+ if (this.firstOpenedStateCheck) {
111
+ this.firstOpenedStateCheck = false;
112
+ // ignore first 'afterClose' callback call
113
+ if (!isOpened) {
114
+ return;
115
+ }
116
+ }
117
+ if (isOpened) {
118
+ this.config.afterOpen?.(this.params, this);
119
+ }
120
+ else {
121
+ this.config.afterClose?.();
122
+ }
123
+ };
117
124
  destroy() {
118
125
  this.abortController.abort();
119
126
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mobx-route",
3
- "version": "0.6.0",
3
+ "version": "0.6.2",
4
4
  "keywords": [
5
5
  "mobx",
6
6
  "react",