@medyll/idae-machine 0.136.9 → 0.136.10

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.
@@ -25,6 +25,7 @@ Smart CRUD form — fetch, validate, submit, field iteration.
25
25
  <script lang="ts">
26
26
  import { machine } from '../../main/machine.js';
27
27
  import { SchemeFieldDefaultValues } from '../../main/machine/SchemeFieldDefaultValues.js';
28
+ import { useRecordData } from '../utils/useRecordData.svelte.js';
28
29
  import DataRecord from './DataRecord.svelte';
29
30
 
30
31
  let {
@@ -37,7 +38,12 @@ Smart CRUD form — fetch, validate, submit, field iteration.
37
38
  groupFieldBy
38
39
  }: DataFormProps = $props();
39
40
 
40
- const store = $derived(collection ? machine.collection(collection) : undefined);
41
+ // Record read goes through machine.store via useRecordData (invariant 8 — store is
42
+ // the reactive read layer; machine.collection stays imperative CRUD).
43
+ const recordSource = useRecordData(
44
+ () => collection,
45
+ () => ({ collectionId: mode === 'create' ? undefined : dataId })
46
+ );
41
47
  const collLogic = $derived(collection ? machine.logic.collection(collection) : null);
42
48
  const formFields = $derived(collLogic?.parse() ?? {});
43
49
  const validator = $derived(collLogic?.validator);
@@ -57,12 +63,9 @@ Smart CRUD form — fetch, validate, submit, field iteration.
57
63
  ...data,
58
64
  ...withData
59
65
  };
60
- } else if (store && dataId) {
61
- const id = isNaN(Number(dataId)) ? dataId : Number(dataId);
62
- (async () => {
63
- const record = await store.get(id) ?? {};
64
- formData = { ...data, ...withData, ...record };
65
- })();
66
+ } else if (collection && dataId) {
67
+ const record = recordSource.record ?? {};
68
+ formData = { ...data, ...withData, ...record };
66
69
  }
67
70
  });
68
71
 
@@ -46,19 +46,19 @@ Renders forward FK relations for a record as DataList sections.
46
46
  return sourceStore.records.find((item) => String(item.id) === String(collectionId)) ?? null;
47
47
  });
48
48
 
