@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.
Files changed (39) hide show
  1. package/dist/core/execution-registry.d.ts +3 -0
  2. package/dist/core/execution-registry.d.ts.map +1 -1
  3. package/dist/core/execution-registry.js +14 -4
  4. package/dist/core/execution-registry.js.map +1 -1
  5. package/dist/core/feature-registry.d.ts +3 -0
  6. package/dist/core/feature-registry.d.ts.map +1 -1
  7. package/dist/core/feature-registry.js +19 -0
  8. package/dist/core/feature-registry.js.map +1 -1
  9. package/dist/core/feature-task-registry.d.ts.map +1 -1
  10. package/dist/core/feature-task-registry.js +6 -3
  11. package/dist/core/feature-task-registry.js.map +1 -1
  12. package/dist/core/feature.d.ts +19 -13
  13. package/dist/core/feature.d.ts.map +1 -1
  14. package/dist/core/feature.js +343 -284
  15. package/dist/core/feature.js.map +1 -1
  16. package/dist/core/interaction-queue.d.ts +5 -0
  17. package/dist/core/interaction-queue.d.ts.map +1 -1
  18. package/dist/core/interaction-queue.js +65 -6
  19. package/dist/core/interaction-queue.js.map +1 -1
  20. package/dist/core/session-manager.d.ts +21 -4
  21. package/dist/core/session-manager.d.ts.map +1 -1
  22. package/dist/core/session-manager.js +74 -39
  23. package/dist/core/session-manager.js.map +1 -1
  24. package/dist/index.d.ts +1 -1
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +1 -1
  27. package/dist/index.js.map +1 -1
  28. package/dist/types/core-packages.d.ts +3 -0
  29. package/dist/types/core-packages.d.ts.map +1 -1
  30. package/dist/types/execution.d.ts +2 -0
  31. package/dist/types/execution.d.ts.map +1 -1
  32. package/dist/types/feature.d.ts +3 -3
  33. package/dist/types/feature.d.ts.map +1 -1
  34. package/dist/types/session.d.ts +1 -1
  35. package/dist/types/session.d.ts.map +1 -1
  36. package/dist/types/session.js.map +1 -1
  37. package/dist/utils/micro-features.d.ts.map +1 -1
  38. package/dist/utils/micro-features.js.map +1 -1
  39. package/package.json +5 -5
