web-background 1.0.5 โ 1.0.7
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
CHANGED
|
@@ -117,33 +117,3 @@ type FunctionInBackground<Fn> = (
|
|
|
117
117
|
- Web APIs (`fetch`, `localStorage`, etc. have separate worker support)
|
|
118
118
|
- Non-serializable data (functions, Symbols, undefined, etc.)
|
|
119
119
|
- ES6 module imports (context passing required in bundler environments)
|
|
120
|
-
|
|
121
|
-
### ๐ก Best Practices
|
|
122
|
-
|
|
123
|
-
1. **Context is optional**:
|
|
124
|
-
|
|
125
|
-
```typescript
|
|
126
|
-
// โ
Pure function without external variables
|
|
127
|
-
const pureWorker = background((data) => complexCalculation(data));
|
|
128
|
-
|
|
129
|
-
// โ
Pass context when needed (more stable)
|
|
130
|
-
const contextWorker = background((data) => processWithConfig(data, config), {
|
|
131
|
-
config,
|
|
132
|
-
});
|
|
133
|
-
```
|
|
134
|
-
|
|
135
|
-
2. **Use only for heavy computations**:
|
|
136
|
-
|
|
137
|
-
```typescript
|
|
138
|
-
// โ Simple operations
|
|
139
|
-
const add = background((a, b) => a + b);
|
|
140
|
-
|
|
141
|
-
// โ
CPU intensive operations
|
|
142
|
-
const processLargeArray = background((data) => {
|
|
143
|
-
// Complex processing logic...
|
|
144
|
-
});
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
3. **Keep data size appropriate**:
|
|
148
|
-
- Large objects or complex contexts cause serialization overhead
|
|
149
|
-
- Pass only necessary data for performance optimization
|
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)}))}}
|
|
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};
|