49
- let fetchedRecord = $state<Record<string, unknown> | null>(null);
50
- let lookupDone = $state(false);
49
+ // Record missing from the store = collection not warmed yet. Warm it up and let
50
+ // sourceStore resolve the record reactively — reads never go through
51
+ // machine.collection (invariant 8).
52
+ let lookupDone = $state(false);
51
53
  $effect(() => {
52
54
  if (data != null || collectionId == null || storeRecord != null) {
53
55
  lookupDone = true;
54
56
  return;
55
57
  }
56
58
  lookupDone = false;
57
- fetchedRecord = null;
58
59
  let cancelled = false;
59
- Promise.resolve(machine.collection(collection).get(collectionId)).then((rec) => {
60
+ void machine.warmup([collection]).then(() => {
60
61
  if (cancelled) return;
61
- fetchedRecord = (rec as Record<string, unknown> | undefined) ?? null;
62
62
  lookupDone = true;
63
63
  });
64
64
  return () => { cancelled = true; };
@@ -67,7 +67,7 @@ Renders forward FK relations for a record as DataList sections.
67
67
  const record = $derived.by(() => {
68
68
  if (data) return data;
69
69
  if (collectionId == null) return null;
70
- return storeRecord ?? fetchedRecord;
70
+ return storeRecord;
71
71
  });
72
72
 
73
73
  const recordState = $derived.by(() => {
@@ -46,19 +46,19 @@ Renders reverse FK relations for a record as DataList sections.
46
46
  return sourceStore.records.find((item) => String(item.id) === String(collectionId)) ?? null;
47
47
  });
48
48
 
49
- let fetchedRecord = $state<Record<string, unknown> | null>(null);
50
- let lookupDone = $state(false);
49
+ // Record missing from the store = collection not warmed yet. Warm it up and let
50
+ // sourceStore resolve the record reactively — reads never go through
51
+ // machine.collection (invariant 8).
52
+ let lookupDone = $state(false);
51
53
  $effect(() => {
52
54
  if (data != null || collectionId == null || storeRecord != null) {
53
55
  lookupDone = true;
54
56
  return;
55
57
  }
56
58
  lookupDone = false;
57
- fetchedRecord = null;
58
59
  let cancelled = false;
59
- Promise.resolve(machine.collection(collection).get(collectionId)).then((rec) => {
60
+ void machine.warmup([collection]).then(() => {
60
61
  if (cancelled) return;
61
- fetchedRecord = (rec as Record<string, unknown> | undefined) ?? null;
62
62
  lookupDone = true;
63
63
  });
64
64
  return () => { cancelled = true; };
@@ -67,7 +67,7 @@ Renders reverse FK relations for a record as DataList sections.
67
67
  const record = $derived.by(() => {
68
68
  if (data) return data;
69
69
  if (collectionId == null) return null;
70
- return storeRecord ?? fetchedRecord;
70
+ return storeRecord;
71
71
  });
72
72
 
73
73
  const recordState = $derived.by(() => {
@@ -36,9 +36,20 @@ function matchWhere(record, where) {
36
36
  }
37
37
  return true;
38
38
  }
39
+ /**
40
+ * Imperative read on purpose. buildGraph is an async one-shot snapshot builder
41
+ * that runs outside any reactive frame, so `machine.store` is not an option here
42
+ * — it wraps useQoolieCollection, whose `$effect` would be orphaned past the
43
+ * first `await`. Invariant 8's actual risk (a cold collection reading empty) is
44
+ * handled the way the root record handles it: warm up once and read again.
45
+ */
39
46
  async function fetchRelated(collection, where) {
40
47
  try {
41
- const all = (await machine.collection(collection).getAll());
48
+ let all = (await machine.collection(collection).getAll());
49
+ if (!all.length) {
50
+ await machine.warmup([collection]);
51
+ all = (await machine.collection(collection).getAll());
52
+ }
42
53
  return all.filter(r => matchWhere(r, where));
43
54
  }
44
55
  catch {
@@ -81,21 +81,35 @@ export function useMachinePrefs(scope, defaults, enabled) {
81
81
  // can't survive a (re)hydration.
82
82
  Object.assign(st.slots, defaults);
83
83
  const prefix = `${String(user.id)}:${s}.`;
84
+ // Imperative read on purpose (invariant 8 allows machine.collection for
85
+ // exactly this): a reactive machine.store would re-run on every prefs write
86
+ // and fight the one-shot `st.loaded` guard above. What the invariant warns
87
+ // about — a cold collection reading empty — is handled by warming up once
88
+ // and reading again, so a first paint can't silently lose stored prefs.
84
89
  untrack(() => {
85
- Promise.resolve(machine.collection('appuser_prefs').getAll())
86
- .then((rows) => {
87
- for (const r of rows) {
88
- if (typeof r.code === 'string' && r.code.startsWith(prefix)) {
89
- const key = r.code.slice(prefix.length);
90
- if (key in st.slots)
91
- st.slots[key] = r.value ?? st.slots[key];
90
+ void (async () => {
91
+ try {
92
+ const read = async () => (await machine.collection('appuser_prefs').getAll());
93
+ let rows = await read();
94
+ if (!rows.length) {
95
+ await machine.warmup(['appuser_prefs']);
96
+ rows = await read();
92
97
  }
98
+ for (const r of rows) {
99
+ if (typeof r.code === 'string' && r.code.startsWith(prefix)) {
100
+ const key = r.code.slice(prefix.length);
101
+ if (key in st.slots)
102
+ st.slots[key] = r.value ?? st.slots[key];
103
+ }
104
+ }
105
+ }
106
+ catch (error) {
107
+ console.warn('[useMachinePrefs] Failed to hydrate preferences:', error);
108
+ }
109
+ finally {
110
+ st.hydrated = true;
93
111
  }
94
- })
95
- .catch((error) => {
96
- console.warn('[useMachinePrefs] Failed to hydrate preferences:', error);
97
- })
98
- .finally(() => { st.hydrated = true; });
112
+ })();
99
113
  });
100
114
  });
101
115
  return {
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "bin": {
5
5
  "idae-machine": "./cli.cjs"
6
6
  },
7
- "version": "0.136.9",
7
+ "version": "0.136.10",
8
8
  "files": [
9
9
  "dist",
10
10
  "!dist/**/*.test.*",
@@ -62,10 +62,10 @@
62
62
  "vite": "^8.2.0",
63
63
  "vitest": "^4.1.10",
64
64
  "@medyll/idae-eslint-config": "0.1.6",
65
- "@medyll/idae-router": "0.1.1",
66
65
  "@medyll/idae-socket": "0.0.5",
67
66
  "@medyll/idae-shared": "1.0.5",
68
- "@medyll/qoolie": "0.0.7"
67
+ "@medyll/idae-router": "0.1.1",
68
+ "@medyll/qoolie": "0.0.8"
69
69
  },
70
70
  "scope": "medyll",
71
71
  "keywords": [
@@ -82,10 +82,10 @@
82
82
  "@medyll/css-base": "^0.7.10",
83
83
  "command": "^1.1.1",
84
84
  "commander": "^15.0.0",
85
- "@medyll/idae-be": "1.97.2",
86
85
  "@medyll/idae-api": "0.187.3",
87
- "@medyll/qoolie": "0.0.7",
88
- "@medyll/idae-idbql": "0.185.3"
86
+ "@medyll/idae-be": "1.97.3",
87
+ "@medyll/idae-idbql": "0.185.3",
88
+ "@medyll/qoolie": "0.0.8"
89
89
  },
90
90
  "scripts": {
91
91
  "dev": "vite dev --host",