mobx-route 0.20.2 → 0.22.0

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.
package/index.cjs CHANGED
@@ -5,41 +5,51 @@ const complex = require("yummies/complex");
5
5
  const linkedAbortController = require("linked-abort-controller");
6
6
  const mobx = require("mobx");
7
7
  const pathToRegexp = require("path-to-regexp");
8
+ const mobx$1 = require("yummies/mobx");
8
9
  const common = require("yummies/common");
9
- let localHistory;
10
10
  const routeConfig = complex.createGlobalDynamicConfig(
11
- (update) => {
12
- if (localHistory && update?.history && mobxLocationHistory.isObservableHistory(localHistory)) {
13
- localHistory.destroy();
14
- }
11
+ (update, current) => {
15
12
  let history;
13
+ let queryParams;
16
14
  if (update?.history) {
17
15
  history = update.history;
18
- } else {
19
- history = localHistory = mobxLocationHistory.createBrowserHistory();
20
- }
21
- let queryParams;
22
- if (update?.history && !update.queryParams) {
23
- queryParams = new mobxLocationHistory.QueryParams({ history });
24
- } else {
25
- if (update?.queryParams) {
26
- queryParams = update.queryParams;
27
- } else {
28
- queryParams = new mobxLocationHistory.QueryParams({ history });
16
+ queryParams = update.queryParams;
17
+ if (current?.history && mobxLocationHistory.isObservableHistory(current.history)) {
18
+ current.history.destroy();
29
19
  }
20
+ } else if (current?.history) {
21
+ history = current.history;
22
+ queryParams = update?.queryParams ?? current.queryParams;
23
+ } else {
24
+ history = mobxLocationHistory.createBrowserHistory();
30
25
  }
26
+ queryParams ??= mobxLocationHistory.createQueryParams({ history });
31
27
  return {
32
28
  ...update,
33
29
  history,
34
- location,
35
30
  queryParams
36
31
  };
37
32
  },
38
33
  Symbol.for("MOBX_ROUTE_CONFIG")
39
34
  );
35
+ const annotations$3 = [
36
+ [
37
+ mobx.computed,
38
+ "isPathMatched",
39
+ "isOpened",
40
+ "isOpening",
41
+ "path",
42
+ "hasOpenedChildren",
43
+ "isAbleToMergeQuery",
44
+ "baseUrl"
45
+ ],
46
+ [mobx.computed.struct, "parsedPathData", "params"],
47
+ [mobx.observable, "children"],
48
+ [mobx.observable.ref, "parent", "status"]
49
+ ];
40
50
  class Route {
41
- constructor(path, config = {}) {
42
- this.path = path;
51
+ constructor(pathDeclaration, config = {}) {
52
+ this.pathDeclaration = pathDeclaration;
43
53
  this.config = config;
44
54
  this.abortController = new linkedAbortController.LinkedAbortController(config.abortSignal);
45
55
  this.history = config.history ?? routeConfig.get().history;
@@ -49,23 +59,7 @@ class Route {
49
59
  this.meta = this.config.meta;
50
60
  this.status = "unknown";
51
61
  this.parent = config.parent ?? null;
52
- mobx.computed(this, "isPathMatched");
53
- mobx.computed(this, "isOpened");
54
- mobx.computed.struct(this, "data");
55
- mobx.computed.struct(this, "params");
56
- mobx.computed(this, "currentPath");
57
- mobx.computed(this, "hasOpenedChildren");
58
- mobx.computed(this, "isAbleToMergeQuery");
59
- mobx.computed(this, "baseUrl");
60
- mobx.observable(this, "children");
61
- mobx.observable.ref(this, "parent");
62
- mobx.observable.ref(this, "status");
63
- mobx.computed(this, "isOpening");
64
- mobx.action(this, "addChildren");
65
- mobx.action(this, "confirmOpening");
66
- mobx.action(this, "confirmClosing");
67
- mobx.action(this, "removeChildren");
68
- mobx.makeObservable(this);
62
+ mobx$1.applyObservable(this, annotations$3);
69
63
  mobx.reaction(() => this.isPathMatched, this.checkPathMatch, {
70
64
  signal: this.abortController.signal,
71
65
  fireImmediately: true
@@ -78,19 +72,19 @@ class Route {
78
72
  _tokenData;
79
73
  _matcher;
80
74
  _compiler;
81
- skipPathMatchCheck = false;
75
+ ignoreOpenByPathMatch = false;
82
76
  status;
83
77
  meta;
84
78
  /**
85
79
  * Indicates if this route is an index route. Index routes activate when parent route path matches exactly.
86
80
  *
87
- * [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#isindex-boolean)
81
+ * [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#isindex)
88
82
  */
89
83
  isIndex;
90
84
  /**
91
85
  * Indicates if this route is an hash route.
92
86
  *
93
- * [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#ishash-boolean)
87
+ * [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#ishash)
94
88
  */
95
89
  isHash;
96
90
  children = [];
@@ -111,7 +105,7 @@ class Route {
111
105
  }
112
106
  pathnameToCheck = pathnameToCheck.replace(this.baseUrl, "");
113
107
  }
114
- if ((this.path === "" || this.path === "/") && (pathnameToCheck === "/" || pathnameToCheck === "")) {
108
+ if ((this.pathDeclaration === "" || this.pathDeclaration === "/") && (pathnameToCheck === "/" || pathnameToCheck === "")) {
115
109
  return { params: {}, path: pathnameToCheck };
116
110
  }
117
111
  this._matcher ??= pathToRegexp.match(this.tokenData, {
@@ -124,21 +118,24 @@ class Route {
124
118
  }
125
119
  return parsed;
126
120
  }
121
+ /**
122
+ * [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#isopening)
123
+ */
127
124
  get isOpening() {
128
125
  return this.status === "opening";
129
126
  }
130
127
  /**
131
128
  * Matched path segment for current URL.
132
129
  *
133
- * [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#currentpath-parsedpathname-null)
130
+ * [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#path)
134
131
  */
135
- get currentPath() {
132
+ get path() {
136
133
  return this.parsedPathData?.path ?? null;
137
134
  }
138
135
  /**
139
136
  * Current parsed path parameters.
140
137
  *
141
- * [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#params-parsedpathparams-null)
138
+ * [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#params)
142
139
  */
143
140
  get params() {
144
141
  if (!this.parsedPathData?.params) {
@@ -164,22 +161,25 @@ class Route {
164
161
  /**
165
162
  * Defines the "open" state for this route.
166
163
  *
167
- * [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#isopened-boolean)
164
+ * [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#isopened)
168
165
  */
169
166
  get isOpened() {
170
167
  if (!this.isPathMatched || this.params === null || this.status !== "open-confirmed") {
171
168
  return false;
172
169
  }
173
- return !this.config.checkOpened || this.config.checkOpened(this.parsedPathData);
170
+ return (
171
+ // this.parsedPathData is defined because this.params !== null
172
+ !this.config.checkOpened || this.config.checkOpened(this.parsedPathData)
173
+ );
174
174
  }
175
175
  /**
176
176
  * Allows to create child route based on this route with merging this route path and extending path.
177
177
  *
178
- * [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#extend-path-config-route)
178
+ * [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#extend)
179
179
  */
180
- extend(path, config) {
180
+ extend(pathDeclaration, config) {
181
181
  const { index, params, exact, ...configFromCurrentRoute } = this.config;
182
- const extendedChild = new Route(`${this.path}${path}`, {
182
+ const extendedChild = new Route(`${this.pathDeclaration}${pathDeclaration}`, {
183
183
  ...configFromCurrentRoute,
184
184
  ...config,
185
185
  parent: this
@@ -194,7 +194,7 @@ class Route {
194
194
  this.children = this.children.filter((child) => !routes.includes(child));
195
195
  }
196
196
  /**
197
- * [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#hasopenedchildren-boolean)
197
+ * [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#hasopenedchildren)
198
198
  */
199
199
  get hasOpenedChildren() {
200
200
  return this.children.some(
@@ -223,7 +223,13 @@ class Route {
223
223
  query
224
224
  };
225
225
  const urlCreateParams = this.config.createUrl?.(defaultUrlCreateParams, this.query.data) ?? routeConfig.get().createUrl?.(defaultUrlCreateParams, this.query.data) ?? defaultUrlCreateParams;
226
- const path = this._compiler(this.processParams(urlCreateParams.params));
226
+ let path;
227
+ try {
228
+ path = this._compiler(this.processParams(urlCreateParams.params));
229
+ } catch (e) {
230
+ console.error("Error while compiling route path", e);
231
+ path = this.config.fallbackPath ?? routeConfig.get().fallbackPath ?? "/";
232
+ }
227
233
  const url = `${urlCreateParams.baseUrl || ""}${this.isHash ? "#" : ""}${path}`;
228
234
  if (outputParams?.omitQuery) {
229
235
  return url;
@@ -233,7 +239,7 @@ class Route {
233
239
  /**
234
240
  * Navigates to this route.
235
241
  *
236
- * [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#open-args)
242
+ * [**Documentation**](https://js2me.github.io/mobx-route/core/Route.html#open)
237
243
  */
238
244
  async open(...args) {
239
245
  const {
@@ -260,47 +266,55 @@ class Route {
260
266
  state,
261
267
  query
262
268
  };
263
- const isConfirmed = await this.confirmOpening(trx);
264
- if (!isConfirmed) {
265
- return;
266
- }
267
- this.skipPathMatchCheck = true;
268
- if (trx.replace) {
269
- this.history.replace(trx.url, trx.state);
270
- } else {
271
- this.history.push(trx.url, trx.state);
269
+ if (await this.confirmOpening(trx)) {
270
+ this.ignoreOpenByPathMatch = true;
272
271
  }
273
272
  }
274
273
  get tokenData() {
275
274
  if (!this._tokenData) {
276
- this._tokenData = pathToRegexp.parse(this.path, this.config.parseOptions);
275
+ this._tokenData = pathToRegexp.parse(this.pathDeclaration, this.config.parseOptions);
277
276
  }
278
277
  return this._tokenData;
279
278
  }
280
279
  async confirmOpening(trx) {
281
- this.status = "opening";
280
+ mobx.runInAction(() => {
281
+ this.status = "opening";
282
+ });
283
+ let skipHistoryUpdate = !!trx.preferSkipHistoryUpdate;
282
284
  if (this.config.beforeOpen) {
283
285
  const feedback = await this.config.beforeOpen(trx);
284
286
  if (feedback === false) {
285
287
  mobx.runInAction(() => {
286
288
  this.status = "open-rejected";
287
289
  });
288
- return false;
289
290
  }
290
291
  if (typeof feedback === "object") {
291
- mobx.runInAction(() => {
292
- this.status = "open-confirmed";
293
- });
294
- return Object.assign(trx, feedback);
292
+ skipHistoryUpdate = false;
293
+ Object.assign(trx, feedback);
295
294
  }
296
295
  }
297
- mobx.runInAction(() => {
298
- this.status = "open-confirmed";
299
- });
296
+ if (this.abortController.signal.aborted) {
297
+ return;
298
+ }
299
+ if (!skipHistoryUpdate) {
300
+ if (trx.replace) {
301
+ this.history.replace(trx.url, trx.state);
302
+ } else {
303
+ this.history.push(trx.url, trx.state);
304
+ }
305
+ }
306
+ if (this.isPathMatched) {
307
+ mobx.runInAction(() => {
308
+ this.status = "open-confirmed";
309
+ });
310
+ this.config.afterOpen?.(this.parsedPathData, this);
311
+ }
300
312
  return true;
301
313
  }
302
314
  confirmClosing() {
303
- this.status = "closed";
315
+ mobx.runInAction(() => {
316
+ this.status = "closed";
317
+ });
304
318
  return true;
305
319
  }
306
320
  firstPathMatchingRun = true;
@@ -311,33 +325,19 @@ class Route {
311
325
  return;
312
326
  }
313
327
  }
314
- if (this.skipPathMatchCheck) {
315
- this.skipPathMatchCheck = false;
316
- return;
317
- }
318
328
  if (isPathMathched) {
329
+ if (this.ignoreOpenByPathMatch) {
330
+ this.ignoreOpenByPathMatch = false;
331
+ return;
332
+ }
319
333
  const trx = {
320
334
  url: this.parsedPathData.path,
321
335
  params: this.parsedPathData.params,
322
336
  state: this.history.location.state,
323
- query: this.query.data
337
+ query: this.query.data,
338
+ preferSkipHistoryUpdate: true
324
339
  };
325
- const nextTrxOrConfirmed = await this.confirmOpening(trx);
326
- if (!nextTrxOrConfirmed) {
327
- return;
328
- }
329
- this.config.afterOpen?.(this.parsedPathData, this);
330
- if (typeof nextTrxOrConfirmed === "object") {
331
- if (nextTrxOrConfirmed.replace) {
332
- this.history.replace(
333
- nextTrxOrConfirmed.url,
334
- nextTrxOrConfirmed.state
335
- );
336
- } else {
337
- this.history.push(nextTrxOrConfirmed.url, nextTrxOrConfirmed.state);
338
- }
339
- }
340
- return;
340
+ await this.confirmOpening(trx);
341
341
  } else {
342
342
  const isConfirmed = this.confirmClosing();
343
343
  if (isConfirmed) {
@@ -353,20 +353,21 @@ class Route {
353
353
  }
354
354
  }
355
355
  const createRoute = (path, config) => new Route(path, config);
356
+ const annotations$2 = [
357
+ [mobx.computed, "isOpened", "indexRoute"],
358
+ [mobx.observable.shallow, "routes"]
359
+ ];
356
360
  class RouteGroup {
357
361
  constructor(routes, _indexRoute) {
358
362
  this._indexRoute = _indexRoute;
359
363
  this.routes = routes;
360
- mobx.computed.struct(this, "isOpened");
361
- mobx.computed.struct(this, "indexRoute");
362
- mobx.observable.shallow(this, "routes");
363
- mobx.makeObservable(this);
364
+ mobx$1.applyObservable(this, annotations$2);
364
365
  }
365
366
  routes;
366
367
  /**
367
368
  * Returns true if at least one route in the group is open.
368
369
  *
369
- * [**Documentation**](https://js2me.github.io/mobx-route/core/RouteGroup.html#isopened-boolean)
370
+ * [**Documentation**](https://js2me.github.io/mobx-route/core/RouteGroup.html#isopened)
370
371
  */
371
372
  get isOpened() {
372
373
  const routes = Object.values(this.routes);
@@ -377,7 +378,7 @@ class RouteGroup {
377
378
  /**
378
379
  * First found index route.
379
380
  *
380
- * [**Documentation**](https://js2me.github.io/mobx-route/core/RouteGroup.html#indexroute-route-undefined)
381
+ * [**Documentation**](https://js2me.github.io/mobx-route/core/RouteGroup.html#indexroute)
381
382
  */
382
383
  get indexRoute() {
383
384
  return this._indexRoute ?? Object.values(this.routes).find(
@@ -387,7 +388,7 @@ class RouteGroup {
387
388
  /**
388
389
  * Main navigation method for the group.
389
390
  *
390
- * [**Documentation**](https://js2me.github.io/mobx-route/core/RouteGroup.html#open-args-any-void)
391
+ * [**Documentation**](https://js2me.github.io/mobx-route/core/RouteGroup.html#open)
391
392
  */
392
393
  open(...args) {
393
394
  let lastGroupRoute;
@@ -411,6 +412,9 @@ class RouteGroup {
411
412
  }
412
413
  }
413
414
  const createRouteGroup = (routes, indexRoute) => new RouteGroup(routes, indexRoute);
415
+ const annotations$1 = [
416
+ [mobx.computed.struct, "location"]
417
+ ];
414
418
  class Router {
415
419
  routes;
416
420
  history;
@@ -419,8 +423,7 @@ class Router {
419
423
  this.routes = config.routes;
420
424
  this.history = config.history ?? routeConfig.get().history;
421
425
  this.query = config.queryParams ?? routeConfig.get().queryParams;
422
- mobx.computed.struct(this, "location");
423
- mobx.makeObservable(this);
426
+ mobx$1.applyObservable(this, annotations$1);
424
427
  }
425
428
  get location() {
426
429
  return this.history.location;
@@ -438,6 +441,12 @@ class Router {
438
441
  }
439
442
  const createRouter = (config) => new Router(config);
440
443
  const isRouteEntity = (route) => route && "isOpened" in route;
444
+ const annotations = [
445
+ [mobx.observable, "params"],
446
+ [mobx.observable.ref, "status", "trx", "openChecker", "isOuterOpened"],
447
+ [mobx.computed, "isOpened", "isOpening", "isClosing"],
448
+ [mobx.action, "setOpenChecker", "open", "close"]
449
+ ];
441
450
  class VirtualRoute {
442
451
  constructor(config = {}) {
443
452
  this.config = config;
@@ -445,110 +454,156 @@ class VirtualRoute {
445
454
  this.query = config.queryParams ?? routeConfig.get().queryParams;
446
455
  this.params = common.callFunction(config.initialParams, this) ?? null;
447
456
  this.openChecker = config.checkOpened;
448
- this.isLocalOpened = this.openChecker?.(this) ?? false;
449
- mobx.observable(this, "params");
450
- mobx.observable.ref(this, "isLocalOpened");
451
- mobx.observable.ref(this, "_isOpened");
452
- mobx.computed.struct(this, "isOpened");
453
- mobx.action(this, "setOpenChecker");
454
- mobx.action(this, "open");
455
- mobx.action(this, "close");
456
- mobx.makeObservable(this);
457
- mobx.onBecomeObserved(this, "isOpened", () => {
458
- if (!config.afterOpen && !config.afterClose) {
459
- return;
460
- }
461
- this.reactionDisposer = mobx.reaction(
462
- () => this.isOpened,
463
- this.processOpenedState,
464
- {
465
- signal: this.abortController.signal,
466
- fireImmediately: true
457
+ this.skipAutoOpenClose = false;
458
+ this.isOuterOpened = this.openChecker?.(this);
459
+ this.status = this.isOuterOpened ? "opened" : "unknown";
460
+ mobx$1.applyObservable(this, annotations);
461
+ this.abortController.signal.addEventListener(
462
+ "abort",
463
+ mobx.action(() => {
464
+ this.status = "unknown";
465
+ })
466
+ );
467
+ mobx.reaction(
468
+ () => this.openChecker?.(this),
469
+ mobx.action((isOuterOpened) => {
470
+ this.isOuterOpened = isOuterOpened;
471
+ if (this.skipAutoOpenClose || this.status === "closing" || this.status === "opening") {
472
+ return;
467
473
  }
468
- );
469
- });
470
- mobx.onBecomeUnobserved(this, "isOpened", () => {
471
- this.reactionDisposer?.();
472
- this.reactionDisposer = void 0;
473
- });
474
+ if (this.isOuterOpened) {
475
+ if (this.status === "opened") {
476
+ return;
477
+ }
478
+ this.confirmOpening({
479
+ params: this.params ?? null,
480
+ ...this.config.getAutomatedOpenParams?.(this)
481
+ });
482
+ } else {
483
+ if (this.status === "closed" || this.status === "unknown") {
484
+ return;
485
+ }
486
+ this.confirmClosing();
487
+ }
488
+ }),
489
+ { signal: this.abortController.signal, fireImmediately: true }
490
+ );
491
+ if (this.status === "opened") {
492
+ this.config.afterOpen?.(this.params, this);
493
+ }
474
494
  }
475
- abortController;
476
495
  query;
477
496
  params;
478
- isLocalOpened;
497
+ abortController;
498
+ status;
479
499
  openChecker;
480
- reactionDisposer;
500
+ trx;
501
+ skipAutoOpenClose;
502
+ /**
503
+ * [**Documentation**](https://js2me.github.io/mobx-route/core/VirtualRoute.html#isouteropened)
504
+ */
505
+ isOuterOpened;
481
506
  /**
482
- * [**Documentation**](https://js2me.github.io/mobx-route/core/VirtualRoute.html#isopened-boolean)
507
+ * [**Documentation**](https://js2me.github.io/mobx-route/core/VirtualRoute.html#isopened)
483
508
  */
484
509
  get isOpened() {
485
- const isOuterOpened = this.openChecker == null || this.openChecker(this);
486
- return this.isLocalOpened && isOuterOpened;
510
+ return this.status === "opened" && this.isOuterOpened !== false;
511
+ }
512
+ /**
513
+ * [**Documentation**](https://js2me.github.io/mobx-route/core/VirtualRoute.html#isopening)
514
+ */
515
+ get isOpening() {
516
+ return this.status === "opening";
517
+ }
518
+ /**
519
+ * [**Documentation**](https://js2me.github.io/mobx-route/core/VirtualRoute.html#isclosing)
520
+ */
521
+ get isClosing() {
522
+ return this.status === "closing";
487
523
  }
488
524
  /**
489
- * [**Documentation**](https://js2me.github.io/mobx-route/core/VirtualRoute.html#setopenchecker-openchecker-void)
525
+ * [**Documentation**](https://js2me.github.io/mobx-route/core/VirtualRoute.html#setopenchecker)
490
526
  */
491
527
  setOpenChecker(openChecker) {
492
528
  this.openChecker = openChecker;
493
529
  }
494
530
  async open(...args) {
495
- const params = args[0] ?? {};
496
- const extraParams = args[1];
497
- if (this.config.beforeOpen) {
498
- const beforeOpenResult = await this.config.beforeOpen(params, this);
499
- if (beforeOpenResult === false) {
500
- return;
501
- }
502
- }
503
- if (this.config.open == null) {
531
+ const params = args[0] ?? null;
532
+ const extra = args[1];
533
+ this.skipAutoOpenClose = true;
534
+ this.trx = {
535
+ params,
536
+ extra,
537
+ manual: true
538
+ };
539
+ await this.confirmOpening(this.trx);
540
+ this.skipAutoOpenClose = false;
541
+ }
542
+ /**
543
+ * [**Documentation**](https://js2me.github.io/mobx-route/core/VirtualRoute.html#close)
544
+ */
545
+ async close() {
546
+ this.skipAutoOpenClose = true;
547
+ const result = await this.confirmClosing();
548
+ this.skipAutoOpenClose = false;
549
+ return result;
550
+ }
551
+ async confirmOpening(trx) {
552
+ mobx.runInAction(() => {
553
+ this.trx = void 0;
554
+ this.status = "opening";
555
+ });
556
+ if (await this.config.beforeOpen?.(trx.params, this) === false) {
504
557
  mobx.runInAction(() => {
505
- this.isLocalOpened = true;
558
+ this.status = "open-rejected";
559
+ this.trx = void 0;
506
560
  });
507
- } else {
508
- const result = await this.config.open(params, this);
561
+ return;
562
+ }
563
+ if (await this.config.open?.(trx.params, this) === false) {
509
564
  mobx.runInAction(() => {
510
- this.isLocalOpened = result !== false;
565
+ this.status = "open-rejected";
566
+ this.trx = void 0;
511
567
  });
512
- }
513
- if (!this.isLocalOpened) {
514
568
  return;
515
569
  }
516
- if (extraParams?.query) {
517
- this.query.update(extraParams.query, extraParams.replace);
518
- }
519
570
  mobx.runInAction(() => {
520
- this.params = params;
521
- });
522
- if (!this.reactionDisposer && this.isOpened) {
571
+ if (trx.extra?.query) {
572
+ this.query.update(trx.extra.query, trx.extra.replace);
573
+ }
574
+ this.trx = void 0;
575
+ this.params = trx.params;
576
+ this.status = "opened";
523
577
  this.config.afterOpen?.(this.params, this);
524
- }
578
+ });
579
+ return true;
525
580
  }
526
- /**
527
- * [**Documentation**](https://js2me.github.io/mobx-route/core/VirtualRoute.html#close-void)
528
- */
529
- close() {
530
- if (this.config.close == null) {
531
- this.isLocalOpened = false;
532
- } else {
533
- const result = this.config.close(this);
534
- this.isLocalOpened = result !== false;
581
+ async confirmClosing() {
582
+ if (this.status === "closed") {
583
+ return true;
535
584
  }
536
- this.params = null;
537
- }
538
- firstOpenedStateCheck = true;
539
- processOpenedState = (isOpened) => {
540
- if (this.firstOpenedStateCheck) {
541
- this.firstOpenedStateCheck = false;
542
- if (!isOpened) {
543
- return;
544
- }
585
+ const lastStatus = this.status;
586
+ mobx.runInAction(() => {
587
+ this.status = "closing";
588
+ });
589
+ if (await this.config.beforeClose?.() === false) {
590
+ mobx.runInAction(() => {
591
+ this.status = lastStatus;
592
+ });
593
+ return;
545
594
  }
546
- if (isOpened) {
547
- this.config.afterOpen?.(this.params, this);
548
- } else {
549
- this.config.afterClose?.();
595
+ if (this.config.close?.(this) === false) {
596
+ mobx.runInAction(() => {
597
+ this.status = lastStatus;
598
+ });
599
+ return;
550
600
  }
551
- };
601
+ mobx.runInAction(() => {
602
+ this.status = "closed";
603
+ this.params = null;
604
+ });
605
+ return true;
606
+ }
552
607
  destroy() {
553
608
  this.abortController.abort();
554
609
  }