@snap/valdi 1.0.6 → 1.0.8
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 +22 -10
- package/bundled-skills/CONTRIBUTING_SKILLS.md +74 -0
- package/bundled-skills/registry.json +20 -0
- package/bundled-skills/skills/valdi-android/skill.md +89 -0
- package/bundled-skills/skills/valdi-async/skill.md +246 -0
- package/bundled-skills/skills/valdi-async/tests/BUILD.bazel +19 -0
- package/bundled-skills/skills/valdi-async/tests/README.md +25 -0
- package/bundled-skills/skills/valdi-async/tests/src/reference.tsx +210 -0
- package/bundled-skills/skills/valdi-async/tests/tsconfig.json +1 -0
- package/bundled-skills/skills/valdi-bazel/skill.md +122 -0
- package/bundled-skills/skills/valdi-compiler/skill.md +108 -0
- package/bundled-skills/skills/valdi-component-tests/skill.md +430 -0
- package/bundled-skills/skills/valdi-component-tests/tests/BUILD.bazel +24 -0
- package/bundled-skills/skills/valdi-component-tests/tests/README.md +35 -0
- package/bundled-skills/skills/valdi-component-tests/tests/src/ProfileCard.tsx +38 -0
- package/bundled-skills/skills/valdi-component-tests/tests/test/ProfileCardTest.spec.tsx +107 -0
- package/bundled-skills/skills/valdi-component-tests/tests/tsconfig.json +1 -0
- package/bundled-skills/skills/valdi-cpp-runtime/skill.md +103 -0
- package/bundled-skills/skills/valdi-custom-view/skill.md +62 -0
- package/bundled-skills/skills/valdi-ios/skill.md +93 -0
- package/bundled-skills/skills/valdi-migrate/skill.md +175 -0
- package/bundled-skills/skills/valdi-migrate/tests/BUILD.bazel +23 -0
- package/bundled-skills/skills/valdi-migrate/tests/README.md +70 -0
- package/bundled-skills/skills/valdi-migrate/tests/check_antipatterns.py +89 -0
- package/bundled-skills/skills/valdi-migrate/tests/compose_example.kt +94 -0
- package/bundled-skills/skills/valdi-migrate/tests/flutter_example.dart +129 -0
- package/bundled-skills/skills/valdi-migrate/tests/react_example.tsx +98 -0
- package/bundled-skills/skills/valdi-migrate/tests/src/expected_valdi.tsx +267 -0
- package/bundled-skills/skills/valdi-migrate/tests/tsconfig.json +3 -0
- package/{.metadata/.cursorrules.template → bundled-skills/skills/valdi-overview/skill.md} +23 -13
- package/bundled-skills/skills/valdi-perf/skill.md +197 -0
- package/bundled-skills/skills/valdi-perf/tests/BUILD.bazel +19 -0
- package/bundled-skills/skills/valdi-perf/tests/README.md +26 -0
- package/bundled-skills/skills/valdi-perf/tests/src/reference.tsx +202 -0
- package/bundled-skills/skills/valdi-perf/tests/tsconfig.json +1 -0
- package/bundled-skills/skills/valdi-polyglot-module/skill.md +85 -0
- package/bundled-skills/skills/valdi-setup/skill.md +129 -0
- package/bundled-skills/skills/valdi-testing/skill.md +65 -0
- package/bundled-skills/skills/valdi-tsx/skill.md +385 -0
- package/bundled-skills/skills/valdi-tsx/tests/BUILD.bazel +19 -0
- package/bundled-skills/skills/valdi-tsx/tests/README.md +29 -0
- package/bundled-skills/skills/valdi-tsx/tests/src/reference.tsx +173 -0
- package/bundled-skills/skills/valdi-tsx/tests/tsconfig.json +1 -0
- package/dist/commands/bootstrap.js +3 -9
- package/dist/commands/newModule.js +10 -14
- package/dist/commands/skills.js +16 -0
- package/dist/commands/skills_commands/create.js +125 -0
- package/dist/commands/skills_commands/install.js +96 -0
- package/dist/commands/skills_commands/list.js +83 -0
- package/dist/commands/skills_commands/remove.js +41 -0
- package/dist/commands/skills_commands/update.js +59 -0
- package/dist/core/constants.js +1 -1
- package/dist/index.js +5 -0
- package/dist/setup/versions.js +1 -1
- package/dist/utils/skillsAdapters.js +140 -0
- package/dist/utils/skillsRegistry.js +40 -0
- package/package.json +6 -1
- package/.bazelignore +0 -2
- package/.bootstrap/apps/cli_application/AGENTS.md.template +0 -291
- package/.bootstrap/apps/cli_application/BUILD.bazel.template +0 -7
- package/.bootstrap/apps/cli_application/modules/{{MODULE_NAME}}/BUILD.bazel.template +0 -11
- package/.bootstrap/apps/cli_application/modules/{{MODULE_NAME}}/src/index.ts +0 -1
- package/.bootstrap/apps/ui_application/.eslint.rc +0 -27
- package/.bootstrap/apps/ui_application/AGENTS.md.template +0 -278
- package/.bootstrap/apps/ui_application/BUILD.bazel.template +0 -19
- package/.bootstrap/apps/ui_application/modules/{{MODULE_NAME}}/BUILD.bazel.template +0 -21
- package/.bootstrap/apps/ui_application/modules/{{MODULE_NAME}}/res/valdi@3x.png +0 -0
- package/.bootstrap/apps/ui_application/modules/{{MODULE_NAME}}/src/App.tsx +0 -133
- package/.bootstrap/apps/ui_application/package.json +0 -24
- package/.bootstrap/modules/cpp_bridge_module/BUILD.bazel.template +0 -21
- package/.bootstrap/modules/cpp_bridge_module/cpp/CalculatorModuleFactory.cpp.template +0 -28
- package/.bootstrap/modules/cpp_bridge_module/src/Calculator.d.ts +0 -4
- package/.bootstrap/modules/ios_android_bridge_module/BUILD.bazel.template +0 -39
- package/.bootstrap/modules/ios_android_bridge_module/android/CalculatorModuleFactory.kt.template +0 -18
- package/.bootstrap/modules/ios_android_bridge_module/ios/SCCCalculatorModuleFactory.m.template +0 -31
- package/.bootstrap/modules/ios_android_bridge_module/src/Calculator.d.ts +0 -4
- package/.bootstrap/modules/ios_android_view_module/BUILD.bazel.template +0 -41
- package/.bootstrap/modules/ios_android_view_module/android/CircleView.kt.template +0 -42
- package/.bootstrap/modules/ios_android_view_module/android/CircleViewAttributesBinder.kt.template +0 -60
- package/.bootstrap/modules/ios_android_view_module/ios/SCCCircleView.h +0 -6
- package/.bootstrap/modules/ios_android_view_module/ios/SCCCircleView.m +0 -82
- package/.bootstrap/modules/ios_android_view_module/src/Circle.tsx.template +0 -15
- package/.bootstrap/modules/ui_component/BUILD.bazel.template +0 -34
- package/.bootstrap/modules/ui_component/src/NewModuleComponent.tsx +0 -45
- package/.config/copyconfig.json +0 -10
- package/.editorconfig +0 -21
- package/.eslintignore +0 -1
- package/.eslintrc.js +0 -72
- package/.github/README.md +0 -164
- package/.metadata/.bazelrc.template +0 -91
- package/.metadata/.bazelversion.template +0 -1
- package/.metadata/.editorconfig.template +0 -37
- package/.metadata/.eslintrc.js.template +0 -27
- package/.metadata/.gitignore.template +0 -4
- package/.metadata/.prettierrc.json.template +0 -7
- package/.metadata/.watchmanconfig.template +0 -8
- package/.metadata/README.md.template +0 -31
- package/.metadata/WORKSPACE.template +0 -57
- package/.metadata/config.yaml.template +0 -1
- package/.metadata/package.json.template +0 -23
- package/.prettierignore +0 -2
- package/.prettierrc.json +0 -7
- package/LINUX_COMPATIBILITY.md +0 -248
- package/prettier.config.js +0 -4
- package/scripts/sync-agents-md.sh +0 -44
- package/scripts/update-agents-templates.ts +0 -170
- package/spec/support/jasmine.json +0 -6
- package/src/commands/bootstrap.ts +0 -409
- package/src/commands/build.ts +0 -89
- package/src/commands/devSetup.ts +0 -17
- package/src/commands/doctor.ts +0 -1832
- package/src/commands/export.ts +0 -131
- package/src/commands/hotreload.ts +0 -108
- package/src/commands/install.ts +0 -93
- package/src/commands/lint.ts +0 -15
- package/src/commands/lint_commands/lintCheck.ts +0 -53
- package/src/commands/lint_commands/lintFormat.ts +0 -49
- package/src/commands/log.ts +0 -108
- package/src/commands/newModule.ts +0 -286
- package/src/commands/package.ts +0 -102
- package/src/commands/projectsync.ts +0 -488
- package/src/commands/test.ts +0 -68
- package/src/core/constants.ts +0 -97
- package/src/core/errors.ts +0 -10
- package/src/index.ts +0 -22
- package/src/setup/DevSetupHelper.ts +0 -290
- package/src/setup/linuxSetup.ts +0 -261
- package/src/setup/macOSSetup.ts +0 -156
- package/src/setup/setupEntryPoint.ts +0 -64
- package/src/setup/versions.ts +0 -8
- package/src/utils/ArgumentsResolver.ts +0 -23
- package/src/utils/BazelClient.ts +0 -285
- package/src/utils/Digraph.ts +0 -81
- package/src/utils/LoadingIndicator.ts +0 -43
- package/src/utils/applicationUtils.ts +0 -276
- package/src/utils/buildInfo.ts +0 -99
- package/src/utils/byString.ts +0 -3
- package/src/utils/cliUtils.ts +0 -112
- package/src/utils/copyBootstrapFiles.ts +0 -30
- package/src/utils/deviceUtils.ts +0 -76
- package/src/utils/errorUtils.ts +0 -63
- package/src/utils/fileUtils.ts +0 -257
- package/src/utils/githubUtils.ts +0 -102
- package/src/utils/jsonUtils.ts +0 -30
- package/src/utils/lintUtils.ts +0 -190
- package/src/utils/linuxDistro.spec.ts +0 -219
- package/src/utils/linuxDistro.ts +0 -326
- package/src/utils/logUtils.ts +0 -9
- package/src/utils/pathUtils.ts +0 -12
- package/src/utils/stringUtils.spec.ts +0 -95
- package/src/utils/stringUtils.ts +0 -103
- package/src/utils/tempDir.ts +0 -24
- package/src/utils/zipUtils.ts +0 -14
- package/test/ValdiSmokeTest.ts +0 -279
- package/test/helpers/AsyncHelpers.ts +0 -113
- package/test/helpers/CommandHelpers.ts +0 -37
- package/test/helpers/ProjectHelpers.ts +0 -153
- package/test/helpers/StreamHelpers.ts +0 -37
- package/test/helpers/TypeScriptClient.ts +0 -217
- package/tsconfig.dist.json +0 -8
- package/tsconfig.json +0 -42
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
# Valdi Performance Patterns
|
|
2
|
+
|
|
3
|
+
Valdi re-renders a child component whenever its viewModel reference changes. Most
|
|
4
|
+
unnecessary re-renders come from creating new object/array/function references in
|
|
5
|
+
`onRender()`. Fix the reference — fix the re-render.
|
|
6
|
+
|
|
7
|
+
## ViewModel Identity Stability
|
|
8
|
+
|
|
9
|
+
**The #1 performance problem in Valdi apps.** Every object or array literal created
|
|
10
|
+
inside `onRender()` is a new reference. Child components will always re-render, even
|
|
11
|
+
when the actual values haven't changed.
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
// ❌ New object every render — child always re-renders
|
|
15
|
+
onRender(): void {
|
|
16
|
+
<UserRow vm={{ name: this.viewModel.user.name, age: this.viewModel.user.age }} />;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// ❌ New array every render
|
|
20
|
+
onRender(): void {
|
|
21
|
+
<TabBar tabs={['Home', 'Profile', 'Settings']} />;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// ✅ Stable class property for constants
|
|
25
|
+
private tabs = ['Home', 'Profile', 'Settings'];
|
|
26
|
+
onRender(): void {
|
|
27
|
+
<TabBar tabs={this.tabs} />;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// ✅ Pre-compute derived viewModels in onViewModelUpdate
|
|
31
|
+
private userRowVM: UserRowViewModel = { name: '', age: 0 };
|
|
32
|
+
|
|
33
|
+
onViewModelUpdate(): void {
|
|
34
|
+
this.userRowVM = { name: this.viewModel.user.name, age: this.viewModel.user.age };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
onRender(): void {
|
|
38
|
+
<UserRow vm={this.userRowVM} />;
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Only update the pre-computed VM when the relevant input actually changes:
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
onViewModelUpdate(previous?: UserProfileViewModel): void {
|
|
46
|
+
if (this.viewModel.userId !== previous?.userId) {
|
|
47
|
+
this.userRowVM = buildUserRowVM(this.viewModel.user);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Navigation Callbacks
|
|
53
|
+
|
|
54
|
+
Navigation callbacks passed into child viewModels have the same identity problem:
|
|
55
|
+
`() => this.navigationController.push(...)` creates a new function each render.
|
|
56
|
+
Use a class arrow function — it is defined once and has a stable reference:
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
// ❌ New function every render
|
|
60
|
+
onRender(): void {
|
|
61
|
+
<UserCard onTap={() => this.navigationController.push(DetailPage, { id: this.viewModel.userId })} />;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// ✅ Class arrow function — stable reference, viewModel.userId read at tap time
|
|
65
|
+
private goToDetail = (): void => {
|
|
66
|
+
this.navigationController.push(DetailPage, { id: this.viewModel.userId });
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
onRender(): void {
|
|
70
|
+
<UserCard onTap={this.goToDetail} />;
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## `<layout>` vs `<view>`
|
|
75
|
+
|
|
76
|
+
`<view>` allocates a native platform view. `<layout>` is virtual — it participates in
|
|
77
|
+
flexbox layout but creates no native view, which is faster and uses less memory.
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
// ❌ Native view wasted on an invisible spacer
|
|
81
|
+
<view height={16} />
|
|
82
|
+
|
|
83
|
+
// ✅ No native view allocated
|
|
84
|
+
<layout height={16} />
|
|
85
|
+
|
|
86
|
+
// ❌ Wrapper with no visual properties or tap handler
|
|
87
|
+
<view flexDirection="column">
|
|
88
|
+
<label value="A" />;
|
|
89
|
+
<label value="B" />;
|
|
90
|
+
</view>;
|
|
91
|
+
|
|
92
|
+
// ✅ Virtual layout node
|
|
93
|
+
<layout flexDirection="column">
|
|
94
|
+
<label value="A" />;
|
|
95
|
+
<label value="B" />;
|
|
96
|
+
</layout>;
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
**Use `<view>` when you need:** `onTap`, `backgroundColor`, `borderRadius`, `style`,
|
|
100
|
+
`overflow`, `opacity`, or any visual/interactive property.
|
|
101
|
+
**Use `<layout>` for everything else:** spacers, invisible wrappers, structural containers.
|
|
102
|
+
|
|
103
|
+
## Keys in Lists
|
|
104
|
+
|
|
105
|
+
Keys determine element identity across re-renders. Without a key (or with an index
|
|
106
|
+
key), reordering or inserting items causes the wrong component instances to receive
|
|
107
|
+
the wrong viewModels.
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
// ❌ No key — identity lost on reorder
|
|
111
|
+
{this.viewModel.items.forEach(item => {
|
|
112
|
+
<ItemRow value={item.name} />;
|
|
113
|
+
})}
|
|
114
|
+
|
|
115
|
+
// ❌ Index key — breaks on insert/remove
|
|
116
|
+
{this.viewModel.items.forEach((item, index) => {
|
|
117
|
+
<ItemRow key={String(index)} value={item.name} />;
|
|
118
|
+
})}
|
|
119
|
+
|
|
120
|
+
// ✅ Stable data ID
|
|
121
|
+
{this.viewModel.items.forEach(item => {
|
|
122
|
+
<ItemRow key={item.id} value={item.name} />;
|
|
123
|
+
})}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## Render Props as Class Arrow Functions
|
|
127
|
+
|
|
128
|
+
When a parent needs to pass a render function to a child (e.g. a list row renderer),
|
|
129
|
+
define it as a class arrow function property so it has a stable reference:
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
// ❌ New function every render — child's renderItem prop always changes
|
|
133
|
+
onRender(): void {
|
|
134
|
+
<List renderItem={(item) => { <Row data={item} />; }} />;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// ✅ Stable class arrow function
|
|
138
|
+
private renderItem = (item: Item): void => {
|
|
139
|
+
<Row data={item} />;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
onRender(): void {
|
|
143
|
+
<List renderItem={this.renderItem} />;
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
For loop closures that must capture a loop variable, use `createReusableCallback`
|
|
148
|
+
inline in `onRender()`. Valdi's diffing engine recognises `Callback` objects and
|
|
149
|
+
updates the internal function reference without treating it as a prop change, so the
|
|
150
|
+
child does not re-render:
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
import { createReusableCallback } from 'valdi_core/src/utils/Callback';
|
|
154
|
+
|
|
155
|
+
// ❌ New plain function every render — child always re-renders
|
|
156
|
+
onRender(): void {
|
|
157
|
+
{this.viewModel.sections.forEach((section, i) => {
|
|
158
|
+
<Section onTap={() => this.handleTap(i)} />;
|
|
159
|
+
})}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// ✅ Inline Callback — identity-merged by Valdi's diffing engine
|
|
163
|
+
onRender(): void {
|
|
164
|
+
{this.viewModel.sections.forEach((section, i) => {
|
|
165
|
+
<Section onTap={createReusableCallback(() => this.handleTap(i))} />;
|
|
166
|
+
})}
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## Style Objects at Module Level
|
|
171
|
+
|
|
172
|
+
`new Style<T>({...})` interns style objects — the same property values always produce
|
|
173
|
+
the same cached object. This interning only works at module initialization time.
|
|
174
|
+
Inside `onRender()` the cache is bypassed and a new allocation happens every render.
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
// ❌ Defeats interning — new allocation every render
|
|
178
|
+
onRender(): void {
|
|
179
|
+
const s = new Style<View>({ backgroundColor: '#fff', borderRadius: 8 });
|
|
180
|
+
<view style={s} />;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// ✅ Interned at module level
|
|
184
|
+
import { View } from 'valdi_tsx/src/NativeTemplateElements';
|
|
185
|
+
|
|
186
|
+
const styles = {
|
|
187
|
+
card: new Style<View>({ backgroundColor: '#fff', borderRadius: 8 }),
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
class MyCard extends Component<MyViewModel> {
|
|
191
|
+
onRender(): void {
|
|
192
|
+
<view style={styles.card} />;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
Group styles in a `const styles = {}` object after the class definition.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
load("//bzl/valdi:valdi_module.bzl", "valdi_module")
|
|
2
|
+
|
|
3
|
+
valdi_module(
|
|
4
|
+
name = "tests",
|
|
5
|
+
srcs = glob([
|
|
6
|
+
"src/**/*.ts",
|
|
7
|
+
"src/**/*.tsx",
|
|
8
|
+
]) + [
|
|
9
|
+
"tsconfig.json",
|
|
10
|
+
],
|
|
11
|
+
android_output_target = "release",
|
|
12
|
+
ios_module_name = "SCCValdiPerfRef",
|
|
13
|
+
ios_output_target = "release",
|
|
14
|
+
visibility = ["//visibility:public"],
|
|
15
|
+
deps = [
|
|
16
|
+
"//src/valdi_modules/src/valdi/valdi_core",
|
|
17
|
+
"//src/valdi_modules/src/valdi/valdi_tsx",
|
|
18
|
+
],
|
|
19
|
+
)
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# valdi-perf skill tests
|
|
2
|
+
|
|
3
|
+
Compile check for the performance patterns taught in `skill.md`.
|
|
4
|
+
|
|
5
|
+
## What's tested
|
|
6
|
+
|
|
7
|
+
`src/reference.tsx` exercises:
|
|
8
|
+
- Pre-computed viewModels in `onViewModelUpdate` (stable child references)
|
|
9
|
+
- `createReusableCallback` for memoized callbacks by argument
|
|
10
|
+
- `<layout>` vs `<view>` for invisible containers and spacers
|
|
11
|
+
- Stable class arrow functions for render props
|
|
12
|
+
- Module-level `Style` objects (not created inside `onRender`)
|
|
13
|
+
- `key={item.id}` with stable data IDs in lists (not array indices)
|
|
14
|
+
|
|
15
|
+
## Compile check
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
cd client/src/open_source
|
|
19
|
+
bazel build //ai-skills/skills/valdi-perf/tests:tests
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Expected: `Build completed successfully`
|
|
23
|
+
|
|
24
|
+
## Updating
|
|
25
|
+
|
|
26
|
+
When Valdi APIs change (import paths, type signatures), update `src/reference.tsx` to match and keep it compiling. Also update `../skill.md` with any corrected patterns.
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
// valdi-perf skill reference — compile check for performance patterns.
|
|
2
|
+
// This file must compile cleanly against the Valdi TypeScript compiler.
|
|
3
|
+
// Run: bzl build //ai-skills/skills/valdi-perf/tests:tests
|
|
4
|
+
|
|
5
|
+
import { Component, StatefulComponent } from 'valdi_core/src/Component';
|
|
6
|
+
import { Style } from 'valdi_core/src/Style';
|
|
7
|
+
import { createReusableCallback } from 'valdi_core/src/utils/Callback';
|
|
8
|
+
import { Layout, View } from 'valdi_tsx/src/NativeTemplateElements';
|
|
9
|
+
|
|
10
|
+
// ─── Module-level styles (defeats interning if created inside onRender) ───────
|
|
11
|
+
|
|
12
|
+
const styles = {
|
|
13
|
+
card: new Style<View>({ backgroundColor: '#fff', borderRadius: 8, padding: 12 }),
|
|
14
|
+
spacer: new Style<Layout>({ height: 16 }),
|
|
15
|
+
row: new Style<View>({ flexDirection: 'row', alignItems: 'center' }),
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// ─── Stable class property for constant arrays ───────────────────────────────
|
|
19
|
+
|
|
20
|
+
interface TabBarViewModel {
|
|
21
|
+
tabs: string[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
class TabBar extends Component<TabBarViewModel> {
|
|
25
|
+
onRender(): void {
|
|
26
|
+
<view style={styles.row}>
|
|
27
|
+
{this.viewModel.tabs.forEach(tab => {
|
|
28
|
+
<label key={tab} value={tab} />;
|
|
29
|
+
})}
|
|
30
|
+
</view>;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// ─── Pre-computed viewModel in onViewModelUpdate ─────────────────────────────
|
|
35
|
+
|
|
36
|
+
interface UserRowViewModel {
|
|
37
|
+
name: string;
|
|
38
|
+
subtitle: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
class UserRow extends Component<UserRowViewModel> {
|
|
42
|
+
onRender(): void {
|
|
43
|
+
<view style={styles.card}>
|
|
44
|
+
<label value={this.viewModel.name} />;
|
|
45
|
+
<label value={this.viewModel.subtitle} />;
|
|
46
|
+
</view>;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface UserListViewModel {
|
|
51
|
+
user: { id: string; name: string; bio: string };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
interface UserListState {
|
|
55
|
+
selectedIndex: number;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
class UserList extends StatefulComponent<UserListViewModel, UserListState> {
|
|
59
|
+
state: UserListState = { selectedIndex: 0 };
|
|
60
|
+
|
|
61
|
+
// Pre-computed to avoid new object on every render
|
|
62
|
+
private userRowVM: UserRowViewModel = { name: '', subtitle: '' };
|
|
63
|
+
|
|
64
|
+
onViewModelUpdate(previous?: UserListViewModel): void {
|
|
65
|
+
if (this.viewModel.user.id !== previous?.user.id) {
|
|
66
|
+
this.userRowVM = {
|
|
67
|
+
name: this.viewModel.user.name,
|
|
68
|
+
subtitle: this.viewModel.user.bio,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
onRender(): void {
|
|
74
|
+
// ✅ Stable reference — UserRow only re-renders when user.id changes
|
|
75
|
+
<UserRow {...this.userRowVM} />;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// ─── createReusableCallback: stable function for same arguments ───────────────
|
|
80
|
+
|
|
81
|
+
interface SectionViewModel {
|
|
82
|
+
id: string;
|
|
83
|
+
title: string;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
interface SectionListViewModel {
|
|
87
|
+
sections: SectionViewModel[];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
interface SectionListState {
|
|
91
|
+
selectedId: string | null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
class SectionList extends StatefulComponent<SectionListViewModel, SectionListState> {
|
|
95
|
+
state: SectionListState = { selectedId: null };
|
|
96
|
+
|
|
97
|
+
private handleSectionTap(id: string): void {
|
|
98
|
+
this.setState({ selectedId: id });
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
onRender(): void {
|
|
102
|
+
<view>
|
|
103
|
+
{this.viewModel.sections.forEach(section => {
|
|
104
|
+
// ✅ Inline Callback — Valdi merges identity, child doesn't re-render
|
|
105
|
+
<view key={section.id} onTap={createReusableCallback(() => this.handleSectionTap(section.id))}>
|
|
106
|
+
<label value={section.title} />;
|
|
107
|
+
</view>;
|
|
108
|
+
})}
|
|
109
|
+
</view>;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// ─── Stable render props as class arrow functions ────────────────────────────
|
|
114
|
+
|
|
115
|
+
interface Item {
|
|
116
|
+
id: string;
|
|
117
|
+
label: string;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
interface FlatListViewModel {
|
|
121
|
+
items: Item[];
|
|
122
|
+
renderItem: (item: Item) => void;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
class FlatList extends Component<FlatListViewModel> {
|
|
126
|
+
onRender(): void {
|
|
127
|
+
<scroll>
|
|
128
|
+
{this.viewModel.items.forEach(item => {
|
|
129
|
+
this.viewModel.renderItem(item);
|
|
130
|
+
})}
|
|
131
|
+
</scroll>;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
interface FeedViewModel {
|
|
136
|
+
items: Item[];
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
class Feed extends Component<FeedViewModel> {
|
|
140
|
+
// ✅ Stable reference — defined as class property, not inline in onRender
|
|
141
|
+
private renderItem = (item: Item): void => {
|
|
142
|
+
<view key={item.id}>
|
|
143
|
+
<label value={item.label} />;
|
|
144
|
+
</view>;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
onRender(): void {
|
|
148
|
+
<FlatList items={this.viewModel.items} renderItem={this.renderItem} />;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// ─── <layout> vs <view>: no native view for invisible containers ──────────────
|
|
153
|
+
|
|
154
|
+
interface CardViewModel {
|
|
155
|
+
title: string;
|
|
156
|
+
subtitle: string;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
class Card extends Component<CardViewModel> {
|
|
160
|
+
onRender(): void {
|
|
161
|
+
<view style={styles.card}>
|
|
162
|
+
<label value={this.viewModel.title} />;
|
|
163
|
+
{/* ✅ layout: no native view allocated for spacer */}
|
|
164
|
+
<layout style={styles.spacer} />;
|
|
165
|
+
{/* ✅ layout: invisible structural wrapper */}
|
|
166
|
+
<layout flexDirection="row" alignItems="center">
|
|
167
|
+
<label value={this.viewModel.subtitle} />;
|
|
168
|
+
</layout>;
|
|
169
|
+
</view>;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// ─── Keys in lists: stable data ID, not index ────────────────────────────────
|
|
174
|
+
|
|
175
|
+
interface ListItem {
|
|
176
|
+
id: string;
|
|
177
|
+
text: string;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
interface StableListViewModel {
|
|
181
|
+
items: ListItem[];
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
class StableList extends Component<StableListViewModel> {
|
|
185
|
+
onRender(): void {
|
|
186
|
+
<scroll>
|
|
187
|
+
{this.viewModel.items.forEach(item => {
|
|
188
|
+
// ✅ Stable key from data ID — reorder/insert/remove works correctly
|
|
189
|
+
<view key={item.id}>
|
|
190
|
+
<label value={item.text} />;
|
|
191
|
+
</view>;
|
|
192
|
+
})}
|
|
193
|
+
</scroll>;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
void TabBar;
|
|
198
|
+
void UserList;
|
|
199
|
+
void SectionList;
|
|
200
|
+
void Feed;
|
|
201
|
+
void Card;
|
|
202
|
+
void StableList;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"extends": "../../../../src/valdi_modules/src/valdi/_configs/base.tsconfig.json"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# Polyglot Module Rules
|
|
2
|
+
|
|
3
|
+
**Applies to**: `**/BUILD.bazel` and source files in modules with platform-specific implementations (`android/`, `ios/`, `macos/`, `web/` directories).
|
|
4
|
+
|
|
5
|
+
## Module Structure
|
|
6
|
+
|
|
7
|
+
A polyglot module contains platform-native implementations alongside Valdi TSX source:
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
my_module/
|
|
11
|
+
src/ # Valdi TSX components (compiled by Valdi compiler)
|
|
12
|
+
android/ # Kotlin/Java native implementation
|
|
13
|
+
ios/ # Objective-C native implementation
|
|
14
|
+
macos/ # Objective-C native implementation (macOS desktop)
|
|
15
|
+
web/ # TypeScript web implementation (compiled by ts_project, NOT Valdi compiler)
|
|
16
|
+
strings/ # Localization
|
|
17
|
+
module.yaml
|
|
18
|
+
tsconfig.json
|
|
19
|
+
BUILD.bazel
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## BUILD.bazel Pattern
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
ts_project(
|
|
26
|
+
name = "my_module_web",
|
|
27
|
+
srcs = glob(["web/**/*.ts", "web/**/*.d.ts"]),
|
|
28
|
+
tsconfig = "web/tsconfig.json",
|
|
29
|
+
visibility = ["//visibility:public"],
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
valdi_module(
|
|
33
|
+
name = "my_module",
|
|
34
|
+
srcs = glob(["src/**/*.ts", "src/**/*.tsx"]) + ["tsconfig.json"],
|
|
35
|
+
android_deps = [":android_impl"],
|
|
36
|
+
ios_deps = [":ios_impl"],
|
|
37
|
+
macos_deps = [":macos_impl"],
|
|
38
|
+
web_deps = [":my_module_web"],
|
|
39
|
+
deps = [...],
|
|
40
|
+
)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
- `web_deps`: makes `web/` outputs available to the web bundle. Any file in `web_deps` that exports `webPolyglotViews` is automatically registered with the `WebViewClassRegistry` at bundle time — no extra configuration needed.
|
|
44
|
+
- After changing `module.yaml` deps, run: `./scripts/regenerate_valdi_modules_build_bazel_files.sh`
|
|
45
|
+
|
|
46
|
+
## Web Polyglot Entry Pattern
|
|
47
|
+
|
|
48
|
+
Web entry files export view factories that are auto-registered with the `WebViewClassRegistry` at bundle time. Export a `webPolyglotViews` record mapping class names to factory functions:
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
export const webPolyglotViews: Record<string, (container: HTMLElement) => void> = {
|
|
52
|
+
MyCustomViewClass: createMyViewFactory(),
|
|
53
|
+
};
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Each key must match the `webClass` attribute used in `<custom-view webClass="MyCustomViewClass">`.
|
|
57
|
+
|
|
58
|
+
A factory receives an `HTMLElement` container and populates it with DOM content:
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
function createMyViewFactory(): (container: HTMLElement) => void {
|
|
62
|
+
return (container: HTMLElement) => {
|
|
63
|
+
const element = document.createElement('div');
|
|
64
|
+
container.appendChild(element);
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Key constraints for `web/` files
|
|
70
|
+
- Compiled by `tsc` via `ts_project`, **not** the Valdi compiler
|
|
71
|
+
- Do **NOT** use Valdi imports (`valdi_core/`, `valdi_tsx/`) — web files run in the browser, not the Valdi runtime
|
|
72
|
+
- Do **NOT** manually call `globalThis.moduleLoader.resolveRequire()` — build system handles registration
|
|
73
|
+
- The `tsconfig.json` in `web/` should target `DOM` + `ES2020` with `module: "commonjs"`
|
|
74
|
+
|
|
75
|
+
## Canonical Example
|
|
76
|
+
|
|
77
|
+
See `valdi_polyglot` module for a complete working example with all four platforms:
|
|
78
|
+
- `src/composer_modules/src/composer/valdi_polyglot/` (in the Snap monorepo)
|
|
79
|
+
- Or the equivalent path in your project
|
|
80
|
+
|
|
81
|
+
## Common Mistakes
|
|
82
|
+
|
|
83
|
+
- Putting `web/` files in `srcs` — they must go through `ts_project` + `web_deps`, not the Valdi compiler
|
|
84
|
+
- Missing platform `_deps` — each platform impl must be wired via `android_deps`, `ios_deps`, `macos_deps`
|
|
85
|
+
- Using Valdi imports in `web/` — they don't exist in the browser bundle
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# Valdi Module Setup
|
|
2
|
+
|
|
3
|
+
## BUILD.bazel — valdi_module()
|
|
4
|
+
|
|
5
|
+
```python
|
|
6
|
+
load("//bzl/valdi:valdi_module.bzl", "valdi_module")
|
|
7
|
+
|
|
8
|
+
valdi_module(
|
|
9
|
+
name = "my_module", # Must match the directory name exactly
|
|
10
|
+
srcs = glob([
|
|
11
|
+
"src/**/*.ts",
|
|
12
|
+
"src/**/*.tsx",
|
|
13
|
+
]) + [
|
|
14
|
+
"tsconfig.json", # tsconfig must be listed in srcs
|
|
15
|
+
],
|
|
16
|
+
android_output_target = "release",
|
|
17
|
+
ios_module_name = "SCCMyModule", # SCC prefix + PascalCase module name
|
|
18
|
+
ios_output_target = "release",
|
|
19
|
+
visibility = ["//visibility:public"],
|
|
20
|
+
deps = [
|
|
21
|
+
"//src/valdi_modules/src/valdi/valdi_core",
|
|
22
|
+
"//src/valdi_modules/src/valdi/valdi_tsx",
|
|
23
|
+
# Add as needed — see dependency table below
|
|
24
|
+
],
|
|
25
|
+
)
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
**Common mistakes that cause build failures:**
|
|
29
|
+
|
|
30
|
+
- `name` must equal the Bazel package directory name. The Valdi compiler derives
|
|
31
|
+
module identity from this — mismatches cause a build error.
|
|
32
|
+
- `tsconfig.json` must be in `srcs`. It won't be picked up automatically even if
|
|
33
|
+
it's in the directory.
|
|
34
|
+
- Missing a dep (e.g. using `HTTPClient` without `valdi_http`) produces a TypeScript
|
|
35
|
+
path resolution error, not a missing import error — can be confusing.
|
|
36
|
+
|
|
37
|
+
## tsconfig.json
|
|
38
|
+
|
|
39
|
+
```json
|
|
40
|
+
{
|
|
41
|
+
"extends": "../../../../src/valdi_modules/src/valdi/_configs/base.tsconfig.json"
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Adjust the `../../../../` prefix to match how many directories deep your module is
|
|
46
|
+
from the repo root. The base config sets up `paths` aliases so `'valdi_core/src/...'`
|
|
47
|
+
imports resolve correctly.
|
|
48
|
+
|
|
49
|
+
## Dependency Table
|
|
50
|
+
|
|
51
|
+
| You're using | Add this dep |
|
|
52
|
+
|---|---|
|
|
53
|
+
| `Component`, `StatefulComponent`, `Style`, providers, `CancelablePromise` | `//src/valdi_modules/src/valdi/valdi_core` |
|
|
54
|
+
| JSX elements (`<view>`, `<label>`, etc.), `NativeTemplateElements` | `//src/valdi_modules/src/valdi/valdi_tsx` |
|
|
55
|
+
| `HTTPClient`, `HTTPResponse` | `//src/valdi_modules/src/valdi/valdi_http` |
|
|
56
|
+
| `PersistentStore` | `//src/valdi_modules/src/valdi/persistence` |
|
|
57
|
+
| Pre-built UI components (buttons, cards, etc.) | `//src/valdi_modules/src/valdi/valdi_widgets_core` |
|
|
58
|
+
|
|
59
|
+
## ios_module_name Convention
|
|
60
|
+
|
|
61
|
+
Must start with `SCC` followed by the module name in PascalCase:
|
|
62
|
+
|
|
63
|
+
| Directory name | ios_module_name |
|
|
64
|
+
|---|---|
|
|
65
|
+
| `chat_thread` | `SCCChatThread` |
|
|
66
|
+
| `profile_editor` | `SCCProfileEditor` |
|
|
67
|
+
| `story_viewer` | `SCCStoryViewer` |
|
|
68
|
+
|
|
69
|
+
This becomes the Swift module name on iOS. Conflicts with other `SCCXxx` modules in
|
|
70
|
+
the same app target will cause a linker error.
|
|
71
|
+
|
|
72
|
+
## New Component File Template
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
import { Component } from 'valdi_core/src/Component';
|
|
76
|
+
|
|
77
|
+
interface MyComponentViewModel {
|
|
78
|
+
// viewModel properties
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export class MyComponent extends Component<MyComponentViewModel> {
|
|
82
|
+
onRender(): void {
|
|
83
|
+
<view>
|
|
84
|
+
<label value={this.viewModel.someText} />;
|
|
85
|
+
</view>;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
For stateful components:
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
import { StatefulComponent } from 'valdi_core/src/Component';
|
|
94
|
+
|
|
95
|
+
interface MyState {
|
|
96
|
+
// state properties
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export class MyComponent extends StatefulComponent<MyComponentViewModel, MyState> {
|
|
100
|
+
state: MyState = { /* initial values */ };
|
|
101
|
+
|
|
102
|
+
onRender(): void {
|
|
103
|
+
// ...
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Building
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
bazel build //path/to/my_module:my_module
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Hot Reload
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
valdi hotreload
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Run from your module directory. The CLI watches for file changes, recompiles, and
|
|
121
|
+
pushes the updated module to a connected simulator or device over USB (or network
|
|
122
|
+
with `--network`).
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
valdi hotreload --network # Discover device over Wi-Fi instead of USB
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
If hot reload stops reflecting changes, stop with `Ctrl+C` and restart — the CLI
|
|
129
|
+
will clean stale build artifacts automatically.
|