Skip to content

Commit 1e8f21e

Browse files
committed
debug-element is now part of dom-utils
1 parent d5d90e5 commit 1e8f21e

9 files changed

+284
-271
lines changed

index.js

+58-37
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,35 @@
1313
*/
1414
const isFunction = fn => typeof fn === 'function';
1515

16+
/**
17+
* Recursively nest a map
18+
*
19+
* @param {Map} map - map to nest
20+
* @param {...any} args - key(s) to nest the map under
21+
* @returns {Map} nested map
22+
*/
23+
const nestMap = (map, ...args) => {
24+
const key = args.shift();
25+
!map.has(key) && map.set(key, new Map());
26+
return args.length ? nestMap(map.get(key), ...args) : map.get(key);
27+
}
28+
29+
/**
30+
* Recursively unnest a map
31+
*
32+
* @param {Map} map - map to unnest
33+
* @returns {any[]} unnested array
34+
*/
35+
const unnestMap = map => {
36+
const result = [];
37+
for (const [key, value] of map) {
38+
(value instanceof Map) ? result.push(key, ...unnestMap(value)) : result.push(key, value);
39+
}
40+
return result;
41+
}
42+
1643
// hold the currently active effect
17-
let activeEffect;
44+
let active;
1845

1946
/**
2047
* Define a reactive state
@@ -24,31 +51,23 @@ let activeEffect;
2451
* @returns {import('./types').FxState} getter function for the current value with a `set` method to update the value
2552
*/
2653
const cause = value => {
27-
const s = () => { // getter function
28-
activeEffect && s.e.add(activeEffect);
54+
const state = () => { // getter function
55+
active && state.effects.add(active);
2956
return value;
3057
};
31-
s.e = new Set(); // set of listeners
32-
s.set = (/** @type {any} */ updater) => { // setter function
58+
state.effects = new Set(); // set of listeners
59+
state.set = (/** @type {any} */ updater) => { // setter function
3360
const old = value;
3461
value = isFunction(updater) && !isFunction(value.set) ? updater(old) : updater;
3562
if (!Object.is(value, old)) {
36-
for (const e of s.e) e();
63+
for (const effect of state.effects) effect();
3764
}
3865
};
39-
return s;
66+
return state;
4067
};
4168

4269
/* === Exported functions === */
4370

44-
/**
45-
* Recursivlely unwrap a given variable if it is a function
46-
*
47-
* @param {any} value
48-
* @returns {any} unwrapped variable
49-
*/
50-
const unwrap = value => isFunction(value) ? unwrap(value()) : value;
51-
5271
/**
5372
* Define what happens when a reactive state changes
5473
*
@@ -60,42 +79,42 @@ const effect = fn => {
6079

6180
/**
6281
* @since 0.6.1
63-
*
6482
* @param {Element} element - target element
6583
* @param {import('./types').FxDOMInstruction} domFn
66-
* @param {any} key
67-
* @param {any} value
84+
* @param {any} key
85+
* @param {any} value
6886
*/
69-
const enqueue = (element, domFn, key, value) => {
70-
!targets.has(element) && targets.set(element, new Map());
71-
const instructions = targets.get(element);
72-
!instructions.has(domFn) && instructions.set(domFn, new Map());
73-
const argsMap = instructions.get(domFn);
74-
key && argsMap.set(key, value);
75-
};
87+
const enqueue = (element, domFn, key, value) => nestMap(targets, element, domFn).set(key, value);
7688

7789
// effect callback function
7890
const next = () => queueMicrotask(() => {
79-
const prev = activeEffect;
80-
activeEffect = next;
91+
const prev = active;
92+
active = next;
8193
const cleanup = fn(enqueue);
82-
activeEffect = prev;
94+
active = prev;
8395
// flush all queued instructions
84-
for (const [element, instructions] of targets.entries()) {
85-
for (const [domFn, argsMap] of instructions.entries()) {
86-
for (const [key, value] of argsMap.entries()) domFn(element, key, value);
87-
}
88-
}
96+
const [element, domFn, key, value] = unnestMap(targets);
97+
isFunction(domFn) && domFn(element, key, value);
8998
// @ts-ignore
9099
isFunction(cleanup) && cleanup();
91100
});
92101
next.targets = targets;
93102
next();
94103
}
95104

105+
/**
106+
* Recursivlely unwrap a given variable if it is a function
107+
*
108+
* @since 0.7.0
109+
* @param {any} value
110+
* @returns {any} unwrapped variable
111+
*/
112+
const unwrap = value => isFunction(value) ? unwrap(value()) : value;
113+
96114
/**
97115
* Parse a boolean attribute to an actual boolean value
98116
*
117+
* @since 0.7.0
99118
* @param {string|undefined} value
100119
* @returns {boolean}
101120
*/
@@ -104,6 +123,7 @@ const asBoolean = value => typeof value === 'string';
104123
/**
105124
* Parse an attribute to a number forced to integer
106125
*
126+
* @since 0.7.0
107127
* @param {string} value
108128
* @returns {number}
109129
*/
@@ -112,6 +132,7 @@ const asInteger = value => parseInt(value, 10);
112132
/**
113133
* Parse an attribute to a number
114134
*
135+
* @since 0.7.0
115136
* @param {string} value
116137
* @returns {number}
117138
*/
@@ -120,6 +141,7 @@ const asNumber = value => parseFloat(value);
120141
/**
121142
* Parse an attribute to a string
122143
*
144+
* @since 0.7.0
123145
* @param {string} value
124146
* @returns {string}
125147
*/
@@ -229,7 +251,7 @@ export default class UIElement extends HTMLElement {
229251
}
230252

231253
/**
232-
* Pass states to a child element
254+
* Passes states from the current UIElement to another UIElement
233255
*
234256
* @since 0.5.0
235257
* @param {import('./types').UIElement} element - child element to pass the states to
@@ -244,16 +266,15 @@ export default class UIElement extends HTMLElement {
244266
}
245267

246268
/**
247-
* Return a set of elements that have effects dependent on the given state
269+
* Return a Set of elements that have effects dependent on the given state
248270
*
249271
* @since 0.7.0
250-
*
251272
* @param {PropertyKey} key - state to get targets for
252273
* @returns {Set<Element>} set of elements that have effects dependent on the given state
253274
*/
254275
targets(key) {
255276
const targets = new Set();
256-
for (const effect of this.#state.get(key).e) {
277+
for (const effect of this.#state.get(key).effects) {
257278
for (const target of effect.targets.keys()) targets.add(target);
258279
}
259280
return targets;

index.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/debug-element.js

-128
This file was deleted.

lib/debug-element.min.js

-1
This file was deleted.

0 commit comments

Comments
 (0)