2023-09-07 17:35:23 +02:00
|
|
|
export const mark= Symbol.for("Signal");
|
2023-09-05 09:25:47 +02:00
|
|
|
|
|
|
|
export function isSignal(candidate){
|
|
|
|
try{ return Reflect.has(candidate, mark); }
|
|
|
|
catch(e){ return false; }
|
|
|
|
}
|
|
|
|
export function S(value, actions){
|
|
|
|
if(typeof value!=="function")
|
|
|
|
return create(value, actions);
|
|
|
|
if(isSignal(value)) return value;
|
|
|
|
|
|
|
|
const out= create();
|
|
|
|
watch(()=> out(value()));
|
|
|
|
return out;
|
2023-09-07 18:21:51 +02:00
|
|
|
//TODO for docs: is auto remove if used for args, if external listener needs also S.clear
|
2023-09-05 09:25:47 +02:00
|
|
|
}
|
|
|
|
S.action= function(signal, name, ...a){
|
|
|
|
const s= signal[mark], { actions }= s;
|
|
|
|
if(!actions || !Reflect.has(actions, name))
|
|
|
|
throw new Error(`'${signal}' has no action with name '${name}'!`);
|
|
|
|
actions[name].apply(s, a);
|
|
|
|
if(s.skip) return Reflect.deleteProperty(s, "skip");
|
|
|
|
s.listeners.forEach(l=> l(s.value));
|
|
|
|
};
|
2023-09-07 17:35:23 +02:00
|
|
|
S.on= function on(signals, listener, options= {}){
|
|
|
|
const { signal: as }= options;
|
|
|
|
if(as && as.aborted) return;
|
2023-09-05 09:25:47 +02:00
|
|
|
if(Array.isArray(signals)) return signals.forEach(s=> on(s, listener, options));
|
|
|
|
addSignalListener(signals, listener);
|
2023-09-07 17:35:23 +02:00
|
|
|
if(as) as.addEventListener("abort", ()=> removeSignalListener(signals, listener));
|
2023-09-07 18:21:51 +02:00
|
|
|
//TODO cleanup when signal removed
|
2023-09-07 13:52:09 +02:00
|
|
|
};
|
2023-09-07 17:35:23 +02:00
|
|
|
S.symbols= {
|
|
|
|
signal: mark,
|
|
|
|
onclear: Symbol.for("Signal.onclear")
|
|
|
|
};
|
2023-09-05 09:25:47 +02:00
|
|
|
S.clear= function(...signals){
|
|
|
|
for(const signal of signals){
|
2023-09-07 17:35:23 +02:00
|
|
|
const s= signal[mark];
|
|
|
|
const { onclear }= S.symbols;
|
|
|
|
if(s.actions && s.actions[onclear])
|
|
|
|
s.actions[onclear].call(s);
|
|
|
|
s.listeners.clear();
|
2023-09-05 09:25:47 +02:00
|
|
|
Reflect.deleteProperty(signal, mark);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
import { typeOf } from './helpers.js';
|
|
|
|
export const signals_config= {
|
|
|
|
isReactiveAtrribute(attr, key){ return isSignal(attr); },
|
|
|
|
isTextContent(attributes){
|
|
|
|
return typeOf(attributes)==="string" || ( isSignal(attributes) && typeOf(valueOfSignal(attributes))==="string" );
|
|
|
|
},
|
|
|
|
processReactiveAttribute(_, key, attrS, assignNth){
|
|
|
|
addSignalListener(attrS, attr=> assignNth([ key, attr ]));
|
|
|
|
return attrS();
|
|
|
|
},
|
|
|
|
reactiveElement(signal, map){
|
2023-09-07 17:35:23 +02:00
|
|
|
const mark_start= document.createComment("<#reactive>");
|
|
|
|
const mark_end= document.createComment("</#reactive>");
|
2023-09-05 09:25:47 +02:00
|
|
|
const out= document.createDocumentFragment();
|
|
|
|
out.append(mark_start, mark_end);
|
2023-09-07 17:35:23 +02:00
|
|
|
const reRenderReactiveElement= v=> {
|
2023-09-05 09:25:47 +02:00
|
|
|
if(!mark_start.parentNode || !mark_end.parentNode)
|
2023-09-07 17:35:23 +02:00
|
|
|
return removeSignalListener(signal, reRenderReactiveElement);
|
2023-09-05 09:25:47 +02:00
|
|
|
let els= map(v);
|
|
|
|
if(!Array.isArray(els))
|
|
|
|
els= [ els ];
|
|
|
|
let el_r= mark_start;
|
|
|
|
while(( el_r= mark_start.nextSibling ) !== mark_end)
|
|
|
|
el_r.remove();
|
|
|
|
mark_start.after(...els);
|
|
|
|
};
|
2023-09-07 17:35:23 +02:00
|
|
|
addSignalListener(signal, reRenderReactiveElement);
|
|
|
|
reRenderReactiveElement(signal());
|
2023-09-05 09:25:47 +02:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function create(value, actions){
|
|
|
|
const signal= (...value)=>
|
|
|
|
value.length ? write(signal, value[0]) : read(signal);
|
|
|
|
return toSignal(signal, value, actions);
|
|
|
|
}
|
|
|
|
const protoSigal= Object.assign(Object.create(null), {
|
|
|
|
stopPropagation(){
|
|
|
|
this.skip= true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
function toSignal(signal, value, actions){
|
|
|
|
if(typeOf(actions)!=="[object Object]")
|
|
|
|
actions= {};
|
|
|
|
signal[mark]= {
|
|
|
|
value, actions,
|
|
|
|
listeners: new Set(),
|
|
|
|
};
|
|
|
|
Object.setPrototypeOf(signal[mark], protoSigal);
|
|
|
|
return signal;
|
|
|
|
}
|
|
|
|
|
|
|
|
const stack_watch= [];
|
|
|
|
export function watch(context){
|
|
|
|
const contextReWatch= function(){
|
|
|
|
stack_watch.push(contextReWatch);
|
|
|
|
context();
|
|
|
|
stack_watch.pop();
|
|
|
|
};
|
|
|
|
stack_watch.push(contextReWatch);
|
|
|
|
context();
|
|
|
|
stack_watch.pop();
|
2023-09-07 13:52:09 +02:00
|
|
|
}
|
2023-09-05 09:25:47 +02:00
|
|
|
function currentContext(){
|
|
|
|
return stack_watch[stack_watch.length - 1];
|
|
|
|
}
|
|
|
|
function read(signal){
|
|
|
|
if(!signal[mark]) return;
|
|
|
|
const { value, listeners }= signal[mark];
|
|
|
|
const context= currentContext();
|
|
|
|
if(context) listeners.add(context);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
function write(signal, value){
|
|
|
|
if(!signal[mark]) return;
|
|
|
|
const s= signal[mark];
|
|
|
|
if(s.value===value) return;
|
|
|
|
s.value= value;
|
2023-09-07 13:52:09 +02:00
|
|
|
s.listeners.forEach(fn=> fn(value));
|
2023-09-05 09:25:47 +02:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function valueOfSignal(signal){
|
|
|
|
return signal[mark].value;
|
|
|
|
}
|
|
|
|
function addSignalListener(signal, listener){
|
|
|
|
return signal[mark].listeners.add(listener);
|
|
|
|
}
|
|
|
|
function removeSignalListener(signal, listener){
|
|
|
|
return signal[mark].listeners.delete(listener);
|
|
|
|
}
|