1
0
mirror of https://github.com/jaandrle/deka-dom-el synced 2025-07-01 20:32:13 +02:00

Refatc signals to .get/.set syntax #26

This commit is contained in:
2025-03-03 14:19:41 +01:00
parent 3168f452ae
commit ed7e6c7963
28 changed files with 405 additions and 297 deletions

View File

@ -1,36 +1,5 @@
//deka-dom-el library is available via global namespace `dde`
(()=> {
// src/signals-lib/common.js
var signals_global = {
/**
* Checks if a value is a signal
* @param {any} attributes - Value to check
* @returns {boolean} Whether the value is a signal
*/
isSignal(attributes) {
return false;
},
/**
* Processes an attribute that might be reactive
* @param {Element} obj - Element that owns the attribute
* @param {string} key - Attribute name
* @param {any} attr - Attribute value
* @param {Function} set - Function to set the attribute
* @returns {any} Processed attribute value
*/
processReactiveAttribute(obj, key, attr, set) {
return attr;
}
};
function registerReactivity(def, global = true) {
if (global) return Object.assign(signals_global, def);
Object.setPrototypeOf(def, signals_global);
return def;
}
function signals(_this) {
return signals_global.isPrototypeOf(_this) && _this !== signals_global ? _this : signals_global;
}
// src/helpers.js
var hasOwn = (...a) => Object.prototype.hasOwnProperty.call(...a);
function isUndef(value) {
@ -42,8 +11,20 @@ function typeOf(v) {
if (v === null) return "null";
return Object.prototype.toString.call(v);
}
function isInstance(obj, cls) {
return obj instanceof cls;
}
function isProtoFrom(obj, cls) {
return Object.prototype.isPrototypeOf.call(cls, obj);
}
function oCreate(proto = null, p = {}) {
return Object.create(proto, p);
}
function oAssign(...o) {
return Object.assign(...o);
}
function onAbort(signal2, listener) {
if (!signal2 || !(signal2 instanceof AbortSignal))
if (!signal2 || !isInstance(signal2, AbortSignal))
return true;
if (signal2.aborted)
return;
@ -67,7 +48,7 @@ var Defined = class extends Error {
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/dom-common.js") ? "src/" : curr_file;
const curr_lib = curr_file.includes("src/helpers.js") ? "src/" : curr_file;
this.stack = rest.find((l) => !l.includes(curr_lib)) || curr;
}
get compact() {
@ -76,6 +57,37 @@ var Defined = class extends Error {
}
};
// src/signals-lib/common.js
var signals_global = {
/**
* Checks if a value is a signal
* @param {any} attributes - Value to check
* @returns {boolean} Whether the value is a signal
*/
isSignal(attributes) {
return false;
},
/**
* Processes an attribute that might be reactive
* @param {Element} obj - Element that owns the attribute
* @param {string} key - Attribute name
* @param {any} attr - Attribute value
* @param {Function} set - Function to set the attribute
* @returns {any} Processed attribute value
*/
processReactiveAttribute(obj, key, attr, set) {
return attr;
}
};
function registerReactivity(def, global = true) {
if (global) return oAssign(signals_global, def);
Object.setPrototypeOf(def, signals_global);
return def;
}
function signals(_this) {
return isProtoFrom(_this, signals_global) && _this !== signals_global ? _this : signals_global;
}
// src/dom-common.js
var enviroment = {
setDeleteAttr,
@ -91,7 +103,7 @@ function setDeleteAttr(obj, prop, val) {
Reflect.set(obj, prop, val);
if (!isUndef(val)) return;
Reflect.deleteProperty(obj, prop);
if (obj instanceof enviroment.H && obj.getAttribute(prop) === "undefined")
if (isInstance(obj, enviroment.H) && obj.getAttribute(prop) === "undefined")
return obj.removeAttribute(prop);
if (Reflect.get(obj, prop) === "undefined")
return Reflect.set(obj, prop, "");
@ -149,7 +161,7 @@ var scope = {
* @returns {number} New length of the scope stack
*/
push(s = {}) {
return scopes.push(Object.assign({}, this.current, { prevent: false }, s));
return scopes.push(oAssign({}, this.current, { prevent: false }, s));
},
/**
* Pushes the root scope to the stack
@ -190,7 +202,7 @@ function createElement(tag, attributes, ...addons) {
const host = (...c) => !c.length ? el_host : (scoped === 1 ? addons.unshift(...c) : c.forEach((c2) => c2(el_host)), void 0);
scope.push({ scope: tag, host });
el = tag(attributes || void 0);
const is_fragment = el instanceof enviroment.F;
const is_fragment = isInstance(el, enviroment.F);
if (el.nodeName === "#comment") break;
const el_mark = createElement.mark({
type: "component",
@ -273,7 +285,7 @@ var { setDeleteAttr: setDeleteAttr2 } = enviroment;
function assign(element, ...attributes) {
if (!attributes.length) return element;
assign_context.set(element, assignContext(element, this));
for (const [key, value] of Object.entries(Object.assign({}, ...attributes)))
for (const [key, value] of Object.entries(oAssign({}, ...attributes)))
assignAttribute.call(this, element, key, value);
assign_context.delete(element);
return element;
@ -314,7 +326,7 @@ function assignAttribute(element, key, value) {
}
function assignContext(element, _this) {
if (assign_context.has(element)) return assign_context.get(element);
const is_svg = element instanceof enviroment.S;
const is_svg = isInstance(element, enviroment.S);
const setRemoveAttr = (is_svg ? setRemoveNS : setRemove).bind(null, element, "Attribute");
const s = signals(_this);
return { setRemoveAttr, s };
@ -331,7 +343,7 @@ function classListDeclarative(element, toggle) {
return element;
}
function elementAttribute(element, op, key, value) {
if (element instanceof enviroment.H)
if (isInstance(element, enviroment.H))
return element[op + "Attribute"](key, value);
return element[op + "AttributeNS"](null, key, value);
}
@ -491,9 +503,9 @@ function connectionsChangesObserverConstructor() {
if (store.size > 30)
await requestIdle();
const out = [];
if (!(element instanceof Node)) return out;
if (!isInstance(element, Node)) return out;
for (const el of store.keys()) {
if (el === element || !(el instanceof Node)) continue;
if (el === element || !isInstance(el, Node)) continue;
if (element.contains(el))
out.push(el);
}
@ -589,7 +601,7 @@ function dispatchEvent(name, options, host) {
d.unshift(element);
element = typeof host === "function" ? host() : host;
}
const event = d.length ? new CustomEvent(name, Object.assign({ detail: d[0] }, options)) : new Event(name, options);
const event = d.length ? new CustomEvent(name, oAssign({ detail: d[0] }, options)) : new Event(name, options);
return element.dispatchEvent(event);
};
}
@ -599,7 +611,7 @@ function on(event, listener, options) {
return element;
};
}
var lifeOptions = (obj) => Object.assign({}, typeof obj === "object" ? obj : null, { once: true });
var lifeOptions = (obj) => oAssign({}, typeof obj === "object" ? obj : null, { once: true });
on.connected = function(listener, options) {
options = lifeOptions(options);
return function registerElement(element) {
@ -657,12 +669,12 @@ var queueSignalWrite = /* @__PURE__ */ (() => {
let scheduled = false;
function flushSignals() {
scheduled = false;
for (const signal2 of pendingSignals) {
pendingSignals.delete(signal2);
const todo = pendingSignals;
pendingSignals = /* @__PURE__ */ new Set();
for (const signal2 of todo) {
const M = signal2[mark];
if (M) M.listeners.forEach((l) => l(M.value));
}
pendingSignals.clear();
}
return function(s) {
pendingSignals.add(s);
@ -673,8 +685,27 @@ var queueSignalWrite = /* @__PURE__ */ (() => {
})();
// src/signals-lib/signals-lib.js
var Signal = oCreate(null, {
get: { value() {
return read(this);
} },
set: { value(...v) {
return write(this, ...v);
} },
toJSON: { value() {
return read(this);
} },
valueOf: { value() {
return this[mark] && this[mark].value;
} }
});
var SignalReadOnly = oCreate(Signal, {
set: { value() {
return;
} }
});
function isSignal(candidate) {
return typeof candidate === "function" && hasOwn(candidate, mark);
return isProtoFrom(candidate, Signal);
}
var stack_watch = [];
var deps = /* @__PURE__ */ new WeakMap();
@ -687,7 +718,7 @@ function signal(value, actions) {
const [origin, ...deps_old] = deps.get(contextReWatch);
deps.set(contextReWatch, /* @__PURE__ */ new Set([origin]));
stack_watch.push(contextReWatch);
write(out, value());
write(out, value.get());
stack_watch.pop();
if (!deps_old.length) return;
const deps_curr = deps.get(contextReWatch);
@ -744,12 +775,8 @@ signal.clear = function(...signals2) {
}
};
var key_reactive = "__dde_reactive";
var storeMemo = /* @__PURE__ */ new WeakMap();
function memo(key, fun, host = fun) {
if (typeof key !== "string") key = JSON.stringify(key);
if (!storeMemo.has(host)) storeMemo.set(host, {});
const cache = storeMemo.get(host);
return hasOwn(cache, key) ? cache[key] : cache[key] = fun();
function cache(store = oCreate()) {
return (key, fun) => hasOwn(store, key) ? store[key] : store[key] = fun();
}
signal.el = function(s, map) {
const mark_start = createElement.mark({ type: "reactive", source: new Defined().compact }, true);
@ -757,15 +784,16 @@ signal.el = function(s, map) {
const out = enviroment.D.createDocumentFragment();
out.append(mark_start, mark_end);
const { current } = scope;
let cache_shared = oCreate();
const reRenderReactiveElement = (v) => {
if (!mark_start.parentNode || !mark_end.parentNode)
return removeSignalListener(s, reRenderReactiveElement);
const cache = {};
const memo = cache(cache_shared);
cache_shared = oCreate();
scope.push(current);
let els = map(v, function useCache(key, fun) {
return cache[key] = memo(key, fun, reRenderReactiveElement);
return cache_shared[key] = memo(key, fun);
});
storeMemo.set(reRenderReactiveElement, cache);
scope.pop();
if (!Array.isArray(els))
els = [els];
@ -781,7 +809,13 @@ signal.el = function(s, map) {
};
addSignalListener(s, reRenderReactiveElement);
removeSignalsFromElements(s, reRenderReactiveElement, mark_start, map);
reRenderReactiveElement(s());
reRenderReactiveElement(s.get());
current.host(on.disconnected(
() => (
/*! Clears cached elements for reactive element `S.el` */
cache_shared = {}
)
));
return out;
};
function requestCleanUpReactives(host) {
@ -797,7 +831,11 @@ var observedAttributeActions = {
};
function observedAttribute(store) {
return function(instance, name) {
const varS = (...args) => !args.length ? read(varS) : instance.setAttribute(name, ...args);
const varS = oCreate(Signal, {
set: { value(...v) {
return instance.setAttribute(name, ...v);
} }
});
const out = toSignal(varS, instance.getAttribute(name), observedAttributeActions);
store[name] = out;
return out;
@ -841,7 +879,7 @@ var signals_config = {
};
addSignalListener(attrs, l);
removeSignalsFromElements(attrs, l, element, key);
return attrs();
return attrs.get();
}
};
function removeSignalsFromElements(s, listener, ...notes) {
@ -864,12 +902,12 @@ var cleanUpRegistry = new FinalizationRegistry(function(s) {
signal.clear({ [mark]: s });
});
function create(is_readonly, value, actions) {
const varS = is_readonly ? () => read(varS) : (...value2) => value2.length ? write(varS, ...value2) : read(varS);
const varS = oCreate(is_readonly ? SignalReadOnly : Signal);
const SI = toSignal(varS, value, actions, is_readonly);
cleanUpRegistry.register(SI, SI[mark]);
return SI;
}
var protoSigal = Object.assign(/* @__PURE__ */ Object.create(null), {
var protoSigal = oAssign(oCreate(), {
/**
* Prevents signal propagation
*/
@ -888,7 +926,7 @@ function toSignal(s, value, actions, readonly = false) {
}
const { host } = scope;
Reflect.defineProperty(s, mark, {
value: {
value: oAssign(oCreate(protoSigal), {
value,
actions,
onclear,
@ -896,14 +934,11 @@ function toSignal(s, value, actions, readonly = false) {
listeners: /* @__PURE__ */ new Set(),
defined: new Defined().stack,
readonly
},
}),
enumerable: false,
writable: false,
configurable: true
});
s.toJSON = () => s();
s.valueOf = () => s[mark] && s[mark].value;
Object.setPrototypeOf(s[mark], protoSigal);
return s;
}
function currentContext() {

83
dist/dde.js vendored
View File

@ -1,5 +1,39 @@
//deka-dom-el library is available via global namespace `dde`
(()=> {
// src/helpers.js
function isUndef(value) {
return typeof value === "undefined";
}
function isInstance(obj, cls) {
return obj instanceof cls;
}
function isProtoFrom(obj, cls) {
return Object.prototype.isPrototypeOf.call(cls, obj);
}
function oAssign(...o) {
return Object.assign(...o);
}
function onAbort(signal, listener) {
if (!signal || !isInstance(signal, AbortSignal))
return true;
if (signal.aborted)
return;
signal.addEventListener("abort", listener);
return function cleanUp() {
signal.removeEventListener("abort", listener);
};
}
function observedAttributes(instance, observedAttribute) {
const { observedAttributes: observedAttributes3 = [] } = instance.constructor;
return observedAttributes3.reduce(function(out, name) {
out[kebabToCamel(name)] = observedAttribute(instance, name);
return out;
}, {});
}
function kebabToCamel(name) {
return name.replace(/-./g, (x) => x[1].toUpperCase());
}
// src/signals-lib/common.js
var signals_global = {
/**
@ -23,37 +57,12 @@ var signals_global = {
}
};
function registerReactivity(def, global = true) {
if (global) return Object.assign(signals_global, def);
if (global) return oAssign(signals_global, def);
Object.setPrototypeOf(def, signals_global);
return def;
}
function signals(_this) {
return signals_global.isPrototypeOf(_this) && _this !== signals_global ? _this : signals_global;
}
// src/helpers.js
function isUndef(value) {
return typeof value === "undefined";
}
function onAbort(signal, listener) {
if (!signal || !(signal instanceof AbortSignal))
return true;
if (signal.aborted)
return;
signal.addEventListener("abort", listener);
return function cleanUp() {
signal.removeEventListener("abort", listener);
};
}
function observedAttributes(instance, observedAttribute) {
const { observedAttributes: observedAttributes3 = [] } = instance.constructor;
return observedAttributes3.reduce(function(out, name) {
out[kebabToCamel(name)] = observedAttribute(instance, name);
return out;
}, {});
}
function kebabToCamel(name) {
return name.replace(/-./g, (x) => x[1].toUpperCase());
return isProtoFrom(_this, signals_global) && _this !== signals_global ? _this : signals_global;
}
// src/dom-common.js
@ -71,7 +80,7 @@ function setDeleteAttr(obj, prop, val) {
Reflect.set(obj, prop, val);
if (!isUndef(val)) return;
Reflect.deleteProperty(obj, prop);
if (obj instanceof enviroment.H && obj.getAttribute(prop) === "undefined")
if (isInstance(obj, enviroment.H) && obj.getAttribute(prop) === "undefined")
return obj.removeAttribute(prop);
if (Reflect.get(obj, prop) === "undefined")
return Reflect.set(obj, prop, "");
@ -129,7 +138,7 @@ var scope = {
* @returns {number} New length of the scope stack
*/
push(s = {}) {
return scopes.push(Object.assign({}, this.current, { prevent: false }, s));
return scopes.push(oAssign({}, this.current, { prevent: false }, s));
},
/**
* Pushes the root scope to the stack
@ -170,7 +179,7 @@ function createElement(tag, attributes, ...addons) {
const host = (...c) => !c.length ? el_host : (scoped === 1 ? addons.unshift(...c) : c.forEach((c2) => c2(el_host)), void 0);
scope.push({ scope: tag, host });
el = tag(attributes || void 0);
const is_fragment = el instanceof enviroment.F;
const is_fragment = isInstance(el, enviroment.F);
if (el.nodeName === "#comment") break;
const el_mark = createElement.mark({
type: "component",
@ -253,7 +262,7 @@ var { setDeleteAttr: setDeleteAttr2 } = enviroment;
function assign(element, ...attributes) {
if (!attributes.length) return element;
assign_context.set(element, assignContext(element, this));
for (const [key, value] of Object.entries(Object.assign({}, ...attributes)))
for (const [key, value] of Object.entries(oAssign({}, ...attributes)))
assignAttribute.call(this, element, key, value);
assign_context.delete(element);
return element;
@ -294,7 +303,7 @@ function assignAttribute(element, key, value) {
}
function assignContext(element, _this) {
if (assign_context.has(element)) return assign_context.get(element);
const is_svg = element instanceof enviroment.S;
const is_svg = isInstance(element, enviroment.S);
const setRemoveAttr = (is_svg ? setRemoveNS : setRemove).bind(null, element, "Attribute");
const s = signals(_this);
return { setRemoveAttr, s };
@ -311,7 +320,7 @@ function classListDeclarative(element, toggle) {
return element;
}
function elementAttribute(element, op, key, value) {
if (element instanceof enviroment.H)
if (isInstance(element, enviroment.H))
return element[op + "Attribute"](key, value);
return element[op + "AttributeNS"](null, key, value);
}
@ -471,9 +480,9 @@ function connectionsChangesObserverConstructor() {
if (store.size > 30)
await requestIdle();
const out = [];
if (!(element instanceof Node)) return out;
if (!isInstance(element, Node)) return out;
for (const el of store.keys()) {
if (el === element || !(el instanceof Node)) continue;
if (el === element || !isInstance(el, Node)) continue;
if (element.contains(el))
out.push(el);
}
@ -569,7 +578,7 @@ function dispatchEvent(name, options, host) {
d.unshift(element);
element = typeof host === "function" ? host() : host;
}
const event = d.length ? new CustomEvent(name, Object.assign({ detail: d[0] }, options)) : new Event(name, options);
const event = d.length ? new CustomEvent(name, oAssign({ detail: d[0] }, options)) : new Event(name, options);
return element.dispatchEvent(event);
};
}
@ -579,7 +588,7 @@ function on(event, listener, options) {
return element;
};
}
var lifeOptions = (obj) => Object.assign({}, typeof obj === "object" ? obj : null, { once: true });
var lifeOptions = (obj) => oAssign({}, typeof obj === "object" ? obj : null, { once: true });
on.connected = function(listener, options) {
options = lifeOptions(options);
return function registerElement(element) {

View File

@ -515,7 +515,14 @@ interface ddeSVGTSpanElement extends SVGTSpanElement{ append: ddeAppend<ddeSVGTS
interface ddeSVGUseElement extends SVGUseElement{ append: ddeAppend<ddeSVGUseElement>; }
interface ddeSVGViewElement extends SVGViewElement{ append: ddeAppend<ddeSVGViewElement>; }
// editorconfig-checker-enable
export type Signal<V, A>= (set?: V)=> V & A;
export interface Signal<V, A> {
/** The current value of the signal */
get(): V;
/** Set new value of the signal */
set(value: V): V;
toJSON(): V;
valueOf(): V;
}
type Action<V>= (this: { value: V, stopPropagation(): void }, ...a: any[])=> typeof signal._ | void;
//type SymbolSignal= Symbol;
type SymbolOnclear= symbol;
@ -544,7 +551,7 @@ interface signal{
* ```js
* const name= S("Jan");
* const surname= S("Andrle");
* const fullname= S(()=> name()+" "+surname());
* const fullname= S(()=> name.get()+" "+surname.get());
* ```
* @param value Initial signal value. Or function computing value from other signals.
* @param actions Use to define actions on the signal. Such as add item to the array.

View File

@ -1,34 +1,3 @@
// src/signals-lib/common.js
var signals_global = {
/**
* Checks if a value is a signal
* @param {any} attributes - Value to check
* @returns {boolean} Whether the value is a signal
*/
isSignal(attributes) {
return false;
},
/**
* Processes an attribute that might be reactive
* @param {Element} obj - Element that owns the attribute
* @param {string} key - Attribute name
* @param {any} attr - Attribute value
* @param {Function} set - Function to set the attribute
* @returns {any} Processed attribute value
*/
processReactiveAttribute(obj, key, attr, set) {
return attr;
}
};
function registerReactivity(def, global = true) {
if (global) return Object.assign(signals_global, def);
Object.setPrototypeOf(def, signals_global);
return def;
}
function signals(_this) {
return signals_global.isPrototypeOf(_this) && _this !== signals_global ? _this : signals_global;
}
// src/helpers.js
var hasOwn = (...a) => Object.prototype.hasOwnProperty.call(...a);
function isUndef(value) {
@ -40,8 +9,20 @@ function typeOf(v) {
if (v === null) return "null";
return Object.prototype.toString.call(v);
}
function isInstance(obj, cls) {
return obj instanceof cls;
}
function isProtoFrom(obj, cls) {
return Object.prototype.isPrototypeOf.call(cls, obj);
}
function oCreate(proto = null, p = {}) {
return Object.create(proto, p);
}
function oAssign(...o) {
return Object.assign(...o);
}
function onAbort(signal2, listener) {
if (!signal2 || !(signal2 instanceof AbortSignal))
if (!signal2 || !isInstance(signal2, AbortSignal))
return true;
if (signal2.aborted)
return;
@ -65,7 +46,7 @@ var Defined = class extends Error {
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/dom-common.js") ? "src/" : curr_file;
const curr_lib = curr_file.includes("src/helpers.js") ? "src/" : curr_file;
this.stack = rest.find((l) => !l.includes(curr_lib)) || curr;
}
get compact() {
@ -74,6 +55,37 @@ var Defined = class extends Error {
}
};
// src/signals-lib/common.js
var signals_global = {
/**
* Checks if a value is a signal
* @param {any} attributes - Value to check
* @returns {boolean} Whether the value is a signal
*/
isSignal(attributes) {
return false;
},
/**
* Processes an attribute that might be reactive
* @param {Element} obj - Element that owns the attribute
* @param {string} key - Attribute name
* @param {any} attr - Attribute value
* @param {Function} set - Function to set the attribute
* @returns {any} Processed attribute value
*/
processReactiveAttribute(obj, key, attr, set) {
return attr;
}
};
function registerReactivity(def, global = true) {
if (global) return oAssign(signals_global, def);
Object.setPrototypeOf(def, signals_global);
return def;
}
function signals(_this) {
return isProtoFrom(_this, signals_global) && _this !== signals_global ? _this : signals_global;
}
// src/dom-common.js
var enviroment = {
setDeleteAttr,
@ -89,7 +101,7 @@ function setDeleteAttr(obj, prop, val) {
Reflect.set(obj, prop, val);
if (!isUndef(val)) return;
Reflect.deleteProperty(obj, prop);
if (obj instanceof enviroment.H && obj.getAttribute(prop) === "undefined")
if (isInstance(obj, enviroment.H) && obj.getAttribute(prop) === "undefined")
return obj.removeAttribute(prop);
if (Reflect.get(obj, prop) === "undefined")
return Reflect.set(obj, prop, "");
@ -147,7 +159,7 @@ var scope = {
* @returns {number} New length of the scope stack
*/
push(s = {}) {
return scopes.push(Object.assign({}, this.current, { prevent: false }, s));
return scopes.push(oAssign({}, this.current, { prevent: false }, s));
},
/**
* Pushes the root scope to the stack
@ -188,7 +200,7 @@ function createElement(tag, attributes, ...addons) {
const host = (...c) => !c.length ? el_host : (scoped === 1 ? addons.unshift(...c) : c.forEach((c2) => c2(el_host)), void 0);
scope.push({ scope: tag, host });
el = tag(attributes || void 0);
const is_fragment = el instanceof enviroment.F;
const is_fragment = isInstance(el, enviroment.F);
if (el.nodeName === "#comment") break;
const el_mark = createElement.mark({
type: "component",
@ -271,7 +283,7 @@ var { setDeleteAttr: setDeleteAttr2 } = enviroment;
function assign(element, ...attributes) {
if (!attributes.length) return element;
assign_context.set(element, assignContext(element, this));
for (const [key, value] of Object.entries(Object.assign({}, ...attributes)))
for (const [key, value] of Object.entries(oAssign({}, ...attributes)))
assignAttribute.call(this, element, key, value);
assign_context.delete(element);
return element;
@ -312,7 +324,7 @@ function assignAttribute(element, key, value) {
}
function assignContext(element, _this) {
if (assign_context.has(element)) return assign_context.get(element);
const is_svg = element instanceof enviroment.S;
const is_svg = isInstance(element, enviroment.S);
const setRemoveAttr = (is_svg ? setRemoveNS : setRemove).bind(null, element, "Attribute");
const s = signals(_this);
return { setRemoveAttr, s };
@ -329,7 +341,7 @@ function classListDeclarative(element, toggle) {
return element;
}
function elementAttribute(element, op, key, value) {
if (element instanceof enviroment.H)
if (isInstance(element, enviroment.H))
return element[op + "Attribute"](key, value);
return element[op + "AttributeNS"](null, key, value);
}
@ -489,9 +501,9 @@ function connectionsChangesObserverConstructor() {
if (store.size > 30)
await requestIdle();
const out = [];
if (!(element instanceof Node)) return out;
if (!isInstance(element, Node)) return out;
for (const el of store.keys()) {
if (el === element || !(el instanceof Node)) continue;
if (el === element || !isInstance(el, Node)) continue;
if (element.contains(el))
out.push(el);
}
@ -587,7 +599,7 @@ function dispatchEvent(name, options, host) {
d.unshift(element);
element = typeof host === "function" ? host() : host;
}
const event = d.length ? new CustomEvent(name, Object.assign({ detail: d[0] }, options)) : new Event(name, options);
const event = d.length ? new CustomEvent(name, oAssign({ detail: d[0] }, options)) : new Event(name, options);
return element.dispatchEvent(event);
};
}
@ -597,7 +609,7 @@ function on(event, listener, options) {
return element;
};
}
var lifeOptions = (obj) => Object.assign({}, typeof obj === "object" ? obj : null, { once: true });
var lifeOptions = (obj) => oAssign({}, typeof obj === "object" ? obj : null, { once: true });
on.connected = function(listener, options) {
options = lifeOptions(options);
return function registerElement(element) {
@ -655,12 +667,12 @@ var queueSignalWrite = /* @__PURE__ */ (() => {
let scheduled = false;
function flushSignals() {
scheduled = false;
for (const signal2 of pendingSignals) {
pendingSignals.delete(signal2);
const todo = pendingSignals;
pendingSignals = /* @__PURE__ */ new Set();
for (const signal2 of todo) {
const M = signal2[mark];
if (M) M.listeners.forEach((l) => l(M.value));
}
pendingSignals.clear();
}
return function(s) {
pendingSignals.add(s);
@ -671,8 +683,27 @@ var queueSignalWrite = /* @__PURE__ */ (() => {
})();
// src/signals-lib/signals-lib.js
var Signal = oCreate(null, {
get: { value() {
return read(this);
} },
set: { value(...v) {
return write(this, ...v);
} },
toJSON: { value() {
return read(this);
} },
valueOf: { value() {
return this[mark] && this[mark].value;
} }
});
var SignalReadOnly = oCreate(Signal, {
set: { value() {
return;
} }
});
function isSignal(candidate) {
return typeof candidate === "function" && hasOwn(candidate, mark);
return isProtoFrom(candidate, Signal);
}
var stack_watch = [];
var deps = /* @__PURE__ */ new WeakMap();
@ -685,7 +716,7 @@ function signal(value, actions) {
const [origin, ...deps_old] = deps.get(contextReWatch);
deps.set(contextReWatch, /* @__PURE__ */ new Set([origin]));
stack_watch.push(contextReWatch);
write(out, value());
write(out, value.get());
stack_watch.pop();
if (!deps_old.length) return;
const deps_curr = deps.get(contextReWatch);
@ -742,12 +773,8 @@ signal.clear = function(...signals2) {
}
};
var key_reactive = "__dde_reactive";
var storeMemo = /* @__PURE__ */ new WeakMap();
function memo(key, fun, host = fun) {
if (typeof key !== "string") key = JSON.stringify(key);
if (!storeMemo.has(host)) storeMemo.set(host, {});
const cache = storeMemo.get(host);
return hasOwn(cache, key) ? cache[key] : cache[key] = fun();
function cache(store = oCreate()) {
return (key, fun) => hasOwn(store, key) ? store[key] : store[key] = fun();
}
signal.el = function(s, map) {
const mark_start = createElement.mark({ type: "reactive", source: new Defined().compact }, true);
@ -755,15 +782,16 @@ signal.el = function(s, map) {
const out = enviroment.D.createDocumentFragment();
out.append(mark_start, mark_end);
const { current } = scope;
let cache_shared = oCreate();
const reRenderReactiveElement = (v) => {
if (!mark_start.parentNode || !mark_end.parentNode)
return removeSignalListener(s, reRenderReactiveElement);
const cache = {};
const memo = cache(cache_shared);
cache_shared = oCreate();
scope.push(current);
let els = map(v, function useCache(key, fun) {
return cache[key] = memo(key, fun, reRenderReactiveElement);
return cache_shared[key] = memo(key, fun);
});
storeMemo.set(reRenderReactiveElement, cache);
scope.pop();
if (!Array.isArray(els))
els = [els];
@ -779,7 +807,13 @@ signal.el = function(s, map) {
};
addSignalListener(s, reRenderReactiveElement);
removeSignalsFromElements(s, reRenderReactiveElement, mark_start, map);
reRenderReactiveElement(s());
reRenderReactiveElement(s.get());
current.host(on.disconnected(
() => (
/*! Clears cached elements for reactive element `S.el` */
cache_shared = {}
)
));
return out;
};
function requestCleanUpReactives(host) {
@ -795,7 +829,11 @@ var observedAttributeActions = {
};
function observedAttribute(store) {
return function(instance, name) {
const varS = (...args) => !args.length ? read(varS) : instance.setAttribute(name, ...args);
const varS = oCreate(Signal, {
set: { value(...v) {
return instance.setAttribute(name, ...v);
} }
});
const out = toSignal(varS, instance.getAttribute(name), observedAttributeActions);
store[name] = out;
return out;
@ -839,7 +877,7 @@ var signals_config = {
};
addSignalListener(attrs, l);
removeSignalsFromElements(attrs, l, element, key);
return attrs();
return attrs.get();
}
};
function removeSignalsFromElements(s, listener, ...notes) {
@ -862,12 +900,12 @@ var cleanUpRegistry = new FinalizationRegistry(function(s) {
signal.clear({ [mark]: s });
});
function create(is_readonly, value, actions) {
const varS = is_readonly ? () => read(varS) : (...value2) => value2.length ? write(varS, ...value2) : read(varS);
const varS = oCreate(is_readonly ? SignalReadOnly : Signal);
const SI = toSignal(varS, value, actions, is_readonly);
cleanUpRegistry.register(SI, SI[mark]);
return SI;
}
var protoSigal = Object.assign(/* @__PURE__ */ Object.create(null), {
var protoSigal = oAssign(oCreate(), {
/**
* Prevents signal propagation
*/
@ -886,7 +924,7 @@ function toSignal(s, value, actions, readonly = false) {
}
const { host } = scope;
Reflect.defineProperty(s, mark, {
value: {
value: oAssign(oCreate(protoSigal), {
value,
actions,
onclear,
@ -894,14 +932,11 @@ function toSignal(s, value, actions, readonly = false) {
listeners: /* @__PURE__ */ new Set(),
defined: new Defined().stack,
readonly
},
}),
enumerable: false,
writable: false,
configurable: true
});
s.toJSON = () => s();
s.valueOf = () => s[mark] && s[mark].value;
Object.setPrototypeOf(s[mark], protoSigal);
return s;
}
function currentContext() {

83
dist/esm.js vendored
View File

@ -1,3 +1,37 @@
// src/helpers.js
function isUndef(value) {
return typeof value === "undefined";
}
function isInstance(obj, cls) {
return obj instanceof cls;
}
function isProtoFrom(obj, cls) {
return Object.prototype.isPrototypeOf.call(cls, obj);
}
function oAssign(...o) {
return Object.assign(...o);
}
function onAbort(signal, listener) {
if (!signal || !isInstance(signal, AbortSignal))
return true;
if (signal.aborted)
return;
signal.addEventListener("abort", listener);
return function cleanUp() {
signal.removeEventListener("abort", listener);
};
}
function observedAttributes(instance, observedAttribute) {
const { observedAttributes: observedAttributes3 = [] } = instance.constructor;
return observedAttributes3.reduce(function(out, name) {
out[kebabToCamel(name)] = observedAttribute(instance, name);
return out;
}, {});
}
function kebabToCamel(name) {
return name.replace(/-./g, (x) => x[1].toUpperCase());
}
// src/signals-lib/common.js
var signals_global = {
/**
@ -21,37 +55,12 @@ var signals_global = {
}
};
function registerReactivity(def, global = true) {
if (global) return Object.assign(signals_global, def);
if (global) return oAssign(signals_global, def);
Object.setPrototypeOf(def, signals_global);
return def;
}
function signals(_this) {
return signals_global.isPrototypeOf(_this) && _this !== signals_global ? _this : signals_global;
}
// src/helpers.js
function isUndef(value) {
return typeof value === "undefined";
}
function onAbort(signal, listener) {
if (!signal || !(signal instanceof AbortSignal))
return true;
if (signal.aborted)
return;
signal.addEventListener("abort", listener);
return function cleanUp() {
signal.removeEventListener("abort", listener);
};
}
function observedAttributes(instance, observedAttribute) {
const { observedAttributes: observedAttributes3 = [] } = instance.constructor;
return observedAttributes3.reduce(function(out, name) {
out[kebabToCamel(name)] = observedAttribute(instance, name);
return out;
}, {});
}
function kebabToCamel(name) {
return name.replace(/-./g, (x) => x[1].toUpperCase());
return isProtoFrom(_this, signals_global) && _this !== signals_global ? _this : signals_global;
}
// src/dom-common.js
@ -69,7 +78,7 @@ function setDeleteAttr(obj, prop, val) {
Reflect.set(obj, prop, val);
if (!isUndef(val)) return;
Reflect.deleteProperty(obj, prop);
if (obj instanceof enviroment.H && obj.getAttribute(prop) === "undefined")
if (isInstance(obj, enviroment.H) && obj.getAttribute(prop) === "undefined")
return obj.removeAttribute(prop);
if (Reflect.get(obj, prop) === "undefined")
return Reflect.set(obj, prop, "");
@ -127,7 +136,7 @@ var scope = {
* @returns {number} New length of the scope stack
*/
push(s = {}) {
return scopes.push(Object.assign({}, this.current, { prevent: false }, s));
return scopes.push(oAssign({}, this.current, { prevent: false }, s));
},
/**
* Pushes the root scope to the stack
@ -168,7 +177,7 @@ function createElement(tag, attributes, ...addons) {
const host = (...c) => !c.length ? el_host : (scoped === 1 ? addons.unshift(...c) : c.forEach((c2) => c2(el_host)), void 0);
scope.push({ scope: tag, host });
el = tag(attributes || void 0);
const is_fragment = el instanceof enviroment.F;
const is_fragment = isInstance(el, enviroment.F);
if (el.nodeName === "#comment") break;
const el_mark = createElement.mark({
type: "component",
@ -251,7 +260,7 @@ var { setDeleteAttr: setDeleteAttr2 } = enviroment;
function assign(element, ...attributes) {
if (!attributes.length) return element;
assign_context.set(element, assignContext(element, this));
for (const [key, value] of Object.entries(Object.assign({}, ...attributes)))
for (const [key, value] of Object.entries(oAssign({}, ...attributes)))
assignAttribute.call(this, element, key, value);
assign_context.delete(element);
return element;
@ -292,7 +301,7 @@ function assignAttribute(element, key, value) {
}
function assignContext(element, _this) {
if (assign_context.has(element)) return assign_context.get(element);
const is_svg = element instanceof enviroment.S;
const is_svg = isInstance(element, enviroment.S);
const setRemoveAttr = (is_svg ? setRemoveNS : setRemove).bind(null, element, "Attribute");
const s = signals(_this);
return { setRemoveAttr, s };
@ -309,7 +318,7 @@ function classListDeclarative(element, toggle) {
return element;
}
function elementAttribute(element, op, key, value) {
if (element instanceof enviroment.H)
if (isInstance(element, enviroment.H))
return element[op + "Attribute"](key, value);
return element[op + "AttributeNS"](null, key, value);
}
@ -469,9 +478,9 @@ function connectionsChangesObserverConstructor() {
if (store.size > 30)
await requestIdle();
const out = [];
if (!(element instanceof Node)) return out;
if (!isInstance(element, Node)) return out;
for (const el of store.keys()) {
if (el === element || !(el instanceof Node)) continue;
if (el === element || !isInstance(el, Node)) continue;
if (element.contains(el))
out.push(el);
}
@ -567,7 +576,7 @@ function dispatchEvent(name, options, host) {
d.unshift(element);
element = typeof host === "function" ? host() : host;
}
const event = d.length ? new CustomEvent(name, Object.assign({ detail: d[0] }, options)) : new Event(name, options);
const event = d.length ? new CustomEvent(name, oAssign({ detail: d[0] }, options)) : new Event(name, options);
return element.dispatchEvent(event);
};
}
@ -577,7 +586,7 @@ function on(event, listener, options) {
return element;
};
}
var lifeOptions = (obj) => Object.assign({}, typeof obj === "object" ? obj : null, { once: true });
var lifeOptions = (obj) => oAssign({}, typeof obj === "object" ? obj : null, { once: true });
on.connected = function(listener, options) {
options = lifeOptions(options);
return function registerElement(element) {