Skip to content

Commit 944bd6d

Browse files
committed
feat: add warning message for problems with accessing to view models
1 parent 11db59b commit 944bd6d

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

src/view-model/abstract-view-model.store.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ describe('AbstractViewModelStore', () => {
5656
expect(vmStore.instanceAttachedCount.get('1')).toBe(undefined);
5757
});
5858

59+
it('is able to get total mounted views count', async () => {
60+
const vmStore = new TestViewModelStoreImpl();
61+
await vmStore.attach(new TestAbstractViewModelImpl({ id: '1' }));
62+
await vmStore.attach(new TestAbstractViewModelImpl({ id: '1' }));
63+
await vmStore.attach(new TestAbstractViewModelImpl({ id: '2' }));
64+
await vmStore.attach(new TestAbstractViewModelImpl({ id: '2' }));
65+
await vmStore.attach(new TestAbstractViewModelImpl({ id: '3' }));
66+
await vmStore.attach(new TestAbstractViewModelImpl({ id: '3' }));
67+
expect(vmStore.mountedViewsCount).toBe(6);
68+
});
69+
5970
it('accessing to parent view models using store [using parentViewModelId and vmStore]', async () => {
6071
class TestViewModelImpl1<
6172
Payload extends AnyObject = EmptyObject,

src/view-model/abstract-view-model.store.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { last } from 'lodash-es';
21
import {
32
action,
43
computed,
@@ -27,12 +26,12 @@ export abstract class AbstractViewModelStore<
2726
instanceAttachedCount = new Map<string, number>();
2827

2928
/**
30-
* Views waiting for loading
29+
* Views waiting for mount
3130
*/
3231
mountingViews = observable.set<string>();
3332

3433
/**
35-
* Вьюшки, ожидающие выгрузки
34+
* Views waiting for unmount
3635
*/
3736
unmountingViews = observable.set<string>();
3837

@@ -70,14 +69,20 @@ export abstract class AbstractViewModelStore<
7069
getId<T extends VMBase>(idOrClass: Maybe<string | Class<T>>): string | null {
7170
if (!idOrClass) return null;
7271

73-
const id =
74-
typeof idOrClass === 'string'
75-
? idOrClass
76-
: last(this.viewModelsByClasses.get(idOrClass.name));
72+
if (typeof idOrClass === 'string') {
73+
return idOrClass;
74+
}
7775

78-
if (!id) return null;
76+
const className = idOrClass.name;
77+
const viewModels = this.viewModelsByClasses.get(className) || [];
78+
79+
if (process.env.NODE_ENV !== 'production' && viewModels.length > 1) {
80+
console.warn(
81+
`Found more than 1 view model with the same identifier "${className}". Last instance will been returned`,
82+
);
83+
}
7984

80-
return id;
85+
return viewModels.at(-1)!;
8186
}
8287

8388
has<T extends VMBase>(idOrClass: Maybe<string | Class<T>>): boolean {

0 commit comments

Comments
 (0)