@davidcal/frosth 1.0.3 → 1.2.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.0.3
3
+ **Latest version**: v1.2.0
4
4
 
5
5
  ## Motivation
6
6
 
@@ -200,7 +200,7 @@ const Component = obtain(async ({ t, s }) => {
200
200
 
201
201
  installed.set(() => [
202
202
  (s) => s.$h1({
203
- ref: () => ref_text.set,
203
+ ref: ["on_create", ref_text.set],
204
204
  }, "Click the button to change my color"),
205
205
  (s) => s.$button({
206
206
  onclick: () => () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@davidcal/frosth",
3
- "version": "1.0.3",
3
+ "version": "1.2.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",
@@ -19,6 +19,7 @@
19
19
  ],
20
20
  "author": "David P. Callanan",
21
21
  "license": "MIT",
22
+ "homepage": "https://frosth.condensis.com",
22
23
  "repository": {
23
24
  "type": "git",
24
25
  "url": "https://github.com/davidcallanan/frosth"
@@ -0,0 +1,11 @@
1
+ import { create_signal } from "./create_signal.js";
2
+
3
+ /**
4
+ * A memo is simply a readonly signal.
5
+ */
6
+ export const create_memo = (initial) => {
7
+ const signal = create_signal(initial);
8
+ const memo = (s) => signal(s);
9
+
10
+ return memo;
11
+ };
@@ -1,7 +1,7 @@
1
1
  import { create_signal_raw } from "./create_signal_raw.js";
2
2
 
3
3
  const error_value = () => {
4
- throw new Error("Signal value must be a reactive function. Wrap the value in either `() =>` or `(self) =>` depending on whether or not the current value should be reactive, beyond the mere reactivity of the signal.");
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
7
  export const create_signal = (initial) => {
@@ -13,6 +13,7 @@ export const create_signal_raw = (initial) => {
13
13
 
14
14
  const set = (new_value) => {
15
15
  value = new_value;
16
+
16
17
  for (const effect of subscribers) {
17
18
  effect();
18
19
  }
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) => {
@@ -21,10 +22,28 @@ export const h = (s, tag, props, children) => {
21
22
  prev = handler;
22
23
  });
23
24
  } else if (key === "ref") {
24
- s.create_effect(() => {
25
- const ref_fn = props[key](s);
26
- ref_fn(() => el);
27
- });
25
+ if (typeof props[key] === "function") {
26
+ console.warn("[Frosth] Deprecated: Instead of `ref: () => signal.set`, use `ref: [\"on_create\", signal.set]`");
27
+
28
+ s.create_effect(() => {
29
+ const ref_set = props[key](s);
30
+ ref_set(() => el);
31
+ });
32
+ } else {
33
+ if (!Array.isArray(props[key])) {
34
+ throw new Error("[Frosth] Ref must be an array, either `[\"on_create\", signal.set]` or `[\"on_mount\", signal.set]`");
35
+ }
36
+
37
+ const [when, ref_set] = props[key];
38
+
39
+ if (when === "on_create") {
40
+ ref_set(() => el);
41
+ } else if (when === "on_mount") {
42
+ outstanding_mount_checks.set(el, () => ref_set(() => el));
43
+ } else {
44
+ throw new Error(`[Frosth] Invalid ref strategy "${when}"`);
45
+ }
46
+ }
28
47
  } else {
29
48
  s.create_effect((s) => {
30
49
  const prop$ = props[key](s);
@@ -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
@@ -5,10 +5,12 @@ import { create_signal } from "./create_signal.js";
5
5
  import { h } from "./h.js";
6
6
  import { render } from "./render.js";
7
7
  import { MARK_INSTALLABLE } from "./constants.js";
8
+ import { create_memo } from "./create_memo.js";
8
9
 
9
10
  export { create_self };
10
11
  export { create_signal };
11
12
  export { h };
13
+ export { create_memo };
12
14
 
13
15
  export const installarea = (s) => {
14
16
  const storage = s.create_housed_signal(() => undefined);