@podto/zi-core 0.0.0-stage → 0.1.0-rc.2
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 +175 -2
- package/core/index.js +90 -0
- package/data/index.js +401 -0
- package/headless/accordion/accordion.js +131 -0
- package/headless/button/button.js +108 -0
- package/headless/card/card.js +91 -0
- package/headless/carousel/carousel.js +343 -0
- package/headless/checkbox/checkbox.js +85 -0
- package/headless/code/code.js +29 -0
- package/headless/combobox/combobox.js +344 -0
- package/headless/contextmenu/contextmenu.js +78 -0
- package/headless/datatable/datatable.js +535 -0
- package/headless/dialog/dialog.js +153 -0
- package/headless/form/form.js +122 -0
- package/headless/index.js +61 -0
- package/headless/input/input.js +31 -0
- package/headless/menu/core.js +167 -0
- package/headless/menu/menu.js +88 -0
- package/headless/pagination/pagination.js +76 -0
- package/headless/popover/popover.js +140 -0
- package/headless/progress/progress.js +150 -0
- package/headless/radio/radio.js +77 -0
- package/headless/select/select.js +291 -0
- package/headless/slider/slider.js +53 -0
- package/headless/spinner/spinner.js +62 -0
- package/headless/splitter/splitter.js +170 -0
- package/headless/switch/switch.js +85 -0
- package/headless/tabbar/tabbar.js +227 -0
- package/headless/tabs/tabs.js +110 -0
- package/headless/textarea/textarea.js +31 -0
- package/headless/textinput/core.js +87 -0
- package/headless/toast/toast.js +79 -0
- package/headless/tooltip/tooltip.js +293 -0
- package/headless/tree/tree.js +440 -0
- package/headless/virtual_list/virtual_list.js +397 -0
- package/i18n/fmt.js +115 -0
- package/i18n/index.js +206 -0
- package/jsx-tags.js +36 -0
- package/package.json +61 -4
- package/service/index.js +433 -0
- package/testing/index.js +178 -0
- package/types/src/data/index.d.ts +206 -0
- package/types/src/headless/accordion/accordion.d.ts +11 -0
- package/types/src/headless/button/button.d.ts +11 -0
- package/types/src/headless/card/card.d.ts +3 -0
- package/types/src/headless/carousel/carousel.d.ts +17 -0
- package/types/src/headless/checkbox/checkbox.d.ts +11 -0
- package/types/src/headless/code/code.d.ts +11 -0
- package/types/src/headless/combobox/combobox.d.ts +20 -0
- package/types/src/headless/contextmenu/contextmenu.d.ts +15 -0
- package/types/src/headless/datatable/datatable.d.ts +13 -0
- package/types/src/headless/dialog/dialog.d.ts +10 -0
- package/types/src/headless/form/form.d.ts +21 -0
- package/types/src/headless/index.d.ts +29 -0
- package/types/src/headless/input/input.d.ts +4 -0
- package/types/src/headless/menu/core.d.ts +12 -0
- package/types/src/headless/menu/menu.d.ts +14 -0
- package/types/src/headless/pagination/pagination.d.ts +12 -0
- package/types/src/headless/popover/popover.d.ts +10 -0
- package/types/src/headless/progress/progress.d.ts +11 -0
- package/types/src/headless/radio/radio.d.ts +11 -0
- package/types/src/headless/select/select.d.ts +14 -0
- package/types/src/headless/slider/slider.d.ts +11 -0
- package/types/src/headless/spinner/spinner.d.ts +12 -0
- package/types/src/headless/splitter/splitter.d.ts +11 -0
- package/types/src/headless/switch/switch.d.ts +11 -0
- package/types/src/headless/tabbar/tabbar.d.ts +15 -0
- package/types/src/headless/tabs/tabs.d.ts +11 -0
- package/types/src/headless/textarea/textarea.d.ts +4 -0
- package/types/src/headless/textinput/core.d.ts +56 -0
- package/types/src/headless/toast/toast.d.ts +1 -0
- package/types/src/headless/tooltip/tooltip.d.ts +10 -0
- package/types/src/headless/tree/tree.d.ts +21 -0
- package/types/src/headless/virtual_list/virtual_list.d.ts +86 -0
- package/types/src/i18n/fmt.d.ts +52 -0
- package/types/src/i18n/index.d.ts +46 -0
- package/types/src/jsx-tags.d.ts +40 -0
- package/types/src/service/index.d.ts +173 -0
- package/types/src/testing/index.d.ts +83 -0
- package/types/src/ui/accordion/accordion.d.ts +1 -0
- package/types/src/ui/alert/alert.d.ts +1 -0
- package/types/src/ui/avatar/avatar.d.ts +1 -0
- package/types/src/ui/badge/badge.d.ts +1 -0
- package/types/src/ui/breadcrumb/breadcrumb.d.ts +1 -0
- package/types/src/ui/button/button.d.ts +2 -0
- package/types/src/ui/button/button_group.d.ts +1 -0
- package/types/src/ui/card/card.d.ts +1 -0
- package/types/src/ui/carousel/carousel.d.ts +1 -0
- package/types/src/ui/chart/bar.d.ts +1 -0
- package/types/src/ui/chart/line.d.ts +1 -0
- package/types/src/ui/chart/pie.d.ts +1 -0
- package/types/src/ui/chart/ring.d.ts +1 -0
- package/types/src/ui/chart/shared.d.ts +2 -0
- package/types/src/ui/checkbox/checkbox.d.ts +2 -0
- package/types/src/ui/checkbox/choice.d.ts +1 -0
- package/types/src/ui/code/code.d.ts +1 -0
- package/types/src/ui/code/format.d.ts +2 -0
- package/types/src/ui/code/stx.d.ts +32 -0
- package/types/src/ui/combobox/combobox.d.ts +1 -0
- package/types/src/ui/contextmenu/contextmenu.d.ts +1 -0
- package/types/src/ui/datatable/datatable.d.ts +2 -0
- package/types/src/ui/datatable/datatable_2d.d.ts +23 -0
- package/types/src/ui/dialog/dialog.d.ts +2 -0
- package/types/src/ui/divider/divider.d.ts +1 -0
- package/types/src/ui/dropzone/dropzone.d.ts +1 -0
- package/types/src/ui/editor/editor.d.ts +1 -0
- package/types/src/ui/editorhint/editorhint.d.ts +2 -0
- package/types/src/ui/empty/empty.d.ts +1 -0
- package/types/src/ui/field/field.d.ts +2 -0
- package/types/src/ui/findbar/findbar.d.ts +6 -0
- package/types/src/ui/form/form.d.ts +1 -0
- package/types/src/ui/gotit/gotit.d.ts +1 -0
- package/types/src/ui/groupheader/groupheader.d.ts +1 -0
- package/types/src/ui/index.d.ts +68 -0
- package/types/src/ui/input/input.d.ts +1 -0
- package/types/src/ui/input/validation.d.ts +2 -0
- package/types/src/ui/inspector/inspector.d.ts +2 -0
- package/types/src/ui/kbd/kbd.d.ts +2 -0
- package/types/src/ui/led/led.d.ts +1 -0
- package/types/src/ui/link/link.d.ts +1 -0
- package/types/src/ui/menu/menu.d.ts +1 -0
- package/types/src/ui/menubar/menubar.d.ts +1 -0
- package/types/src/ui/meter/meter.d.ts +1 -0
- package/types/src/ui/numberinput/numberinput.d.ts +1 -0
- package/types/src/ui/pagination/pagination.d.ts +1 -0
- package/types/src/ui/pane/pane.d.ts +9 -0
- package/types/src/ui/popover/popover.d.ts +1 -0
- package/types/src/ui/progress/progress.d.ts +1 -0
- package/types/src/ui/radio/radio.d.ts +2 -0
- package/types/src/ui/search/search.d.ts +1 -0
- package/types/src/ui/select/select.d.ts +1 -0
- package/types/src/ui/skeleton/skeleton.d.ts +1 -0
- package/types/src/ui/slider/slider.d.ts +1 -0
- package/types/src/ui/spacer/spacer.d.ts +1 -0
- package/types/src/ui/spinner/spinner.d.ts +1 -0
- package/types/src/ui/splitter/splitter.d.ts +16 -0
- package/types/src/ui/statusbar/statusbar.d.ts +1 -0
- package/types/src/ui/stepper/stepper.d.ts +1 -0
- package/types/src/ui/stripe/stripe.d.ts +2 -0
- package/types/src/ui/switch/switch.d.ts +1 -0
- package/types/src/ui/tabbar/tabbar.d.ts +1 -0
- package/types/src/ui/table/table.d.ts +1 -0
- package/types/src/ui/tabs/tabs.d.ts +1 -0
- package/types/src/ui/tag/tag.d.ts +1 -0
- package/types/src/ui/text/text.d.ts +1 -0
- package/types/src/ui/textarea/textarea.d.ts +1 -0
- package/types/src/ui/theme/bubble.d.ts +5 -0
- package/types/src/ui/theme/icons.d.ts +30 -0
- package/types/src/ui/theme/menu.d.ts +4 -0
- package/types/src/ui/theme/runtime.d.ts +19 -0
- package/types/src/ui/theme/status.d.ts +2 -0
- package/types/src/ui/theme/themes/dark.d.ts +5127 -0
- package/types/src/ui/theme/themes/default.d.ts +5127 -0
- package/types/src/ui/theme/themes/styles.d.ts +4646 -0
- package/types/src/ui/theme/themes/tokens.dark.d.ts +479 -0
- package/types/src/ui/theme/themes/tokens.light.d.ts +479 -0
- package/types/src/ui/theme/tone.d.ts +21 -0
- package/types/src/ui/toast/toast.d.ts +3 -0
- package/types/src/ui/toolbar/toolbar.d.ts +3 -0
- package/types/src/ui/tooltip/tooltip.d.ts +1 -0
- package/types/src/ui/tree/tree.d.ts +1 -0
- package/types/src/ui/virtual_list/virtual_list.d.ts +1 -0
- package/types/src/window/drag.d.ts +1 -0
- package/types/src/window/frame.d.ts +2 -0
- package/types/src/window/index.d.ts +4 -0
- package/types/src/window/main_toolbar/main_toolbar.d.ts +2 -0
- package/types/src/window/title_bar/header.d.ts +1 -0
- package/types/src/window/title_bar/title_bar.d.ts +3 -0
- package/types/src/window/window_resizer/window_resizer.d.ts +1 -0
- package/types/tools/types/zi-barrel.d.ts +75 -0
- package/types/tools/types/zi.d.ts +2826 -0
- package/types/zi.d.ts +2826 -0
- package/ui/accordion/accordion.js +57 -0
- package/ui/alert/alert.js +107 -0
- package/ui/avatar/avatar.js +87 -0
- package/ui/badge/badge.js +54 -0
- package/ui/breadcrumb/breadcrumb.js +64 -0
- package/ui/button/button.js +289 -0
- package/ui/button/button_group.js +45 -0
- package/ui/card/card.js +62 -0
- package/ui/carousel/carousel.js +117 -0
- package/ui/chart/bar.js +29 -0
- package/ui/chart/line.js +35 -0
- package/ui/chart/pie.js +89 -0
- package/ui/chart/ring.js +74 -0
- package/ui/chart/shared.js +18 -0
- package/ui/checkbox/checkbox.js +104 -0
- package/ui/checkbox/choice.js +31 -0
- package/ui/code/code.js +8 -0
- package/ui/code/format.js +22 -0
- package/ui/code/stx.js +96 -0
- package/ui/combobox/combobox.js +77 -0
- package/ui/contextmenu/contextmenu.js +25 -0
- package/ui/datatable/datatable.js +798 -0
- package/ui/datatable/datatable_2d.js +1110 -0
- package/ui/dialog/dialog.js +158 -0
- package/ui/divider/divider.js +16 -0
- package/ui/dropzone/dropzone.js +102 -0
- package/ui/editor/editor.js +47 -0
- package/ui/editorhint/editorhint.js +87 -0
- package/ui/empty/empty.js +34 -0
- package/ui/field/field.js +96 -0
- package/ui/findbar/findbar.js +73 -0
- package/ui/form/form.js +23 -0
- package/ui/gotit/gotit.js +64 -0
- package/ui/groupheader/groupheader.js +50 -0
- package/ui/index.js +158 -0
- package/ui/input/input.js +202 -0
- package/ui/input/validation.js +48 -0
- package/ui/inspector/inspector.js +126 -0
- package/ui/kbd/kbd.js +52 -0
- package/ui/led/led.js +17 -0
- package/ui/link/link.js +48 -0
- package/ui/menu/menu.js +29 -0
- package/ui/menubar/menubar.js +78 -0
- package/ui/meter/meter.js +34 -0
- package/ui/numberinput/numberinput.js +137 -0
- package/ui/pagination/pagination.js +138 -0
- package/ui/pane/pane.js +105 -0
- package/ui/popover/popover.js +30 -0
- package/ui/progress/progress.js +80 -0
- package/ui/radio/radio.js +125 -0
- package/ui/search/search.js +82 -0
- package/ui/select/select.js +158 -0
- package/ui/skeleton/skeleton.js +50 -0
- package/ui/slider/slider.js +135 -0
- package/ui/spacer/spacer.js +9 -0
- package/ui/spinner/spinner.js +17 -0
- package/ui/splitter/splitter.js +63 -0
- package/ui/statusbar/statusbar.js +47 -0
- package/ui/stepper/stepper.js +39 -0
- package/ui/stripe/stripe.js +38 -0
- package/ui/switch/switch.js +72 -0
- package/ui/tabbar/tabbar.js +271 -0
- package/ui/table/table.js +27 -0
- package/ui/tabs/tabs.js +71 -0
- package/ui/tag/tag.js +40 -0
- package/ui/text/text.js +40 -0
- package/ui/textarea/textarea.js +135 -0
- package/ui/theme/bubble.js +29 -0
- package/ui/theme/icons.js +61 -0
- package/ui/theme/menu.js +308 -0
- package/ui/theme/runtime.js +172 -0
- package/ui/theme/status.js +38 -0
- package/ui/theme/themes/dark.js +9 -0
- package/ui/theme/themes/default.js +9 -0
- package/ui/theme/themes/styles.js +2114 -0
- package/ui/theme/themes/tokens.dark.js +59 -0
- package/ui/theme/themes/tokens.light.js +59 -0
- package/ui/theme/tone.js +45 -0
- package/ui/toast/toast.js +216 -0
- package/ui/toolbar/toolbar.js +74 -0
- package/ui/tooltip/tooltip.js +81 -0
- package/ui/tree/tree.js +128 -0
- package/ui/virtual_list/virtual_list.js +303 -0
- package/window/chrome.js +57 -0
- package/window/drag.js +4 -0
- package/window/frame.js +9 -0
- package/window/index.js +12 -0
- package/window/main_toolbar/main_toolbar.js +64 -0
- package/window/title_bar/header.js +4 -0
- package/window/title_bar/title_bar.js +171 -0
- package/window/window_resizer/window_resizer.js +89 -0
package/README.md
CHANGED
|
@@ -1,3 +1,176 @@
|
|
|
1
|
-
#
|
|
1
|
+
# zi
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A small, low-resource UI framework in Zig with a **SolidJS-style**
|
|
4
|
+
authoring model on a retained, GPU-rendered engine.
|
|
5
|
+
|
|
6
|
+
You write UI as JSX + TypeScript (`.tsx`); a build-time transpiler
|
|
7
|
+
turns it into JS that runs on an embedded QuickJS. State is
|
|
8
|
+
**fine-grained signals** — components run once and a signal change
|
|
9
|
+
re-runs only the effects that read it, calling widget setters directly.
|
|
10
|
+
No virtual DOM, no per-frame tree diff. Layout is a pure-Zig flexbox;
|
|
11
|
+
rendering is Impeller (GPU). Styling is typed inline CSS-like objects.
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
// Counter.tsx → zig build / zi run → runs on screen
|
|
15
|
+
import { Column, Text } from "zi"; // engine widgets — always imported
|
|
16
|
+
import { Button } from "zi/ui"; // themed components
|
|
17
|
+
const n = zi.signal(0);
|
|
18
|
+
const label = <Text/>;
|
|
19
|
+
zi.createEffect(() => { zi.setText(label, `Count = ${n()}`); });
|
|
20
|
+
const inc = <Button text=" +1 " onClick={() => n.set(n() + 1)} />;
|
|
21
|
+
zi.mount(<Column>{label}{inc}</Column>);
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```zig
|
|
25
|
+
// main.zig — ~10 lines wire the engine; the UI is the .tsx above
|
|
26
|
+
var engine_root = zi.Flex.initColumn();
|
|
27
|
+
defer engine_root.deinit(gpa);
|
|
28
|
+
var root = try zi.reactive.Root.init(gpa);
|
|
29
|
+
defer root.deinit();
|
|
30
|
+
var app = try zi.App.init("counter", 420, 280, engine_root.widget());
|
|
31
|
+
app.setupCallbacks();
|
|
32
|
+
defer app.deinit();
|
|
33
|
+
try root.mount(app.fontCtx(), &engine_root, @embedFile("Counter.js"));
|
|
34
|
+
try root.run(&app); // frame loop + input routing
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
See `examples/ts-counter/`.
|
|
38
|
+
|
|
39
|
+
## Requirements
|
|
40
|
+
|
|
41
|
+
- Zig 0.17.0 (see `build.zig.zon`)
|
|
42
|
+
- Platforms: Linux/X11 + Wayland, macOS/**arm64** (Metal), Android
|
|
43
|
+
(NativeActivity), iOS Simulator (UIKit/Metal — see `build/ios/`)
|
|
44
|
+
- Vendored: GLFW 3.5.1 (desktop), Impeller SDK, QuickJS, SQLite, stb, esbuild
|
|
45
|
+
(`third_party/`, pinned in `third_party/sources.json`). SQLite backs `zi.db` —
|
|
46
|
+
build with `-Ddb=false` to leave it out entirely.
|
|
47
|
+
|
|
48
|
+
See **Setup** below — `third_party/` is gitignored, so nothing is vendored until
|
|
49
|
+
you fetch it.
|
|
50
|
+
|
|
51
|
+
## Setup (fresh clone)
|
|
52
|
+
|
|
53
|
+
`third_party/` is **entirely gitignored** except `sources.json` and
|
|
54
|
+
`esbuild/VERSION` (`.gitignore`: `third_party/*`), so a fresh clone has no
|
|
55
|
+
Impeller, no esbuild, no GLFW/QuickJS/stb/SQLite sources. Fetch them all:
|
|
56
|
+
|
|
57
|
+
```sh
|
|
58
|
+
python3 build.py --tool impeller -p <linux-x64|macos-arm64> # ~900MB .a + shared lib
|
|
59
|
+
python3 build.py --tool esbuild -p <linux-x64|macos-arm64>
|
|
60
|
+
python3 build.py --tool glfw
|
|
61
|
+
python3 build.py --tool quickjs
|
|
62
|
+
python3 build.py --tool stb
|
|
63
|
+
python3 build.py --tool sqlite
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
To check upstream for newer pins and refresh vendored trees (dry-run by default is off; use `--dry-run` first):
|
|
67
|
+
|
|
68
|
+
```sh
|
|
69
|
+
python3 scripts/upgrade-third-party.py --dry-run
|
|
70
|
+
python3 scripts/upgrade-third-party.py --only quickjs
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Toolchain:
|
|
74
|
+
|
|
75
|
+
- **Zig 0.17.0 exactly** (`build.zig.zon`'s `minimum_zig_version`). There is no
|
|
76
|
+
`.tool-versions` / `.mise.toml` in the repo — pin it yourself (`mise use -g
|
|
77
|
+
zig@0.17.0`, or the tarball from ziglang.org). Homebrew ships the newest Zig,
|
|
78
|
+
which does not compile this tree.
|
|
79
|
+
- **python3** — only for `build.py` above.
|
|
80
|
+
- **node** — only for the chores (`make check-design`, `make package`, i.e.
|
|
81
|
+
`tools/*.mjs`). **Not needed to build**: transpiling uses the vendored esbuild
|
|
82
|
+
*binary*, not node.
|
|
83
|
+
|
|
84
|
+
### macOS
|
|
85
|
+
|
|
86
|
+
Apple Silicon only. `xcode-select --install` covers everything else — the SDK,
|
|
87
|
+
the frameworks `build.zig` links (Metal / QuartzCore / Cocoa / IOKit / AppKit …),
|
|
88
|
+
the clang that compiles the Objective-C glue (`macos_metal.m`), and
|
|
89
|
+
`install_name_tool`.
|
|
90
|
+
|
|
91
|
+
That last one matters: `build.py --tool impeller` runs
|
|
92
|
+
`install_name_tool -id @rpath/libimpeller.dylib` when it detects a macOS host
|
|
93
|
+
(`build.py:115`). **Don't skip it** — Debug links the dylib (`static-impeller`
|
|
94
|
+
defaults to `optimize != .Debug`), and dyld can't find it with the wrong install
|
|
95
|
+
name. Release (`ReleaseFast`) links `libimpeller.a` and is self-contained.
|
|
96
|
+
|
|
97
|
+
> **Intel Macs are not wired up.** `build.zig:1447` and
|
|
98
|
+
> `tools/transpiler/esbuild.zig:24` hardcode `macos-arm64` by **OS tag, ignoring
|
|
99
|
+
> arch** — so an x86_64 host would try to link the arm64 archive even after
|
|
100
|
+
> fetching `-p macos-x64`. Deliberate scope call, not an oversight; those two
|
|
101
|
+
> lines (plus a `macos-x64` esbuild fetch) are all it would take.
|
|
102
|
+
|
|
103
|
+
macOS defaults to Impeller's Metal backend. The `ZI_GLES=1` spike path (ANGLE)
|
|
104
|
+
additionally needs `./scripts/fetch-angle-macos.sh`.
|
|
105
|
+
|
|
106
|
+
### Installing the CLI
|
|
107
|
+
|
|
108
|
+
```sh
|
|
109
|
+
make install # → $(BINDIR)/zi + $(BINDIR)/runtime/<os-arch>/zi-runtime
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Both, not just `zi`: `zi build` resolves the packaging stub relative to **the
|
|
113
|
+
CLI's own directory**, while `zi run` uses a runtime baked into the CLI binary.
|
|
114
|
+
Install only `zi` and you get a CLI where `zi run` works fine and `zi build`
|
|
115
|
+
always fails with "未找到 target … 的 zi-runtime".
|
|
116
|
+
|
|
117
|
+
## Build / Test / Run
|
|
118
|
+
|
|
119
|
+
```sh
|
|
120
|
+
zig build test # engine + css + reactive + scroll + ui unit tests
|
|
121
|
+
zig build examples # all examples → zig-out/bin/example-*
|
|
122
|
+
zig build run # examples/hello
|
|
123
|
+
./zig-out/bin/example-ts-counter # the SolidJS-style demo
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### App bundle tree-shaking
|
|
127
|
+
|
|
128
|
+
`zi run`/`zi create` projects import components from the `zi/ui` barrel
|
|
129
|
+
(`src/ui/index.ts`) and esbuild bundles only what an app actually uses. This
|
|
130
|
+
relies on the root **`package.json` with `"sideEffects": false`** — esbuild
|
|
131
|
+
resolves the `sideEffects` hint from the package boundary of the *component*
|
|
132
|
+
files (the zi source tree), **not** the app's. Without it, presets whose module
|
|
133
|
+
has a top-level side effect (toast.tsx's renderer wiring) are retained even when
|
|
134
|
+
unused — e.g. `import { Button }` alone would drag in the toaster. So the hint lives at the zi source root and is **not**
|
|
135
|
+
something a scaffolded project carries. Don't delete that `package.json`.
|
|
136
|
+
|
|
137
|
+
## Architecture
|
|
138
|
+
|
|
139
|
+
A SolidJS-style stack on the unchanged retained engine:
|
|
140
|
+
|
|
141
|
+
| Layer | What |
|
|
142
|
+
|---|---|
|
|
143
|
+
| `.tsx` → `tools/transpiler` | build-time JSX→JS transpiler (esbuild) |
|
|
144
|
+
| `src/js/` | QuickJS engine + JS↔native marshalling/bindings |
|
|
145
|
+
| `src/reactive/` | signal graph, JS façade, element registry, `Root` |
|
|
146
|
+
| `src/css/` | typed `Style`/`StyleProps` + pure-Zig flexbox (`flex.zig`) |
|
|
147
|
+
| `src/widget.zig`, `app.zig`, `canvas/renderer/text` | retained engine + Impeller GPU (unchanged) |
|
|
148
|
+
|
|
149
|
+
The engine stays engine-agnostic; the reactive layer drives it via
|
|
150
|
+
`Root.run`; flexbox is hand-written pure Zig. See `docs/architecture/`
|
|
151
|
+
for the per-subsystem functionality docs.
|
|
152
|
+
|
|
153
|
+
## Public API
|
|
154
|
+
|
|
155
|
+
```zig
|
|
156
|
+
const zi = @import("zi");
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
- `zi.App` — window/frame lifecycle (engine)
|
|
160
|
+
- `zi.reactive.Root` — `init` → `mount(fontCtx, engineRoot, jsSource)` → `run(&app)`
|
|
161
|
+
- `zi.css` — `Style` / `StyleProps` / `State` typed inline styling
|
|
162
|
+
- engine widgets: `Widget`, `SolidRect`, `Flex` (the unified
|
|
163
|
+
Column/Row/Box container — `Flex.init` = row, `Flex.initColumn` =
|
|
164
|
+
column), `StyledBox`, `Text`, `ui.Button`, …
|
|
165
|
+
- JS API (in the mounted JS): `zi.signal` / `createEffect` / `h` /
|
|
166
|
+
`mount` / `setText` / `setChildren` / `For` / `Show` / `color`, `ref` prop
|
|
167
|
+
- `zi.FrameTimer` / `zi.FrameStats` — always-on instrumentation
|
|
168
|
+
|
|
169
|
+
## Resource profile
|
|
170
|
+
|
|
171
|
+
Low-footprint by construction: retained tree + per-widget DisplayList
|
|
172
|
+
cache; fine-grained signals mean a static UI is **0 dirty / ~0 alloc
|
|
173
|
+
per frame** at idle (measured via `FrameStats` on `ts-counter`).
|
|
174
|
+
Dropping Clay's arena cut test-suite RSS ~28M→~16M. QuickJS is retained
|
|
175
|
+
(it is the logic language) but no per-frame JS runs — effects fire only
|
|
176
|
+
on signal change.
|
package/core/index.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
const h = zi.h;
|
|
2
|
+
const signal = zi.signal;
|
|
3
|
+
const createEffect = zi.createEffect;
|
|
4
|
+
const createRoot = zi.createRoot;
|
|
5
|
+
const mount = zi.mount;
|
|
6
|
+
const setText = zi.setText;
|
|
7
|
+
const batchOps = zi.batchOps;
|
|
8
|
+
const compileStyle = zi.compileStyle;
|
|
9
|
+
const composeStyles = zi.composeStyles;
|
|
10
|
+
const setChildren = zi.setChildren;
|
|
11
|
+
const isWidget = zi.isWidget;
|
|
12
|
+
const exit = zi.exit;
|
|
13
|
+
const Flex = zi.Flex;
|
|
14
|
+
const Column = zi.Column;
|
|
15
|
+
const Row = zi.Row;
|
|
16
|
+
const Box = zi.Box;
|
|
17
|
+
const Text = zi.Text;
|
|
18
|
+
const Stack = zi.Stack;
|
|
19
|
+
const Scrollable = zi.Scrollable;
|
|
20
|
+
const ListView = zi.ListView;
|
|
21
|
+
const Control = zi.Control;
|
|
22
|
+
const Image = zi.Image;
|
|
23
|
+
const Checkmark = zi.Checkmark;
|
|
24
|
+
const Divider = zi.Divider;
|
|
25
|
+
const LineChart = zi.LineChart;
|
|
26
|
+
const BarChart = zi.BarChart;
|
|
27
|
+
const PieChart = zi.PieChart;
|
|
28
|
+
const Ring = zi.Ring;
|
|
29
|
+
const Input = zi.Input;
|
|
30
|
+
const Textarea = zi.Textarea;
|
|
31
|
+
const Modal = zi.Modal;
|
|
32
|
+
const Show = zi.Show;
|
|
33
|
+
const Switch = zi.Switch;
|
|
34
|
+
const Match = zi.Match;
|
|
35
|
+
const Dynamic = zi.Dynamic;
|
|
36
|
+
const ErrorBoundary = zi.ErrorBoundary;
|
|
37
|
+
const For = zi.For;
|
|
38
|
+
const Transition = zi.Transition;
|
|
39
|
+
const headless = zi.headless;
|
|
40
|
+
const ffi = zi.ffi;
|
|
41
|
+
const platform = zi.platform;
|
|
42
|
+
const http = zi.http;
|
|
43
|
+
const db = zi.db;
|
|
44
|
+
var zi_barrel_default = zi;
|
|
45
|
+
export {
|
|
46
|
+
BarChart,
|
|
47
|
+
Box,
|
|
48
|
+
Checkmark,
|
|
49
|
+
Column,
|
|
50
|
+
Control,
|
|
51
|
+
Divider,
|
|
52
|
+
Dynamic,
|
|
53
|
+
ErrorBoundary,
|
|
54
|
+
Flex,
|
|
55
|
+
For,
|
|
56
|
+
Image,
|
|
57
|
+
Input,
|
|
58
|
+
LineChart,
|
|
59
|
+
ListView,
|
|
60
|
+
Match,
|
|
61
|
+
Modal,
|
|
62
|
+
PieChart,
|
|
63
|
+
Ring,
|
|
64
|
+
Row,
|
|
65
|
+
Scrollable,
|
|
66
|
+
Show,
|
|
67
|
+
Stack,
|
|
68
|
+
Switch,
|
|
69
|
+
Text,
|
|
70
|
+
Textarea,
|
|
71
|
+
Transition,
|
|
72
|
+
batchOps,
|
|
73
|
+
compileStyle,
|
|
74
|
+
composeStyles,
|
|
75
|
+
createEffect,
|
|
76
|
+
createRoot,
|
|
77
|
+
db,
|
|
78
|
+
zi_barrel_default as default,
|
|
79
|
+
exit,
|
|
80
|
+
ffi,
|
|
81
|
+
h,
|
|
82
|
+
headless,
|
|
83
|
+
http,
|
|
84
|
+
isWidget,
|
|
85
|
+
mount,
|
|
86
|
+
platform,
|
|
87
|
+
setChildren,
|
|
88
|
+
setText,
|
|
89
|
+
signal
|
|
90
|
+
};
|
package/data/index.js
ADDED
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
function paramKey(sql, params) {
|
|
2
|
+
let key = "sql:" + sql;
|
|
3
|
+
for (let i = 0; i < params.length; i++) {
|
|
4
|
+
const p = params[i];
|
|
5
|
+
const t = typeof p;
|
|
6
|
+
if (p === null || p === void 0) key += "\0n";
|
|
7
|
+
else if (t === "number" || t === "boolean" || t === "bigint") key += "\0" + p;
|
|
8
|
+
else if (t === "string") key += "\0s" + p;
|
|
9
|
+
else return null;
|
|
10
|
+
}
|
|
11
|
+
return key;
|
|
12
|
+
}
|
|
13
|
+
const inFlight = /* @__PURE__ */ new Map();
|
|
14
|
+
function share(key, run) {
|
|
15
|
+
const hit = inFlight.get(key);
|
|
16
|
+
if (hit) return hit;
|
|
17
|
+
const p = run().finally(() => {
|
|
18
|
+
if (inFlight.get(key) === p) inFlight.delete(key);
|
|
19
|
+
});
|
|
20
|
+
inFlight.set(key, p);
|
|
21
|
+
return p;
|
|
22
|
+
}
|
|
23
|
+
const subscribers = /* @__PURE__ */ new Set();
|
|
24
|
+
let busUnsubscribe = null;
|
|
25
|
+
function sameTables(deps, extra, detected) {
|
|
26
|
+
if (extra.length !== 0) return false;
|
|
27
|
+
if (deps.size !== detected.length) return false;
|
|
28
|
+
for (const t of detected) if (!deps.has(t)) return false;
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
function touches(written, deps) {
|
|
32
|
+
for (const t of written) if (deps.has(t)) return true;
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
function dispatch(tables, partial = false) {
|
|
36
|
+
if (partial) return refetchAll();
|
|
37
|
+
const targets = subscribers.size > 1 ? Array.from(subscribers) : subscribers;
|
|
38
|
+
for (const sub of targets) {
|
|
39
|
+
if (sub.inFlight) {
|
|
40
|
+
if (!sub.missed) sub.missed = /* @__PURE__ */ new Set();
|
|
41
|
+
for (const t of tables) sub.missed.add(t);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (sub.all || touches(tables, sub.tables)) sub.refetch();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function requireDb() {
|
|
48
|
+
if (!zi.db) {
|
|
49
|
+
throw new Error("zi/data needs zi.db, which this build lacks \u2014 rebuild without -Ddb=false");
|
|
50
|
+
}
|
|
51
|
+
return zi.db;
|
|
52
|
+
}
|
|
53
|
+
function refetchAll() {
|
|
54
|
+
for (const sub of Array.from(subscribers)) sub.refetch();
|
|
55
|
+
}
|
|
56
|
+
function ensureBus() {
|
|
57
|
+
if (busUnsubscribe) return;
|
|
58
|
+
const db = zi.db;
|
|
59
|
+
if (!db) return;
|
|
60
|
+
busUnsubscribe = db.onTablesChanged(dispatch);
|
|
61
|
+
}
|
|
62
|
+
function createAsync(track, initial, extraDeps, hooks = {}) {
|
|
63
|
+
const data = zi.signal(initial);
|
|
64
|
+
const loading = zi.signal(false);
|
|
65
|
+
const error = zi.signal(null);
|
|
66
|
+
const stale = zi.signal(false);
|
|
67
|
+
let latest = 0;
|
|
68
|
+
let settledId = 0;
|
|
69
|
+
const sub = { tables: new Set(extraDeps), all: false, inFlight: false, missed: null, refetch: () => {
|
|
70
|
+
} };
|
|
71
|
+
subscribers.add(sub);
|
|
72
|
+
ensureBus();
|
|
73
|
+
zi.createEffect(() => {
|
|
74
|
+
zi.onCleanup(() => {
|
|
75
|
+
subscribers.delete(sub);
|
|
76
|
+
if (subscribers.size === 0 && busUnsubscribe) {
|
|
77
|
+
busUnsubscribe();
|
|
78
|
+
busUnsubscribe = null;
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
function start(spec, fresh) {
|
|
83
|
+
const id = ++latest;
|
|
84
|
+
loading.set(true);
|
|
85
|
+
sub.inFlight = true;
|
|
86
|
+
sub.missed = null;
|
|
87
|
+
let p;
|
|
88
|
+
try {
|
|
89
|
+
const key = fresh ? null : spec.key();
|
|
90
|
+
p = key === null ? spec.run() : share(key, spec.run);
|
|
91
|
+
} catch (e) {
|
|
92
|
+
p = Promise.reject(e);
|
|
93
|
+
}
|
|
94
|
+
if (hooks.prime) {
|
|
95
|
+
hooks.prime().then(
|
|
96
|
+
(cached) => {
|
|
97
|
+
if (cached === void 0) return;
|
|
98
|
+
if (id !== latest || id <= settledId) return;
|
|
99
|
+
zi.batch(() => {
|
|
100
|
+
data.set(cached);
|
|
101
|
+
stale.set(true);
|
|
102
|
+
});
|
|
103
|
+
},
|
|
104
|
+
() => {
|
|
105
|
+
}
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
return p.then(
|
|
109
|
+
(res) => {
|
|
110
|
+
if (id !== latest) return;
|
|
111
|
+
settledId = id;
|
|
112
|
+
const missed = sub.missed;
|
|
113
|
+
sub.inFlight = false;
|
|
114
|
+
sub.missed = null;
|
|
115
|
+
if (!sameTables(sub.tables, extraDeps, res.tables)) {
|
|
116
|
+
sub.tables = /* @__PURE__ */ new Set([...extraDeps, ...res.tables]);
|
|
117
|
+
}
|
|
118
|
+
sub.all = res.partial;
|
|
119
|
+
let value = res.value;
|
|
120
|
+
try {
|
|
121
|
+
if (hooks.adopt) value = hooks.adopt(res.value, data.peek());
|
|
122
|
+
} catch {
|
|
123
|
+
value = res.value;
|
|
124
|
+
}
|
|
125
|
+
zi.batch(() => {
|
|
126
|
+
data.set(value);
|
|
127
|
+
error.set(null);
|
|
128
|
+
loading.set(false);
|
|
129
|
+
stale.set(false);
|
|
130
|
+
});
|
|
131
|
+
if (hooks.onValue) hooks.onValue(value);
|
|
132
|
+
if (missed && (sub.all || touches(missed, sub.tables))) {
|
|
133
|
+
void start(spec, true);
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
(e) => {
|
|
137
|
+
if (id !== latest) return;
|
|
138
|
+
sub.inFlight = false;
|
|
139
|
+
sub.missed = null;
|
|
140
|
+
sub.all = true;
|
|
141
|
+
zi.batch(() => {
|
|
142
|
+
error.set(e instanceof Error ? e : new Error(String(e)));
|
|
143
|
+
loading.set(false);
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
zi.createEffect(() => {
|
|
149
|
+
const spec = track();
|
|
150
|
+
if (!spec) {
|
|
151
|
+
latest++;
|
|
152
|
+
sub.inFlight = false;
|
|
153
|
+
sub.missed = null;
|
|
154
|
+
zi.batch(() => {
|
|
155
|
+
loading.set(false);
|
|
156
|
+
data.set(initial);
|
|
157
|
+
});
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
void start(spec, false);
|
|
161
|
+
});
|
|
162
|
+
const refetch = () => {
|
|
163
|
+
const spec = zi.untrack(track);
|
|
164
|
+
if (!spec) return Promise.resolve();
|
|
165
|
+
return start(spec, true).then(() => void 0);
|
|
166
|
+
};
|
|
167
|
+
sub.refetch = () => void refetch();
|
|
168
|
+
return {
|
|
169
|
+
data: () => data(),
|
|
170
|
+
loading: () => loading(),
|
|
171
|
+
error: () => error(),
|
|
172
|
+
stale: () => stale(),
|
|
173
|
+
refetch
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
function countKeys(o) {
|
|
177
|
+
let n = 0;
|
|
178
|
+
for (const _k in o) n++;
|
|
179
|
+
return n;
|
|
180
|
+
}
|
|
181
|
+
function sameRowValues(a, b, cols) {
|
|
182
|
+
for (let i = 0; i < cols.length; i++) if (a[cols[i]] !== b[cols[i]]) return false;
|
|
183
|
+
return countKeys(a) === cols.length && countKeys(b) === cols.length;
|
|
184
|
+
}
|
|
185
|
+
function reconcileRows(next, prev, keyFn) {
|
|
186
|
+
if (!Array.isArray(next) || !Array.isArray(prev)) return next;
|
|
187
|
+
if (next.length === 0 || prev.length === 0) return next;
|
|
188
|
+
if (next[0] == null || prev[0] == null) return next;
|
|
189
|
+
const cols = Object.keys(next[0]);
|
|
190
|
+
const prevCols = Object.keys(prev[0]);
|
|
191
|
+
if (cols.length !== prevCols.length) return next;
|
|
192
|
+
for (let c = 0; c < cols.length; c++) if (cols[c] !== prevCols[c]) return next;
|
|
193
|
+
let byKey = null;
|
|
194
|
+
if (keyFn) {
|
|
195
|
+
byKey = /* @__PURE__ */ new Map();
|
|
196
|
+
for (let i = 0; i < prev.length; i++) {
|
|
197
|
+
const k = keyFn(prev[i]);
|
|
198
|
+
if (k !== void 0) byKey.set(k, prev[i]);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
let out = null;
|
|
202
|
+
for (let i = 0; i < next.length; i++) {
|
|
203
|
+
const cand = byKey ? byKey.get(keyFn(next[i])) ?? prev[i] : prev[i];
|
|
204
|
+
if (cand != null && next[i] != null && sameRowValues(cand, next[i], cols)) {
|
|
205
|
+
if (!out) out = next.slice();
|
|
206
|
+
out[i] = cand;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return out || next;
|
|
210
|
+
}
|
|
211
|
+
function createQuery(source, opts = {}) {
|
|
212
|
+
return createAsync(
|
|
213
|
+
() => {
|
|
214
|
+
const spec = source();
|
|
215
|
+
if (!spec || !spec.sql) return null;
|
|
216
|
+
const params = spec.params || [];
|
|
217
|
+
return {
|
|
218
|
+
key: () => paramKey(spec.sql, params),
|
|
219
|
+
run: () => requireDb().run([{ sql: spec.sql, params }], false).then((raw) => ({
|
|
220
|
+
value: zi.db.rowsToObjects(raw.results[0]),
|
|
221
|
+
tables: raw.tablesRead,
|
|
222
|
+
partial: !!raw.tablesReadPartial
|
|
223
|
+
}))
|
|
224
|
+
};
|
|
225
|
+
},
|
|
226
|
+
opts.initial !== void 0 ? opts.initial : [],
|
|
227
|
+
opts.deps || [],
|
|
228
|
+
{ adopt: (next, prev) => reconcileRows(next, prev, opts.rowKey) }
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
function createResource(a, b, c) {
|
|
232
|
+
const twoArg = typeof b === "function";
|
|
233
|
+
const source = twoArg ? a : () => true;
|
|
234
|
+
const fetcher = twoArg ? b : () => a();
|
|
235
|
+
const opts = (twoArg ? c : b) || {};
|
|
236
|
+
return createAsync(
|
|
237
|
+
() => {
|
|
238
|
+
const s = source();
|
|
239
|
+
if (s === null || s === false || s === void 0) return null;
|
|
240
|
+
return {
|
|
241
|
+
key: () => opts.key ? "res:" + opts.key(s) : null,
|
|
242
|
+
// Lazy on purpose — see the note above.
|
|
243
|
+
run: () => Promise.resolve(fetcher(s)).then((value) => ({ value, tables: [], partial: false }))
|
|
244
|
+
};
|
|
245
|
+
},
|
|
246
|
+
opts.initial,
|
|
247
|
+
opts.deps || []
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
function bindRealtime(url, opts = {}) {
|
|
251
|
+
if (!zi.realtime) {
|
|
252
|
+
throw new Error("zi/data: bindRealtime needs zi.realtime, missing in this build (-Drealtime=false)");
|
|
253
|
+
}
|
|
254
|
+
const eventName = opts.event ?? "changed";
|
|
255
|
+
return zi.realtime.connect(url, {
|
|
256
|
+
headers: opts.headers,
|
|
257
|
+
onEvent: (e) => {
|
|
258
|
+
if (e.event !== eventName) return;
|
|
259
|
+
try {
|
|
260
|
+
feedChange(JSON.parse(e.data));
|
|
261
|
+
} catch {
|
|
262
|
+
refetchAll();
|
|
263
|
+
}
|
|
264
|
+
},
|
|
265
|
+
// Back-pressure dropped events: we can't know what was in them.
|
|
266
|
+
onDropped: () => refetchAll(),
|
|
267
|
+
onState: (state) => {
|
|
268
|
+
if (state === "open") refetchAll();
|
|
269
|
+
opts.onState?.(state);
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
function bindChanges(subscribe, opts = {}) {
|
|
274
|
+
return subscribe(opts.event ?? "changed", feedChange);
|
|
275
|
+
}
|
|
276
|
+
function resync() {
|
|
277
|
+
refetchAll();
|
|
278
|
+
}
|
|
279
|
+
function feedChange(payload) {
|
|
280
|
+
const items = Array.isArray(payload) ? payload : [payload];
|
|
281
|
+
const names = [];
|
|
282
|
+
for (const it of items) {
|
|
283
|
+
const list = it?.resources ?? it?.tables;
|
|
284
|
+
if (list === void 0) continue;
|
|
285
|
+
if (!Array.isArray(list)) {
|
|
286
|
+
refetchAll();
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
for (const n of list) if (typeof n === "string") names.push(n);
|
|
290
|
+
}
|
|
291
|
+
if (names.length) dispatch(names);
|
|
292
|
+
}
|
|
293
|
+
function cacheTable(key) {
|
|
294
|
+
return "cache_" + key;
|
|
295
|
+
}
|
|
296
|
+
function cacheTableDDL(key) {
|
|
297
|
+
return `create table if not exists ${cacheTable(key)} (pk text primary key, data text not null)`;
|
|
298
|
+
}
|
|
299
|
+
function createRemoteQuery(opts) {
|
|
300
|
+
const table = cacheTable(opts.key);
|
|
301
|
+
const pk = String(opts.pk ?? "id");
|
|
302
|
+
const useCache = opts.cache !== false;
|
|
303
|
+
return createAsync(
|
|
304
|
+
() => ({
|
|
305
|
+
key: () => "remote:" + opts.key,
|
|
306
|
+
run: () => opts.fetch().then((value) => ({ value, tables: [], partial: false }))
|
|
307
|
+
}),
|
|
308
|
+
opts.initial !== void 0 ? opts.initial : [],
|
|
309
|
+
// ONLY the resource name. The snapshot table must NOT be a dependency:
|
|
310
|
+
// this query is the only thing that reads it (via `prime`) and the only
|
|
311
|
+
// thing that writes it (via `onValue`), so listing it means the write
|
|
312
|
+
// invalidates the very query that produced it — an unbounded refetch loop.
|
|
313
|
+
// Measured before this was fixed: 55 requests in 18 seconds.
|
|
314
|
+
[opts.key, ...opts.deps || []],
|
|
315
|
+
{
|
|
316
|
+
// 身份保持同 `createQuery`(#0045),而且这里更该有:remote 的每一批行都是
|
|
317
|
+
// 逐行 `JSON.parse` 出来的新对象,`pk` 又已经是现成的行主键,不必新增选项。
|
|
318
|
+
// 认 `rowKey`(显式给的行标识),没给才退回现成的 `pk`。不认的话
|
|
319
|
+
// `{ pk: "id", rowKey: r => r.slug }` 会编译通过、运行时被静默丢掉。
|
|
320
|
+
adopt: (next, prev) => reconcileRows(next, prev, opts.rowKey ?? ((r) => r == null ? void 0 : r[pk])),
|
|
321
|
+
...useCache ? {
|
|
322
|
+
prime: () => readSnapshot(table),
|
|
323
|
+
onValue: (rows) => {
|
|
324
|
+
void writeSnapshot(table, pk, rows).catch(() => {
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
} : {}
|
|
328
|
+
}
|
|
329
|
+
);
|
|
330
|
+
}
|
|
331
|
+
async function readSnapshot(table) {
|
|
332
|
+
const rows = await requireDb().query(`select data from ${table}`);
|
|
333
|
+
if (rows.length === 0) return void 0;
|
|
334
|
+
return rows.map((r) => JSON.parse(r.data));
|
|
335
|
+
}
|
|
336
|
+
async function writeSnapshot(table, pk, rows) {
|
|
337
|
+
const stmts = [{ sql: `delete from ${table}`, params: [] }];
|
|
338
|
+
for (const row of rows) {
|
|
339
|
+
stmts.push({
|
|
340
|
+
sql: `insert into ${table} (pk, data) values (?, ?)`,
|
|
341
|
+
params: [String(row?.[pk] ?? ""), JSON.stringify(row)]
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
await requireDb().tx(stmts);
|
|
345
|
+
}
|
|
346
|
+
function createMutation(fn) {
|
|
347
|
+
const pending = zi.signal(0);
|
|
348
|
+
const error = zi.signal(null);
|
|
349
|
+
const call = (...args) => {
|
|
350
|
+
pending.set(pending.peek() + 1);
|
|
351
|
+
let started;
|
|
352
|
+
try {
|
|
353
|
+
started = fn(...args);
|
|
354
|
+
} catch (e) {
|
|
355
|
+
started = Promise.reject(e);
|
|
356
|
+
}
|
|
357
|
+
return started.then(
|
|
358
|
+
(r) => {
|
|
359
|
+
zi.batch(() => {
|
|
360
|
+
pending.set(pending.peek() - 1);
|
|
361
|
+
error.set(null);
|
|
362
|
+
});
|
|
363
|
+
return r;
|
|
364
|
+
},
|
|
365
|
+
(e) => {
|
|
366
|
+
const err = e instanceof Error ? e : new Error(String(e));
|
|
367
|
+
zi.batch(() => {
|
|
368
|
+
pending.set(pending.peek() - 1);
|
|
369
|
+
error.set(err);
|
|
370
|
+
});
|
|
371
|
+
throw err;
|
|
372
|
+
}
|
|
373
|
+
);
|
|
374
|
+
};
|
|
375
|
+
call.pending = () => pending() > 0;
|
|
376
|
+
call.error = () => error();
|
|
377
|
+
return call;
|
|
378
|
+
}
|
|
379
|
+
async function migrate(steps) {
|
|
380
|
+
const rows = await requireDb().query("pragma user_version");
|
|
381
|
+
let version = rows[0] && rows[0].user_version || 0;
|
|
382
|
+
for (let i = version; i < steps.length; i++) {
|
|
383
|
+
const step = steps[i];
|
|
384
|
+
const sql = typeof step === "string" ? step : step.sql;
|
|
385
|
+
await requireDb().tx([{ sql }, { sql: "pragma user_version = " + (i + 1) }]);
|
|
386
|
+
version = i + 1;
|
|
387
|
+
}
|
|
388
|
+
return version;
|
|
389
|
+
}
|
|
390
|
+
export {
|
|
391
|
+
bindChanges,
|
|
392
|
+
bindRealtime,
|
|
393
|
+
cacheTable,
|
|
394
|
+
cacheTableDDL,
|
|
395
|
+
createMutation,
|
|
396
|
+
createQuery,
|
|
397
|
+
createRemoteQuery,
|
|
398
|
+
createResource,
|
|
399
|
+
migrate,
|
|
400
|
+
resync
|
|
401
|
+
};
|