dphelper 3.7.1 → 3.7.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.
Files changed (61) hide show
  1. package/docs/README.md +385 -0
  2. package/docs/SUMMARY.md +83 -0
  3. package/docs/_config.yml +1 -0
  4. package/docs/markdown/ai.md +345 -0
  5. package/docs/markdown/anchor.md +156 -0
  6. package/docs/markdown/array.md +208 -0
  7. package/docs/markdown/audio.md +113 -0
  8. package/docs/markdown/avoid.md +53 -0
  9. package/docs/markdown/biometric.md +338 -0
  10. package/docs/markdown/browser.md +203 -0
  11. package/docs/markdown/check.md +65 -0
  12. package/docs/markdown/color.md +159 -0
  13. package/docs/markdown/compress.md +310 -0
  14. package/docs/markdown/cookie.md +115 -0
  15. package/docs/markdown/coords.md +127 -0
  16. package/docs/markdown/credits.md +56 -0
  17. package/docs/markdown/date.md +260 -0
  18. package/docs/markdown/disable.md +109 -0
  19. package/docs/markdown/dispatch.md +108 -0
  20. package/docs/markdown/element.md +53 -0
  21. package/docs/markdown/event.md +85 -0
  22. package/docs/markdown/fetch.md +122 -0
  23. package/docs/markdown/form.md +302 -0
  24. package/docs/markdown/format.md +122 -0
  25. package/docs/markdown/i18n.md +292 -0
  26. package/docs/markdown/image.md +298 -0
  27. package/docs/markdown/json.md +269 -0
  28. package/docs/markdown/load.md +133 -0
  29. package/docs/markdown/logging.md +99 -0
  30. package/docs/markdown/math.md +172 -0
  31. package/docs/markdown/memory.md +85 -0
  32. package/docs/markdown/navigation.md +152 -0
  33. package/docs/markdown/net.md +60 -0
  34. package/docs/markdown/obj.md +242 -0
  35. package/docs/markdown/path.md +46 -0
  36. package/docs/markdown/promise.md +94 -0
  37. package/docs/markdown/sanitize.md +118 -0
  38. package/docs/markdown/screen.md +78 -0
  39. package/docs/markdown/scrollbar.md +82 -0
  40. package/docs/markdown/security.md +289 -0
  41. package/docs/markdown/shortcut.md +100 -0
  42. package/docs/markdown/socket.md +134 -0
  43. package/docs/markdown/sse.md +120 -0
  44. package/docs/markdown/svg.md +167 -0
  45. package/docs/markdown/sync.md +147 -0
  46. package/docs/markdown/system.md +78 -0
  47. package/docs/markdown/terminal.md +73 -0
  48. package/docs/markdown/text.md +245 -0
  49. package/docs/markdown/timer.md +98 -0
  50. package/docs/markdown/tools.md +111 -0
  51. package/docs/markdown/translators.md +65 -0
  52. package/docs/markdown/trigger.md +99 -0
  53. package/docs/markdown/triggers.md +133 -0
  54. package/docs/markdown/type.md +109 -0
  55. package/docs/markdown/types.md +102 -0
  56. package/docs/markdown/ui.md +45 -0
  57. package/docs/markdown/window.md +211 -0
  58. package/docs/markdown/worker.md +223 -0
  59. package/index.cjs +1 -1
  60. package/index.js +1 -1
  61. package/package.json +1 -1
