j-templates 7.0.65 → 7.0.66

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/Utils/thread.d.ts CHANGED
@@ -1,10 +1,85 @@
1
+ /**
2
+ * A threading utility for managing asynchronous and synchronous callbacks
3
+ * in an efficient manner, similar to how requestIdleCallback works but with
4
+ * more control over execution timing.
5
+ *
6
+ * Only the following functions result in actual thread processing:
7
+ * - Synch() - executes callbacks synchronously or asynchronously based on context
8
+ * - Thread() - schedules callbacks that may run synchronously or asynchronously
9
+ * - ThreadAsync() - creates async operations that return promises
10
+ *
11
+ * Schedule() and After() are scheduling functions that add work to the queue,
12
+ * but don't trigger processing themselves. They only take effect when called
13
+ * within a callback passed to one of the three processing functions above.
14
+ *
15
+ * Example usage:
16
+ * ```typescript
17
+ * import { Schedule, Thread, ThreadAsync } from './thread';
18
+ *
19
+ * // Run a callback in the next tick
20
+ * Thread(() => {
21
+ * console.log('This runs in thread context');
22
+ * // Schedule work to be done after this callback
23
+ * Schedule(() => {
24
+ * console.log('This runs asynchronously after the thread callback');
25
+ * });
26
+ * });
27
+ *
28
+ * // Create an async operation that returns a promise
29
+ * ThreadAsync(async () => {
30
+ * const result = await someAsyncOperation();
31
+ * console.log('Result:', result);
32
+ * }).then(completed => {
33
+ * console.log('Async operation completed:', completed);
34
+ * });
35
+ *
36
+ * // Create a callback wrapper
37
+ * const wrappedCallback = Callback((data: string) => {
38
+ * console.log('Received data:', data);
39
+ * });
40
+ *
41
+ * // Schedule the wrapped callback (this will be processed by Thread)
42
+ * Thread(() => {
43
+ * wrappedCallback('Hello World');
44
+ * });
45
+ * ```
46
+ */
47
+ /**
48
+ * Type definition for thread callback functions
49
+ */
1
50
  type ThreadCallback = {
2
51
  (async: boolean): void;
3
52
  };
53
+ /**
54
+ * Schedules a callback to run asynchronously
55
+ * @param callback - The callback function to schedule
56
+ */
4
57
  export declare function Schedule(callback: ThreadCallback): void;
58
+ /**
59
+ * Schedules a callback to run after existing callbacks in the same context
60
+ * @param callback - The callback function to schedule
61
+ */
5
62
  export declare function After(callback: ThreadCallback): void;
63
+ /**
64
+ * Creates a wrapper function that schedules callbacks to run asynchronously
65
+ * @param callback - The original callback function
66
+ * @returns A wrapped function that schedules execution
67
+ */
6
68
  export declare function Callback<A = void, B = void, C = void, D = void>(callback: (a: A, b: B, c: C, d: D) => void): (a?: A, b?: B, c?: C, d?: D) => void;
69
+ /**
70
+ * Executes a callback synchronously or asynchronously based on context
71
+ * @param callback - The callback function to execute
72
+ */
7
73
  export declare function Synch(callback: ThreadCallback): void;
74
+ /**
75
+ * Schedules a callback that can run either synchronously or asynchronously
76
+ * @param callback - The callback function to schedule
77
+ */
8
78
  export declare function Thread(callback: ThreadCallback): void;
79
+ /**
80
+ * Creates an asynchronous thread operation that returns a promise
81
+ * @param callback - The callback function to execute
82
+ * @returns A promise that resolves when the callback completes
83
+ */
9
84
  export declare function ThreadAsync(callback: ThreadCallback): Promise<boolean>;
10
85
  export {};
package/Utils/thread.js CHANGED
@@ -7,21 +7,43 @@ exports.Synch = Synch;
7
7
  exports.Thread = Thread;
8
8
  exports.ThreadAsync = ThreadAsync;
9
9
  const list_1 = require("./list");
10
+ // Constants
11
+ /**
12
+ * The maximum time allowed for work execution in milliseconds
13
+ */
10
14
  const workTimeMs = 16;
15
+ /**
16
+ * Queue of thread contexts that need processing
17
+ */
11
18
  const contextQueue = list_1.List.Create();
19
+ // State variables
12
20
  let threadContext = null;
13
21
  let timeoutRunning = false;
22
+ // Callback scheduling functions
14
23
  const scheduleInitialCallback = queueMicrotask;
15
24
  const scheduleCallback = setTimeout;
25
+ /**
26
+ * Calculates the remaining time until the deadline
27
+ * @param this - The deadline object
28
+ * @returns The remaining time in milliseconds
29
+ */
16
30
  function timeRemaining() {
17
31
  return this.end - Date.now();
18
32
  }
33
+ /**
34
+ * Creates a deadline object for time management
35
+ * @returns An IdleDeadline object with timeRemaining function
36
+ */
19
37
  function createDeadline() {
20
38
  return {
21
39
  end: Date.now() + workTimeMs,
22
40
  timeRemaining,
23
41
  };
24
42
  }
