@davidcal/frosth 1.1.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Frosth - Yet another UI framework
2
2
 
3
- **Latest version**: v1.1.0
3
+ **Latest version**: v1.3.0
4
4
 
5
5
  ## Motivation
6
6
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@davidcal/frosth",
3
- "version": "1.1.0",
3
+ "version": "1.3.0",
4
4
  "description": "Async-first CRUD-last UI framework that treats encapsulation as a solved problem.",
5
5
  "type": "module",
6
6
  "main": "./src/sigew.js",
@@ -0,0 +1,20 @@
1
+ /**
2
+ * A beacon is a valueless-sink. The output is a stable interface as provided on creation. Subscribers are notified whenever the beacon is fired.
3
+ */
4
+ export const create_beacon = (intf) => {
5
+ let next_version = 0n;
6
+
7
+ const version_signal = create_signal(((version) => () => version)(next_version++));
8
+
9
+ const result = (self) => {
10
+ version_signal(self);
11
+
12
+ return intf;
13
+ };
14
+
15
+ result.fire = () => {
16
+ version_signal.set(((version) => () => version)(next_version++));
17
+ };
18
+
19
+ return result;
20
+ };
@@ -4,6 +4,9 @@ const error_value = () => {
4
4
  throw new Error("Signal value must be a reactive function. Wrap the value in either `() =>` or `(s) =>` depending on whether or not the current value should be reactive, beyond the mere reactivity of the signal.");
5
5
  }
6
6
 
7
+ /**
8
+ * A signal is a value-based sink. Each update is a new value. If the value does not change, subscribers are not notified.
9
+ */
7
10
  export const create_signal = (initial) => {
8
11
  if (initial === undefined) {
9
12
  initial = () => undefined;
@@ -13,9 +16,11 @@ export const create_signal = (initial) => {
13
16
  error_value();
14
17
  }
15
18
 
16
- const raw = create_signal_raw({
17
- retrieve: initial,
18
- });
19
+ const raw = create_signal_raw(
20
+ { retrieve: initial },
21
+ Object.is,
22
+ (value) => value.retrieve(),
23
+ );
19
24
 
20
25
  const result = (self) => {
21
26
  return raw(self).retrieve();
@@ -1,10 +1,18 @@
1
- export const create_signal_raw = (initial) => {
1
+ export const create_signal_raw = (initial, is_equal, get_snapshot) => {
2
+ is_equal ??= Object.is;
3
+ get_snapshot ??= (v) => v;
4
+
2
5
  let value = initial;
6
+ let snapshot = undefined;
3
7
 
4
8
  const subscribers = new Set();
5
9
 
6
10
  const get = (self) => {
7
11
  if (self?._run_effect) {
12
+ if (subscribers.size === 0) {
13
+ snapshot = get_snapshot(value);
14
+ }
15
+
8
16
  subscribers.add(self._run_effect);
9
17
  }
10
18
 
@@ -12,7 +20,26 @@ export const create_signal_raw = (initial) => {
12
20
  };
13
21
 
14
22
  const set = (new_value) => {
23
+ if (subscribers.size === 0) {
24
+ // Signals are lazy. We are not permitted to invoke `is_equal` or `get_snapshot` if there are no subscribers.
25
+
26
+ value = new_value;
27
+
28
+ return;
29
+ }
30
+
31
+ const new_snapshot = get_snapshot(new_value);
32
+
33
+ if (is_equal(snapshot, new_snapshot)) {
34
+ // Must still assign, as underlying value may become different in the future.
35
+
36
+ value = new_value;
37
+
38
+ return;
39
+ }
40
+
15
41
  value = new_value;
42
+ snapshot = new_snapshot;
16
43
 
17
44
  for (const effect of subscribers) {
18
45
  effect();
package/src/h.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { create_fragment } from "./create_fragment.js";
2
+ import { outstanding_mount_checks } from "./mount_checks.js";
2
3
  import { render } from "./render.js";
3
4
 
4
5
  export const h = (s, tag, props, children) => {
@@ -38,7 +39,7 @@ export const h = (s, tag, props, children) => {
38
39
  if (when === "on_create") {
39
40
  ref_set(() => el);
40
41
  } else if (when === "on_mount") {
41
- throw new Error("[Frosth] Ref \"on_mount\" strategy not yet implemented.");
42
+ outstanding_mount_checks.set(el, () => ref_set(() => el));
42
43
  } else {
43
44
  throw new Error(`[Frosth] Invalid ref strategy "${when}"`);
44
45
  }
@@ -0,0 +1,30 @@
1
+ export const outstanding_mount_checks = new Map();
2
+
3
+ const mutation_observer = new MutationObserver((mutations) => {
4
+ for (const mutation of mutations) {
5
+ for (const node of mutation.addedNodes) {
6
+ if (!outstanding_mount_checks.has(node)) {
7
+ continue;
8
+ }
9
+
10
+ outstanding_mount_checks.get(node)();
11
+ outstanding_mount_checks.delete(node);
12
+ }
13
+ }
14
+ });
15
+
16
+ if (typeof document !== "undefined") {
17
+ mutation_observer.observe(document, {
18
+ childList: true,
19
+ subtree: true,
20
+ });
21
+ }
22
+
23
+ setInterval(() => {
24
+ if (outstanding_mount_checks.size === 0) {
25
+ return;
26
+ }
27
+
28
+ console.warn(`[Frosth] One or more (${outstanding_mount_checks.size}) element(s) were created but not mounted. At least one \`ref: ["on_mount", ...]\` was never fired as a result.`);
29
+ outstanding_mount_checks.clear();
30
+ }, 0);
package/src/render.js CHANGED
@@ -100,6 +100,7 @@ export const render = async (s, target, installed, state) => {
100
100
  }
101
101
 
102
102
  target.innerHTML = "";
103
+
103
104
  const fragment = create_fragment(target);
104
105
 
105
106
  const new_state = { prev: [] };
package/src/sigew.js CHANGED
@@ -6,11 +6,13 @@ import { h } from "./h.js";
6
6
  import { render } from "./render.js";
7
7
  import { MARK_INSTALLABLE } from "./constants.js";
8
8
  import { create_memo } from "./create_memo.js";
9
+ import { create_beacon } from "./create_beacon.js";
9
10
 
10
11
  export { create_self };
11
12
  export { create_signal };
12
13
  export { h };
13
14
  export { create_memo };
15
+ export { create_beacon };
14
16
 
15
17
  export const installarea = (s) => {
16
18
  const storage = s.create_housed_signal(() => undefined);