dphelper 4.2.2 → 4.2.3

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 (58) hide show
  1. package/index.cjs +1 -1
  2. package/index.js +1 -1
  3. package/package.json +1 -1
  4. package/markdown/ai.md +0 -345
  5. package/markdown/anchor.md +0 -156
  6. package/markdown/array.md +0 -208
  7. package/markdown/audio.md +0 -113
  8. package/markdown/avoid.md +0 -53
  9. package/markdown/biometric.md +0 -338
  10. package/markdown/browser.md +0 -203
  11. package/markdown/check.md +0 -65
  12. package/markdown/color.md +0 -159
  13. package/markdown/compress.md +0 -310
  14. package/markdown/cookie.md +0 -115
  15. package/markdown/coords.md +0 -127
  16. package/markdown/credits.md +0 -56
  17. package/markdown/date.md +0 -260
  18. package/markdown/disable.md +0 -109
  19. package/markdown/dispatch.md +0 -108
  20. package/markdown/element.md +0 -53
  21. package/markdown/event.md +0 -85
  22. package/markdown/fetch.md +0 -122
  23. package/markdown/form.md +0 -302
  24. package/markdown/format.md +0 -122
  25. package/markdown/i18n.md +0 -292
  26. package/markdown/image.md +0 -298
  27. package/markdown/json.md +0 -269
  28. package/markdown/load.md +0 -133
  29. package/markdown/logging.md +0 -99
  30. package/markdown/math.md +0 -172
  31. package/markdown/memory.md +0 -85
  32. package/markdown/navigation.md +0 -152
  33. package/markdown/net.md +0 -60
  34. package/markdown/obj.md +0 -242
  35. package/markdown/path.md +0 -46
  36. package/markdown/promise.md +0 -94
  37. package/markdown/sanitize.md +0 -118
  38. package/markdown/screen.md +0 -78
  39. package/markdown/scrollbar.md +0 -82
  40. package/markdown/security.md +0 -289
  41. package/markdown/shortcut.md +0 -100
  42. package/markdown/socket.md +0 -134
  43. package/markdown/sse.md +0 -120
  44. package/markdown/svg.md +0 -167
  45. package/markdown/sync.md +0 -147
  46. package/markdown/system.md +0 -78
  47. package/markdown/terminal.md +0 -73
  48. package/markdown/text.md +0 -245
  49. package/markdown/timer.md +0 -98
  50. package/markdown/tools.md +0 -111
  51. package/markdown/translators.md +0 -65
  52. package/markdown/trigger.md +0 -99
  53. package/markdown/triggers.md +0 -133
  54. package/markdown/type.md +0 -109
  55. package/markdown/types.md +0 -102
  56. package/markdown/ui.md +0 -45
  57. package/markdown/window.md +0 -211
  58. package/markdown/worker.md +0 -223
@@ -1,223 +0,0 @@
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*