43
+ /**
44
+ * Processes the queue of thread contexts, executing callbacks within the time limit
45
+ * @param deadline - The deadline for execution
46
+ */
25
47
  function ProcessQueue(deadline = createDeadline()) {
26
48
  let ctx;
27
49
  while (deadline.timeRemaining() > 0 && (ctx = list_1.List.Pop(contextQueue)))
@@ -31,6 +53,10 @@ function ProcessQueue(deadline = createDeadline()) {
31
53
  else
32
54
  timeoutRunning = false;
33
55
  }
56
+ /**
57
+ * Schedules work for a thread context to be processed
58
+ * @param ctx - The thread context to schedule
59
+ */
34
60
  function ScheduleWork(ctx) {
35
61
  list_1.List.Add(contextQueue, ctx);
36
62
  if (timeoutRunning)
@@ -38,12 +64,22 @@ function ScheduleWork(ctx) {
38
64
  timeoutRunning = true;
39
65
  scheduleInitialCallback(ProcessQueue);
40
66
  }
67
+ /**
68
+ * Invokes a callback in the specified thread context
69
+ * @param ctx - The thread context
70
+ * @param callback - The callback to invoke
71
+ */
41
72
  function Invoke(ctx, callback) {
42
73
  const parent = ctx.workEndNode;
43
74
  ctx.workEndNode = ctx.workList.head;
44
75
  callback(true);
45
76
  ctx.workEndNode = parent;
46
77
  }
78
+ /**
79
+ * Executes work for a specific thread context within a deadline
80
+ * @param ctx - The thread context to execute work for
81
+ * @param deadline - The deadline for execution (default: createDeadline())
82
+ */
47
83
  function DoWork(ctx, deadline = createDeadline()) {
48
84
  const parentContext = threadContext;
49
85
  threadContext = ctx;
@@ -57,6 +93,10 @@ function DoWork(ctx, deadline = createDeadline()) {
57
93
  ScheduleWork(ctx);
58
94
  threadContext = parentContext;
59
95
  }
96
+ /**
97
+ * Creates a new thread context
98
+ * @returns A new IThreadContext object
99
+ */
60
100
  function CreateContext() {
61
101
  return {
62
102
  async: false,
@@ -64,6 +104,12 @@ function CreateContext() {
64
104
  workList: list_1.List.Create(),
65
105
  };
66
106
  }
107
+ /**
108
+ * Schedules a callback to be executed in the appropriate context
109
+ * @param callback - The callback function to schedule
110
+ * @param before - Whether to add the callback before existing ones
111
+ * @param async - Whether this is an asynchronous operation
112
+ */
67
113
  function ScheduleCallback(callback, before, async) {
68
114
  threadContext = threadContext || CreateContext();
69
115
  threadContext.async = threadContext.async || async;
@@ -74,6 +120,10 @@ function ScheduleCallback(callback, before, async) {
74
120
  else
75
121
  threadContext.workEndNode = list_1.List.Add(threadContext.workList, callback);
76
122
  }
123
+ /**
124
+ * Executes a callback synchronously without using threading
125
+ * @param callback - The callback to execute
126
+ */
77
127
  function SynchWithoutThread(callback) {
78
128
  callback(false);
79
129
  if (threadContext)
@@ -83,12 +133,25 @@ function SynchWithoutThread(callback) {
83
133
  DoWork(threadContext);
84
134
  threadContext = null;
85
135
  }
136
+ /**
137
+ * Schedules a callback to run asynchronously
138
+ * @param callback - The callback function to schedule
139
+ */
86
140
  function Schedule(callback) {
87
141
  ScheduleCallback(callback, true, true);
88
142
  }
143
+ /**
144
+ * Schedules a callback to run after existing callbacks in the same context
145
+ * @param callback - The callback function to schedule
146
+ */
89
147
  function After(callback) {
90
148
  ScheduleCallback(callback, false, false);
91
149
  }
150
+ /**
151
+ * Creates a wrapper function that schedules callbacks to run asynchronously
152
+ * @param callback - The original callback function
153
+ * @returns A wrapped function that schedules execution
154
+ */
92
155
  function Callback(callback) {
93
156
  return function (a, b, c, d) {
94
157
  Schedule(function () {
@@ -96,7 +159,14 @@ function Callback(callback) {
96
159
  });
97
160
  };
98
161
  }
162
+ /**
163
+ * Flag to track if we're currently in a synchronous callback
164
+ */
99
165
  var inSynchCallback = false;
166
+ /**
167
+ * Executes a callback synchronously or asynchronously based on context
168
+ * @param callback - The callback function to execute
169
+ */
100
170
  function Synch(callback) {
101
171
  if (threadContext || inSynchCallback)
102
172
  callback(false);
@@ -106,12 +176,21 @@ function Synch(callback) {
106
176
  inSynchCallback = false;
107
177
  }
108
178
  }
179
+ /**
180
+ * Schedules a callback that can run either synchronously or asynchronously
181
+ * @param callback - The callback function to schedule
182
+ */
109
183
  function Thread(callback) {
110
184
  if (threadContext)
111
185
  ScheduleCallback(callback, true, false);
112
186
  else
113
187
  Synch(callback);
114
188
  }
189
+ /**
190
+ * Creates an asynchronous thread operation that returns a promise
191
+ * @param callback - The callback function to execute
192
+ * @returns A promise that resolves when the callback completes
193
+ */
115
194
  function ThreadAsync(callback) {
116
195
  return new Promise((resolve) => Thread(function (async) {
117
196
  callback(async);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "j-templates",
3
- "version": "7.0.65",
3
+ "version": "7.0.66",
4
4
  "description": "j-templates",
5
5
  "license": "MIT",
6
6
  "repository": "https://github.com/TypesInCode/jTemplates",