Skip to content

Commit a233d78

Browse files
committed
feat: call unmount() method when RouteViewModel will be closed
1 parent 2ff886d commit a233d78

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

.changeset/all-flies-matter.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"mobx-route": minor
3+
---
4+
5+
added `unmount()` method call for `RouteViewModel` when route will be closed

src/view-model/route-view-model.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { computed } from 'mobx';
1+
import { computed, reaction, when } from 'mobx';
22
import {
33
type AnyAbstractRouteEntity,
44
type IQueryParams,
@@ -28,7 +28,23 @@ export abstract class RouteViewModel<
2828
['pathParams', computed.struct],
2929
['query', computed],
3030
],
31-
this.vmConfig.observable.viewModelStores,
31+
this.vmConfig.observable.viewModels,
32+
);
33+
34+
when(
35+
() => this.isMounted,
36+
() => {
37+
reaction(
38+
() => this.route.isOpened,
39+
(isOpened) => {
40+
if (!isOpened && this.isMounted) {
41+
this.unmount();
42+
}
43+
},
44+
{ fireImmediately: true, signal: this.unmountSignal },
45+
);
46+
},
47+
{ signal: this.unmountSignal },
3248
);
3349
}
3450

0 commit comments

Comments
 (0)