web-background 1.0.6 โ 1.0.8
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/README.md +8 -20
- package/dist/index.js +1 -1
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -1,28 +1,16 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Web Background
|
|
2
2
|
|
|
3
3
|
**Simple Web Worker Library - Run complex tasks in the background!**
|
|
4
4
|
|
|
5
5
|
Web Background is a simple yet powerful library that enables you to execute heavy computations in the background without blocking the main thread in web browsers.
|
|
6
6
|
|
|
7
|
-
##
|
|
7
|
+
## Features
|
|
8
8
|
|
|
9
9
|
- ๐ฏ **Simple API**: Execute workers instantly with a single function
|
|
10
|
-
- ๐ง **Automatic Context Capture**: Automatically detects and transfers external variables
|
|
11
|
-
- ๐ **High-Performance Caching**: Cache identical functions for faster re-execution
|
|
12
10
|
- ๐งน **Automatic Memory Management**: Auto-cleanup workers and cache on GC
|
|
13
11
|
- ๐ช **Full TypeScript Support**: Complete type safety
|
|
14
12
|
- ๐ฆ **Zero Dependencies**: No external dependencies
|
|
15
13
|
|
|
16
|
-
## ๐ฆ Installation
|
|
17
|
-
|
|
18
|
-
```bash
|
|
19
|
-
npm install web-background
|
|
20
|
-
# or
|
|
21
|
-
yarn add web-background
|
|
22
|
-
# or
|
|
23
|
-
pnpm add web-background
|
|
24
|
-
```
|
|
25
|
-
|
|
26
14
|
## ๐ Quick Start
|
|
27
15
|
|
|
28
16
|
### Basic Usage
|
|
@@ -44,7 +32,7 @@ console.log(result); // 15
|
|
|
44
32
|
const multiplier = 10;
|
|
45
33
|
const processWithContext = background(
|
|
46
34
|
(numbers: number[]) => numbers.map((x) => x * multiplier),
|
|
47
|
-
{ multiplier } // Explicit passing when needed
|
|
35
|
+
{ multiplier }, // Explicit passing when needed
|
|
48
36
|
);
|
|
49
37
|
```
|
|
50
38
|
|
|
@@ -52,7 +40,7 @@ const processWithContext = background(
|
|
|
52
40
|
|
|
53
41
|
```typescript
|
|
54
42
|
// Image data processing
|
|
55
|
-
const
|
|
43
|
+
const generateBluredData = background((imageData: ImageData) => {
|
|
56
44
|
const { data, width, height } = imageData;
|
|
57
45
|
const processed = new Uint8ClampedArray(data.length);
|
|
58
46
|
|
|
@@ -67,7 +55,7 @@ const processImage = background((imageData: ImageData) => {
|
|
|
67
55
|
processed[i + 3] = data[i + 3]; // A
|
|
68
56
|
}
|
|
69
57
|
|
|
70
|
-
return
|
|
58
|
+
return processed;
|
|
71
59
|
});
|
|
72
60
|
|
|
73
61
|
// Usage
|
|
@@ -75,11 +63,11 @@ const canvas = document.querySelector("canvas");
|
|
|
75
63
|
const ctx = canvas.getContext("2d");
|
|
76
64
|
const originalData = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
|
77
65
|
|
|
78
|
-
const blurredData = await
|
|
79
|
-
ctx.putImageData(blurredData, 0, 0);
|
|
66
|
+
const blurredData = await generateBluredData(originalData);
|
|
67
|
+
ctx.putImageData(new ImageData(blurredData), 0, 0);
|
|
80
68
|
```
|
|
81
69
|
|
|
82
|
-
##
|
|
70
|
+
## API Reference
|
|
83
71
|
|
|
84
72
|
### `background<T>(fn: T, context?: Record<string, any>): FunctionInBackground<T>`
|
|
85
73
|
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
class e extends Worker{postMessage(e){super.postMessage(e)}addEventListener(e,t){super.addEventListener(e,t)}}class t extends e{constructor(e,t){super(e,t)}request(e){return new Promise((t=>{const n=({data:e})=>{t(e),this.removeEventListener("message",n)};this.postMessage(e),this.addEventListener("message",n)}))}}function n(e,t){
|
|
1
|
+
class e extends Worker{postMessage(e){super.postMessage(e)}addEventListener(e,t){super.addEventListener(e,t)}}class t extends e{constructor(e,t){super(e,t)}request(e){return new Promise((t=>{const n=({data:e})=>{t(e),this.removeEventListener("message",n)};this.postMessage(e),this.addEventListener("message",n)}))}}function n(e,t){return function(e,t){const n=e.toString();let r="(function() {\n";const o=Object.entries(t),s=o.filter((([,e])=>"function"!=typeof e)),c=o.filter((([,e])=>"function"==typeof e));s.forEach((([e,t])=>{try{r+=` const ${e} = ${JSON.stringify(t)};\n`}catch(t){r+=` const ${e} = undefined;\n`}})),c.forEach((([e,t])=>{"function"==typeof t&&(r+=` const ${e} = ${t.toString()};\n`)})),o.length>0&&(r+="\n");return r+=` return ${n};\n`,r+="})()",r}(e,t||{})}"function"==typeof SuppressedError&&SuppressedError;class r{static fromModule(e,t){var{module:r,context:o}=t,s=function(e,t){var n={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&t.indexOf(r)<0&&(n[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(r=Object.getOwnPropertySymbols(e);o<r.length;o++)t.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(e,r[o])&&(n[r[o]]=e[r[o]])}return n}(t,["module","context"]);const c=`\n self.addEventListener('message', async event => {\n const func = ${n(r,o)};\n const result = await func(${r.length>1?"...event.data":"event.data"});\n self.postMessage(result);\n });\n `.trim(),i=new Blob([c]),u=URL.createObjectURL(i),a=new e(u,s),l=a.terminate;return a.terminate=()=>{l.call(a),URL.revokeObjectURL(u)},a}}const o=new FinalizationRegistry((e=>e.terminate()));function s(e,n){let s=null;return function c(...i){if("undefined"==typeof window)throw new Error("[web-background] You must use background in browser");return null==s&&(s=r.fromModule(t,{module:e,context:n}),o.register(c,s)),s.request.call(s,i.length>1?i:i[0])}}export{s as background};
|
package/package.json
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
"Background",
|
|
8
8
|
"Web Background"
|
|
9
9
|
],
|
|
10
|
-
"version": "1.0.
|
|
10
|
+
"version": "1.0.8",
|
|
11
11
|
"main": "dist/index.js",
|
|
12
12
|
"module": "dist/index.js",
|
|
13
13
|
"type": "module",
|
|
@@ -30,5 +30,7 @@
|
|
|
30
30
|
"rollup-plugin-typescript2": "^0.36.0",
|
|
31
31
|
"typescript": "5.2"
|
|
32
32
|
},
|
|
33
|
-
"files": [
|
|
33
|
+
"files": [
|
|
34
|
+
"dist"
|
|
35
|
+
]
|
|
34
36
|
}
|