Skip to content

Commit 9264cf7

Browse files
committed
wip
1 parent cf4e5f7 commit 9264cf7

File tree

7 files changed

+148
-0
lines changed

7 files changed

+148
-0
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"scripts": {
3+
"build": "bundle "
4+
}
5+
}

pkg/lib/@hstd/std/src/core/task.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const Task = () => {
2+
3+
let resolve, reject;
4+
5+
return Object.assign(new Promise((...r) => [resolve, reject] = r), { resolve, reject });
6+
}

pkg/lib/@hstd/task/src/mod.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export class Task {
2+
constructor() {
3+
let resolveBuf, rejectBuf;
4+
return Object.assign(new Promise((resolve, reject) => {
5+
resolveBuf = resolve;
6+
rejectBuf = reject
7+
}), {
8+
resolve: resolveBuf, rejectBuf: rejectBuf
9+
});
10+
}
11+
}

pkg/lib/@hstd/wc/jsr.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "@hstd/wc",
3+
"version": "0.1.1",
4+
"imports": {
5+
"hstd": "jsr:@hstd/std@^0.1.33"
6+
},
7+
"exports": {
8+
".": "./src/mod.ts"
9+
}
10+
}

pkg/lib/@hstd/wc/src/define.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import { h, Pointer } from "hstd";
2+
3+
const
4+
WC_TAG_VALIDATOR = /^[a-z](?:[a-z0-9]|[a-z0-9][\-\.][a-z0-9])*-(?:[a-z0-9]|[a-z0-9][\-\.][a-z0-9])*$/,
5+
createAttrState = () => {
6+
const body = {};
7+
return [
8+
body,
9+
new Proxy({}, {
10+
get(_, prop) {
11+
return body[prop] ||= Pointer("");
12+
}
13+
})
14+
]
15+
},
16+
ATTR_BUF = Symbol(),
17+
IS_CONNECTED = Symbol(),
18+
CONNECT = Symbol()
19+
;
20+
21+
const define = (componentDeclarations: { [key: string]: (...any) => (NodeList | Promise<NodeList> | AsyncGenerator<NodeList>) }): void => Object.entries(componentDeclarations).forEach(([name, component]) => {
22+
23+
if(!WC_TAG_VALIDATOR.test(name)) {
24+
if(WC_TAG_VALIDATOR.test(`hstd-${name}`)) {
25+
name = `hstd-${name}`;
26+
} else {
27+
throw new SyntaxError(`Failed to execute 'define': '${name}' is not a valid custom element name`)
28+
}
29+
};
30+
31+
let isInit = true;
32+
33+
const
34+
initAttrBuf = createAttrState(),
35+
initNodeListBuf = h`${component(initAttrBuf[1])}`,
36+
initAttrKeys = Object.keys(initAttrBuf[0]),
37+
initAttrList = initAttrKeys.reduce((acc, attr) => {
38+
if(attr.includes(":")) throw new Error("Cannot include property that starts with ':', which is reserved for binding type identifier.");
39+
acc.push(attr, `${attr}:number`, `${attr}:boolean`);
40+
return acc;
41+
}, []);
42+
;
43+
44+
if(customElements.get(name)) {
45+
46+
let altCount = -1;
47+
while(customElements.get(`${name}-${(++altCount).toString(36)}`));
48+
name += `-${altCount.toString(36)}`;
49+
};
50+
51+
customElements.define(name, class extends HTMLElement {
52+
53+
static get observedAttributes() {
54+
return initAttrList;
55+
}
56+
57+
constructor() {
58+
super();
59+
const thisRef = this;
60+
const [attrPtrList] = thisRef[ATTR_BUF] = isInit ? (isInit = false, initAttrBuf) : createAttrState();
61+
thisRef[IS_CONNECTED] = new Promise(r => thisRef[CONNECT] = r);
62+
Object.defineProperties(thisRef, initAttrKeys.reduce((acc, key) => {
63+
const ptr = attrPtrList[name];
64+
acc[key] = {
65+
get() {
66+
return ptr.$;
67+
},
68+
set(newValue) {
69+
return ptr.$ = newValue;
70+
},
71+
configurable: false,
72+
writable: false,
73+
}
74+
return acc;
75+
}, {}))
76+
}
77+
78+
async attributeChangedCallback(name, _, newValue) {
79+
await this[IS_CONNECTED];
80+
81+
const temp = (
82+
name.endsWith(":number") ? Number
83+
: name.endsWith(":boolean") ? Boolean
84+
: String
85+
);
86+
87+
this.removeAttribute(name);
88+
89+
name = name.includes(":") ? name.slice(0, name.indexOf(":")) : name;
90+
91+
this.setAttribute(name, newValue)
92+
93+
console.log(this[ATTR_BUF][0][name].$ = temp(newValue));
94+
}
95+
96+
connectedCallback() {
97+
this.attachShadow({ mode: "open" });
98+
this.shadowRoot.append(...(isInit
99+
? initNodeListBuf
100+
: h`${component(this[ATTR_BUF][1])}`
101+
));
102+
this[CONNECT]();
103+
}
104+
105+
})
106+
});
107+
108+
export { define }

pkg/lib/@hstd/wc/src/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./define.js";

pkg/lib/@hstd/wc/tsconfig.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"module": "nodenext",
4+
"target": "esnext",
5+
"moduleResolution": "nodenext"
6+
}
7+
}

0 commit comments

Comments
 (0)