@cldmv/slothlet 3.3.0 → 3.3.2
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/README.md +6 -8
- package/REFERENCE.md +23 -0
- package/dist/lib/builders/api-assignment.mjs +1 -589
- package/dist/lib/builders/api_builder.mjs +1 -1385
- package/dist/lib/builders/builder.mjs +1 -78
- package/dist/lib/builders/modes-processor.mjs +1 -1800
- package/dist/lib/errors.mjs +9 -211
- package/dist/lib/factories/component-base.mjs +1 -80
- package/dist/lib/factories/context.mjs +1 -22
- package/dist/lib/handlers/api-cache-manager.mjs +1 -200
- package/dist/lib/handlers/api-manager.mjs +1 -2536
- package/dist/lib/handlers/context-async.mjs +1 -172
- package/dist/lib/handlers/context-live.mjs +1 -173
- package/dist/lib/handlers/hook-manager.mjs +1 -667
- package/dist/lib/handlers/lifecycle-token.mjs +1 -28
- package/dist/lib/handlers/lifecycle.mjs +1 -115
- package/dist/lib/handlers/materialize-manager.mjs +1 -48
- package/dist/lib/handlers/metadata.mjs +1 -501
- package/dist/lib/handlers/ownership.mjs +1 -322
- package/dist/lib/handlers/permission-manager.mjs +1 -392
- package/dist/lib/handlers/unified-wrapper.mjs +1 -3110
- package/dist/lib/handlers/version-manager.mjs +1 -885
- package/dist/lib/helpers/class-instance-wrapper.mjs +1 -109
- package/dist/lib/helpers/config.mjs +1 -439
- package/dist/lib/helpers/eventemitter-context.mjs +1 -349
- package/dist/lib/helpers/hint-detector.mjs +1 -47
- package/dist/lib/helpers/modes-utils.mjs +1 -37
- package/dist/lib/helpers/pattern-matcher.mjs +1 -125
- package/dist/lib/helpers/resolve-from-caller.mjs +1 -169
- package/dist/lib/helpers/sanitize.mjs +1 -340
- package/dist/lib/helpers/utilities.mjs +1 -70
- package/dist/lib/i18n/translations.mjs +1 -126
- package/dist/lib/modes/eager.mjs +1 -59
- package/dist/lib/modes/lazy.mjs +1 -81
- package/dist/lib/processors/flatten.mjs +1 -437
- package/dist/lib/processors/loader.mjs +1 -339
- package/dist/lib/processors/type-generator.mjs +1 -275
- package/dist/lib/processors/typescript.mjs +1 -172
- package/dist/lib/runtime/runtime-asynclocalstorage.mjs +1 -113
- package/dist/lib/runtime/runtime-livebindings.mjs +1 -78
- package/dist/lib/runtime/runtime.mjs +1 -102
- package/dist/slothlet.mjs +1 -817
- package/package.json +34 -31
- package/types/dist/lib/builders/api-assignment.d.mts +3 -92
- package/types/dist/lib/builders/api-assignment.d.mts.map +1 -1
- package/types/dist/lib/builders/api_builder.d.mts +102 -91
- package/types/dist/lib/builders/api_builder.d.mts.map +1 -1
- package/types/dist/lib/builders/builder.d.mts +1 -55
- package/types/dist/lib/builders/builder.d.mts.map +1 -1
- package/types/dist/lib/builders/modes-processor.d.mts +3 -27
- package/types/dist/lib/builders/modes-processor.d.mts.map +1 -1
- package/types/dist/lib/errors.d.mts +19 -109
- package/types/dist/lib/errors.d.mts.map +1 -1
- package/types/dist/lib/factories/component-base.d.mts +7 -177
- package/types/dist/lib/factories/component-base.d.mts.map +1 -1
- package/types/dist/lib/factories/context.d.mts +4 -22
- package/types/dist/lib/factories/context.d.mts.map +1 -1
- package/types/dist/lib/handlers/api-cache-manager.d.mts +20 -203
- package/types/dist/lib/handlers/api-cache-manager.d.mts.map +1 -1
- package/types/dist/lib/handlers/api-manager.d.mts +33 -408
- package/types/dist/lib/handlers/api-manager.d.mts.map +1 -1
- package/types/dist/lib/handlers/context-async.d.mts +23 -61
- package/types/dist/lib/handlers/context-async.d.mts.map +1 -1
- package/types/dist/lib/handlers/context-live.d.mts +22 -59
- package/types/dist/lib/handlers/context-live.d.mts.map +1 -1
- package/types/dist/lib/handlers/hook-manager.d.mts +46 -185
- package/types/dist/lib/handlers/hook-manager.d.mts.map +1 -1
- package/types/dist/lib/handlers/lifecycle-token.d.mts +3 -48
- package/types/dist/lib/handlers/lifecycle-token.d.mts.map +1 -1
- package/types/dist/lib/handlers/lifecycle.d.mts +5 -82
- package/types/dist/lib/handlers/lifecycle.d.mts.map +1 -1
- package/types/dist/lib/handlers/materialize-manager.d.mts +8 -70
- package/types/dist/lib/handlers/materialize-manager.d.mts.map +1 -1
- package/types/dist/lib/handlers/metadata.d.mts +17 -221
- package/types/dist/lib/handlers/metadata.d.mts.map +1 -1
- package/types/dist/lib/handlers/ownership.d.mts +44 -160
- package/types/dist/lib/handlers/ownership.d.mts.map +1 -1
- package/types/dist/lib/handlers/permission-manager.d.mts +37 -141
- package/types/dist/lib/handlers/permission-manager.d.mts.map +1 -1
- package/types/dist/lib/handlers/unified-wrapper.d.mts +26 -239
- package/types/dist/lib/handlers/unified-wrapper.d.mts.map +1 -1
- package/types/dist/lib/handlers/version-manager.d.mts +28 -225
- package/types/dist/lib/handlers/version-manager.d.mts.map +1 -1
- package/types/dist/lib/helpers/class-instance-wrapper.d.mts +2 -52
- package/types/dist/lib/helpers/class-instance-wrapper.d.mts.map +1 -1
- package/types/dist/lib/helpers/config.d.mts +125 -139
- package/types/dist/lib/helpers/config.d.mts.map +1 -1
- package/types/dist/lib/helpers/eventemitter-context.d.mts +3 -29
- package/types/dist/lib/helpers/eventemitter-context.d.mts.map +1 -1
- package/types/dist/lib/helpers/hint-detector.d.mts +2 -15
- package/types/dist/lib/helpers/hint-detector.d.mts.map +1 -1
- package/types/dist/lib/helpers/modes-utils.d.mts +3 -30
- package/types/dist/lib/helpers/modes-utils.d.mts.map +1 -1
- package/types/dist/lib/helpers/pattern-matcher.d.mts +3 -43
- package/types/dist/lib/helpers/pattern-matcher.d.mts.map +1 -1
- package/types/dist/lib/helpers/resolve-from-caller.d.mts +3 -27
- package/types/dist/lib/helpers/resolve-from-caller.d.mts.map +1 -1
- package/types/dist/lib/helpers/sanitize.d.mts +4 -92
- package/types/dist/lib/helpers/sanitize.d.mts.map +1 -1
- package/types/dist/lib/helpers/utilities.d.mts +4 -52
- package/types/dist/lib/helpers/utilities.d.mts.map +1 -1
- package/types/dist/lib/i18n/translations.d.mts +4 -37
- package/types/dist/lib/i18n/translations.d.mts.map +1 -1
- package/types/dist/lib/modes/eager.d.mts +8 -30
- package/types/dist/lib/modes/eager.d.mts.map +1 -1
- package/types/dist/lib/modes/lazy.d.mts +10 -43
- package/types/dist/lib/modes/lazy.d.mts.map +1 -1
- package/types/dist/lib/processors/flatten.d.mts +56 -107
- package/types/dist/lib/processors/flatten.d.mts.map +1 -1
- package/types/dist/lib/processors/loader.d.mts +6 -41
- package/types/dist/lib/processors/loader.d.mts.map +1 -1
- package/types/dist/lib/processors/type-generator.d.mts +2 -16
- package/types/dist/lib/processors/type-generator.d.mts.map +1 -1
- package/types/dist/lib/processors/typescript.d.mts +6 -53
- package/types/dist/lib/processors/typescript.d.mts.map +1 -1
- package/types/dist/lib/runtime/runtime-asynclocalstorage.d.mts +3 -71
- package/types/dist/lib/runtime/runtime-asynclocalstorage.d.mts.map +1 -1
- package/types/dist/lib/runtime/runtime-livebindings.d.mts +2 -37
- package/types/dist/lib/runtime/runtime-livebindings.d.mts.map +1 -1
- package/types/dist/lib/runtime/runtime.d.mts +3 -39
- package/types/dist/lib/runtime/runtime.d.mts.map +1 -1
- package/types/dist/slothlet.d.mts +3 -249
- package/types/dist/slothlet.d.mts.map +1 -1
- package/types/index.d.mts +36 -16
- package/types/index.d.mts.map +1 -0
- package/AGENT-USAGE.md +0 -736
- package/docs/API-RULES.md +0 -712
|
@@ -14,175 +14,4 @@
|
|
|
14
14
|
limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
import { AsyncLocalStorage } from "node:async_hooks";
|
|
21
|
-
import { SlothletError } from "@cldmv/slothlet/errors";
|
|
22
|
-
import { runtime_isClassInstance, runtime_wrapClassInstance } from "@cldmv/slothlet/helpers/class-instance-wrapper";
|
|
23
|
-
import { setApiContextChecker } from "@cldmv/slothlet/helpers/eventemitter-context";
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
export class AsyncContextManager {
|
|
27
|
-
constructor() {
|
|
28
|
-
this.als = new AsyncLocalStorage();
|
|
29
|
-
this.instances = new Map();
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
registerEventEmitterContextChecker() {
|
|
34
|
-
setApiContextChecker(() => {
|
|
35
|
-
const store = this.als.getStore();
|
|
36
|
-
return store && store.instanceID ? true : false;
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
initialize(instanceID, config = {}) {
|
|
42
|
-
if (this.instances.has(instanceID)) {
|
|
43
|
-
throw new SlothletError("CONTEXT_ALREADY_EXISTS", { instanceID }, null, { validationError: true });
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const store = {
|
|
47
|
-
instanceID,
|
|
48
|
-
self: {},
|
|
49
|
-
context: {},
|
|
50
|
-
config: { ...config },
|
|
51
|
-
createdAt: Date.now()
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
this.instances.set(instanceID, store);
|
|
55
|
-
return store;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
runInContext(instanceID, fn, thisArg, args, currentWrapper) {
|
|
60
|
-
|
|
61
|
-
const activeStore = this.als.getStore();
|
|
62
|
-
let baseStore;
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const isActiveOurInstance = activeStore && (activeStore.instanceID === instanceID || activeStore.parentInstanceID === instanceID);
|
|
66
|
-
|
|
67
|
-
if (isActiveOurInstance) {
|
|
68
|
-
|
|
69
|
-
baseStore = activeStore;
|
|
70
|
-
} else {
|
|
71
|
-
|
|
72
|
-
baseStore = this.instances.get(instanceID);
|
|
73
|
-
if (!baseStore) {
|
|
74
|
-
throw new SlothletError("CONTEXT_NOT_FOUND", {
|
|
75
|
-
instanceID,
|
|
76
|
-
availableInstances: Array.from(this.instances.keys())
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
const executionStore = { ...baseStore };
|
|
83
|
-
if (currentWrapper) {
|
|
84
|
-
executionStore.callerWrapper = baseStore.currentWrapper;
|
|
85
|
-
executionStore.currentWrapper = currentWrapper;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
if (isActiveOurInstance) {
|
|
90
|
-
|
|
91
|
-
return this.als.run(executionStore, () => {
|
|
92
|
-
try {
|
|
93
|
-
const result = fn.apply(thisArg, args);
|
|
94
|
-
|
|
95
|
-
if (runtime_isClassInstance(result)) {
|
|
96
|
-
const instanceCache = new WeakMap();
|
|
97
|
-
return runtime_wrapClassInstance(result, this, instanceID, instanceCache);
|
|
98
|
-
}
|
|
99
|
-
return result;
|
|
100
|
-
} catch (error) {
|
|
101
|
-
|
|
102
|
-
if (error instanceof SlothletError) throw error;
|
|
103
|
-
throw new SlothletError(
|
|
104
|
-
"CONTEXT_EXECUTION_FAILED",
|
|
105
|
-
{
|
|
106
|
-
instanceID
|
|
107
|
-
},
|
|
108
|
-
error
|
|
109
|
-
);
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
return this.als.run(executionStore, () => {
|
|
116
|
-
try {
|
|
117
|
-
const result = fn.apply(thisArg, args);
|
|
118
|
-
|
|
119
|
-
if (runtime_isClassInstance(result)) {
|
|
120
|
-
const instanceCache = new WeakMap();
|
|
121
|
-
return runtime_wrapClassInstance(result, this, instanceID, instanceCache);
|
|
122
|
-
}
|
|
123
|
-
return result;
|
|
124
|
-
} catch (error) {
|
|
125
|
-
|
|
126
|
-
if (error instanceof SlothletError) throw error;
|
|
127
|
-
throw new SlothletError(
|
|
128
|
-
"CONTEXT_EXECUTION_FAILED",
|
|
129
|
-
{
|
|
130
|
-
instanceID
|
|
131
|
-
},
|
|
132
|
-
error
|
|
133
|
-
);
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
getContext() {
|
|
140
|
-
const store = this.als.getStore();
|
|
141
|
-
if (!store) {
|
|
142
|
-
throw new SlothletError("NO_ACTIVE_CONTEXT_ASYNC", {}, null, { validationError: true });
|
|
143
|
-
}
|
|
144
|
-
return store;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
tryGetContext() {
|
|
149
|
-
return this.als.getStore();
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
cleanup(instanceID) {
|
|
154
|
-
const store = this.instances.get(instanceID);
|
|
155
|
-
if (!store) {
|
|
156
|
-
throw new SlothletError(
|
|
157
|
-
"CONTEXT_NOT_FOUND",
|
|
158
|
-
{ instanceID, availableInstances: Array.from(this.instances.keys()).join(", ") || "none" },
|
|
159
|
-
null,
|
|
160
|
-
{ validationError: true }
|
|
161
|
-
);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
store.self = {};
|
|
166
|
-
store.context = {};
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
this.instances.delete(instanceID);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
getDiagnostics() {
|
|
174
|
-
return {
|
|
175
|
-
type: "async",
|
|
176
|
-
activeStore: this.als.getStore()?.instanceID || null,
|
|
177
|
-
instances: Array.from(this.instances.entries()).map(([id, store]) => ({
|
|
178
|
-
id,
|
|
179
|
-
createdAt: store.createdAt,
|
|
180
|
-
contextKeys: Object.keys(store.context),
|
|
181
|
-
selfKeys: Object.keys(store.self)
|
|
182
|
-
}))
|
|
183
|
-
};
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
export const asyncContextManager = new AsyncContextManager();
|
|
17
|
+
import{AsyncLocalStorage}from"node:async_hooks";import{SlothletError}from"@cldmv/slothlet/errors";import{runtime_isClassInstance,runtime_wrapClassInstance}from"@cldmv/slothlet/helpers/class-instance-wrapper";import{setApiContextChecker}from"@cldmv/slothlet/helpers/eventemitter-context";class AsyncContextManager{constructor(){this.als=new AsyncLocalStorage;this.instances=new Map}registerEventEmitterContextChecker(){setApiContextChecker(()=>{const store=this.als.getStore();return store&&store.instanceID?true:false})}initialize(instanceID,config={}){if(this.instances.has(instanceID)){throw new SlothletError("CONTEXT_ALREADY_EXISTS",{instanceID},null,{validationError:true})}const store={instanceID,self:{},context:{},config:{...config},createdAt:Date.now()};this.instances.set(instanceID,store);return store}runInContext(instanceID,fn,thisArg,args,currentWrapper){const activeStore=this.als.getStore();let baseStore;const isActiveOurInstance=activeStore&&(activeStore.instanceID===instanceID||activeStore.parentInstanceID===instanceID);if(isActiveOurInstance){baseStore=activeStore}else{baseStore=this.instances.get(instanceID);if(!baseStore){throw new SlothletError("CONTEXT_NOT_FOUND",{instanceID,availableInstances:Array.from(this.instances.keys())})}}const executionStore={...baseStore};if(currentWrapper){executionStore.callerWrapper=baseStore.currentWrapper;executionStore.currentWrapper=currentWrapper}if(isActiveOurInstance){return this.als.run(executionStore,()=>{try{const result=fn.apply(thisArg,args);if(runtime_isClassInstance(result)){const instanceCache=new WeakMap;return runtime_wrapClassInstance(result,this,instanceID,instanceCache)}return result}catch(error){if(error instanceof SlothletError)throw error;throw new SlothletError("CONTEXT_EXECUTION_FAILED",{instanceID},error)}})}return this.als.run(executionStore,()=>{try{const result=fn.apply(thisArg,args);if(runtime_isClassInstance(result)){const instanceCache=new WeakMap;return runtime_wrapClassInstance(result,this,instanceID,instanceCache)}return result}catch(error){if(error instanceof SlothletError)throw error;throw new SlothletError("CONTEXT_EXECUTION_FAILED",{instanceID},error)}})}getContext(){const store=this.als.getStore();if(!store){throw new SlothletError("NO_ACTIVE_CONTEXT_ASYNC",{},null,{validationError:true})}return store}tryGetContext(){return this.als.getStore()}cleanup(instanceID){const store=this.instances.get(instanceID);if(!store){throw new SlothletError("CONTEXT_NOT_FOUND",{instanceID,availableInstances:Array.from(this.instances.keys()).join(", ")||"none"},null,{validationError:true})}store.self={};store.context={};this.instances.delete(instanceID)}getDiagnostics(){return{type:"async",activeStore:this.als.getStore()?.instanceID||null,instances:Array.from(this.instances.entries()).map(([id,store])=>({id,createdAt:store.createdAt,contextKeys:Object.keys(store.context),selfKeys:Object.keys(store.self)}))}}}const asyncContextManager=new AsyncContextManager;export{AsyncContextManager,asyncContextManager};
|
|
@@ -14,176 +14,4 @@
|
|
|
14
14
|
limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
import { SlothletError } from "@cldmv/slothlet/errors";
|
|
21
|
-
import { setApiContextChecker } from "@cldmv/slothlet/helpers/eventemitter-context";
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
export class LiveContextManager {
|
|
25
|
-
constructor() {
|
|
26
|
-
this.instances = new Map();
|
|
27
|
-
this.currentInstanceID = null;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
registerEventEmitterContextChecker() {
|
|
32
|
-
setApiContextChecker(() => {
|
|
33
|
-
return this.currentInstanceID !== null;
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
initialize(instanceID, config = {}) {
|
|
39
|
-
if (this.instances.has(instanceID)) {
|
|
40
|
-
throw new SlothletError("CONTEXT_ALREADY_EXISTS", { instanceID }, null, { validationError: true });
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const store = {
|
|
44
|
-
instanceID,
|
|
45
|
-
self: {},
|
|
46
|
-
context: {},
|
|
47
|
-
config: { ...config },
|
|
48
|
-
createdAt: Date.now()
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
this.instances.set(instanceID, store);
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
if (!this.currentInstanceID) {
|
|
55
|
-
this.currentInstanceID = instanceID;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return store;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
runInContext(instanceID, fn, thisArg, args, currentWrapper) {
|
|
63
|
-
|
|
64
|
-
const currentID = this.currentInstanceID;
|
|
65
|
-
let isAlreadyInContext = false;
|
|
66
|
-
|
|
67
|
-
if (currentID) {
|
|
68
|
-
const currentStore = this.instances.get(currentID);
|
|
69
|
-
isAlreadyInContext =
|
|
70
|
-
currentID === instanceID || currentStore?.parentInstanceID === instanceID || currentID.startsWith(instanceID + "__run_");
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const targetInstanceID = isAlreadyInContext ? currentID : instanceID;
|
|
75
|
-
|
|
76
|
-
const store = this.instances.get(targetInstanceID);
|
|
77
|
-
if (!store) {
|
|
78
|
-
throw new SlothletError("CONTEXT_NOT_FOUND", {
|
|
79
|
-
instanceID: targetInstanceID,
|
|
80
|
-
availableInstances: Array.from(this.instances.keys())
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const previousInstanceID = this.currentInstanceID;
|
|
86
|
-
const previousWrapper = store.currentWrapper;
|
|
87
|
-
const previousCallerWrapper = store.callerWrapper;
|
|
88
|
-
|
|
89
|
-
this.currentInstanceID = targetInstanceID;
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
if (currentWrapper) {
|
|
94
|
-
store.callerWrapper = previousWrapper;
|
|
95
|
-
store.currentWrapper = currentWrapper;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
try {
|
|
99
|
-
return fn.apply(thisArg, args);
|
|
100
|
-
} catch (error) {
|
|
101
|
-
|
|
102
|
-
if (error instanceof SlothletError) throw error;
|
|
103
|
-
throw new SlothletError(
|
|
104
|
-
"CONTEXT_EXECUTION_FAILED",
|
|
105
|
-
{
|
|
106
|
-
instanceID
|
|
107
|
-
},
|
|
108
|
-
error
|
|
109
|
-
);
|
|
110
|
-
} finally {
|
|
111
|
-
|
|
112
|
-
this.currentInstanceID = previousInstanceID;
|
|
113
|
-
store.currentWrapper = previousWrapper;
|
|
114
|
-
store.callerWrapper = previousCallerWrapper;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
getContext() {
|
|
120
|
-
if (!this.currentInstanceID) {
|
|
121
|
-
throw new SlothletError("NO_ACTIVE_CONTEXT_LIVE", {}, null, { validationError: true });
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
const store = this.instances.get(this.currentInstanceID);
|
|
125
|
-
if (!store) {
|
|
126
|
-
throw new SlothletError(
|
|
127
|
-
"CONTEXT_NOT_FOUND",
|
|
128
|
-
{
|
|
129
|
-
instanceID: this.currentInstanceID,
|
|
130
|
-
availableInstances: Array.from(this.instances.keys()).join(", ") || "none"
|
|
131
|
-
},
|
|
132
|
-
null,
|
|
133
|
-
{ validationError: true }
|
|
134
|
-
);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return store;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
tryGetContext() {
|
|
142
|
-
if (!this.currentInstanceID) {
|
|
143
|
-
return undefined;
|
|
144
|
-
}
|
|
145
|
-
return this.instances.get(this.currentInstanceID);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
cleanup(instanceID) {
|
|
150
|
-
const store = this.instances.get(instanceID);
|
|
151
|
-
if (!store) {
|
|
152
|
-
throw new SlothletError(
|
|
153
|
-
"CONTEXT_NOT_FOUND",
|
|
154
|
-
{ instanceID, availableInstances: Array.from(this.instances.keys()).join(", ") || "none" },
|
|
155
|
-
null,
|
|
156
|
-
{ validationError: true }
|
|
157
|
-
);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
store.self = {};
|
|
162
|
-
store.context = {};
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
this.instances.delete(instanceID);
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
if (this.currentInstanceID === instanceID) {
|
|
169
|
-
this.currentInstanceID = null;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
getDiagnostics() {
|
|
175
|
-
return {
|
|
176
|
-
type: "live",
|
|
177
|
-
currentInstanceID: this.currentInstanceID,
|
|
178
|
-
instances: Array.from(this.instances.entries()).map(([id, store]) => ({
|
|
179
|
-
id,
|
|
180
|
-
createdAt: store.createdAt,
|
|
181
|
-
contextKeys: Object.keys(store.context),
|
|
182
|
-
selfKeys: Object.keys(store.self)
|
|
183
|
-
}))
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
export const liveContextManager = new LiveContextManager();
|
|
17
|
+
import{SlothletError}from"@cldmv/slothlet/errors";import{setApiContextChecker}from"@cldmv/slothlet/helpers/eventemitter-context";class LiveContextManager{constructor(){this.instances=new Map;this.currentInstanceID=null}registerEventEmitterContextChecker(){setApiContextChecker(()=>{return this.currentInstanceID!==null})}initialize(instanceID,config={}){if(this.instances.has(instanceID)){throw new SlothletError("CONTEXT_ALREADY_EXISTS",{instanceID},null,{validationError:true})}const store={instanceID,self:{},context:{},config:{...config},createdAt:Date.now()};this.instances.set(instanceID,store);if(!this.currentInstanceID){this.currentInstanceID=instanceID}return store}runInContext(instanceID,fn,thisArg,args,currentWrapper){const currentID=this.currentInstanceID;let isAlreadyInContext=false;if(currentID){const currentStore=this.instances.get(currentID);isAlreadyInContext=currentID===instanceID||currentStore?.parentInstanceID===instanceID||currentID.startsWith(instanceID+"__run_")}const targetInstanceID=isAlreadyInContext?currentID:instanceID;const store=this.instances.get(targetInstanceID);if(!store){throw new SlothletError("CONTEXT_NOT_FOUND",{instanceID:targetInstanceID,availableInstances:Array.from(this.instances.keys())})}const previousInstanceID=this.currentInstanceID;const previousWrapper=store.currentWrapper;const previousCallerWrapper=store.callerWrapper;this.currentInstanceID=targetInstanceID;if(currentWrapper){store.callerWrapper=previousWrapper;store.currentWrapper=currentWrapper}try{return fn.apply(thisArg,args)}catch(error){if(error instanceof SlothletError)throw error;throw new SlothletError("CONTEXT_EXECUTION_FAILED",{instanceID},error)}finally{this.currentInstanceID=previousInstanceID;store.currentWrapper=previousWrapper;store.callerWrapper=previousCallerWrapper}}getContext(){if(!this.currentInstanceID){throw new SlothletError("NO_ACTIVE_CONTEXT_LIVE",{},null,{validationError:true})}const store=this.instances.get(this.currentInstanceID);if(!store){throw new SlothletError("CONTEXT_NOT_FOUND",{instanceID:this.currentInstanceID,availableInstances:Array.from(this.instances.keys()).join(", ")||"none"},null,{validationError:true})}return store}tryGetContext(){if(!this.currentInstanceID){return void 0}return this.instances.get(this.currentInstanceID)}cleanup(instanceID){const store=this.instances.get(instanceID);if(!store){throw new SlothletError("CONTEXT_NOT_FOUND",{instanceID,availableInstances:Array.from(this.instances.keys()).join(", ")||"none"},null,{validationError:true})}store.self={};store.context={};this.instances.delete(instanceID);if(this.currentInstanceID===instanceID){this.currentInstanceID=null}}getDiagnostics(){return{type:"live",currentInstanceID:this.currentInstanceID,instances:Array.from(this.instances.entries()).map(([id,store])=>({id,createdAt:store.createdAt,contextKeys:Object.keys(store.context),selfKeys:Object.keys(store.self)}))}}}const liveContextManager=new LiveContextManager;export{LiveContextManager,liveContextManager};
|