This repository was archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 320
/
Copy pathLabelStudio.js
168 lines (140 loc) · 4.24 KB
/
LabelStudio.js
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { createRoot } from 'react-dom/client';
import App from './components/App/App';
import { configureStore } from './configureStore';
import { LabelStudio as LabelStudioReact } from './Component';
import { registerPanels } from './registerPanels';
import { configure } from 'mobx';
import { EventInvoker } from './utils/events';
import legacyEvents from './core/External';
import { toCamelCase } from 'strman';
import { isDefined } from './utils/utilities';
import { Hotkey } from './core/Hotkey';
import defaultOptions from './defaultOptions';
import { destroy } from 'mobx-state-tree';
import { destroy as destroySharedStore } from './mixins/SharedChoiceStore/mixin';
import { cleanDomAfterReact, findReactKey } from './utils/reactCleaner';
import { FF_LSDV_4620_3_ML, isFF } from './utils/feature-flags';
import { StrictMode } from 'react';
configure({
isolateGlobalState: true,
});
export class LabelStudio {
static instances = new Set();
static destroyAll() {
this.instances.forEach(inst => inst.destroy?.());
this.instances.clear();
}
constructor(root, userOptions = {}) {
const options = Object.assign({}, defaultOptions, userOptions ?? {});
if (options.keymap) {
Hotkey.setKeymap(options.keymap);
}
this.root = root;
this.events = new EventInvoker();
this.options = options ?? {};
this.destroy = (() => { /* noop */ });
this.supportLgacyEvents(options);
this.createApp();
this.constructor.instances.add(this);
}
on(...args) {
this.events.on(...args);
}
off(eventName, callback) {
if (isDefined(callback)) {
this.events.off(eventName, callback);
} else {
this.events.removeAll(eventName);
}
}
async createApp() {
const { store, getRoot } = await configureStore(this.options, this.events);
const rootElement = getRoot(this.root);
const appRoot = createRoot(rootElement);
this.store = store;
window.Htx = this.store;
const isRendered = false;
const renderApp = () => {
if (isRendered) {
unmountApp();
}
appRoot.render(
<StrictMode>
<App
store={this.store}
panels={registerPanels(this.options.panels) ?? []}
/>
</StrictMode>,
);
};
const PERFORM_REACT_CLEANUP = true;
const cleanupReactNodes = (callback) => {
let childNodes, reactKey;
if (PERFORM_REACT_CLEANUP) {
childNodes = Array.from(rootElement.childNodes);
console.log(childNodes);
// cleanDomAfterReact needs this key to be sure that cleaning affects
// only current react subtree
reactKey = findReactKey(childNodes[0]);
}
callback();
/*
Unmounting doesn't help with clearing React's fibers
but removing the manually helps
@see https://github.com/facebook/react/pull/20290 (similar problem)
That's maybe not relevant in version 18
*/
if (childNodes && reactKey) {
cleanDomAfterReact(childNodes, reactKey);
cleanDomAfterReact([rootElement], reactKey);
}
};
const unmountApp = () => {
cleanupReactNodes(() => {
appRoot.unmount();
});
};
renderApp();
store.setAppControls({
isRendered() {
return isRendered;
},
render: renderApp,
clear: unmountApp,
});
this.destroy = () => {
if (isFF(FF_LSDV_4620_3_ML)) {
unmountApp();
}
destroySharedStore();
if (isFF(FF_LSDV_4620_3_ML)) {
/*
It seems that destroying children separately helps GC to collect garbage
...
*/
this.store.selfDestroy();
}
destroy(this.store);
if (isFF(FF_LSDV_4620_3_ML)) {
/*
...
as well as nulling all these this.store
*/
this.store = null;
this.destroy = null;
this.constructor.instances.delete(this);
}
};
}
supportLgacyEvents() {
const keys = Object.keys(legacyEvents);
keys.forEach(key => {
const callback = this.options[key];
if (isDefined(callback)) {
const eventName = toCamelCase(key.replace(/^on/, ''));
this.events.on(eventName, callback);
}
});
}
}
LabelStudio.Component = LabelStudioReact;