j-templates 7.0.71 → 7.0.73

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.
@@ -5,7 +5,6 @@ exports.CalcScope = CalcScope;
5
5
  const array_1 = require("../../Utils/array");
6
6
  const emitter_1 = require("../../Utils/emitter");
7
7
  const functions_1 = require("../../Utils/functions");
8
- const list_1 = require("../../Utils/list");
9
8
  /**
10
9
  * Creates a dynamic (reactive) observable scope.
11
10
  * @template T The type of value stored in the scope.
@@ -23,15 +22,14 @@ function CreateDynamicScope(getFunction, greedy, value) {
23
22
  dirty: false,
24
23
  destroyed: false,
25
24
  getFunction,
26
- setCallback: function () {
27
- return OnSet(scope);
28
- },
25
+ setCallback: null,
29
26
  value,
30
27
  emitter: emitter_1.Emitter.Create(),
31
28
  emitters: null,
32
29
  onDestroyed: null,
33
30
  calcScopes: null,
34
31
  };
32
+ scope.setCallback = OnSet.bind(scope);
35
33
  return scope;
36
34
  }
37
35
  /**
@@ -80,18 +78,15 @@ function OnSetQueued(scope) {
80
78
  * @param scope The scope to mark for update.
81
79
  * @returns true if the scope is static or destroyed, false if queued or non-greedy scope emitted.
82
80
  */