@@ -0,0 +1,223 @@
1
+ # worker
2
+
3
+ Web Worker management utilities for multi-threaded JavaScript execution, including worker creation, pool management, and parallel task processing.
4
+
5
+ ## Functions
6
+
7
+ | Function | Description | Example |
8
+ |----------|-------------|---------|
9
+ | `create` | Create a worker from URL | `dphelper.worker.create('worker.js', { onmessage: (e) => console.log(e.data) })` |
10
+ | `createInline` | Create worker from inline code | `dphelper.worker.createInline('self.onmessage = e => postMessage(e.data * 2)')` |
11
+ | `post` | Send message to worker | `dphelper.worker.post(worker, { type: 'compute', data: 42 })` |
12
+ | `terminate` | Stop a worker | `dphelper.worker.terminate(worker)` |
13
+ | `pool` | Create worker pool | `dphelper.worker.pool('worker.js', 4)` |
14
+ | `poolExec` | Execute tasks in pool | `dphelper.worker.poolExec(pool, tasks)` |
15
+ | `importScripts` | Import scripts into worker | `dphelper.worker.importScripts(worker, ['lib1.js', 'lib2.js'])` |
16
+ | `shared` | Create SharedWorker | `dphelper.worker.shared('my-worker.js', { name: 'shared' })` |
17
+
18
+ ## Description
19
+
20
+ Powerful Web Worker management module providing:
21
+ - **Inline Workers** - Create workers from JavaScript strings without external files
22
+ - **Worker Pools** - Manage multiple workers for parallel processing
23
+ - **SharedWorkers** - Cross-tab communication via shared worker
24
+ - **Script Import** - Dynamically import external scripts into workers
25
+ - **Transferable Support** - Efficient data transfer using Transferable objects
26
+
27
+ ## Usage Examples
28
+
29
+ ### Basic Worker Creation
30
+
31
+ ```javascript
32
+ // Create worker from external file
33
+ const worker = dphelper.worker.create('worker.js', {
34
+ onmessage: (e) => {
35
+ console.log('Received:', e.data);
36
+ },
37
+ onerror: (e) => {
38
+ console.error('Worker error:', e);
39
+ }
40
+ });
41
+
42
+ // Send message to worker
43
+ dphelper.worker.post(worker, { type: 'hello', data: 'world' });
44
+
45
+ // When done, terminate worker
46
+ dphelper.worker.terminate(worker);
47
+ ```
48
+
49
+ ### Inline Workers
50
+
51
+ ```javascript
52
+ // Create worker from inline code
53
+ const worker = dphelper.worker.createInline(`
54
+ self.onmessage = function(e) {
55
+ // Perform heavy computation
56
+ const result = e.data * 2;
57
+ postMessage(result);
58
+ };
59
+ `, {
60
+ onmessage: (e) => {
61
+ console.log('Result:', e.data); // 84
62
+ }
63
+ });
64
+
65
+ dphelper.worker.post(worker, 42);
66
+ ```
67
+
68
+ ### Worker Pool for Parallel Processing
69
+
70
+ ```javascript
71
+ // Create a pool of 4 workers
72
+ const pool = dphelper.worker.pool('compute-worker.js', 4);
73
+
74
+ // Prepare tasks
75
+ const tasks = [
76
+ { id: 1, data: 100 },
77
+ { id: 2, data: 200 },
78
+ { id: 3, data: 300 },
79
+ { id: 4, data: 400 },
80
+ { id: 5, data: 500 }
81
+ ];
82
+
83
+ // Execute all tasks in parallel across the pool
84
+ const results = await dphelper.worker.poolExec(pool, tasks);
85
+ console.log(results); // [200, 400, 600, 800, 1000]
86
+ ```
87
+
88
+ ### SharedWorker for Cross-Tab Communication
89
+
90
+ ```javascript
91
+ // Create shared worker (accessible from multiple tabs)
92
+ const shared = dphelper.worker.shared('shared-worker.js', {
93
+ name: 'my-shared-worker',
94
+ onconnect: (e) => {
95
+ console.log('New connection:', e.ports[0]);
96
+ },
97
+ onmessage: (e) => {
98
+ console.log('Shared message:', e.data);
99
+ }
100
+ });
101
+
102
+ // Send message through port
103
+ shared.port.postMessage({ type: 'hello' });
104
+ shared.port.start();
105
+ ```
106
+
107
+ ### Importing Scripts
108
+
109
+ ```javascript
110
+ const worker = dphelper.worker.create('main-worker.js');
111
+
112
+ // Import external libraries into worker
113
+ dphelper.worker.importScripts(worker, [
114
+ 'https://cdn.example.com/lib1.js',
115
+ 'https://cdn.example.com/lib2.js'
116
+ ]);
117
+ ```
118
+
119
+ ## Advanced Usage
120
+
121
+ ### Parallel Data Processing
122
+
123
+ ```javascript
124
+ // Worker file: data-processor.js
125
+ /*
126
+ self.onmessage = function(e) {
127
+ const { id, numbers } = e.data;
128
+
129
+ // Heavy computation
130
+ const sum = numbers.reduce((a, b) => a + b, 0);
131
+ const avg = sum / numbers.length;
132
+
133
+ postMessage({ id, sum, avg });
134
+ };
135
+ */
136
+
137
+ async function processData(items) {
138
+ const pool = dphelper.worker.pool('data-processor.js', navigator.hardwareConcurrency || 4);
139
+
140
+ const tasks = items.map((item, index) => ({
141
+ index,
142
+ data: { id: item.id, numbers: item.values }
143
+ }));
144
+
145
+ const results = await dphelper.worker.poolExec(pool, tasks);
146
+
147
+ // Sort by original index
148
+ return results.sort((a, b) => a.id - b.id);
149
+ }
150
+
151
+ // Usage
152
+ const data = [
153
+ { id: 1, values: [1, 2, 3, 4, 5] },
154
+ { id: 2, values: [10, 20, 30] },
155
+ { id: 3, values: [100, 200] }
156
+ ];
157
+
158
+ const processed = await processData(data);
159
+ console.log(processed);
160
+ ```
161
+
162
+ ### Real-time Communication with Pool
163
+
164
+ ```javascript
165
+ class WorkerPoolManager {
166
+ constructor(workerScript, poolSize = 4) {
167
+ this.pool = dphelper.worker.pool(workerScript, poolSize);
168
+ this.results = new Map();
169
+ }
170
+
171
+ async processTask(taskId, data) {
172
+ return new Promise((resolve, reject) => {
173
+ const handler = (e) => {
174
+ if (e.data.taskId === taskId) {
175
+ this.results.delete(taskId);
176
+ resolve(e.data.result);
177
+ }
178
+ };
179
+
180
+ this.pool.workers.forEach(w => w.addEventListener('message', handler));
181
+
182
+ dphelper.worker.post(this.pool.workers[0], { taskId, data });
183
+ });
184
+ }
185
+
186
+ terminate() {
187
+ this.pool.workers.forEach(w => dphelper.worker.terminate(w));
188
+ }
189
+ }
190
+ ```
191
+
192
+ ### Web Worker with Transferables
193
+
194
+ ```javascript
195
+ // Efficiently transfer large data without copying
196
+ const buffer = new ArrayBuffer(1024 * 1024); // 1MB
197
+ const numbers = new Uint8Array(buffer);
198
+
199
+ // Fill with data...
200
+
201
+ const worker = dphelper.worker.createInline(`
202
+ self.onmessage = function(e) {
203
+ const data = new Uint8Array(e.data.buffer);
204
+ // Process data...
205
+ postMessage({ processed: true }, [e.data.buffer]);
206
+ };
207
+ `);
208
+
209
+ // Transfer the buffer ownership to worker
210
+ dphelper.worker.post(worker, numbers, [buffer]);
211
+ ```
212
+
213
+ ## Details
214
+
215
+ - **Author:** Dario Passariello
216
+ - **Version:** 0.0.1
217
+ - **Creation Date:** 20260313
218
+ - **Last Modified:** 20260313
219
+ - **Environment:** Client-side only (browser)
220
+
221
+ ---
222
+
223
+ *Automatically generated document*