reflected 0.0.2 → 0.0.4
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 +6 -3
- package/dist/async-CKeQT1Lz.js +1 -0
- package/dist/index.js +1 -1
- package/dist/{message-DLH7cJo6.js → message-DoGrs_Ai.js} +1 -1
- package/dist/xhr-CKCWEqUE.js +1 -0
- package/package.json +1 -1
- package/src/index.js +2 -2
- package/dist/xhr-DAG_4oRo.js +0 -1
package/README.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# reflected
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<sup>**Social Media Photo by [Marc-Olivier Jodoin](https://unsplash.com/@marcojodoin) on [Unsplash](https://unsplash.com/)**</sup>
|
|
4
|
+
|
|
5
|
+
A primitive to allow workers to call **synchronously** any functionality exposed on the main thread, without blocking it.
|
|
4
6
|
|
|
5
7
|
## Strategies
|
|
6
8
|
|
|
@@ -135,10 +137,11 @@ const worker = await reflect(
|
|
|
135
137
|
const value = await worker.send({ any: 'payload' });
|
|
136
138
|
```
|
|
137
139
|
|
|
140
|
+
Test [live](https://webreflection.github.io/reflected/test/README/) or read the [main thread](./test/README/index.js) and [worker thread](./test/README/worker.js) code.
|
|
141
|
+
|
|
142
|
+
|
|
138
143
|
### Extras
|
|
139
144
|
|
|
140
145
|
- **Named export `channel`:** After initialization, `import reflect, { channel } from 'reflected'` gives the active strategy name (`'message'`, `'broadcast'`, `'xhr'`, or `'async'`).
|
|
141
146
|
- **Errors:** From main-thread `onsync`, return `new Int32Array(0)` (or a convention of your choice) so the worker always gets a result; handle that in the worker’s `onsync` to avoid hanging.
|
|
142
147
|
- **Types:** you can import `MainOptions` and `WorkerOptions` from the root of the porject because *main* `reflect(string, MainOptions)` and *worker* `reflect(WorkerOptions)` are different in a subtle way you probably don't want to mess around with (in particular, the `onsync` which must be sync on the *worker* side of affairs or it cannnot work)
|
|
143
|
-
|
|
144
|
-
Test [live](https://webreflection.github.io/reflected/test/README/) or read the [main thread](./test/README/index.js) and [worker thread](./test/README/worker.js) code.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{m as e,b as a,S as s,u as t,p as r,h as n}from"./shared-MkVjuKUg.js";import{S as o}from"./shared-array-buffer-cwdMr2mc.js";import{r as c}from"./shared-RFmxa5x4.js";var d=({initByteLength:a=1024,maxByteLength:s=8192})=>new o(e+a,{maxByteLength:e+s});const m="async";let p=class extends s{constructor(e,a,s){const o=c(),p=new BroadcastChannel(o),h=d(a),i=new Int32Array(h),g=n(h,a,!1);p.addEventListener("message",async({data:[e,a]})=>{await g({data:a}),p.postMessage([e,i.slice(0,2+i[1])])}),super(...t(e,m,a)),super.addEventListener("message",()=>s(this),{once:!0}),super.postMessage(r(h,a).concat(o))}get channel(){return m}};var h=a(p),i=Object.freeze({__proto__:null,Worker:p,default:h});export{d as S,p as W,i as a};
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{w as a}from"./with-resolvers-CHEvl4oe.js";import{n as s}from"./shared-array-buffer-cwdMr2mc.js";let e
|
|
1
|
+
import{w as a}from"./with-resolvers-CHEvl4oe.js";import{n as s}from"./shared-array-buffer-cwdMr2mc.js";let t,e;if("importScripts"in globalThis){let s;const{promise:r,resolve:o}=a(),i=new URL(location).searchParams.get("reflected");t=i,s="message"===i?import("./message-D7s0I-EX.js"):"broadcast"===i?import("./broadcast-Dc7wAEE6.js"):"xhr"===i?import("./xhr-D5y-AocB.js"):import("./async-DF1WaSCr.js"),e=async a=>{const{data:t,ports:e}=await r,{default:o}=await s,i=new Event("message");return i.data=t,i.ports=e,dispatchEvent(i),o(a)},addEventListener("message",o,{once:!0})}else s?"InstallTrigger"in globalThis?(t="broadcast",e=(await import("./broadcast-D0xibjmN.js")).default):(t="message",e=(await import("./message-DoGrs_Ai.js")).default):navigator.serviceWorker?(t="xhr",e=(await import("./xhr-CKCWEqUE.js")).default):(t="async",e=(await import("./async-CKeQT1Lz.js").then(function(a){return a.a})).default);var r=e;export{t as channel,r as default};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{b as s,S as e,a as t,h as r,u as a,p as n}from"./shared-MkVjuKUg.js";import"./with-resolvers-CHEvl4oe.js";import"./i32-C78nBJH2.js";import"./channel-CdS9bLt4.js";const o="message";var p=s(class extends e{constructor(s,e,p){const{port1:
|
|
1
|
+
import{b as s,S as e,a as t,h as r,u as a,p as n}from"./shared-MkVjuKUg.js";import"./with-resolvers-CHEvl4oe.js";import"./i32-C78nBJH2.js";import"./channel-CdS9bLt4.js";const o="message";var p=s(class extends e{constructor(s,e,p){const{port1:i,port2:c}=new MessageChannel,d=t(e);i.addEventListener(o,r(d,e,!0)),i.start(),super(...a(s,o,e)),super.addEventListener(o,()=>p(this),{once:!0}),super.postMessage(n(d,e),[c])}get channel(){return o}});export{p as default};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{w as e}from"./with-resolvers-CHEvl4oe.js";import{W as t,S as s}from"./async-CKeQT1Lz.js";import{S as r}from"./channel-CdS9bLt4.js";import{S as a,u as n,p as o,h as i}from"./shared-MkVjuKUg.js";import{r as c}from"./shared-RFmxa5x4.js";import"./shared-array-buffer-cwdMr2mc.js";import"./i32-C78nBJH2.js";const l=new Map,d=new BroadcastChannel(r);d.addEventListener("message",async({data:[e,t]})=>{if("request"===e){const[e,[s,r]]=t,a=l.get(r);a&&(d.postMessage(["response",[e,await a.get(s)]]),a.delete(s))}});const{promise:h,resolve:p}=e();let f=!0;const m=(e,t)=>{let s,r=!0,{url:a}=t;e.getRegistration(a).then(s=>s??e.register(a,t)).then(function n(o){const{controller:i}=e;if(r=r&&!!i,s=o.installing||o.waiting||o.active,!s)return m(e,t);if("activated"===s.state){if(r){if(i.scriptURL===a)return p();o.unregister()}location.reload()}else s.addEventListener("statechange",()=>n(o),{once:!0})})};class u extends a{#e;constructor(r,a,d){if(f){f=!1;let{serviceWorker:e}=a;if(!e)return new t(r,a,d);"string"==typeof e&&(e={url:e}),e.url=new URL(e.url,location.href).href,m(navigator.serviceWorker,e)}const h=c(),p=new BroadcastChannel(h),u=s(a),g=new Map,v=new Int32Array(u),w=i(u,a,!1);l.set(h,g),p.addEventListener("message",async({data:[t,s]})=>{const{promise:r,resolve:a}=e();g.set(t,r),await w({data:s}),a(v.slice(0,2+v[1]))}),super(...n(r,"xhr",a)),super.addEventListener("message",()=>d(this),{once:!0}),super.postMessage(o(u,a).concat(h)),this.#e=h}terminate(){l.delete(this.#e),super.terminate()}get channel(){return"xhr"}}var g=(s,r)=>{const{promise:a,resolve:n}=e();return new u(s,r,n)instanceof t?a:h.then(()=>a)};export{g as default};
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -48,8 +48,8 @@ else if (navigator.serviceWorker) {
|
|
|
48
48
|
module = (await import(/* webpackIgnore: true */'./main/xhr.js')).default;
|
|
49
49
|
}
|
|
50
50
|
else {
|
|
51
|
-
channel = '
|
|
52
|
-
module = (
|
|
51
|
+
channel = 'async';
|
|
52
|
+
module = (await import(/* webpackIgnore: true */'./main/async.js')).default;
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
export { channel };
|
package/dist/xhr-DAG_4oRo.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{w as e}from"./with-resolvers-CHEvl4oe.js";import{m as t,S as s,u as n,p as a,h as r}from"./shared-MkVjuKUg.js";import{S as o}from"./shared-array-buffer-cwdMr2mc.js";import{r as i}from"./shared-RFmxa5x4.js";import{S as c}from"./channel-CdS9bLt4.js";import"./i32-C78nBJH2.js";var l=({initByteLength:e=1024,maxByteLength:s=8192})=>new o(t+e,{maxByteLength:t+s});const d="async";let h=class extends s{constructor(e,t,s){const o=i(),c=new BroadcastChannel(o),h=l(t),p=new Int32Array(h),g=r(h,t,!1);c.addEventListener("message",async({data:[e,t]})=>{await g({data:t}),c.postMessage([e,p.slice(0,2+p[1])])}),super(...n(e,d,t)),super.addEventListener("message",()=>s(this),{once:!0}),super.postMessage(a(h,t).concat(o))}get channel(){return d}};const p=new Map,g=new BroadcastChannel(c);g.addEventListener("message",async({data:[e,t]})=>{if("request"===e){const[e,[s,n]]=t,a=p.get(n);a&&(g.postMessage(["response",[e,await a.get(s)]]),a.delete(s))}});const{promise:u,resolve:m}=e();let f=!0;const v=(e,t)=>{let s,n=!0,{url:a}=t;e.getRegistration(a).then(s=>s??e.register(a,t)).then(function r(o){const{controller:i}=e;if(n=n&&!!i,s=o.installing||o.waiting||o.active,!s)return v(e,t);if("activated"===s.state){if(n){if(i.scriptURL===a)return m();o.unregister()}location.reload()}else s.addEventListener("statechange",()=>r(o),{once:!0})})};class w extends s{#e;constructor(t,s,o){if(f){f=!1;let{serviceWorker:e}=s;if(!e)return new h(t,s,o);"string"==typeof e&&(e={url:e}),e.url=new URL(e.url,location.href).href,v(navigator.serviceWorker,e)}const c=i(),d=new BroadcastChannel(c),g=l(s),u=new Map,m=new Int32Array(g),w=r(g,s,!1);p.set(c,u),d.addEventListener("message",async({data:[t,s]})=>{const{promise:n,resolve:a}=e();u.set(t,n),await w({data:s}),a(m.slice(0,2+m[1]))}),super(...n(t,"xhr",s)),super.addEventListener("message",()=>o(this),{once:!0}),super.postMessage(a(g,s).concat(c)),this.#e=c}terminate(){p.delete(this.#e),super.terminate()}get channel(){return"xhr"}}var y=(t,s)=>{const{promise:n,resolve:a}=e();return new w(t,s,a)instanceof h?n:u.then(()=>n)};export{y as default};
|