1
0
mirror of https://github.com/jaandrle/deka-dom-el synced 2025-07-04 21:42:14 +02:00

🐛 __dde_reactive

This commit is contained in:
2025-03-17 10:32:02 +01:00
parent 992370b4e3
commit d1b017c1a1
10 changed files with 37 additions and 40 deletions

View File

@ -1,5 +1,5 @@
import { hasOwn, oCreate } from "./helpers.js";
const memoMark= "__dde_memo_of";
const memoMark= "__dde_memo";
const memo_scope= [];
/**
* ```js
@ -20,7 +20,7 @@ export function memo(key, generator){
const [ { cache, after } ]= memo_scope;
return after(k, hasOwn(cache, k) ? cache[k] : generator(key));
}
memo.isScope= function(obj){ return Boolean(obj[memoMark]); };
memo.isScope= function(obj){ return obj[memoMark]; };
/**
* @param {Function} fun
* @param {Object} [options={}]
@ -43,7 +43,7 @@ memo.scope= function memoScopeCreate(fun, { signal, onlyLast }= {}){
cache= cache_local;
return out;
}
memoScope[memoMark]= fun;
memoScope[memoMark]= true;
memoScope.clear= ()=> cache= oCreate();
if(signal) signal.addEventListener("abort", memoScope.clear);
return memoScope;

View File

@ -169,11 +169,11 @@ import { memo } from "../memo.js";
* Creates a reactive DOM element that re-renders when signal changes
*
* @param {Object} s - Signal object to watch
* @param {Function} map - Function mapping signal value to DOM elements
* @param {Function} mapScoped - Function mapping signal value to DOM elements
* @returns {DocumentFragment} Fragment containing reactive elements
*/
signal.el= function(s, map){
map= memo.isScope(map) ? map : memo.scope(map, { onlyLast: true });
const mapScoped= memo.isScope(map) ? map : memo.scope(map, { onlyLast: true });
const { current }= scope, { scope: sc }= current;
const mark_start= el.mark({ type: "reactive", component: sc && sc.name || "" }, true);
const mark_end= mark_start.end;
@ -183,7 +183,7 @@ signal.el= function(s, map){
if(!mark_start.parentNode || !mark_end.parentNode) // === `isConnected` or wasnt yet rendered
return removeSignalListener(s, reRenderReactiveElement);
scope.push(current);
let els= map(v);
let els= mapScoped(v);
scope.pop();
if(!Array.isArray(els))
els= [ els ];
@ -202,7 +202,7 @@ signal.el= function(s, map){
reRenderReactiveElement(s.get());
current.host(on.disconnected(()=>
/*! Clears cached elements for reactive element `S.el` */
map.clear()
mapScoped.clear()
));
return out;
};
@ -314,9 +314,8 @@ export const signals_config= {
function removeSignalsFromElements(s, listener, ...notes){
const { current }= scope;
current.host(function(element){
if(element[key_reactive])
return element[key_reactive].push([ [ s, listener ], ...notes ]);
element[key_reactive]= [];
if(!element[key_reactive]) element[key_reactive]= [];
element[key_reactive].push([ [ s, listener ], ...notes ]);
if(current.prevent) return; // typically document.body, doenst need auto-remove as it should happen on page leave
on.disconnected(()=>
/*! Clears all Signals listeners added in the current scope/host (`S.el`, `assign`, …?).