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

Replaces defined with name/host

This commit is contained in:
2025-03-17 09:47:09 +01:00
parent 3301a6600c
commit 992370b4e3
14 changed files with 57 additions and 107 deletions

View File

@ -19,7 +19,7 @@ const store_abort= new WeakMap();
export const scope= {
/**
* Gets the current scope
* @returns {Object} Current scope context
* @returns {typeof scopes[number]} Current scope context
*/
get current(){ return scopes[scopes.length-1]; },

View File

@ -68,21 +68,3 @@ export function observedAttributes(instance, observedAttribute){
* @returns {string} The camelCase string
*/
function kebabToCamel(name){ return name.replace(/-./g, x=> x[1].toUpperCase()); }
/**
* Error class for definition tracking
* Shows the correct stack trace for debugging (signal) creation
*/
export class Defined extends Error{
constructor(){
super();
const [ curr, ...rest ]= this.stack.split("\n");
const curr_file= curr.slice(curr.indexOf("@"), curr.indexOf(".js:")+4);
const curr_lib= curr_file.includes("src/helpers.js") ? "src/" : curr_file;
this.stack= rest.find(l=> !l.includes(curr_lib)) || curr;
}
get compact(){
const { stack }= this;
return stack.slice(0, stack.indexOf("@")+1)+"…"+stack.slice(stack.lastIndexOf("/"));
}
}

View File

@ -1,5 +1,5 @@
import { hasOwn, oCreate } from "./helpers.js";
const memoMark= "__dde_memo";
const memoMark= "__dde_memo_of";
const memo_scope= [];
/**
* ```js
@ -20,14 +20,14 @@ 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 obj[memoMark]; };
memo.isScope= function(obj){ return Boolean(obj[memoMark]); };
/**
* @param {Function} fun
* @param {Object} [options={}]
* @param {AbortSignal} options.signal
* @param {boolean} [options.onlyLast=false]
* */
memo.scope= function memoScope(fun, { signal, onlyLast }= {}){
memo.scope= function memoScopeCreate(fun, { signal, onlyLast }= {}){
let cache= oCreate();
function memoScope(...args){
if(signal && signal.aborted)
@ -43,7 +43,7 @@ memo.scope= function memoScope(fun, { signal, onlyLast }= {}){
cache= cache_local;
return out;
}
memoScope[memoMark]= true;
memoScope[memoMark]= fun;
memoScope.clear= ()=> cache= oCreate();
if(signal) signal.addEventListener("abort", memoScope.clear);
return memoScope;

View File

@ -1,6 +1,6 @@
import { queueSignalWrite, mark } from "./helpers.js";
export { mark };
import { hasOwn, Defined, oCreate, oAssign } from "../helpers.js";
import { hasOwn, oCreate, oAssign } from "../helpers.js";
const Signal = oCreate(null, {
get: { value(){ return read(this); } },
@ -174,11 +174,11 @@ import { memo } from "../memo.js";
*/
signal.el= function(s, map){
map= memo.isScope(map) ? map : memo.scope(map, { onlyLast: true });
const mark_start= el.mark({ type: "reactive", source: new Defined().compact }, 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;
const out= env.D.createDocumentFragment();
out.append(mark_start, mark_end);
const { current }= scope;
const reRenderReactiveElement= v=> {
if(!mark_start.parentNode || !mark_end.parentNode) // === `isConnected` or wasnt yet rendered
return removeSignalListener(s, reRenderReactiveElement);
@ -386,7 +386,6 @@ function toSignal(s, value, actions, readonly= false){
value: oAssign(oCreate(protoSigal), {
value, actions, onclear, host,
listeners: new Set(),
defined: new Defined().stack,
readonly
}),
enumerable: false,