@davidcal/frosth 1.2.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.2.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.2.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/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);