-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
257 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
"cSpell.words": [ | ||
"ents", | ||
"plah", | ||
"Shae", | ||
"testresult", | ||
"transferables" | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<link rel="icon" type="image/svg+xml" href="/vite.svg" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>shae-worker</title> | ||
</head> | ||
<body> | ||
<shae-worker id="worker"></shae-worker> | ||
<section id="tests"></section> | ||
<script type="module" src="/src/shae-worker.js"></script> | ||
</body> | ||
</html> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {GlobalNS} from '@spearwolf/shadow-ents'; | ||
import '@spearwolf/shadow-ents/shae-worker.js'; | ||
import './style.css'; | ||
import {testAsyncAction} from './testAsyncAction.js'; | ||
import {testBooleanAction} from './testBooleanAction.js'; | ||
|
||
main(); | ||
|
||
async function main() { | ||
const worker = document.getElementById('worker'); | ||
|
||
window.worker = worker; | ||
console.log('shae-worker element', worker); | ||
|
||
await testAsyncAction('shae-worker-whenDefined', () => customElements.whenDefined('shae-worker')); | ||
|
||
const shadowEnv = worker.shadowEnv; | ||
window.shadowEnv = shadowEnv; | ||
console.log('shadowEnv', shadowEnv); | ||
|
||
testBooleanAction('shae-worker-ns', () => worker.ns === GlobalNS); | ||
|
||
await testAsyncAction('shadow-env-ready', async () => { | ||
await shadowEnv.ready(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,7 @@ main { | |
} | ||
|
||
#tests { | ||
margin-top: 1rem; | ||
margin-left: auto; | ||
margin-right: auto; | ||
display: grid; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import {createSignal} from '@spearwolf/signalize'; | ||
import {GlobalNS} from '../constants.js'; | ||
import {ComponentContext, ViewComponent} from '../core.js'; | ||
import {generateUUID} from '../generateUUID.js'; | ||
import {toNamespace} from '../toNamespace.js'; | ||
|
||
export class ShaeEntElement extends HTMLElement { | ||
static observedAttributes = ['ns', 'token']; | ||
|
||
readonly isShaeElement = true; | ||
readonly isShaeEntElement = true; | ||
|
||
readonly uuid = generateUUID(); | ||
|
||
readonly #namespace = createSignal<string | symbol | undefined>(); | ||
readonly #componentContext = createSignal<ComponentContext | undefined>(); | ||
readonly #viewComponent = createSignal<ViewComponent | undefined>(); | ||
|
||
get ns() { | ||
return this.#namespace.value; | ||
} | ||
|
||
set ns(ns: string | symbol) { | ||
this.#namespace.set(toNamespace(ns)); | ||
} | ||
|
||
constructor() { | ||
super(); | ||
|
||
this.#namespace.onChange((ns) => { | ||
this.#componentContext.set(ComponentContext.get(ns)); | ||
|
||
if (typeof ns === 'symbol') { | ||
if (this.hasAttribute('ns')) { | ||
this.removeAttribute('ns'); | ||
} | ||
} else { | ||
this.setAttribute('ns', ns); | ||
} | ||
}); | ||
|
||
this.#componentContext.onChange((context) => { | ||
const vc = this.#viewComponent.value; | ||
|
||
if (context == null) { | ||
if (vc != null) { | ||
vc.destroy(); | ||
} | ||
this.#viewComponent.set(undefined); | ||
return; | ||
} | ||
|
||
if (vc == null) { | ||
this.#viewComponent.set(new ViewComponent(this.uuid, {context})); | ||
} else { | ||
vc.context = context; | ||
} | ||
}); | ||
|
||
this.#namespace.set(GlobalNS); | ||
} | ||
|
||
connectedCallback() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import {createSignal} from '@spearwolf/signalize'; | ||
import {GlobalNS} from '../constants.js'; | ||
import {toNamespace} from '../toNamespace.js'; | ||
import {ComponentContext} from '../view/ComponentContext.js'; | ||
import {LocalShadowObjectEnv} from '../view/LocalShadowObjectEnv.js'; | ||
import {RemoteWorkerEnv} from '../view/RemoteWorkerEnv.js'; | ||
import {ShadowEnv} from '../view/ShadowEnv.js'; | ||
|
||
const readNamespaceAttribute = (el: HTMLElement) => toNamespace(el.getAttribute('ns')); | ||
|
||
const readBooleanAttribute = (el: HTMLElement, name: string) => { | ||
if (el.hasAttribute(name)) { | ||
const val = el.getAttribute(name)?.trim()?.toLowerCase() || 'on'; | ||
return ['true', 'on', 'yes', 'local'].includes(val); | ||
} | ||
return false; | ||
}; | ||
|
||
const AttrNamespace = 'ns'; | ||
const AttrLocal = 'local'; | ||
|
||
export class ShaeWorkerElement extends HTMLElement { | ||
static observedAttributes = [AttrNamespace]; | ||
|
||
readonly isShaeElement = true; | ||
readonly isShaeWorkerElement = true; | ||
|
||
readonly shadowEnv = new ShadowEnv(); | ||
|
||
readonly #ns = createSignal<string | symbol>(GlobalNS); | ||
|
||
#shouldDestroy = false; | ||
|
||
constructor() { | ||
super(); | ||
|
||
this.#ns.onChange((ns) => { | ||
this.shadowEnv.view = ComponentContext.get(ns); | ||
}); | ||
|
||
this.shadowEnv.on(ShadowEnv.ContextCreated, () => { | ||
this.dispatchEvent( | ||
new CustomEvent(ShadowEnv.ContextCreated.toLowerCase(), { | ||
bubbles: false, | ||
detail: {shadowEnv: this.shadowEnv}, | ||
}), | ||
); | ||
}); | ||
|
||
this.shadowEnv.on(ShadowEnv.ContextLost, () => { | ||
this.dispatchEvent( | ||
new CustomEvent(ShadowEnv.ContextLost.toLowerCase(), { | ||
bubbles: false, | ||
detail: {shadowEnv: this.shadowEnv}, | ||
}), | ||
); | ||
}); | ||
|
||
this.shadowEnv.on(ShadowEnv.AfterSync, () => { | ||
this.dispatchEvent( | ||
new CustomEvent(ShadowEnv.AfterSync.toLowerCase(), { | ||
bubbles: false, | ||
detail: {shadowEnv: this.shadowEnv}, | ||
}), | ||
); | ||
}); | ||
} | ||
|
||
get ns(): string | symbol { | ||
return this.#ns.value; | ||
} | ||
|
||
set ns(ns: string | symbol) { | ||
if (typeof ns === 'symbol') { | ||
this.#ns.set(ns); | ||
} else { | ||
this.#ns.set(toNamespace(ns)); | ||
} | ||
} | ||
|
||
connectedCallback() { | ||
this.start(); | ||
} | ||
|
||
disconnectedCallback() { | ||
this.#deferDestroy(); | ||
} | ||
|
||
attributeChangedCallback(name: string) { | ||
if (name === AttrNamespace) { | ||
this.#ns.set(readNamespaceAttribute(this)); | ||
} | ||
if (name === AttrLocal) { | ||
if (this.shadowEnv.envProxy != null) { | ||
throw new Error( | ||
'[ShaeWorkerElement] Changing the "local" attribute after the shadowEnv has been created is not supported.', | ||
); | ||
} | ||
} | ||
} | ||
|
||
start(): Promise<ShadowEnv> { | ||
this.#shouldDestroy = false; | ||
if (this.shadowEnv.view == null) { | ||
this.shadowEnv.view = ComponentContext.get(this.#ns.value); | ||
} | ||
if (this.shadowEnv.envProxy == null) { | ||
const envProxy = readBooleanAttribute(this, AttrLocal) ? new LocalShadowObjectEnv() : new RemoteWorkerEnv(); | ||
this.shadowEnv.envProxy = envProxy; | ||
} | ||
return this.shadowEnv.ready(); | ||
} | ||
|
||
destroy() { | ||
this.shadowEnv.envProxy = undefined; | ||
} | ||
|
||
#deferDestroy() { | ||
if (!this.#shouldDestroy) { | ||
this.#shouldDestroy = true; | ||
queueMicrotask(() => { | ||
if (this.#shouldDestroy) { | ||
this.destroy(); | ||
} | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import {ShaeWorkerElement} from './elements/ShaeWorkerElement.js'; | ||
import {SHAE_WORKER} from './elements/constants.js'; | ||
|
||
customElements.define(SHAE_WORKER, ShaeWorkerElement); |
Oops, something went wrong.