From b493366e29c01325be136307e40a3d65f6bc22c9 Mon Sep 17 00:00:00 2001 From: webreflection Date: Wed, 21 Jun 2023 20:50:55 +0200 Subject: [PATCH] Fix #9 - Rename /global or global to window and export isWindowProxy helper instead of isGlobal --- README.md | 18 ++++---- es.js | 4 +- esm/channel.js | 2 +- esm/uhtml.js | 6 +-- esm/{global.js => window.js} | 10 ++-- esm/{global => window}/main.js | 0 esm/{global => window}/thread.js | 15 +++++- esm/{global => window}/traps.js | 0 esm/{global => window}/types.js | 0 esm/{global => window}/utils.js | 0 package.json | 8 +++- rollup/global.config.js | 4 +- structured.js | 4 +- test/global.js | 76 ------------------------------- test/hyperhtml.html | 2 +- test/hyperhtml.js | 6 +-- test/input.html | 2 +- test/input.js | 4 +- test/uhtml.js | 4 +- test/{global.html => window.html} | 4 +- test/window.js | 76 +++++++++++++++++++++++++++++++ uhtml.js | 6 +-- window.js | 2 + 23 files changed, 135 insertions(+), 118 deletions(-) rename esm/{global.js => window.js} (74%) rename esm/{global => window}/main.js (100%) rename esm/{global => window}/thread.js (89%) rename esm/{global => window}/traps.js (100%) rename esm/{global => window}/types.js (100%) rename esm/{global => window}/utils.js (100%) delete mode 100644 test/global.js rename test/{global.html => window.html} (73%) create mode 100644 test/window.js create mode 100644 window.js diff --git a/README.md b/README.md index 4eb5c74..d805f7e 100644 --- a/README.md +++ b/README.md @@ -89,34 +89,34 @@ This entry point exports the exact same module except it uses [@ungap/structured Please keep in mind not all complex types are supported by the polyfill. -### coincident/global +### coincident/window This entry point exports the same `coincident` module (using *JSON* as default) **but** the *Worker* utility returns an obejct with 3 fields: * **proxy** it's the usual proxy utility to expose or invoke functions defined in the main counter-part - * **global** it's the proxy that orchestrates access to the *main* world, including the ability to pass callbacks from the *Worker*, with the only caveat these will be inevitably executed asynchronously on the main thread, so that *Promise* or *thenable* work out of the box but *accessors* or defined callbacks will need to be awaited from the worker too. DOM listeners should be also handled with no issues but the `event` can't *preventDefault* or *stopPropagation* as the listener will be asynchronous too. All global/well known *Symbol* also cross boundaries so that `[...global.document.querySelectorAll('*')]` or any other *Symbol* based functionality should be preserved, as long as the `symbol` is known as runtime symbols can't cross paths in any meaningful way. - * **isGlobal** is an utility that helps introspection of global proxies, callbacks, classes, or references + * **window** it's the proxy that orchestrates access to the *main* world, including the ability to pass callbacks from the *Worker*, with the only caveat these will be inevitably executed asynchronously on the main thread, so that *Promise* or *thenable* work out of the box but *accessors* or defined callbacks will need to be awaited from the worker too. DOM listeners should be also handled with no issues but the `event` can't *preventDefault* or *stopPropagation* as the listener will be asynchronous too. All well known *Symbol* also cross boundaries so that `[...window.document.querySelectorAll('*')]` or any other *Symbol* based functionality should be preserved, as long as the `symbol` is known as runtime symbols can't cross paths in any meaningful way. + * **isWindowProxy** is an utility that helps introspection of window proxies, callbacks, classes, or main thread references in general -While the initial utility/behavior is preserved on both sides, the *Worker* can seamlessly use *global* / *main* thread to operate on DOM, *localStorage*, or literally anything else, included *Promise* based operations, DOM listeners, and so on. +While the initial utility/behavior is preserved on both sides, the *Worker* can seamlessly use *window* / *main* thread to operate on DOM, *localStorage*, or literally anything else, included *Promise* based operations, DOM listeners, and so on. ```html ``` ```js // The worker.js !!! -import coincident from 'coincident/global'; +import coincident from 'coincident/window'; -const {proxy, global, isGlobal} = coincident(self); +const {proxy, window, isWindowProxy} = coincident(self); // the proxy can expose or answer to main proxy counter part // ... but here is the fun part ... -const {document} = global; +const {document} = window; document.body.innerHTML = '

