@cldmv/slothlet 2.5.6 → 2.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.
@@ -0,0 +1,298 @@
1
+ /*
2
+ Copyright 2025 CLDMV/Shinrai
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ 
18
+
19
+
20
+
21
+ import {
22
+ detectCurrentInstanceId,
23
+ getInstanceData,
24
+ setActiveInstance,
25
+ getCurrentActiveInstanceId
26
+ } from "@cldmv/slothlet/helpers/instance-manager";
27
+
28
+
29
+ function getCurrentInstanceContext() {
30
+
31
+
32
+
33
+
34
+
35
+
36
+
37
+
38
+
39
+
40
+
41
+ const instanceId = detectCurrentInstanceId();
42
+
43
+
44
+
45
+
46
+
47
+
48
+
49
+
50
+
51
+
52
+
53
+
54
+
55
+ if (instanceId) {
56
+ const instanceData = getInstanceData(instanceId);
57
+ if (instanceData) {
58
+ return instanceData;
59
+ }
60
+ }
61
+
62
+
63
+
64
+
65
+ return null;
66
+ }
67
+
68
+
69
+ export const self = new Proxy(
70
+ {},
71
+ {
72
+ get(target, prop) {
73
+ const ctx = getCurrentInstanceContext();
74
+ if (ctx && ctx.self) {
75
+ return ctx.self[prop];
76
+ }
77
+ return undefined;
78
+ },
79
+
80
+ set(target, prop, value) {
81
+ const ctx = getCurrentInstanceContext();
82
+ if (ctx && ctx.self) {
83
+ ctx.self[prop] = value;
84
+ return true;
85
+ }
86
+ return false;
87
+ },
88
+
89
+ ownKeys(_) {
90
+
91
+
92
+
93
+
94
+ const ctx = getCurrentInstanceContext();
95
+ if (ctx && ctx.self) {
96
+ return Reflect.ownKeys(ctx.self);
97
+ }
98
+ return [];
99
+ },
100
+
101
+ has(target, prop) {
102
+ const ctx = getCurrentInstanceContext();
103
+ if (ctx && ctx.self) {
104
+ return prop in ctx.self;
105
+ }
106
+ return false;
107
+ },
108
+
109
+ getOwnPropertyDescriptor(target, prop) {
110
+ const ctx = getCurrentInstanceContext();
111
+ if (ctx && ctx.self) {
112
+ return Reflect.getOwnPropertyDescriptor(ctx.self, prop);
113
+ }
114
+ return undefined;
115
+ }
116
+ }
117
+ );
118
+
119
+
120
+ export const context = new Proxy(
121
+ {},
122
+ {
123
+ get(target, prop) {
124
+ const ctx = getCurrentInstanceContext();
125
+ if (ctx && ctx.context) {
126
+ return ctx.context[prop];
127
+ }
128
+ return undefined;
129
+ },
130
+
131
+ set(target, prop, value) {
132
+ const ctx = getCurrentInstanceContext();
133
+ if (ctx && ctx.context) {
134
+ ctx.context[prop] = value;
135
+ return true;
136
+ }
137
+ return false;
138
+ },
139
+
140
+ ownKeys(_) {
141
+ const ctx = getCurrentInstanceContext();
142
+ if (ctx && ctx.context) {
143
+ return Reflect.ownKeys(ctx.context);
144
+ }
145
+ return [];
146
+ },
147
+
148
+ has(target, prop) {
149
+ const ctx = getCurrentInstanceContext();
150
+ if (ctx && ctx.context) {
151
+ return prop in ctx.context;
152
+ }
153
+ return false;
154
+ },
155
+
156
+ getOwnPropertyDescriptor(target, prop) {
157
+ const ctx = getCurrentInstanceContext();
158
+ if (ctx && ctx.context) {
159
+ return Reflect.getOwnPropertyDescriptor(ctx.context, prop);
160
+ }
161
+ return undefined;
162
+ }
163
+ }
164
+ );
165
+
166
+
167
+ export const reference = new Proxy(
168
+ {},
169
+ {
170
+ get(target, prop) {
171
+ const ctx = getCurrentInstanceContext();
172
+ if (ctx && ctx.reference) {
173
+ return ctx.reference[prop];
174
+ }
175
+ return undefined;
176
+ },
177
+
178
+ set(target, prop, value) {
179
+ const ctx = getCurrentInstanceContext();
180
+ if (ctx && ctx.reference) {
181
+ ctx.reference[prop] = value;
182
+ return true;
183
+ }
184
+ return false;
185
+ },
186
+
187
+ ownKeys(_) {
188
+ const ctx = getCurrentInstanceContext();
189
+ if (ctx && ctx.reference) {
190
+ return Reflect.ownKeys(ctx.reference);
191
+ }
192
+ return [];
193
+ },
194
+
195
+ has(target, prop) {
196
+ const ctx = getCurrentInstanceContext();
197
+ if (ctx && ctx.reference) {
198
+ return prop in ctx.reference;
199
+ }
200
+ return false;
201
+ },
202
+
203
+ getOwnPropertyDescriptor(target, prop) {
204
+ const ctx = getCurrentInstanceContext();
205
+ if (ctx && ctx.reference) {
206
+ return Reflect.getOwnPropertyDescriptor(ctx.reference, prop);
207
+ }
208
+ return undefined;
209
+ }
210
+ }
211
+ );
212
+
213
+
214
+ export const instanceId = new Proxy(
215
+ {},
216
+ {
217
+ get(target, prop) {
218
+ if (prop === "valueOf" || prop === "toString" || prop === Symbol.toPrimitive) {
219
+ const currentId = detectCurrentInstanceId();
220
+ return () => currentId || "unknown";
221
+ }
222
+ return detectCurrentInstanceId() || "unknown";
223
+ },
224
+
225
+ ownKeys(_) {
226
+ return ["valueOf", "toString"];
227
+ },
228
+
229
+ has(target, prop) {
230
+ return prop === "valueOf" || prop === "toString" || prop === Symbol.toPrimitive;
231
+ },
232
+
233
+ getOwnPropertyDescriptor(target, prop) {
234
+ if (prop === "valueOf" || prop === "toString" || prop === Symbol.toPrimitive) {
235
+ return {
236
+ configurable: true,
237
+ enumerable: false,
238
+ writable: false,
239
+ value: () => detectCurrentInstanceId() || "unknown"
240
+ };
241
+ }
242
+ return undefined;
243
+ }
244
+ }
245
+ );
246
+
247
+
248
+ export function runWithCtx(ctx, fn, thisArg, args) {
249
+
250
+ const previousActiveInstance = getCurrentActiveInstanceId();
251
+
252
+
253
+ if (ctx && ctx.instanceId) {
254
+ setActiveInstance(ctx.instanceId);
255
+ }
256
+
257
+ try {
258
+
259
+ return Reflect.apply(fn, thisArg, args);
260
+ } finally {
261
+
262
+ setActiveInstance(previousActiveInstance);
263
+ }
264
+ }
265
+
266
+
267
+ export function makeWrapper(_) {
268
+ return function wrapperFunction(obj) {
269
+
270
+
271
+ return obj;
272
+ };
273
+ }
274
+
275
+
276
+
277
+
278
+
279
+ export function getContext() {
280
+ return getCurrentInstanceContext();
281
+ }
282
+
283
+ export function setContext(newContext) {
284
+
285
+
286
+ const ctx = getCurrentInstanceContext();
287
+ if (ctx) {
288
+
289
+ Object.assign(ctx, newContext);
290
+ }
291
+
292
+ }
293
+
294
+ export const contextManager = {
295
+ get: getContext,
296
+ set: setContext,
297
+ runWithCtx
298
+ };