Repository navigation
Expand file tree
/
Copy pathInstanceManager.ts
More file actions
146 lines (119 loc) · 4.05 KB
/
Copy pathInstanceManager.ts
File metadata and controls
146 lines (119 loc) · 4.05 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* 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 type {HoistBase, HoistService, HoistModel} from '../';
import {isNil} from 'lodash';
import type {Cube, Store, View} from '@xh/hoist/data';
import {observableShallow, runInAction} from '@xh/hoist/mobx';
import {wait} from '@xh/hoist/promise';
import {useEffect} from 'react';
/**
* Generic singleton object for cataloging global models and services.
*
* Everything is held weakly, so cataloging an object never keeps it in memory. One that is never
* destroyed is still collected once nothing else references it.
* @internal
*/
class InstanceManager {
readonly services = new IterableWeakSet<HoistService>();
readonly models = new IterableWeakSet<HoistModel>();
readonly stores = new IterableWeakSet<Store>();
readonly cubes = new IterableWeakSet<Cube>();
readonly views = new IterableWeakSet<View>();
private modelsByTestId: Map<string, HoistModel> = new Map();
private testSupportedModels = new Set([
'GridModel',
'DataViewModel',
'FormModel',
'TabModel',
'ViewManagerModel'
]);
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));
}
registerCube(c: Cube) {
wait().thenAction(() => this.cubes.add(c));
}
unregisterCube(c: Cube) {
wait().thenAction(() => this.cubes.delete(c));
}
registerView(v: View) {
wait().thenAction(() => this.views.add(v));
}
unregisterView(v: View) {
wait().thenAction(() => this.views.delete(v));
}
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);
}
useTestIdRegistration(testId: string, model: HoistModel) {
useEffect(() => {
this.registerModelWithTestId(testId, model);
return () => this.unregisterModelWithTestId(testId);
}, [testId, model]);
}
}
/**
* An observable, insertion-ordered collection that holds its members weakly. Members are dropped
* when deleted, or when garbage collected.
*/
class IterableWeakSet<T extends HoistBase> {
@observableShallow private accessor refs: Map<string, WeakRef<T>> = new Map();
// Called with a member's xhId after the member is garbage collected.
private registry = new FinalizationRegistry<string>(xhId => {
runInAction(() => this.refs.delete(xhId));
});
/** Number of members, including any collected but not yet finalized. */
get size(): number {
return this.refs.size;
}
add(member: T) {
const {xhId} = member,
ref = new WeakRef(member);
this.registry.register(member, xhId, ref);
this.refs.set(xhId, ref);
}
delete(member: T) {
const {xhId} = member,
ref = this.refs.get(xhId);
if (!ref) return;
this.registry.unregister(ref);
this.refs.delete(xhId);
}
*[Symbol.iterator](): Iterator<T> {
for (const ref of this.refs.values()) {
const member = ref.deref();
if (member) yield member;
}
}
}
export const instanceManager = new InstanceManager();