@asyra/feature-system 0.5.4 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/execution-registry.d.ts +3 -0
- package/dist/core/execution-registry.d.ts.map +1 -1
- package/dist/core/execution-registry.js +14 -4
- package/dist/core/execution-registry.js.map +1 -1
- package/dist/core/feature-registry.d.ts +3 -0
- package/dist/core/feature-registry.d.ts.map +1 -1
- package/dist/core/feature-registry.js +19 -0
- package/dist/core/feature-registry.js.map +1 -1
- package/dist/core/feature-task-registry.d.ts.map +1 -1
- package/dist/core/feature-task-registry.js +6 -3
- package/dist/core/feature-task-registry.js.map +1 -1
- package/dist/core/feature.d.ts +19 -13
- package/dist/core/feature.d.ts.map +1 -1
- package/dist/core/feature.js +343 -284
- package/dist/core/feature.js.map +1 -1
- package/dist/core/interaction-queue.d.ts +5 -0
- package/dist/core/interaction-queue.d.ts.map +1 -1
- package/dist/core/interaction-queue.js +65 -6
- package/dist/core/interaction-queue.js.map +1 -1
- package/dist/core/session-manager.d.ts +21 -4
- package/dist/core/session-manager.d.ts.map +1 -1
- package/dist/core/session-manager.js +74 -39
- package/dist/core/session-manager.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/types/core-packages.d.ts +3 -0
- package/dist/types/core-packages.d.ts.map +1 -1
- package/dist/types/execution.d.ts +2 -0
- package/dist/types/execution.d.ts.map +1 -1
- package/dist/types/feature.d.ts +3 -3
- package/dist/types/feature.d.ts.map +1 -1
- package/dist/types/session.d.ts +1 -1
- package/dist/types/session.d.ts.map +1 -1
- package/dist/types/session.js.map +1 -1
- package/dist/utils/micro-features.d.ts.map +1 -1
- package/dist/utils/micro-features.js.map +1 -1
- package/package.json +5 -5
package/dist/core/feature.js
CHANGED
|
@@ -1,22 +1,10 @@
|
|
|
1
|
-
import { measureBrowserDragAsyncPhase } from '@asyra/utils';
|
|
1
|
+
import { SessionCancelPolicies, measureBrowserDragAsyncPhase } from '@asyra/utils';
|
|
2
2
|
import { FeatureRegistry } from './feature-registry.js';
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
let sessionManager = new SessionManager();
|
|
9
|
-
let runtimeState = 'open';
|
|
10
|
-
let runtimeIdentity = Symbol('feature-runtime');
|
|
11
|
-
let disposal = null;
|
|
12
|
-
const assertRuntimeOpen = () => {
|
|
13
|
-
if (runtimeState !== 'open')
|
|
14
|
-
throw new FeatureRuntimeClosedError();
|
|
15
|
-
};
|
|
16
|
-
let corePackages = {};
|
|
17
|
-
let isPackagesSet = false;
|
|
18
|
-
const eventBindings = new Map();
|
|
19
|
-
const pendingRegistrations = [];
|
|
3
|
+
import { subscribeToEventBatches } from '@asyra/reactive-events';
|
|
4
|
+
import { SessionManager, SessionRuntime, sharedSessions } from './session-manager.js';
|
|
5
|
+
import sharedExecution, { ExecutionRegistryClass } from './execution-registry.js';
|
|
6
|
+
import { FeatureRuntimeClosedError, InteractionQueue } from './interaction-queue.js';
|
|
7
|
+
import { featureTaskRegistry as sharedTasks, FeatureTaskRegistry } from './feature-task-registry.js';
|
|
20
8
|
export class FeatureUnregisterError extends Error {
|
|
21
9
|
constructor(featureName) {
|
|
22
10
|
super(`Feature "${featureName}" cannot be unregistered while active`);
|
|
@@ -25,311 +13,382 @@ export class FeatureUnregisterError extends Error {
|
|
|
25
13
|
this.featureName = featureName;
|
|
26
14
|
}
|
|
27
15
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
for (const [eventName, binding] of eventBindings) {
|
|
42
|
-
const inUse = binding.participantType === 'session'
|
|
43
|
-
? sessionManager.hasSessionHandlers(binding.sessionName)
|
|
44
|
-
: executionRegistry.hasHandlers(eventName);
|
|
45
|
-
if (!inUse) {
|
|
46
|
-
removeEventBinding(eventName);
|
|
16
|
+
export function createFeatureSystem(owner) {
|
|
17
|
+
return composeFeatureSystem(new SessionRuntime(new InteractionQueue(), owner), new ExecutionRegistryClass(owner), new FeatureTaskRegistry());
|
|
18
|
+
}
|
|
19
|
+
function composeFeatureSystem(sessions, executionRegistry, featureTaskRegistry) {
|
|
20
|
+
const interactionQueue = sessions.queue;
|
|
21
|
+
const featureRegistry = new FeatureRegistry();
|
|
22
|
+
let sessionManager = new SessionManager(sessions);
|
|
23
|
+
let runtimeState = 'open';
|
|
24
|
+
let runtimeIdentity = Symbol('feature-runtime');
|
|
25
|
+
let disposal = null;
|
|
26
|
+
const assertRuntimeOpen = () => {
|
|
27
|
+
if (runtimeState !== 'open') {
|
|
28
|
+
throw new FeatureRuntimeClosedError();
|
|
47
29
|
}
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
const createInputSnapshot = (systemContext, raw) => {
|
|
51
|
-
const snapshot = systemContext.getSystemContextSnapshot?.() ?? raw;
|
|
52
|
-
return {
|
|
53
|
-
...snapshot,
|
|
54
|
-
...(raw.detail ? { detail: raw.detail } : {})
|
|
55
30
|
};
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
if (eventName.endsWith('.update')) {
|
|
65
|
-
phase = 'update';
|
|
31
|
+
let corePackages = {};
|
|
32
|
+
let isPackagesSet = false;
|
|
33
|
+
const eventBindings = new Map();
|
|
34
|
+
const pendingRegistrations = [];
|
|
35
|
+
const removeEventBinding = (eventName) => {
|
|
36
|
+
const binding = eventBindings.get(eventName);
|
|
37
|
+
if (!binding) {
|
|
38
|
+
return;
|
|
66
39
|
}
|
|
67
|
-
|
|
68
|
-
|
|
40
|
+
eventBindings.delete(eventName);
|
|
41
|
+
binding.cleanupRequested = true;
|
|
42
|
+
if (binding.inputSystem && binding.inputCallback) {
|
|
43
|
+
binding.inputSystem.off(eventName, binding.inputCallback);
|
|
69
44
|
}
|
|
70
|
-
|
|
45
|
+
binding.subscription?.unsubscribe();
|
|
71
46
|
};
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
};
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
return
|
|
85
|
-
|
|
86
|
-
|
|
47
|
+
const cleanupUnusedEventBindings = () => {
|
|
48
|
+
for (const [eventName, binding] of eventBindings) {
|
|
49
|
+
const inUse = binding.participantType === 'session'
|
|
50
|
+
? sessionManager.hasSessionHandlers(binding.sessionName)
|
|
51
|
+
: executionRegistry.hasHandlers(eventName);
|
|
52
|
+
if (!inUse) {
|
|
53
|
+
removeEventBinding(eventName);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
const createInputSnapshot = (systemContext, raw) => {
|
|
58
|
+
const snapshot = systemContext.getSystemContextSnapshot?.() ?? raw;
|
|
59
|
+
return {
|
|
60
|
+
...snapshot,
|
|
61
|
+
...(raw.detail ? { detail: raw.detail } : {})
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
const registerSessionEventBinding = (eventName, sessionName, inputSystem, systemContext) => {
|
|
65
|
+
if (eventBindings.has(eventName)) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const manager = sessionManager;
|
|
69
|
+
const inputCallback = async (raw) => {
|
|
70
|
+
let phase = 'end';
|
|
71
|
+
if (eventName.endsWith('.update')) {
|
|
72
|
+
phase = 'update';
|
|
73
|
+
}
|
|
74
|
+
if (eventName.endsWith('.start')) {
|
|
75
|
+
phase = 'start';
|
|
76
|
+
}
|
|
77
|
+
await measureBrowserDragAsyncPhase(`feature:event:${eventName}`, () => manager.handleSessionInput(sessionName, phase, () => createInputSnapshot(systemContext, raw), eventName));
|
|
78
|
+
};
|
|
79
|
+
inputSystem.on(eventName, inputCallback);
|
|
80
|
+
eventBindings.set(eventName, {
|
|
81
|
+
eventName,
|
|
82
|
+
participantType: 'session',
|
|
83
|
+
sessionName,
|
|
84
|
+
inputSystem,
|
|
85
|
+
inputCallback,
|
|
86
|
+
cleanupRequested: false
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
const registerExecutionEventBinding = (eventName, inputSystem, systemContext) => {
|
|
90
|
+
if (eventBindings.has(eventName)) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
if (eventName.startsWith('render.')) {
|
|
94
|
+
const binding = {
|
|
95
|
+
eventName,
|
|
96
|
+
participantType: 'execution',
|
|
97
|
+
cleanupRequested: false
|
|
98
|
+
};
|
|
99
|
+
eventBindings.set(eventName, binding);
|
|
100
|
+
binding.subscription = subscribeToEventBatches((events) => {
|
|
101
|
+
for (const raw of events) {
|
|
102
|
+
if (binding.cleanupRequested || raw.type !== eventName) {
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
const owner = 'interactionOwner' in raw ? raw.interactionOwner : undefined;
|
|
106
|
+
if (owner !== corePackages.render?.interactionOwner) {
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
const payload = 'payload' in raw ? raw.payload : undefined;
|
|
110
|
+
const snapshot = systemContext.getSystemContextSnapshot?.() ?? raw;
|
|
111
|
+
const mergedSnapshot = {
|
|
112
|
+
...snapshot,
|
|
113
|
+
detail: ('detail' in raw ? raw.detail : undefined) ?? payload,
|
|
114
|
+
payload
|
|
115
|
+
};
|
|
116
|
+
void interactionQueue
|
|
117
|
+
.run(() => executionRegistry.execute(eventName, mergedSnapshot))
|
|
118
|
+
.catch(console.error);
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
const manager = sessionManager;
|
|
87
124
|
const binding = {
|
|
88
125
|
eventName,
|
|
89
126
|
participantType: 'execution',
|
|
127
|
+
inputSystem,
|
|
90
128
|
cleanupRequested: false
|
|
91
129
|
};
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
.
|
|
97
|
-
|
|
98
|
-
if (binding.cleanupRequested
|
|
99
|
-
|
|
130
|
+
const inputCallback = async (raw) => {
|
|
131
|
+
const run = executionRegistry.getHandlers(eventName)[0]?.executionMode ===
|
|
132
|
+
'immediate'
|
|
133
|
+
? manager.runImmediatelyAfterCancellingActiveSessions.bind(manager)
|
|
134
|
+
: manager.runAfterCancellingActiveSessions.bind(manager);
|
|
135
|
+
await run(() => {
|
|
136
|
+
if (binding.cleanupRequested) {
|
|
137
|
+
throw new FeatureRuntimeClosedError();
|
|
100
138
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
139
|
+
return createInputSnapshot(systemContext, raw);
|
|
140
|
+
}, (mergedSnapshot) => binding.cleanupRequested
|
|
141
|
+
? undefined
|
|
142
|
+
: executionRegistry.execute(eventName, mergedSnapshot), eventName);
|
|
143
|
+
};
|
|
144
|
+
binding.inputCallback = inputCallback;
|
|
145
|
+
inputSystem.on(eventName, inputCallback);
|
|
146
|
+
eventBindings.set(eventName, binding);
|
|
147
|
+
};
|
|
148
|
+
function registerFeatureHandlers(name, keyConfig, definition) {
|
|
149
|
+
const hasSession = !!definition.session;
|
|
150
|
+
const hasExecution = !!definition.execution;
|
|
151
|
+
const { priority = 0, exclusive = true, cancelPolicy = SessionCancelPolicies.COMMIT_CURRENT } = definition;
|
|
152
|
+
if (!keyConfig) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
let eventsToRegister = [];
|
|
156
|
+
if (hasSession) {
|
|
157
|
+
const baseEvent = keyConfig;
|
|
158
|
+
eventsToRegister = [
|
|
159
|
+
`${baseEvent}.start`,
|
|
160
|
+
`${baseEvent}.update`,
|
|
161
|
+
`${baseEvent}.end`
|
|
162
|
+
];
|
|
163
|
+
}
|
|
164
|
+
else if (hasExecution) {
|
|
165
|
+
eventsToRegister = [keyConfig];
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
eventsToRegister = [keyConfig];
|
|
169
|
+
}
|
|
170
|
+
if (hasSession && definition.session) {
|
|
171
|
+
sessionManager.registerSession(keyConfig, name, priority, exclusive, cancelPolicy, {
|
|
172
|
+
...definition.session,
|
|
173
|
+
onStart: (snapshot) => featureRegistry.isInteractionEnabled(name)
|
|
174
|
+
? (definition.session?.onStart?.(snapshot) ?? null)
|
|
175
|
+
: null
|
|
110
176
|
});
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
177
|
+
}
|
|
178
|
+
if (hasExecution && definition.execution) {
|
|
179
|
+
const execute = definition.execution;
|
|
180
|
+
for (const eventName of eventsToRegister) {
|
|
181
|
+
executionRegistry.register(eventName, name, { priority, exclusive, executionMode: definition.executionMode }, (snapshot) => featureRegistry.isInteractionEnabled(name)
|
|
182
|
+
? execute(snapshot)
|
|
183
|
+
: null);
|
|
114
184
|
}
|
|
115
|
-
binding.subscription = subscription;
|
|
116
|
-
})
|
|
117
|
-
.catch(console.error);
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
const manager = sessionManager;
|
|
121
|
-
const inputCallback = async (raw) => {
|
|
122
|
-
await manager.runAfterCancellingActiveSessions(() => createInputSnapshot(systemContext, raw), (mergedSnapshot) => executionRegistry.execute(eventName, mergedSnapshot), eventName);
|
|
123
|
-
};
|
|
124
|
-
inputSystem.on(eventName, inputCallback);
|
|
125
|
-
eventBindings.set(eventName, {
|
|
126
|
-
eventName,
|
|
127
|
-
participantType: 'execution',
|
|
128
|
-
inputSystem,
|
|
129
|
-
inputCallback,
|
|
130
|
-
cleanupRequested: false
|
|
131
|
-
});
|
|
132
|
-
};
|
|
133
|
-
function registerFeatureHandlers(name, keyConfig, definition) {
|
|
134
|
-
const hasSession = !!definition.session;
|
|
135
|
-
const hasExecution = !!definition.execution;
|
|
136
|
-
const { priority = 0, exclusive = true, cancelPolicy = 'commit-current' } = definition;
|
|
137
|
-
if (!keyConfig) {
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
let eventsToRegister = [];
|
|
141
|
-
if (hasSession) {
|
|
142
|
-
const baseEvent = keyConfig;
|
|
143
|
-
eventsToRegister = [
|
|
144
|
-
`${baseEvent}.start`,
|
|
145
|
-
`${baseEvent}.update`,
|
|
146
|
-
`${baseEvent}.end`
|
|
147
|
-
];
|
|
148
|
-
}
|
|
149
|
-
else if (hasExecution) {
|
|
150
|
-
eventsToRegister = [keyConfig];
|
|
151
|
-
}
|
|
152
|
-
else {
|
|
153
|
-
eventsToRegister = [keyConfig];
|
|
154
|
-
}
|
|
155
|
-
if (hasSession && definition.session) {
|
|
156
|
-
sessionManager.registerSession(keyConfig, name, priority, exclusive, cancelPolicy, definition.session);
|
|
157
|
-
}
|
|
158
|
-
if (hasExecution && definition.execution) {
|
|
159
|
-
for (const eventName of eventsToRegister) {
|
|
160
|
-
executionRegistry.register(eventName, name, { priority, exclusive }, definition.execution);
|
|
161
185
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
186
|
+
const { inputSystem, systemContext } = corePackages;
|
|
187
|
+
if (inputSystem && systemContext) {
|
|
188
|
+
for (const event of eventsToRegister) {
|
|
189
|
+
if (hasSession) {
|
|
190
|
+
if (event.startsWith('render.')) {
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
registerSessionEventBinding(event, keyConfig, inputSystem, systemContext);
|
|
194
|
+
}
|
|
195
|
+
else if (hasExecution) {
|
|
196
|
+
registerExecutionEventBinding(event, inputSystem, systemContext);
|
|
169
197
|
}
|
|
170
|
-
registerSessionEventBinding(event, keyConfig, inputSystem, systemContext);
|
|
171
|
-
}
|
|
172
|
-
else if (hasExecution) {
|
|
173
|
-
registerExecutionEventBinding(event, inputSystem, systemContext);
|
|
174
198
|
}
|
|
175
199
|
}
|
|
176
200
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
throw new Error(`Feature ${name} uses feature-defined cancelPolicy without onCancel`);
|
|
185
|
-
}
|
|
186
|
-
const api = featureRegistry.register(name, definition);
|
|
187
|
-
if (definition.task) {
|
|
188
|
-
featureTaskRegistry.register(name, {
|
|
189
|
-
priority: definition.priority ?? 0,
|
|
190
|
-
exclusive: definition.exclusive ?? true,
|
|
191
|
-
handler: definition.task
|
|
192
|
-
});
|
|
193
|
-
}
|
|
194
|
-
const hasSession = !!definition.session;
|
|
195
|
-
const hasExecution = !!definition.execution;
|
|
196
|
-
if ((hasSession || hasExecution) && keyConfig !== undefined) {
|
|
197
|
-
if (isPackagesSet) {
|
|
198
|
-
registerFeatureHandlers(name, keyConfig, definition);
|
|
201
|
+
function defineFeature(name, keyConfig, definition) {
|
|
202
|
+
assertRuntimeOpen();
|
|
203
|
+
const identity = runtimeIdentity;
|
|
204
|
+
if (definition.session &&
|
|
205
|
+
definition.cancelPolicy === SessionCancelPolicies.FEATURE_DEFINED &&
|
|
206
|
+
!definition.session.onCancel) {
|
|
207
|
+
throw new Error(`Feature ${name} uses feature-defined cancelPolicy without onCancel`);
|
|
199
208
|
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
keyConfig
|
|
204
|
-
|
|
209
|
+
if (definition.executionMode === 'immediate' &&
|
|
210
|
+
(!definition.execution ||
|
|
211
|
+
definition.session ||
|
|
212
|
+
!keyConfig ||
|
|
213
|
+
keyConfig.startsWith('render.'))) {
|
|
214
|
+
throw new Error('Immediate execution requires a one-shot input Feature');
|
|
215
|
+
}
|
|
216
|
+
if (definition.execution && keyConfig) {
|
|
217
|
+
const mode = definition.executionMode ?? 'queued';
|
|
218
|
+
const conflicts = executionRegistry
|
|
219
|
+
.getHandlers(keyConfig)
|
|
220
|
+
.some((entry) => entry.executionMode !== mode) ||
|
|
221
|
+
pendingRegistrations.some((entry) => entry.keyConfig === keyConfig &&
|
|
222
|
+
entry.definition.execution &&
|
|
223
|
+
(entry.definition.executionMode ?? 'queued') !== mode);
|
|
224
|
+
if (conflicts) {
|
|
225
|
+
throw new Error(`Execution event ${keyConfig} cannot mix execution modes`);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
const api = featureRegistry.register(name, definition);
|
|
229
|
+
if (definition.task) {
|
|
230
|
+
featureTaskRegistry.register(name, {
|
|
231
|
+
priority: definition.priority ?? 0,
|
|
232
|
+
exclusive: definition.exclusive ?? true,
|
|
233
|
+
handler: definition.task
|
|
205
234
|
});
|
|
206
235
|
}
|
|
236
|
+
const hasSession = !!definition.session;
|
|
237
|
+
const hasExecution = !!definition.execution;
|
|
238
|
+
if ((hasSession || hasExecution) && keyConfig !== undefined) {
|
|
239
|
+
if (isPackagesSet) {
|
|
240
|
+
registerFeatureHandlers(name, keyConfig, definition);
|
|
241
|
+
}
|
|
242
|
+
else {
|
|
243
|
+
pendingRegistrations.push({
|
|
244
|
+
featureName: name,
|
|
245
|
+
keyConfig,
|
|
246
|
+
definition: definition
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return {
|
|
251
|
+
api,
|
|
252
|
+
dispose: () => identity === runtimeIdentity && unregisterFeature(name)
|
|
253
|
+
};
|
|
207
254
|
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
registerFeatureHandlers(registration.featureName, registration.keyConfig, registration.definition);
|
|
255
|
+
function setCorePackages(packages) {
|
|
256
|
+
assertRuntimeOpen();
|
|
257
|
+
corePackages = packages;
|
|
258
|
+
isPackagesSet = true;
|
|
259
|
+
for (const registration of pendingRegistrations) {
|
|
260
|
+
try {
|
|
261
|
+
registerFeatureHandlers(registration.featureName, registration.keyConfig, registration.definition);
|
|
262
|
+
}
|
|
263
|
+
catch (error) {
|
|
264
|
+
console.error(`[defineFeature] Failed to register "${registration.featureName}":`, error);
|
|
265
|
+
}
|
|
220
266
|
}
|
|
221
|
-
|
|
222
|
-
|
|
267
|
+
pendingRegistrations.length = 0;
|
|
268
|
+
}
|
|
269
|
+
function getFeature(featureName) {
|
|
270
|
+
if (!featureRegistry.has(featureName)) {
|
|
271
|
+
throw new Error(`Feature "${featureName}" not found`);
|
|
223
272
|
}
|
|
273
|
+
return featureRegistry.getAPI(featureName);
|
|
224
274
|
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
275
|
+
function unregisterFeature(featureName) {
|
|
276
|
+
if (!featureRegistry.has(featureName)) {
|
|
277
|
+
return false;
|
|
278
|
+
}
|
|
279
|
+
if (executionRegistry.isFeatureActive(featureName) ||
|
|
280
|
+
sessionManager.isFeatureActive(featureName) ||
|
|
281
|
+
featureTaskRegistry.isActive(featureName)) {
|
|
282
|
+
throw new FeatureUnregisterError(featureName);
|
|
283
|
+
}
|
|
284
|
+
for (let index = pendingRegistrations.length - 1; index >= 0; index -= 1) {
|
|
285
|
+
if (pendingRegistrations[index].featureName === featureName) {
|
|
286
|
+
pendingRegistrations.splice(index, 1);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
executionRegistry.unregisterFeature(featureName);
|
|
290
|
+
sessionManager.unregisterFeature(featureName);
|
|
291
|
+
featureTaskRegistry.unregister(featureName);
|
|
292
|
+
const removed = featureRegistry.unregister(featureName);
|
|
293
|
+
cleanupUnusedEventBindings();
|
|
294
|
+
return removed;
|
|
230
295
|
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
export function unregisterFeature(featureName) {
|
|
234
|
-
if (!featureRegistry.has(featureName)) {
|
|
235
|
-
return false;
|
|
296
|
+
function getFeatureRegistry() {
|
|
297
|
+
return featureRegistry;
|
|
236
298
|
}
|
|
237
|
-
|
|
238
|
-
sessionManager
|
|
239
|
-
featureTaskRegistry.isActive(featureName)) {
|
|
240
|
-
throw new FeatureUnregisterError(featureName);
|
|
299
|
+
function getSessionManager() {
|
|
300
|
+
return sessionManager;
|
|
241
301
|
}
|
|
242
|
-
|
|
243
|
-
if (
|
|
244
|
-
|
|
302
|
+
function invokeFeatureTask(featureName, input, options) {
|
|
303
|
+
if (runtimeState !== 'open') {
|
|
304
|
+
return Promise.reject(new FeatureRuntimeClosedError());
|
|
245
305
|
}
|
|
306
|
+
return featureTaskRegistry.invoke(featureName, input, options);
|
|
246
307
|
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
});
|
|
278
|
-
runtimeState = 'closing';
|
|
279
|
-
const draining = interactionQueue.close();
|
|
280
|
-
SessionManager.abortRuntime();
|
|
281
|
-
const tasks = featureTaskRegistry.dispose();
|
|
282
|
-
// The shared completion is installed before abort/off can reenter disposal.
|
|
283
|
-
void (async () => {
|
|
284
|
-
const errors = [];
|
|
285
|
-
for (const eventName of [...eventBindings.keys()]) {
|
|
308
|
+
function cancelFeatureTask(featureName, reason) {
|
|
309
|
+
return featureTaskRegistry.cancel(featureName, reason);
|
|
310
|
+
}
|
|
311
|
+
/** Core lifecycle boundary, not a user-command or ordinary cancel path. */
|
|
312
|
+
function disposeFeatureSystem(getSnapshot) {
|
|
313
|
+
if (disposal) {
|
|
314
|
+
return disposal;
|
|
315
|
+
}
|
|
316
|
+
let complete;
|
|
317
|
+
let fail;
|
|
318
|
+
disposal = new Promise((resolve, reject) => {
|
|
319
|
+
complete = resolve;
|
|
320
|
+
fail = reject;
|
|
321
|
+
});
|
|
322
|
+
runtimeState = 'closing';
|
|
323
|
+
const draining = interactionQueue.close();
|
|
324
|
+
sessionManager.abortRuntime();
|
|
325
|
+
const tasks = featureTaskRegistry.dispose();
|
|
326
|
+
// The shared completion is installed before abort/off can reenter disposal.
|
|
327
|
+
void (async () => {
|
|
328
|
+
const errors = [];
|
|
329
|
+
for (const eventName of [...eventBindings.keys()]) {
|
|
330
|
+
try {
|
|
331
|
+
removeEventBinding(eventName);
|
|
332
|
+
}
|
|
333
|
+
catch (error) {
|
|
334
|
+
errors.push(error);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
await draining;
|
|
286
338
|
try {
|
|
287
|
-
|
|
339
|
+
await sessionManager.disposeRuntime(getSnapshot);
|
|
288
340
|
}
|
|
289
341
|
catch (error) {
|
|
290
342
|
errors.push(error);
|
|
291
343
|
}
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
await tasks;
|
|
301
|
-
for (const name of featureRegistry.getFeatureNames()) {
|
|
302
|
-
try {
|
|
303
|
-
unregisterFeature(name);
|
|
344
|
+
await tasks;
|
|
345
|
+
for (const name of featureRegistry.getFeatureNames()) {
|
|
346
|
+
try {
|
|
347
|
+
unregisterFeature(name);
|
|
348
|
+
}
|
|
349
|
+
catch (error) {
|
|
350
|
+
errors.push(error);
|
|
351
|
+
}
|
|
304
352
|
}
|
|
305
|
-
|
|
306
|
-
|
|
353
|
+
pendingRegistrations.length = 0;
|
|
354
|
+
corePackages = {};
|
|
355
|
+
isPackagesSet = false;
|
|
356
|
+
if (errors.length > 0) {
|
|
357
|
+
throw errors[0];
|
|
307
358
|
}
|
|
359
|
+
runtimeState = 'closed';
|
|
360
|
+
})().then(complete, (error) => {
|
|
361
|
+
runtimeState = 'failed';
|
|
362
|
+
fail(error);
|
|
363
|
+
});
|
|
364
|
+
return disposal;
|
|
365
|
+
}
|
|
366
|
+
/** Core calls this only after every old runtime owner has completed cleanup. */
|
|
367
|
+
function beginFeatureSystemRuntime() {
|
|
368
|
+
if (runtimeState !== 'closed') {
|
|
369
|
+
throw new FeatureRuntimeClosedError();
|
|
308
370
|
}
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
sessionManager = new SessionManager();
|
|
329
|
-
runtimeIdentity = Symbol('feature-runtime');
|
|
330
|
-
disposal = null;
|
|
331
|
-
runtimeState = 'open';
|
|
371
|
+
interactionQueue.beginRuntime();
|
|
372
|
+
featureTaskRegistry.beginRuntime();
|
|
373
|
+
sessionManager = new SessionManager(sessions);
|
|
374
|
+
runtimeIdentity = Symbol('feature-runtime');
|
|
375
|
+
disposal = null;
|
|
376
|
+
runtimeState = 'open';
|
|
377
|
+
}
|
|
378
|
+
return {
|
|
379
|
+
defineFeature,
|
|
380
|
+
setCorePackages,
|
|
381
|
+
getFeature,
|
|
382
|
+
unregisterFeature,
|
|
383
|
+
getFeatureRegistry,
|
|
384
|
+
getSessionManager,
|
|
385
|
+
invokeFeatureTask,
|
|
386
|
+
cancelFeatureTask,
|
|
387
|
+
disposeFeatureSystem,
|
|
388
|
+
beginFeatureSystemRuntime
|
|
389
|
+
};
|
|
332
390
|
}
|
|
391
|
+
export const { defineFeature, setCorePackages, getFeature, unregisterFeature, getFeatureRegistry, getSessionManager, invokeFeatureTask, cancelFeatureTask, disposeFeatureSystem, beginFeatureSystemRuntime } = composeFeatureSystem(sharedSessions, sharedExecution, sharedTasks);
|
|
333
392
|
export { FeatureRegistry } from './feature-registry.js';
|
|
334
393
|
export { SessionManager } from './session-manager.js';
|
|
335
394
|
//# sourceMappingURL=feature.js.map
|