-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlit.ts
35 lines (30 loc) · 921 Bytes
/
lit.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import {html, render,TemplateResult} from 'lit-html';
import { Behavior, BehaviorStream } from './reactive';
type BehaviorType<T> = T extends Behavior<infer G> ? G : T;
type Infer<T> = {
[P in keyof T]: BehaviorType<T[P]>;
};
export function defineElement<T extends {[key:string]:BehaviorStream<any>|Function}>(
fn:()=>T,
temp:(props:Infer<T>)=>TemplateResult
){
const updateTemplate = (p:Infer<T>)=>{
render(temp(p),document.body);
}
const g = fn();
let out:any = {};
for(let key in g){
if(typeof g[key] === "function"){
out[key] = g[key];
} else if(g[key] instanceof BehaviorStream){ // is behavior
out[key] = (g[key] as BehaviorStream<unknown>).value;
(g[key] as BehaviorStream<unknown>).addListener({
emit(v){
out[key] = (g[key] as BehaviorStream<unknown>).value;
updateTemplate(out);
}
})
}
}
updateTemplate(out);
}