web-background 1.0.2 → 1.0.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.
- package/README.md +38 -33
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,63 +1,68 @@
|
|
|
1
1
|
# Web Background
|
|
2
|
-
This module
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
It runs in isolation, cannot be access external variables and modules
|
|
6
|
-
격리된 환경에서 실행되므로 외부 모듈 및 외부 변수에 접근할 수 없습니다.
|
|
7
|
-
|
|
8
|
-
In most cases, You don't have to use it
|
|
9
|
-
Recommended for long task
|
|
10
|
-
대부분의 경우 이 모듈을 사용할 필요는 없으나, long task를 처리해야 할 경우 사용할 것을 추천합니다.
|
|
11
|
-
|
|
12
|
-
Most WebAPIs including DOM APIs are cannot be used.
|
|
13
|
-
DOM API를 포함한 대부분의 WebAPI를 사용하지 못합니다.
|
|
14
|
-
[Read More](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API)
|
|
15
|
-
|
|
16
|
-
To send arguments, you must use data from structured clone algorithm types.
|
|
17
|
-
인자로 전송 가능한 데이터 유형은 복사 가능한 타입의 데이터만 가능합니다.
|
|
18
|
-
[Read More](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm)
|
|
2
|
+
This module provides background runtime available on the web.
|
|
3
|
+
웹에서 사용할 수 있는 백그라운드 실행환경을 제공합니다.
|
|
19
4
|
|
|
5
|
+
## API
|
|
6
|
+
### background(fn)
|
|
7
|
+
|
|
8
|
+
```tsx
|
|
9
|
+
type FunctionInBackground<Fn> = (...params: Parameters<Fn>) => Promise<ReturnType<Fn>>;
|
|
10
|
+
|
|
11
|
+
background<Fn>(fn: Fn): FunctionInBackground<Fn>
|
|
12
|
+
```
|
|
20
13
|
|
|
21
14
|
## Example
|
|
15
|
+
[CodeSandbox](https://8pj3sf.csb.app/)
|
|
22
16
|
|
|
23
|
-
**background**
|
|
24
17
|
```ts
|
|
25
18
|
async function runLongTask () {
|
|
26
|
-
const
|
|
19
|
+
const getVertexCoordinates = background((imageData: ImageData) => {
|
|
27
20
|
const { data, width } = imageData;
|
|
28
|
-
|
|
29
|
-
let [
|
|
30
|
-
minX, minY,
|
|
31
|
-
maxX, maxY
|
|
32
|
-
] = [Infinity, Infinity, 0, 0];
|
|
21
|
+
let [minX, minY, maxX, maxY] = [Infinity, Infinity, 0, 0];
|
|
33
22
|
|
|
34
23
|
for(let i = 0, leng = data.length; i < leng; i += 4) {
|
|
35
24
|
const [r, g, b, a] = data.slice(i, i+4);
|
|
36
25
|
const isFilled = Math.max(r, g, b, a) > 0;
|
|
37
|
-
|
|
38
26
|
if(isFilled) {
|
|
39
27
|
const y = Math.floor(i / 4 / width);
|
|
40
28
|
const x = Math.floor(i / 4 - width * y);
|
|
41
29
|
|
|
42
30
|
minX = Math.min(minX, x);
|
|
43
|
-
maxX = Math.max(maxX, x);
|
|
44
|
-
|
|
45
31
|
minY = Math.min(minY, y);
|
|
32
|
+
maxX = Math.max(maxX, x);
|
|
46
33
|
maxY = Math.max(maxY, y);
|
|
47
34
|
}
|
|
48
35
|
}
|
|
49
36
|
|
|
50
|
-
return {
|
|
51
|
-
x: minX === Infinity ? 0 : minX,
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
height: maxY
|
|
37
|
+
return {
|
|
38
|
+
x: minX === Infinity ? 0 : minX,
|
|
39
|
+
y: minY === Infinity ? 0 : minY,
|
|
40
|
+
width: maxX,
|
|
41
|
+
height: maxY
|
|
55
42
|
};
|
|
56
43
|
})
|
|
57
44
|
|
|
58
|
-
const coordinate = await
|
|
45
|
+
const coordinate = await getVertexCoordinates(
|
|
59
46
|
canvas.getContext('2d').getImageData(0, 0, 1000, 1000)
|
|
60
47
|
)
|
|
61
48
|
}
|
|
62
49
|
```
|
|
63
50
|
|
|
51
|
+
|
|
52
|
+
## Tip
|
|
53
|
+
- Web Background is implemented as a Web Worker and is available in web browsers.
|
|
54
|
+
Web Background는 웹 워커로 구현되며 브라우저에서 사용할 수 있습니다.
|
|
55
|
+
|
|
56
|
+
- In most cases, you don't need to use it. It's recommended to use it for long working
|
|
57
|
+
대부분의 경우 이 모듈을 사용할 필요는 없으나, 비용이 큰 작업을 처리해야 할 경우 사용할 것을 추천합니다.
|
|
58
|
+
|
|
59
|
+
- Most WebAPIs including DOM APIs are cannot be used.
|
|
60
|
+
DOM API를 포함한 대부분의 WebAPI를 사용하지 못합니다.
|
|
61
|
+
[Read More](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API)
|
|
62
|
+
|
|
63
|
+
- To send arguments, you must use data from structured clone algorithm types.
|
|
64
|
+
인자로 전송 가능한 데이터 유형은 복사 가능한 타입의 데이터만 가능합니다.
|
|
65
|
+
[Read More](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm)
|
|
66
|
+
|
|
67
|
+
- It runs in isolation, cannot be access external variables and modules
|
|
68
|
+
격리된 환경에서 실행되므로 외부 모듈 및 외부 변수에 접근할 수 없습니다.
|
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
|
|
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"==typeof SuppressedError&&SuppressedError;class n{static fromModule(e,t){var{module:n}=t,r=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 s=0;for(r=Object.getOwnPropertySymbols(e);s<r.length;s++)t.indexOf(r[s])<0&&Object.prototype.propertyIsEnumerable.call(e,r[s])&&(n[r[s]]=e[r[s]])}return n}(t,["module"]);const s=n.toString();console.log(null);const o=`\n self.addEventListener('message', async event => {\n const result = await (${s})(${n.length>1?"...event.data":"event.data"});\n \n self.postMessage(result);\n });\n `.trim(),a=new Blob([o]),l=URL.createObjectURL(a),u=new e(l,r),i=u.terminate;return u.terminate=()=>{i.call(u),URL.revokeObjectURL(l)},u}}const r=new FinalizationRegistry((e=>e.terminate()));function s(e){let s=null;return function o(...a){if("undefined"==typeof window)throw new Error("[web-background] You must use background in browser");return null==s&&(s=n.fromModule(t,{module:e}),r.register(o,s)),s.request.call(s,a.length>1?a:a[0])}}export{s as background};
|