83
- function OnSet(scope) {
84
- if (scope.type === "static" || scope.destroyed)
85
- return true;
86
- if (scope.dirty)
87
- return false;
88
- scope.dirty = true;
89
- if (scope.greedy) {
90
- OnSetQueued(scope);
81
+ function OnSet() {
82
+ if (this.dirty)
83
+ return;
84
+ this.dirty = true;
85
+ if (this.greedy) {
86
+ OnSetQueued(this);
91
87
  return;
92
88
  }
93
- emitter_1.Emitter.Emit(scope.emitter, scope);
94
- return false;
89
+ emitter_1.Emitter.Emit(this.emitter, this);
95
90
  }
96
91
  /**
97
92
  * Registers an emitter using SAME_STRATEGY.
@@ -214,38 +209,6 @@ const SORTED_STRATEGY = 3;
214
209
  const DISTINCT_STRATEGY = 4;
215
210
  const SHRINK_STRATEGY = 5;
216
211
  let watchState = null;
217
- const watchPool = list_1.List.Create();
218
- /**
219
- * Creates a new watch state object, reusing from pool if available.
220
- * Object pooling reduces GC pressure during frequent watch operations.
221
- * @returns A new or recycled watch state object.
222
- */
223
- function CreateWatchState() {
224
- return (list_1.List.Pop(watchPool) ?? {
225
- emitterIndex: 0,
226
- value: null,
227
- emitters: null,
228
- emitterIds: null,
229
- currentCalc: null,
230
- nextCalc: null,
231
- strategy: PUSH_STRATEGY,
232
- });
233
- }
234
- /**
235
- * Returns a watch state object to the pool for reuse.
236
- * Resets all fields to their default values before pooling.
237
- * @param state The watch state to return to the pool.
238
- */
239
- function ReturnWatchState(state) {
240
- state.emitterIndex = 0;
241
- state.value = null;
242
- state.emitters = null;
243
- state.emitterIds = null;
244
- state.currentCalc = null;
245
- state.nextCalc = null;
246
- state.strategy = SORTED_STRATEGY;
247
- list_1.List.Push(watchPool, state);
248
- }
249
212
  /**
250
213
  * Executes a callback while tracking all scope and emitter dependencies.
251
214
  * Creates a watch context that records what was accessed during execution.
@@ -255,11 +218,15 @@ function ReturnWatchState(state) {
255
218
  */
256
219
  function WatchFunction(callback, currentCalc, initialEmitters) {
257
220
  const parent = watchState;
258
- watchState = CreateWatchState();
259
- watchState.emitters = initialEmitters ?? [];
260
- watchState.currentCalc = currentCalc;
261
- if (initialEmitters !== null)
262
- watchState.strategy = SAME_STRATEGY;
221
+ watchState = {
222
+ emitterIndex: 0,
223
+ value: null,
224
+ emitters: initialEmitters ?? [],
225
+ emitterIds: null,
226
+ currentCalc,
227
+ nextCalc: null,
228
+ strategy: initialEmitters === null ? SORTED_STRATEGY : SAME_STRATEGY,
229
+ };
263
230
  watchState.value = callback();
264
231
  const resultState = watchState;
265
232
  watchState = parent;
@@ -290,7 +257,6 @@ function ExecuteScope(scope) {
290
257
  });
291
258
  else
292
259
  scope.value = state.value;
293
- ReturnWatchState(state);
294
260
  }
295
261
  /**
296
262
  * Creates a scope from a function, choosing between static and dynamic based on dependencies.
@@ -312,11 +278,9 @@ function ExecuteFunction(callback, greedy, allowStatic) {
312
278
  scope.value = result;
313
279
  emitter_1.Emitter.Emit(scope.emitter, scope);
314
280
  });
315
- ReturnWatchState(state);
316
281
  return scope;
317
282
  }
318
283
  const value = state.value;
319
- ReturnWatchState(state);
320
284
  return CreateStaticScope(value);
321
285
  }
322
286
  /**
@@ -401,9 +365,9 @@ function DestroyAllScopes(scopes) {
401
365
  function DestroyScope(scope) {
402
366
  if (!scope || scope.type === "static")
403
367
  return;
404
- emitter_1.Emitter.Clear(scope.emitter);
405
- const scopes = scope.calcScopes && Object.values(scope.calcScopes);
406
- scopes && DestroyAllScopes(scopes);
368
+ emitter_1.Emitter.Destroy(scope.emitter);
369
+ for (const key in scope.calcScopes)
370
+ DestroyScope(scope.calcScopes[key]);
407
371
  scope.calcScopes = null;
408
372
  for (let x = 0; x < scope.emitters.length; x++)
409
373
  emitter_1.Emitter.Remove(scope.emitters[x], scope.setCallback);
@@ -513,9 +477,10 @@ var ObservableScope;
513
477
  * @param scope The scope to mark for update.
514
478
  */
515
479
  function Update(scope) {
516
- if (!scope)
480
+ if (!scope || scope.type === "static")
517
481
  return;
518
- OnSet(scope);
482
+ // OnSet(scope);
483
+ scope.setCallback();
519
484
  }
520
485
  ObservableScope.Update = Update;
521
486
  /**
@@ -1,10 +1,11 @@
1
- export type EmitterCallback<T extends readonly any[] = any[]> = (...args: T) => boolean | void;
1
+ export type EmitterCallback<T extends readonly any[] = any[]> = (...args: T) => void;
2
2
  export type Emitter = [number, ...EmitterCallback[]];
3
3
  export declare namespace Emitter {
4
4
  function Create(): Emitter;
5
5
  function GetId(emitter: Emitter): number;
6
6
  function On(emitter: Emitter, callback: EmitterCallback): void;
7
7
  function Emit(emitter: Emitter, ...args: any[]): void;
8
+ function Destroy(emitter: Emitter): void;
8
9
  function Remove(emitter: Emitter, callback: EmitterCallback): void;
9
10
  function Clear(emitter: Emitter): void;
10
11
  function Distinct(emitters: Emitter[]): void;
package/Utils/emitter.js CHANGED
@@ -19,17 +19,26 @@ var Emitter;
19
19
  }
20
20
  Emitter.On = On;
21
21
  function Emit(emitter, ...args) {
22
+ if (emitter[0] === -1)
23
+ return;
22
24
  let writePos = 1;
23
25
  for (let x = 1; x < emitter.length; x++) {
24
- if (emitter[x] !== null &&
25
- emitter[x](...args) !== true)
26
+ if (emitter[x] !== null) {
27
+ emitter[x](...args);
26
28
  emitter[writePos++] = emitter[x];
29
+ }
27
30
  }
28
31
  if (writePos < emitter.length)
29
32
  emitter.splice(writePos);
30
33
  }
31
34
  Emitter.Emit = Emit;
35
+ function Destroy(emitter) {
36
+ emitter[0] = -1;
37
+ }
38
+ Emitter.Destroy = Destroy;
32
39
  function Remove(emitter, callback) {
40
+ if (emitter[0] === -1)
41
+ return;
33
42
  const index = emitter.indexOf(callback);
34
43
  if (index >= 1)
35
44
  emitter[index] = null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "j-templates",
3
- "version": "7.0.71",
3
+ "version": "7.0.73",
4
4
  "description": "j-templates",
5
5
  "license": "MIT",
6
6
  "repository": "https://github.com/TypesInCode/jTemplates",