Hello World!

'; document.body.appendChild( @@ -129,4 +129,4 @@ document.body.addEventListener('click', event => { }); ``` -See the [test/global.js](./test/global.js) file or reach `http://localhost:8080/test/global.html` locally to play around this feature. +See the [test/window.js](./test/window.js) file or reach `http://localhost:8080/test/window.html` locally to play around this feature. diff --git a/es.js b/es.js index 93163b2..53dea50 100644 --- a/es.js +++ b/es.js @@ -1,2 +1,2 @@ -var e="417f77a3-3f54-4e3f-95fa-50ee1cb1635d",t=e=>({value:new Promise((t=>{let n=new Worker("data:application/javascript,"+encodeURIComponent("onmessage=({data:b})=>(Atomics.wait(b,0),postMessage(0))"));n.onmessage=t,n.postMessage(e)}))}) -/*! (c) Andrea Giammarchi - ISC */;const{Int32Array:n,Map:a,SharedArrayBuffer:s,Uint16Array:r}=globalThis,{BYTES_PER_ELEMENT:o}=n,{BYTES_PER_ELEMENT:i}=r,{isArray:c}=Array,{notify:f,wait:l,waitAsync:w}=Atomics,{fromCharCode:d}=String,g=(e,n)=>e?(w||t)(n,0):(l(n,0),{value:{then:e=>e()}}),p=new WeakSet,h=new WeakMap;let u=0;const E=(t,{parse:l,stringify:w}=JSON)=>{if(!h.has(t)){const E=(n,...a)=>t.postMessage({[e]:a},{transfer:n});h.set(t,new Proxy(new a,{get:(e,a)=>"then"===a?null:(...e)=>{const c=u++;let f=new n(new s(o)),w=[];p.has(e.at(-1)||w)&&p.delete(w=e.pop()),E(w,c,f,a,e);const h=t instanceof Worker;return g(h,f).value.then((()=>{const e=f[0];if(!e)return;const t=i*e;return f=new n(new s(t+t%o)),E([],c,f),g(h,f).value.then((()=>l(d(...new r(f.buffer).slice(0,e)))))}))},set(n,s,o){if(!n.size){const s=new a;t.addEventListener("message",(async t=>{const a=t.data?.[e];if(c(a)){t.stopImmediatePropagation();const[e,o,...i]=a;if(i.length){const[t,a]=i;if(!n.has(t))throw new Error(`Unsupported action: ${t}`);{const r=w(await n.get(t)(...a));r&&(s.set(e,r),o[0]=r.length)}}else{const t=s.get(e);s.delete(e);for(let e=new r(o.buffer),n=0;n(p.add(e),e);export{E as default}; +var e="659055af-b151-4f8b-9b33-ff1e8e4038a6",t=e=>({value:new Promise((t=>{let n=new Worker("data:application/javascript,"+encodeURIComponent("onmessage=({data:b})=>(Atomics.wait(b,0),postMessage(0))"));n.onmessage=t,n.postMessage(e)}))}) +/*! (c) Andrea Giammarchi - ISC */;const{Int32Array:n,Map:a,SharedArrayBuffer:s,Uint16Array:r}=globalThis,{BYTES_PER_ELEMENT:o}=n,{BYTES_PER_ELEMENT:i}=r,{isArray:c}=Array,{notify:f,wait:l,waitAsync:w}=Atomics,{fromCharCode:g}=String,p=(e,n)=>e?(w||t)(n,0):(l(n,0),{value:{then:e=>e()}}),d=new WeakSet,h=new WeakMap;let u=0;const E=(t,{parse:l,stringify:w}=JSON)=>{if(!h.has(t)){const E=(n,...a)=>t.postMessage({[e]:a},{transfer:n});h.set(t,new Proxy(new a,{get:(e,a)=>"then"===a?null:(...e)=>{const c=u++;let f=new n(new s(o)),w=[];d.has(e.at(-1)||w)&&d.delete(w=e.pop()),E(w,c,f,a,e);const h=t instanceof Worker;return p(h,f).value.then((()=>{const e=f[0];if(!e)return;const t=i*e;return f=new n(new s(t+t%o)),E([],c,f),p(h,f).value.then((()=>l(g(...new r(f.buffer).slice(0,e)))))}))},set(n,s,o){if(!n.size){const s=new a;t.addEventListener("message",(async t=>{const a=t.data?.[e];if(c(a)){t.stopImmediatePropagation();const[e,o,...i]=a;if(i.length){const[t,a]=i;if(!n.has(t))throw new Error(`Unsupported action: ${t}`);{const r=w(await n.get(t)(...a));r&&(s.set(e,r),o[0]=r.length)}}else{const t=s.get(e);s.delete(e);for(let e=new r(o.buffer),n=0;n(d.add(e),e);export{E as default}; diff --git a/esm/channel.js b/esm/channel.js index dce4446..983a42b 100644 --- a/esm/channel.js +++ b/esm/channel.js @@ -1 +1 @@ -export default '417f77a3-3f54-4e3f-95fa-50ee1cb1635d'; +export default '659055af-b151-4f8b-9b33-ff1e8e4038a6'; diff --git a/esm/uhtml.js b/esm/uhtml.js index e277e72..6568d35 100644 --- a/esm/uhtml.js +++ b/esm/uhtml.js @@ -1,15 +1,15 @@ -import $coincident from './global.js'; +import $coincident from './window.js'; import init from 'uhtml/init'; /** * Create once a `Proxy` able to orchestrate synchronous `postMessage` out of the box. - * In workers, returns a `{proxy, global, isGlobal}` namespace to reach globals synchronously. + * In workers, returns a `{proxy, window, isWindowProxy}` namespace to reach main globals synchronously. * @param {Worker | globalThis} self the context in which code should run */ const coincident = (self, ...args) => { const utility = $coincident(self, ...args); if (!(self instanceof Worker)) - utility.uhtml = init(utility.global); + utility.uhtml = init(utility.window); return utility; } diff --git a/esm/global.js b/esm/window.js similarity index 74% rename from esm/global.js rename to esm/window.js index ad9f286..fa7d94a 100644 --- a/esm/global.js +++ b/esm/window.js @@ -1,7 +1,7 @@ import CHANNEL from './channel.js'; import $coincident from './index.js'; -import main from './global/main.js'; -import thread from './global/thread.js'; +import main from './window/main.js'; +import thread from './window/thread.js'; const MAIN = CHANNEL + 'M'; const THREAD = CHANNEL + 'T'; @@ -11,13 +11,13 @@ const proxies = new WeakMap; /** * @typedef {object} Coincident * @property {ProxyHandler} proxy - * @property {ProxyHandler} global - * @property {(value: any) => boolean} isGlobal + * @property {ProxyHandler} window + * @property {(value: any) => boolean} isWindowProxy */ /** * Create once a `Proxy` able to orchestrate synchronous `postMessage` out of the box. - * In workers, returns a `{proxy, global, isGlobal}` namespace to reach globals synchronously. + * In workers, returns a `{proxy, window, isWindowProxy}` namespace to reach main globals synchronously. * @param {Worker | globalThis} self the context in which code should run * @returns {ProxyHandler | Coincident} */ diff --git a/esm/global/main.js b/esm/window/main.js similarity index 100% rename from esm/global/main.js rename to esm/window/main.js diff --git a/esm/global/thread.js b/esm/window/thread.js similarity index 89% rename from esm/global/thread.js rename to esm/window/thread.js index 7bf5adc..9274924 100644 --- a/esm/global/thread.js +++ b/esm/window/thread.js @@ -134,8 +134,19 @@ export default (main, MAIN, THREAD) => { return { proxy: main, - global: new Proxy([OBJECT, null], proxyHandler), - isGlobal: value => typeof value === OBJECT && !!value && __proxied__ in value + window: new Proxy([OBJECT, null], proxyHandler), + isWindowProxy: value => typeof value === OBJECT && !!value && __proxied__ in value, + // TODO: remove this stuff ASAP + get global() { + console.warn('Deprecated: please access `window` field instead'); + return this.window; + }, + get isGlobal() { + return function (value) { + console.warn('Deprecated: please access `isWindowProxy` field instead'); + return this.isWindowProxy(value); + }.bind(this); + } }; }; diff --git a/esm/global/traps.js b/esm/window/traps.js similarity index 100% rename from esm/global/traps.js rename to esm/window/traps.js diff --git a/esm/global/types.js b/esm/window/types.js similarity index 100% rename from esm/global/types.js rename to esm/window/types.js diff --git a/esm/global/utils.js b/esm/window/utils.js similarity index 100% rename from esm/global/utils.js rename to esm/window/utils.js diff --git a/package.json b/package.json index d194d21..d0de821 100644 --- a/package.json +++ b/package.json @@ -40,8 +40,12 @@ "default": "./cjs/index.js" }, "./global": { - "import": "./esm/global.js", - "default": "./cjs/global.js" + "import": "./esm/window.js", + "default": "./cjs/window.js" + }, + "./window": { + "import": "./esm/window.js", + "default": "./cjs/window.js" }, "./structured": { "import": "./esm/structured.js", diff --git a/rollup/global.config.js b/rollup/global.config.js index d2c5d81..502e21f 100644 --- a/rollup/global.config.js +++ b/rollup/global.config.js @@ -2,13 +2,13 @@ import {nodeResolve} from '@rollup/plugin-node-resolve'; import terser from '@rollup/plugin-terser'; export default { - input: './esm/global.js', + input: './esm/window.js', plugins: [ nodeResolve(), terser() ], output: { esModule: true, - file: './global.js', + file: './window.js', } }; diff --git a/structured.js b/structured.js index da8451c..14444ee 100644 --- a/structured.js +++ b/structured.js @@ -1,2 +1,2 @@ -const e="object"==typeof self?self:globalThis,t=t=>((t,n)=>{const r=(e,n)=>(t.set(n,e),e),s=a=>{if(t.has(a))return t.get(a);const[o,c]=n[a];switch(o){case 0:case-1:return r(c,a);case 1:{const e=r([],a);for(const t of c)e.push(s(t));return e}case 2:{const e=r({},a);for(const[t,n]of c)e[s(t)]=s(n);return e}case 3:return r(new Date(c),a);case 4:{const{source:e,flags:t}=c;return r(new RegExp(e,t),a)}case 5:{const e=r(new Map,a);for(const[t,n]of c)e.set(s(t),s(n));return e}case 6:{const e=r(new Set,a);for(const t of c)e.add(s(t));return e}case 7:{const{name:t,message:n}=c;return r(new e[t](n),a)}case 8:return r(BigInt(c),a);case"BigInt":return r(Object(BigInt(c)),a)}return r(new e[o](c),a)};return s})(new Map,t)(0),n="",{toString:r}={},{keys:s}=Object,a=e=>{const t=typeof e;if("object"!==t||!e)return[0,t];const s=r.call(e).slice(8,-1);switch(s){case"Array":return[1,n];case"Object":return[2,n];case"Date":return[3,n];case"RegExp":return[4,n];case"Map":return[5,n];case"Set":return[6,n]}return s.includes("Array")?[1,s]:s.includes("Error")?[7,s]:[2,s]},o=([e,t])=>0===e&&("function"===t||"symbol"===t),c=(e,{json:t,lossy:n}={})=>{const r=[];return((e,t,n,r)=>{const c=(e,t)=>{const s=r.push(e)-1;return n.set(t,s),s},u=r=>{if(n.has(r))return n.get(r);let[i,f]=a(r);switch(i){case 0:{let t=r;switch(f){case"bigint":i=8,t=r.toString();break;case"function":case"symbol":if(e)throw new TypeError("unable to serialize "+f);t=null;break;case"undefined":return c([-1],r)}return c([i,t],r)}case 1:{if(f)return c([f,[...r]],r);const e=[],t=c([i,e],r);for(const t of r)e.push(u(t));return t}case 2:{if(f)switch(f){case"BigInt":return c([f,r.toString()],r);case"Boolean":case"Number":case"String":return c([f,r.valueOf()],r)}if(t&&"toJSON"in r)return u(r.toJSON());const n=[],l=c([i,n],r);for(const t of s(r))!e&&o(a(r[t]))||n.push([u(t),u(r[t])]);return l}case 3:return c([i,r.toISOString()],r);case 4:{const{source:e,flags:t}=r;return c([i,{source:e,flags:t}],r)}case 5:{const t=[],n=c([i,t],r);for(const[n,s]of r)(e||!o(a(n))&&!o(a(s)))&&t.push([u(n),u(s)]);return n}case 6:{const t=[],n=c([i,t],r);for(const n of r)!e&&o(a(n))||t.push(u(n));return n}}const{message:l}=r;return c([i,{name:f,message:l}],r)};return u})(!(t||n),!!t,new Map,r)(e),r},{parse:u,stringify:i}=JSON,f={json:!0,lossy:!0};var l=Object.freeze({__proto__:null,parse:e=>t(u(e)),stringify:e=>i(c(e,f))}),g="417f77a3-3f54-4e3f-95fa-50ee1cb1635d",p=e=>({value:new Promise((t=>{let n=new Worker("data:application/javascript,"+encodeURIComponent("onmessage=({data:b})=>(Atomics.wait(b,0),postMessage(0))"));n.onmessage=t,n.postMessage(e)}))}) -/*! (c) Andrea Giammarchi - ISC */;const{Int32Array:w,Map:h,SharedArrayBuffer:d,Uint16Array:y}=globalThis,{BYTES_PER_ELEMENT:b}=w,{BYTES_PER_ELEMENT:m}=y,{isArray:S}=Array,{notify:E,wait:A,waitAsync:M}=Atomics,{fromCharCode:O}=String,j=(e,t)=>e?(M||p)(t,0):(A(t,0),{value:{then:e=>e()}}),v=new WeakSet,B=new WeakMap;let I=0;const _=(e,{parse:t,stringify:n}=JSON)=>{if(!B.has(e)){const r=(t,...n)=>e.postMessage({[g]:n},{transfer:t});B.set(e,new Proxy(new h,{get:(n,s)=>"then"===s?null:(...n)=>{const a=I++;let o=new w(new d(b)),c=[];v.has(n.at(-1)||c)&&v.delete(c=n.pop()),r(c,a,o,s,n);const u=e instanceof Worker;return j(u,o).value.then((()=>{const e=o[0];if(!e)return;const n=m*e;return o=new w(new d(n+n%b)),r([],a,o),j(u,o).value.then((()=>t(O(...new y(o.buffer).slice(0,e)))))}))},set(t,r,s){if(!t.size){const r=new h;e.addEventListener("message",(async e=>{const s=e.data?.[g];if(S(s)){e.stopImmediatePropagation();const[a,o,...c]=s;if(c.length){const[e,s]=c;if(!t.has(e))throw new Error(`Unsupported action: ${e}`);{const c=n(await t.get(e)(...s));c&&(r.set(a,c),o[0]=c.length)}}else{const e=r.get(a);r.delete(a);for(let t=new y(o.buffer),n=0;n_(e,l);k.transfer=_.transfer=(...e)=>(v.add(e),e);export{k as default}; +const e="object"==typeof self?self:globalThis,t=t=>((t,n)=>{const r=(e,n)=>(t.set(n,e),e),s=a=>{if(t.has(a))return t.get(a);const[o,c]=n[a];switch(o){case 0:case-1:return r(c,a);case 1:{const e=r([],a);for(const t of c)e.push(s(t));return e}case 2:{const e=r({},a);for(const[t,n]of c)e[s(t)]=s(n);return e}case 3:return r(new Date(c),a);case 4:{const{source:e,flags:t}=c;return r(new RegExp(e,t),a)}case 5:{const e=r(new Map,a);for(const[t,n]of c)e.set(s(t),s(n));return e}case 6:{const e=r(new Set,a);for(const t of c)e.add(s(t));return e}case 7:{const{name:t,message:n}=c;return r(new e[t](n),a)}case 8:return r(BigInt(c),a);case"BigInt":return r(Object(BigInt(c)),a)}return r(new e[o](c),a)};return s})(new Map,t)(0),n="",{toString:r}={},{keys:s}=Object,a=e=>{const t=typeof e;if("object"!==t||!e)return[0,t];const s=r.call(e).slice(8,-1);switch(s){case"Array":return[1,n];case"Object":return[2,n];case"Date":return[3,n];case"RegExp":return[4,n];case"Map":return[5,n];case"Set":return[6,n]}return s.includes("Array")?[1,s]:s.includes("Error")?[7,s]:[2,s]},o=([e,t])=>0===e&&("function"===t||"symbol"===t),c=(e,{json:t,lossy:n}={})=>{const r=[];return((e,t,n,r)=>{const c=(e,t)=>{const s=r.push(e)-1;return n.set(t,s),s},u=r=>{if(n.has(r))return n.get(r);let[i,f]=a(r);switch(i){case 0:{let t=r;switch(f){case"bigint":i=8,t=r.toString();break;case"function":case"symbol":if(e)throw new TypeError("unable to serialize "+f);t=null;break;case"undefined":return c([-1],r)}return c([i,t],r)}case 1:{if(f)return c([f,[...r]],r);const e=[],t=c([i,e],r);for(const t of r)e.push(u(t));return t}case 2:{if(f)switch(f){case"BigInt":return c([f,r.toString()],r);case"Boolean":case"Number":case"String":return c([f,r.valueOf()],r)}if(t&&"toJSON"in r)return u(r.toJSON());const n=[],l=c([i,n],r);for(const t of s(r))!e&&o(a(r[t]))||n.push([u(t),u(r[t])]);return l}case 3:return c([i,r.toISOString()],r);case 4:{const{source:e,flags:t}=r;return c([i,{source:e,flags:t}],r)}case 5:{const t=[],n=c([i,t],r);for(const[n,s]of r)(e||!o(a(n))&&!o(a(s)))&&t.push([u(n),u(s)]);return n}case 6:{const t=[],n=c([i,t],r);for(const n of r)!e&&o(a(n))||t.push(u(n));return n}}const{message:l}=r;return c([i,{name:f,message:l}],r)};return u})(!(t||n),!!t,new Map,r)(e),r},{parse:u,stringify:i}=JSON,f={json:!0,lossy:!0};var l=Object.freeze({__proto__:null,parse:e=>t(u(e)),stringify:e=>i(c(e,f))}),g="659055af-b151-4f8b-9b33-ff1e8e4038a6",p=e=>({value:new Promise((t=>{let n=new Worker("data:application/javascript,"+encodeURIComponent("onmessage=({data:b})=>(Atomics.wait(b,0),postMessage(0))"));n.onmessage=t,n.postMessage(e)}))}) +/*! (c) Andrea Giammarchi - ISC */;const{Int32Array:w,Map:h,SharedArrayBuffer:b,Uint16Array:d}=globalThis,{BYTES_PER_ELEMENT:y}=w,{BYTES_PER_ELEMENT:m}=d,{isArray:S}=Array,{notify:E,wait:A,waitAsync:M}=Atomics,{fromCharCode:O}=String,j=(e,t)=>e?(M||p)(t,0):(A(t,0),{value:{then:e=>e()}}),v=new WeakSet,B=new WeakMap;let I=0;const _=(e,{parse:t,stringify:n}=JSON)=>{if(!B.has(e)){const r=(t,...n)=>e.postMessage({[g]:n},{transfer:t});B.set(e,new Proxy(new h,{get:(n,s)=>"then"===s?null:(...n)=>{const a=I++;let o=new w(new b(y)),c=[];v.has(n.at(-1)||c)&&v.delete(c=n.pop()),r(c,a,o,s,n);const u=e instanceof Worker;return j(u,o).value.then((()=>{const e=o[0];if(!e)return;const n=m*e;return o=new w(new b(n+n%y)),r([],a,o),j(u,o).value.then((()=>t(O(...new d(o.buffer).slice(0,e)))))}))},set(t,r,s){if(!t.size){const r=new h;e.addEventListener("message",(async e=>{const s=e.data?.[g];if(S(s)){e.stopImmediatePropagation();const[a,o,...c]=s;if(c.length){const[e,s]=c;if(!t.has(e))throw new Error(`Unsupported action: ${e}`);{const c=n(await t.get(e)(...s));c&&(r.set(a,c),o[0]=c.length)}}else{const e=r.get(a);r.delete(a);for(let t=new d(o.buffer),n=0;n_(e,l);k.transfer=_.transfer=(...e)=>(v.add(e),e);export{k as default}; diff --git a/test/global.js b/test/global.js deleted file mode 100644 index 8db1346..0000000 --- a/test/global.js +++ /dev/null @@ -1,76 +0,0 @@ -import coincident from '../global.js'; - -const {proxy, global, isGlobal} = coincident(self); - -proxy.greetings(); - -const {document} = global; - -const arr = global.Array(1, 2, 3); -const obj = arr[Symbol.iterator](); -console.assert( - typeof obj.next === 'function' && ( - typeof obj[Symbol.iterator] === 'function' || - typeof obj[Symbol.asyncIterator] !== 'function' -)); - -console.assert(document.body === document.body); -console.assert(typeof document.body === 'object'); -console.assert(typeof document.addEventListener === 'function'); - -document.body.innerHTML = '

