-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathInstanceManager.ts
More file actions
75 lines (61 loc) · 1.99 KB
/
Copy pathInstanceManager.ts
File metadata and controls
75 lines (61 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* This file belongs to Hoist, an application development toolkit
* developed by Extremely Heavy Industries (www.xh.io | info@xh.io)
*
* Copyright © 2026 Extremely Heavy Industries Inc.
*/
import {HoistService, HoistModel} from '../';
import {isNil} from 'lodash';
import {Store} from '@xh/hoist/data';
import {observable, makeObservable} from '@xh/hoist/mobx';
import {wait} from '@xh/hoist/promise';
/**
* Generic singleton object for cataloging global models and services.
* @internal
*/
class InstanceManager {
@observable.shallow
services: Set<HoistService> = new Set();
@observable.shallow
models: Set<HoistModel> = new Set();
@observable.shallow
stores: Set<Store> = new Set();
private modelsByTestId: Map<string, HoistModel> = new Map();
private testSupportedModels = new Set(['GridModel', 'DataViewModel', 'FormModel', 'TabModel']);
registerModel(m: HoistModel) {
wait().thenAction(() => this.models.add(m));
}
unregisterModel(m: HoistModel) {
wait().thenAction(() => this.models.delete(m));
}
registerService(s: HoistService) {
wait().thenAction(() => this.services.add(s));
}
registerStore(s: Store) {
wait().thenAction(() => this.stores.add(s));
}
unregisterStore(s: Store) {
wait().thenAction(() => this.stores.delete(s));
}
registerModelWithTestId(testId: string, m: HoistModel) {
if (
isNil(testId) ||
isNil(m) ||
!m.isHoistModel ||
!this.testSupportedModels.has(m.constructor.name) ||
this.modelsByTestId.has(testId)
)
return;
this.modelsByTestId.set(testId, m);
}
unregisterModelWithTestId(testId: string) {
this.modelsByTestId.delete(testId);
}
getModelByTestId(testId: string): HoistModel {
return this.modelsByTestId.get(testId);
}
constructor() {
makeObservable(this);
}
}
export const instanceManager = new InstanceManager();