1
0
mirror of https://github.com/jaandrle/deka-dom-el synced 2024-11-25 01:39:37 +01:00

🐛 renamig issues

This commit is contained in:
Jan Andrle 2023-11-24 21:07:48 +01:00
parent 3bbcb0eff7
commit 420defe4a2
Signed by: jaandrle
GPG Key ID: B3A25AED155AFFAB
3 changed files with 71 additions and 71 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -43,20 +43,20 @@ export function observable(value, actions){
return out; return out;
} }
export { observable as O }; export { observable as O };
observable.action= function(observable, name, ...a){ observable.action= function(o, name, ...a){
const s= observable[mark], { actions }= s; const s= o[mark], { actions }= s;
if(!actions || !Reflect.has(actions, name)) if(!actions || !Reflect.has(actions, name))
throw new Error(`'${observable}' has no action with name '${name}'!`); throw new Error(`'${o}' has no action with name '${name}'!`);
actions[name].apply(s, a); actions[name].apply(s, a);
if(s.skip) return Reflect.deleteProperty(s, "skip"); if(s.skip) return Reflect.deleteProperty(s, "skip");
s.listeners.forEach(l=> l(s.value)); s.listeners.forEach(l=> l(s.value));
}; };
observable.on= function on(observables, listener, options= {}){ observable.on= function on(o, listener, options= {}){
const { observable: as }= options; const { signal: as }= options;
if(as && as.aborted) return; if(as && as.aborted) return;
if(Array.isArray(observables)) return observables.forEach(s=> on(s, listener, options)); if(Array.isArray(o)) return o.forEach(s=> on(s, listener, options));
addObservableListener(observables, listener); addObservableListener(o, listener);
if(as) as.addEventListener("abort", ()=> removeObservableListener(observables, listener)); if(as) as.addEventListener("abort", ()=> removeObservableListener(o, listener));
//TODO cleanup when observable removed //TODO cleanup when observable removed
}; };
observable.symbols= { observable.symbols= {
@ -64,23 +64,23 @@ observable.symbols= {
onclear: Symbol.for("Observable.onclear") onclear: Symbol.for("Observable.onclear")
}; };
observable.clear= function(...observables){ observable.clear= function(...observables){
for(const observable of observables){ for(const o of observables){
Reflect.deleteProperty(observable, "toJSON"); Reflect.deleteProperty(o, "toJSON");
const s= observable[mark]; const s= o[mark];
s.onclear.forEach(f=> f.call(s)); s.onclear.forEach(f=> f.call(s));
clearListDeps(observable, s); clearListDeps(o, s);
Reflect.deleteProperty(observable, mark); Reflect.deleteProperty(o, mark);
} }
function clearListDeps(observable, s){ function clearListDeps(o, s){
s.listeners.forEach(l=> { s.listeners.forEach(l=> {
s.listeners.delete(l); s.listeners.delete(l);
if(!deps.has(l)) return; if(!deps.has(l)) return;
const ls= deps.get(l); const ls= deps.get(l);
ls.delete(observable); ls.delete(o);
if(ls.size>1) return; if(ls.size>1) return;
observable.clear(...ls); o.clear(...ls);
deps.delete(l); deps.delete(l);
}); });
} }
@ -88,7 +88,7 @@ observable.clear= function(...observables){
const key_reactive= "__dde_reactive"; const key_reactive= "__dde_reactive";
import { el, elementAttribute } from "./dom.js"; import { el, elementAttribute } from "./dom.js";
import { scope } from "./dom.js"; import { scope } from "./dom.js";
observable.el= function(observable, map){ observable.el= function(o, map){
const mark_start= el.mark({ type: "reactive" }, false); const mark_start= el.mark({ type: "reactive" }, false);
const mark_end= mark_start.end; const mark_end= mark_start.end;
const out= document.createDocumentFragment(); const out= document.createDocumentFragment();
@ -96,7 +96,7 @@ observable.el= function(observable, map){
const { current }= scope; const { current }= scope;
const reRenderReactiveElement= v=> { const reRenderReactiveElement= v=> {
if(!mark_start.parentNode || !mark_end.parentNode) if(!mark_start.parentNode || !mark_end.parentNode)
return removeObservableListener(observable, reRenderReactiveElement); return removeObservableListener(o, reRenderReactiveElement);
scope.push(current); scope.push(current);
let els= map(v); let els= map(v);
scope.pop(); scope.pop();
@ -107,16 +107,16 @@ observable.el= function(observable, map){
el_r.remove(); el_r.remove();
mark_start.after(...els); mark_start.after(...els);
}; };
addObservableListener(observable, reRenderReactiveElement); addObservableListener(o, reRenderReactiveElement);
removeObservablesFromElements(observable, reRenderReactiveElement, mark_start, map); removeObservablesFromElements(o, reRenderReactiveElement, mark_start, map);
reRenderReactiveElement(observable()); reRenderReactiveElement(o());
return out; return out;
}; };
import { on } from "./events.js"; import { on } from "./events.js";
const key_attributes= "__dde_attributes"; const key_attributes= "__dde_attributes";
observable.attribute= function(name, initial= null){ observable.attribute= function(name, initial= null){
//TODO host=element & reuse existing //TODO host=element & reuse existing
const out= O(initial); const out= observable(initial);
let element; let element;
scope.host(el=> { scope.host(el=> {
element= el; element= el;
@ -138,7 +138,7 @@ observable.attribute= function(name, initial= null){
on.disconnected(function(){ on.disconnected(function(){
/*! This removes all observables mapped to attributes (`S.attribute`). /*! This removes all observables mapped to attributes (`S.attribute`).
* Investigate `__dde_attributes` key of the element.*/ * Investigate `__dde_attributes` key of the element.*/
O.clear(...Object.values(element[key_attributes])); observable.clear(...Object.values(element[key_attributes]));
})(element); })(element);
}); });
return new Proxy(out, { return new Proxy(out, {
@ -161,7 +161,7 @@ export const observables_config= {
return attrs(); return attrs();
} }
}; };
function removeObservablesFromElements(observable, listener, ...notes){ function removeObservablesFromElements(o, listener, ...notes){
const { current }= scope; const { current }= scope;
if(current.prevent) return; if(current.prevent) return;
current.host(function(element){ current.host(function(element){
@ -172,18 +172,18 @@ function removeObservablesFromElements(observable, listener, ...notes){
* Clears all Observables listeners added in the current scope/host (`S.el`, `assign`, ?). * Clears all Observables listeners added in the current scope/host (`S.el`, `assign`, ?).
* You can investigate the `__dde_reactive` key of the element. * You can investigate the `__dde_reactive` key of the element.
* */ * */
element[key_reactive].forEach(([ [ observable, listener ] ])=> element[key_reactive].forEach(([ [ o, listener ] ])=>
removeObservableListener(observable, listener, observable[mark]?.host() === element)) removeObservableListener(o, listener, o[mark]?.host() === element))
)(element); )(element);
} }
element[key_reactive].push([ [ observable, listener ], ...notes ]); element[key_reactive].push([ [ o, listener ], ...notes ]);
}); });
} }
function create(value, actions){ function create(value, actions){
const observable= (...value)=> const o= (...value)=>
value.length ? write(observable, ...value) : read(observable); value.length ? write(o, ...value) : read(o);
return toObservable(observable, value, actions); return toObservable(o, value, actions);
} }
const protoSigal= Object.assign(Object.create(null), { const protoSigal= Object.assign(Object.create(null), {
stopPropagation(){ stopPropagation(){
@ -198,17 +198,17 @@ class ObservableDefined extends Error{
this.stack= rest.find(l=> !l.includes(curr_file)); this.stack= rest.find(l=> !l.includes(curr_file));
} }
} }
function toObservable(observable, value, actions){ function toObservable(o, value, actions){
const onclear= []; const onclear= [];
if(typeOf(actions)!=="[object Object]") if(typeOf(actions)!=="[object Object]")
actions= {}; actions= {};
const { onclear: ocs }= O.symbols; const { onclear: ocs }= observable.symbols;
if(actions[ocs]){ if(actions[ocs]){
onclear.push(actions[ocs]); onclear.push(actions[ocs]);
Reflect.deleteProperty(actions, ocs); Reflect.deleteProperty(actions, ocs);
} }
const { host }= scope; const { host }= scope;
Reflect.defineProperty(observable, mark, { Reflect.defineProperty(o, mark, {
value: { value: {
value, actions, onclear, host, value, actions, onclear, host,
listeners: new Set(), listeners: new Set(),
@ -218,40 +218,40 @@ function toObservable(observable, value, actions){
writable: false, writable: false,
configurable: true configurable: true
}); });
observable.toJSON= ()=> observable(); o.toJSON= ()=> o();
Object.setPrototypeOf(observable[mark], protoSigal); Object.setPrototypeOf(o[mark], protoSigal);
return observable; return o;
} }
function currentContext(){ function currentContext(){
return stack_watch[stack_watch.length - 1]; return stack_watch[stack_watch.length - 1];
} }
function read(observable){ function read(o){
if(!observable[mark]) return; if(!o[mark]) return;
const { value, listeners }= observable[mark]; const { value, listeners }= o[mark];
const context= currentContext(); const context= currentContext();
if(context) listeners.add(context); if(context) listeners.add(context);
if(deps.has(context)) deps.get(context).add(observable); if(deps.has(context)) deps.get(context).add(o);
return value; return value;
} }
function write(observable, value, force){ function write(o, value, force){
if(!observable[mark]) return; if(!o[mark]) return;
const s= observable[mark]; const s= o[mark];
if(!force && s.value===value) return; if(!force && s.value===value) return;
s.value= value; s.value= value;
s.listeners.forEach(l=> l(value)); s.listeners.forEach(l=> l(value));
return value; return value;
} }
function addObservableListener(observable, listener){ function addObservableListener(o, listener){
if(!observable[mark]) return; if(!o[mark]) return;
return observable[mark].listeners.add(listener); return o[mark].listeners.add(listener);
} }
function removeObservableListener(observable, listener, clear_when_empty){ function removeObservableListener(o, listener, clear_when_empty){
const s= observable[mark]; const s= o[mark];
if(!s) return; if(!s) return;
const out= s.listeners.delete(listener); const out= s.listeners.delete(listener);
if(clear_when_empty && !s.listeners.size){ if(clear_when_empty && !s.listeners.size){
observable.clear(observable); o.clear(o);
if(!deps.has(s)) return out; if(!deps.has(s)) return out;
const c= deps.get(s); const c= deps.get(s);
if(!deps.has(c)) return out; if(!deps.has(c)) return out;