Hello World!

'; - -document.body.addEventListener('click', event => { - console.log(event.type); -}); - -console.log(document.body.querySelectorAll('*')[0].tagName); - -// to log global objects in the global/main thread -global.console.log(document.body); - -document.body.appendChild(document.createElement('div')).textContent = '🥳'; - -const remote = new global.Object; - -console.assert(isGlobal(remote)); - -console.log(JSON.stringify( - global.Object.getOwnPropertyDescriptor(global.Int32Array, 'BYTES_PER_ELEMENT') -)); - -let test = 0; -Object.defineProperty(remote, 'test', { - configurable: true, - get: () => test++, -}); - -Promise.all([ - remote.test, - remote.test, - remote.test -]).then(results => { - console.log(results); - console.log(test); -}); - -console.assert(delete remote.key); -console.assert(Reflect.setPrototypeOf(remote, global.Array.prototype)); -console.assert(global.Reflect.preventExtensions(remote)); -console.log(Reflect.ownKeys(global.Promise)); -console.assert(new global.Object(123) instanceof global.Number); -console.log(global.Math.max.apply(global.Math, [1, 2])); -console.log(global.Math.max.call(global.Math, 1, 2)); -console.assert(global.navigator instanceof global.Object); -console.log(global.parseInt('01', 10), parseInt('01', 10)); -console.log(global.navigator.userAgent); -console.assert(Object.isExtensible(global.navigator)); -console.log(global.location.href); -console.assert('length' in global.localStorage); -console.log(global.localStorage.length); -console.assert(global.Symbol.iterator === Symbol.iterator); -global.document.title = 'coincident global'; -console.log(global.document.title); -new global.Promise($ => $('Promise: OK')).then(console.log).then(() => { - console.log('Answer:', global.prompt('Have you seen the console?')); -}); diff --git a/test/hyperhtml.html b/test/hyperhtml.html index e32d8ed..12d2254 100644 --- a/test/hyperhtml.html +++ b/test/hyperhtml.html @@ -4,7 +4,7 @@ diff --git a/test/hyperhtml.js b/test/hyperhtml.js index 2147a14..eb94d17 100644 --- a/test/hyperhtml.js +++ b/test/hyperhtml.js @@ -1,9 +1,9 @@ -import coincident from '../global.js'; +import coincident from '../window.js'; -const {global} = coincident(self); +const {window} = coincident(self); // neededd as global in hyperHTML module -globalThis.document = global.document; +globalThis.document = window.document; import('./hyperhtml.mjs').then(({bind}) => { const html = bind(document.body); diff --git a/test/input.html b/test/input.html index 25e314b..bd34cef 100644 --- a/test/input.html +++ b/test/input.html @@ -4,7 +4,7 @@ diff --git a/test/input.js b/test/input.js index 8bb6f72..d620748 100644 --- a/test/input.js +++ b/test/input.js @@ -1,8 +1,8 @@ import coincident from '../uhtml.js'; -const {global, uhtml} = coincident(self); +const {window, uhtml} = coincident(self); const {render, html} = uhtml; -const {document} = global; +const {document} = window; input('', handler); diff --git a/test/uhtml.js b/test/uhtml.js index 47626ab..ca088da 100644 --- a/test/uhtml.js +++ b/test/uhtml.js @@ -1,8 +1,8 @@ import coincident from '../uhtml.js'; -const {global, uhtml} = coincident(self); +const {window, uhtml} = coincident(self); const {render, html} = uhtml; -const {document} = global; +const {document} = window; tick(uhtml); setInterval(tick, 1000, uhtml); diff --git a/test/global.html b/test/window.html similarity index 73% rename from test/global.html rename to test/window.html index be200dd..1d2ce15 100644 --- a/test/global.html +++ b/test/window.html @@ -4,8 +4,8 @@