@@ -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 { SessionManager } from './session-manager.js';
4
- import executionRegistry from './execution-registry.js';
5
- import { FeatureRuntimeClosedError, interactionQueue } from './interaction-queue.js';
6
- import { featureTaskRegistry } from './feature-task-registry.js';
7
- const featureRegistry = new FeatureRegistry();
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
- const removeEventBinding = (eventName) => {
29
- const binding = eventBindings.get(eventName);
30
- if (!binding) {
31
- return;
32
- }
33
- eventBindings.delete(eventName);
34
- binding.cleanupRequested = true;
35
- if (binding.inputSystem && binding.inputCallback) {
36
- binding.inputSystem.off(eventName, binding.inputCallback);
37
- }
38
- binding.subscription?.unsubscribe();
39
- };
40
- const cleanupUnusedEventBindings = () => {
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
- const registerSessionEventBinding = (eventName, sessionName, inputSystem, systemContext) => {
58
- if (eventBindings.has(eventName)) {
59
- return;
60
- }
61
- const manager = sessionManager;
62
- const inputCallback = async (raw) => {
63
- let phase = 'end';
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
- if (eventName.endsWith('.start')) {
68
- phase = 'start';
40
+ eventBindings.delete(eventName);
41
+ binding.cleanupRequested = true;
42
+ if (binding.inputSystem && binding.inputCallback) {
43
+ binding.inputSystem.off(eventName, binding.inputCallback);
69
44
  }
70
- await measureBrowserDragAsyncPhase(`feature:event:${eventName}`, () => manager.handleSessionInput(sessionName, phase, () => createInputSnapshot(systemContext, raw), eventName));
45
+ binding.subscription?.unsubscribe();
71
46
  };
72
- inputSystem.on(eventName, inputCallback);
73
- eventBindings.set(eventName, {
74
- eventName,
75
- participantType: 'session',
76
- sessionName,
77
- inputSystem,
78
- inputCallback,
79
- cleanupRequested: false
80
- });
81
- };
82
- const registerExecutionEventBinding = (eventName, inputSystem, systemContext) => {
83
- if (eventBindings.has(eventName)) {
84
- return;
85
- }
86
- if (eventName.startsWith('render.')) {
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
- eventBindings.set(eventName, binding);
93
- import('@asyra/reactive-events')
94
- .then((module) => {
95
- const subscription = module
96
- .getEventBus()
97
- .subscribe((raw) => {
98
- if (binding.cleanupRequested || raw.type !== eventName) {
99
- return;
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
- const snapshot = systemContext.getSystemContextSnapshot?.() ?? raw;
102
- const mergedSnapshot = {
103
- ...snapshot,
104
- detail: raw.detail ?? raw.payload,
105
- payload: raw.payload
106
- };
107
- void interactionQueue
108
- .run(() => executionRegistry.execute(eventName, mergedSnapshot))
109
- .catch(console.error);
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
- if (binding.cleanupRequested) {
112
- subscription.unsubscribe();
113
- return;
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
- const { inputSystem, systemContext } = corePackages;
164
- if (inputSystem && systemContext) {
165
- for (const event of eventsToRegister) {
166
- if (hasSession) {
167
- if (event.startsWith('render.')) {
168
- continue;
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
- export function defineFeature(name, keyConfig, definition) {
179
- assertRuntimeOpen();
180
- const identity = runtimeIdentity;
181
- if (definition.session &&
182
- definition.cancelPolicy === 'feature-defined' &&
183
- !definition.session.onCancel) {
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
- else {
201
- pendingRegistrations.push({
202
- featureName: name,
203
- keyConfig,
204
- definition: definition
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
- return {
209
- api,
210
- dispose: () => identity === runtimeIdentity && unregisterFeature(name)
211
- };
212
- }
213
- export function setCorePackages(packages) {
214
- assertRuntimeOpen();
215
- corePackages = packages;
216
- isPackagesSet = true;
217
- for (const registration of pendingRegistrations) {
218
- try {
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
- catch (error) {
222
- console.error(`[defineFeature] Failed to register "${registration.featureName}":`, error);
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
- pendingRegistrations.length = 0;
226
- }
227
- export function getFeature(featureName) {
228
- if (!featureRegistry.has(featureName)) {
229
- throw new Error(`Feature "${featureName}" not found`);
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
- return featureRegistry.getAPI(featureName);
232
- }
233
- export function unregisterFeature(featureName) {
234
- if (!featureRegistry.has(featureName)) {
235
- return false;
296
+ function getFeatureRegistry() {
297
+ return featureRegistry;
236
298
  }
237
- if (executionRegistry.isFeatureActive(featureName) ||
238
- sessionManager.isFeatureActive(featureName) ||
239
- featureTaskRegistry.isActive(featureName)) {
240
- throw new FeatureUnregisterError(featureName);
299
+ function getSessionManager() {
300
+ return sessionManager;
241
301
  }
242
- for (let index = pendingRegistrations.length - 1; index >= 0; index -= 1) {
243
- if (pendingRegistrations[index].featureName === featureName) {
244
- pendingRegistrations.splice(index, 1);
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
- executionRegistry.unregisterFeature(featureName);
248
- sessionManager.unregisterFeature(featureName);
249
- featureTaskRegistry.unregister(featureName);
250
- const removed = featureRegistry.unregister(featureName);
251
- cleanupUnusedEventBindings();
252
- return removed;
253
- }
254
- export function getFeatureRegistry() {
255
- return featureRegistry;
256
- }
257
- export function getSessionManager() {
258
- return sessionManager;
259
- }
260
- export function invokeFeatureTask(featureName, input, options) {
261
- if (runtimeState !== 'open')
262
- return Promise.reject(new FeatureRuntimeClosedError());
263
- return featureTaskRegistry.invoke(featureName, input, options);
264
- }
265
- export function cancelFeatureTask(featureName, reason) {
266
- return featureTaskRegistry.cancel(featureName, reason);
267
- }
268
- /** Core lifecycle boundary, not a user-command or ordinary cancel path. */
269
- export function disposeFeatureSystem(getSnapshot) {
270
- if (disposal)
271
- return disposal;
272
- let complete;
273
- let fail;
274
- disposal = new Promise((resolve, reject) => {
275
- complete = resolve;
276
- fail = reject;
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
- removeEventBinding(eventName);
339
+ await sessionManager.disposeRuntime(getSnapshot);
288
340
  }
289
341
  catch (error) {
290
342
  errors.push(error);
291
343
  }
292
- }
293
- await draining;
294
- try {
295
- await SessionManager.disposeRuntime(getSnapshot);
296
- }
297
- catch (error) {
298
- errors.push(error);
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
- catch (error) {
306
- errors.push(error);
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
- pendingRegistrations.length = 0;
310
- corePackages = {};
311
- isPackagesSet = false;
312
- if (errors.length > 0) {
313
- throw errors[0];
314
- }
315
- runtimeState = 'closed';
316
- })().then(complete, (error) => {
317
- runtimeState = 'failed';
318
- fail(error);
319
- });
320
- return disposal;
321
- }
322
- /** Core calls this only after every old runtime owner has completed cleanup. */
323
- export function beginFeatureSystemRuntime() {
324
- if (runtimeState !== 'closed')
325
- throw new FeatureRuntimeClosedError();
326
- interactionQueue.beginRuntime();
327
- featureTaskRegistry.beginRuntime();
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