Skip to content

Commit

Permalink
0.0.876
Browse files Browse the repository at this point in the history
bahrus committed Dec 28, 2024
1 parent 0be0534 commit c0548f1
Showing 3 changed files with 85 additions and 1 deletion.
40 changes: 40 additions & 0 deletions lib/hookUp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Mount } from '../Mount.js';
const cache = new Map();
export function hookUp(jsExpr) {
if (cache.has(jsExpr))
return;
const guid = `a_${crypto.randomUUID()}`;
const JSExpr = `
document.currentScript['${guid}'] = e => {
with(e.target){
${jsExpr}
}
}
`;
const script = document.createElement('script');
script.innerHTML = JSExpr;
document.head.appendChild(script);
const commands = script[guid];
cache.set(jsExpr, true);
const mnt = class extends Mount {
#commandToMethodLookup = new Map();
async connectedCallback() {
await super.connectedCallback();
for (const key in commands) {
let handler = commands[key];
let commandType = key;
if (Array.isArray(handler)) {
commandType = handler[0];
handler = handler[1];
}
this.addEventListener(commandType, this);
}
}
handleEvent(e) {
const handler = this.#commandToMethodLookup.get(e.type);
if (handler === undefined)
throw 404;
this[handler](this, e);
}
};
}
44 changes: 44 additions & 0 deletions lib/hookUp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {HookupConfig} from '../ts-refs/trans-render/froop/types';
import {Mount} from '../Mount.js';

const cache = new Map<string, boolean>();

export function hookUp<T extends HTMLElement>(jsExpr: string){
if(cache.has(jsExpr)) return;
const guid = `a_${crypto.randomUUID()}`;
const JSExpr = `
document.currentScript['${guid}'] = e => {
with(e.target){
${jsExpr}
}
}
`;
const script = document.createElement('script');
script.innerHTML = JSExpr;
document.head.appendChild(script);
const commands = (<any>script)[guid] as HookupConfig;
cache.set(jsExpr, true);
const mnt = class extends Mount implements EventListenerObject {
#commandToMethodLookup = new Map<string, string>();
async connectedCallback(){
await super.connectedCallback();
for(const key in commands){
let handler = commands[key];
let commandType = key;
if(Array.isArray(handler)){
commandType = handler[0];
handler = handler[1];
}
this.addEventListener(commandType, this);
}
}

handleEvent(e: Event): void {
const handler = this.#commandToMethodLookup.get(e.type);
if(handler === undefined) throw 404;
(<any>this)[handler](this, e);
}
}


}
2 changes: 1 addition & 1 deletion ts-refs
Submodule ts-refs updated from c71fe0 to e545c4

0 comments on commit c0548f1

Please sign in to comment.