@c9up/orbit 0.0.0-stage → 0.1.1
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/Cargo.lock +139 -0
- package/Cargo.toml +20 -0
- package/LICENSE +21 -0
- package/README.md +320 -2
- package/android/orbit.cmake +31 -0
- package/android/src/main/cpp/NativeOrbit.cpp +309 -0
- package/android/src/main/cpp/NativeOrbit.h +35 -0
- package/android/src/main/cpp/orbit_host.h +34 -0
- package/android/src/main/java/com/c9up/orbit/OrbitEffects.kt +286 -0
- package/android/src/main/java/com/c9up/orbit/OrbitHost.kt +258 -0
- package/android/src/main/java/com/c9up/orbit/OrbitImage.kt +346 -0
- package/android/src/main/java/com/c9up/orbit/OrbitInput.kt +161 -0
- package/android/src/main/java/com/c9up/orbit/OrbitPress.kt +110 -0
- package/android/src/main/java/com/c9up/orbit/OrbitVector.kt +115 -0
- package/android/src/main/java/com/c9up/orbit/OrbitViews.kt +362 -0
- package/crates/orbit-host/Cargo.toml +14 -0
- package/crates/orbit-host/src/gesture.rs +561 -0
- package/crates/orbit-host/src/lib.rs +2042 -0
- package/crates/orbit-host/src/motion.rs +328 -0
- package/crates/orbit-host/src/vector.rs +1173 -0
- package/dist/color.d.ts +10 -0
- package/dist/color.d.ts.map +1 -0
- package/dist/color.js +62 -0
- package/dist/color.js.map +1 -0
- package/dist/commit.d.ts +77 -0
- package/dist/commit.d.ts.map +1 -0
- package/dist/commit.js +302 -0
- package/dist/commit.js.map +1 -0
- package/dist/errors.d.ts +76 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +89 -0
- package/dist/errors.js.map +1 -0
- package/dist/host.d.ts +49 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +20 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -0
- package/dist/motion.d.ts +18 -0
- package/dist/motion.d.ts.map +1 -0
- package/dist/motion.js +67 -0
- package/dist/motion.js.map +1 -0
- package/dist/node.d.ts +46 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +110 -0
- package/dist/node.js.map +1 -0
- package/dist/pages.d.ts +56 -0
- package/dist/pages.d.ts.map +1 -0
- package/dist/pages.js +157 -0
- package/dist/pages.js.map +1 -0
- package/dist/react-native/index.d.ts +33 -0
- package/dist/react-native/index.d.ts.map +1 -0
- package/dist/react-native/index.js +71 -0
- package/dist/react-native/index.js.map +1 -0
- package/dist/report.d.ts +13 -0
- package/dist/report.d.ts.map +1 -0
- package/dist/report.js +22 -0
- package/dist/report.js.map +1 -0
- package/dist/server.d.ts +109 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +290 -0
- package/dist/server.js.map +1 -0
- package/dist/stack.d.ts +117 -0
- package/dist/stack.d.ts.map +1 -0
- package/dist/stack.js +299 -0
- package/dist/stack.js.map +1 -0
- package/dist/styles.d.ts +138 -0
- package/dist/styles.d.ts.map +1 -0
- package/dist/styles.js +920 -0
- package/dist/styles.js.map +1 -0
- package/dist/template.d.ts +27 -0
- package/dist/template.d.ts.map +1 -0
- package/dist/template.js +1436 -0
- package/dist/template.js.map +1 -0
- package/dist/textDecoder.d.ts +29 -0
- package/dist/textDecoder.d.ts.map +1 -0
- package/dist/textDecoder.js +149 -0
- package/dist/textDecoder.js.map +1 -0
- package/dist/url.d.ts +45 -0
- package/dist/url.d.ts.map +1 -0
- package/dist/url.js +303 -0
- package/dist/url.js.map +1 -0
- package/package.json +96 -3
- package/scripts/build-android.sh +27 -0
- package/scripts/test-styles.mjs +31 -0
- package/specs/NativeOrbit.ts +34 -0
- package/src/color.ts +68 -0
- package/src/commit.ts +373 -0
- package/src/errors.ts +178 -0
- package/src/host.ts +91 -0
- package/src/index.ts +64 -0
- package/src/motion.ts +88 -0
- package/src/node.ts +133 -0
- package/src/pages.ts +207 -0
- package/src/react-native/index.ts +115 -0
- package/src/react-native/react-native.d.ts +39 -0
- package/src/report.ts +25 -0
- package/src/server.ts +395 -0
- package/src/stack.ts +428 -0
- package/src/styles.ts +1021 -0
- package/src/template.ts +1681 -0
- package/src/textDecoder.ts +162 -0
- package/src/url.ts +333 -0
package/src/commit.ts
ADDED
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
import {
|
|
2
|
+
E_ORBIT_COMMIT_FAILED,
|
|
3
|
+
E_ORBIT_SVG_VALUE,
|
|
4
|
+
E_ORBIT_TREE_TOO_DEEP,
|
|
5
|
+
} from "./errors.js";
|
|
6
|
+
import { currentHost, type OrbitHost } from "./host.js";
|
|
7
|
+
import { type NativeNode, type Props, setDirtyListener } from "./node.js";
|
|
8
|
+
import { type ErrorListener, report } from "./report.js";
|
|
9
|
+
import { viewportSize } from "./styles.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Committing the JS tree to the Rust host: one JSON batch per commit over JSI
|
|
13
|
+
* (create, props patch, children, root). Layout, diffing and mounting happen in
|
|
14
|
+
* Rust and on the platform side. Commits are batched to a microtask, so many
|
|
15
|
+
* signal writes in one turn cost one commit.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export interface OrbitReport {
|
|
19
|
+
deleted: number[];
|
|
20
|
+
parseUs: number;
|
|
21
|
+
layoutUs: number;
|
|
22
|
+
diffUs: number;
|
|
23
|
+
measures: number;
|
|
24
|
+
nodes: number;
|
|
25
|
+
opsBytes: number;
|
|
26
|
+
/** What the host left out of the drawings of this commit, coded (`E_ORBIT_SVG_VALUE: …`). */
|
|
27
|
+
warnings?: string[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function isReport(value: unknown): value is OrbitReport {
|
|
31
|
+
return (
|
|
32
|
+
typeof value === "object" &&
|
|
33
|
+
value !== null &&
|
|
34
|
+
Array.isArray(Reflect.get(value, "deleted"))
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The codes the host prefixes its messages with, as the errors orbit throws. */
|
|
39
|
+
const HOST_CODES = {
|
|
40
|
+
E_ORBIT_TREE_TOO_DEEP,
|
|
41
|
+
E_ORBIT_SVG_VALUE,
|
|
42
|
+
} as const;
|
|
43
|
+
|
|
44
|
+
/** A coded host message (`E_ORBIT_X: what happened`) as orbit's error for it. */
|
|
45
|
+
function hostError(message: string, cause?: unknown): Error | null {
|
|
46
|
+
const at = message.indexOf(": ");
|
|
47
|
+
const code = at < 0 ? "" : message.slice(0, at);
|
|
48
|
+
for (const [name, ErrorClass] of Object.entries(HOST_CODES)) {
|
|
49
|
+
if (name === code)
|
|
50
|
+
return new ErrorClass([message.slice(at + 2)], { cause });
|
|
51
|
+
}
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Why the host refused a commit: its `{ "error": … }` report, thrown by the
|
|
57
|
+
* native module as the message of a JS error (or returned, by a host that
|
|
58
|
+
* does not throw).
|
|
59
|
+
*/
|
|
60
|
+
function refusal(answer: unknown, cause?: unknown): Error {
|
|
61
|
+
let detail = answer;
|
|
62
|
+
if (typeof answer === "string") {
|
|
63
|
+
try {
|
|
64
|
+
detail = JSON.parse(answer);
|
|
65
|
+
} catch {
|
|
66
|
+
detail = answer;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const message =
|
|
70
|
+
typeof detail === "object" && detail !== null
|
|
71
|
+
? Reflect.get(detail, "error")
|
|
72
|
+
: undefined;
|
|
73
|
+
if (typeof message === "string") {
|
|
74
|
+
const coded = hostError(message, cause);
|
|
75
|
+
if (coded) return coded;
|
|
76
|
+
}
|
|
77
|
+
return new E_ORBIT_COMMIT_FAILED([detail], { cause });
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* `text` with every unpaired surrogate replaced by U+FFFD, as
|
|
82
|
+
* `String.prototype.toWellFormed` does (Hermes has none). Half an emoji is
|
|
83
|
+
* ordinary content — `"Ana😀".slice(0, 4)` — and a browser shows it as U+FFFD.
|
|
84
|
+
*/
|
|
85
|
+
export function toWellFormed(text: string): string {
|
|
86
|
+
let out = "";
|
|
87
|
+
let from = 0;
|
|
88
|
+
for (let i = 0; i < text.length; i++) {
|
|
89
|
+
const unit = text.charCodeAt(i);
|
|
90
|
+
if (unit < 0xd800 || unit > 0xdfff) continue;
|
|
91
|
+
if (unit <= 0xdbff && i + 1 < text.length) {
|
|
92
|
+
const next = text.charCodeAt(i + 1);
|
|
93
|
+
if (next >= 0xdc00 && next <= 0xdfff) {
|
|
94
|
+
i += 1;
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
out += `${text.slice(from, i)}\ufffd`;
|
|
99
|
+
from = i + 1;
|
|
100
|
+
}
|
|
101
|
+
return from === 0 ? text : out + text.slice(from);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const wellFormed = (_key: string, value: unknown): unknown =>
|
|
105
|
+
typeof value === "string" ? toWellFormed(value) : value;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* The batch as JSON the host reads. `JSON.stringify` writes an unpaired
|
|
109
|
+
* surrogate as a `\ud…` escape (lower-case, per ECMA-262 and Hermes), which
|
|
110
|
+
* the host's strict JSON reader refuses: one cut emoji would fail the whole
|
|
111
|
+
* commit. Only a batch holding such an escape pays for the second pass.
|
|
112
|
+
*/
|
|
113
|
+
export function serialise(ops: readonly unknown[]): string {
|
|
114
|
+
const json = JSON.stringify(ops);
|
|
115
|
+
return json.includes("\\ud") ? JSON.stringify(ops, wellFormed) : json;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const created = new WeakSet<NativeNode>();
|
|
119
|
+
const byTag = new Map<number, NativeNode>();
|
|
120
|
+
const lastChildren = new WeakMap<NativeNode, number[]>();
|
|
121
|
+
const afterCommitHooks: Array<() => void> = [];
|
|
122
|
+
const commitListeners = new Set<() => void>();
|
|
123
|
+
let mountedHost: OrbitHost | null = null;
|
|
124
|
+
|
|
125
|
+
/** Run `listener` after every commit; returns what stops it. */
|
|
126
|
+
export function onEveryCommit(listener: () => void): () => void {
|
|
127
|
+
commitListeners.add(listener);
|
|
128
|
+
return () => commitListeners.delete(listener);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The host the mounted tree commits to (layout reads, scrolling). */
|
|
132
|
+
export function treeHost(): OrbitHost {
|
|
133
|
+
return mountedHost ?? currentHost();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Run `hook` once the next commit has reached the host (aurora's onMount timing). */
|
|
137
|
+
export function afterCommit(hook: () => void): void {
|
|
138
|
+
afterCommitHooks.push(hook);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function sameTags(
|
|
142
|
+
a: readonly number[] | undefined,
|
|
143
|
+
b: readonly number[],
|
|
144
|
+
): boolean {
|
|
145
|
+
if (!a || a.length !== b.length) return false;
|
|
146
|
+
for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
type Bytes = Array<[number, Uint8Array]>;
|
|
151
|
+
|
|
152
|
+
// Only nodes the commit reaches hand their bytes over: the host drops them
|
|
153
|
+
// with the node, so bytes of a node it never saw would stay behind.
|
|
154
|
+
function takeBytes(node: NativeNode, bytes: Bytes): void {
|
|
155
|
+
if (node.pendingBytes) {
|
|
156
|
+
bytes.push([node.tag, node.pendingBytes]);
|
|
157
|
+
node.pendingBytes = null;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* What a commit settled in the JS tree before the host answered, so that a
|
|
163
|
+
* batch the host refuses is sent again rather than taken for applied.
|
|
164
|
+
*/
|
|
165
|
+
interface Sent {
|
|
166
|
+
/** Nodes this batch created. */
|
|
167
|
+
created: NativeNode[];
|
|
168
|
+
/** Patches this batch carried, by node. */
|
|
169
|
+
patched: Array<[NativeNode, Props]>;
|
|
170
|
+
/** Existing nodes whose children this batch walked. */
|
|
171
|
+
walked: NativeNode[];
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function visit(
|
|
175
|
+
node: NativeNode,
|
|
176
|
+
ops: unknown[],
|
|
177
|
+
bytes: Bytes,
|
|
178
|
+
sent: Sent,
|
|
179
|
+
): void {
|
|
180
|
+
takeBytes(node, bytes);
|
|
181
|
+
if (!created.has(node)) {
|
|
182
|
+
ops.push(["c", node.tag, node.viewName, node.props]);
|
|
183
|
+
created.add(node);
|
|
184
|
+
byTag.set(node.tag, node);
|
|
185
|
+
sent.created.push(node);
|
|
186
|
+
node.pendingProps = null;
|
|
187
|
+
const kids = node.flatChildren();
|
|
188
|
+
for (const kid of kids) visit(kid, ops, bytes, sent);
|
|
189
|
+
const tags = kids.map((kid) => kid.tag);
|
|
190
|
+
if (tags.length > 0) ops.push(["k", node.tag, tags]);
|
|
191
|
+
lastChildren.set(node, tags);
|
|
192
|
+
node.childrenDirty = false;
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
if (node.pendingProps) {
|
|
196
|
+
ops.push(["u", node.tag, node.pendingProps]);
|
|
197
|
+
sent.patched.push([node, node.pendingProps]);
|
|
198
|
+
node.pendingProps = null;
|
|
199
|
+
}
|
|
200
|
+
if (node.childrenDirty) {
|
|
201
|
+
sent.walked.push(node);
|
|
202
|
+
const kids = node.flatChildren();
|
|
203
|
+
for (const kid of kids) visit(kid, ops, bytes, sent);
|
|
204
|
+
const tags = kids.map((kid) => kid.tag);
|
|
205
|
+
if (!sameTags(lastChildren.get(node), tags)) {
|
|
206
|
+
ops.push(["k", node.tag, tags]);
|
|
207
|
+
lastChildren.set(node, tags);
|
|
208
|
+
}
|
|
209
|
+
node.childrenDirty = false;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* The host refused the batch: nothing it carried counts as applied. Created
|
|
215
|
+
* nodes are created again, patches sent again, and every children list the
|
|
216
|
+
* batch walked is sent again whole — the host may have applied part of the
|
|
217
|
+
* batch before it refused, so a list it holds is not known any more.
|
|
218
|
+
*/
|
|
219
|
+
function unsend(sent: Sent): void {
|
|
220
|
+
for (const node of sent.created) {
|
|
221
|
+
created.delete(node);
|
|
222
|
+
byTag.delete(node.tag);
|
|
223
|
+
lastChildren.delete(node);
|
|
224
|
+
node.childrenDirty = true;
|
|
225
|
+
}
|
|
226
|
+
for (const [node, patch] of sent.patched) {
|
|
227
|
+
node.pendingProps = node.pendingProps
|
|
228
|
+
? Object.assign(patch, node.pendingProps)
|
|
229
|
+
: patch;
|
|
230
|
+
}
|
|
231
|
+
for (const node of sent.walked) {
|
|
232
|
+
lastChildren.delete(node);
|
|
233
|
+
node.childrenDirty = true;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export class Root {
|
|
238
|
+
readonly #host: OrbitHost;
|
|
239
|
+
readonly #tree: NativeNode;
|
|
240
|
+
#scheduled = false;
|
|
241
|
+
#rootSent = false;
|
|
242
|
+
/** Every commit's report, newest last (development and benchmarks). */
|
|
243
|
+
readonly reports: OrbitReport[] = [];
|
|
244
|
+
|
|
245
|
+
constructor(host: OrbitHost, tree: NativeNode) {
|
|
246
|
+
this.#host = host;
|
|
247
|
+
this.#tree = tree;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
schedule(): void {
|
|
251
|
+
if (this.#scheduled) return;
|
|
252
|
+
this.#scheduled = true;
|
|
253
|
+
void Promise.resolve().then(() => {
|
|
254
|
+
// A synchronous flush() may already have committed this batch.
|
|
255
|
+
if (this.#scheduled) this.commitOrReport();
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** Commit what changed now, instead of at the end of the turn. */
|
|
260
|
+
flush(): OrbitReport {
|
|
261
|
+
return this.commit();
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* A commit nobody waits on (scheduled, or after the window resized): a
|
|
266
|
+
* refusal goes to `mount`'s `onError` instead of out of a microtask or a
|
|
267
|
+
* native event. What the batch carried is sent again with the next commit.
|
|
268
|
+
*/
|
|
269
|
+
commitOrReport(): void {
|
|
270
|
+
try {
|
|
271
|
+
this.commit();
|
|
272
|
+
} catch (error) {
|
|
273
|
+
report(
|
|
274
|
+
error instanceof Error
|
|
275
|
+
? error
|
|
276
|
+
: new E_ORBIT_COMMIT_FAILED([String(error)]),
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
commit(): OrbitReport {
|
|
282
|
+
this.#scheduled = false;
|
|
283
|
+
const ops: unknown[] = [];
|
|
284
|
+
const bytes: Bytes = [];
|
|
285
|
+
const sent: Sent = { created: [], patched: [], walked: [] };
|
|
286
|
+
visit(this.#tree, ops, bytes, sent);
|
|
287
|
+
const rootSent = this.#rootSent;
|
|
288
|
+
if (!rootSent) ops.push(["r", this.#tree.tag]);
|
|
289
|
+
// Before the ops that show them: the host reads them when it mounts.
|
|
290
|
+
// Bytes stay with the host once handed over, refused batch or not.
|
|
291
|
+
for (const [tag, data] of bytes) this.#host.imageBytes(tag, data);
|
|
292
|
+
let answer: unknown;
|
|
293
|
+
try {
|
|
294
|
+
answer = JSON.parse(this.#host.commit(serialise(ops)));
|
|
295
|
+
} catch (error) {
|
|
296
|
+
unsend(sent);
|
|
297
|
+
throw refusal(error instanceof Error ? error.message : error, error);
|
|
298
|
+
}
|
|
299
|
+
if (!isReport(answer)) {
|
|
300
|
+
unsend(sent);
|
|
301
|
+
throw refusal(answer);
|
|
302
|
+
}
|
|
303
|
+
this.#rootSent = true;
|
|
304
|
+
for (const tag of answer.deleted) {
|
|
305
|
+
const node = byTag.get(tag);
|
|
306
|
+
if (!node) continue;
|
|
307
|
+
byTag.delete(tag);
|
|
308
|
+
created.delete(node);
|
|
309
|
+
lastChildren.delete(node);
|
|
310
|
+
}
|
|
311
|
+
for (const warning of answer.warnings ?? []) {
|
|
312
|
+
report(hostError(warning) ?? new E_ORBIT_SVG_VALUE([warning]));
|
|
313
|
+
}
|
|
314
|
+
this.reports.push(answer);
|
|
315
|
+
for (const hook of afterCommitHooks.splice(0)) hook();
|
|
316
|
+
for (const listener of [...commitListeners]) listener();
|
|
317
|
+
return answer;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
export interface MountOptions {
|
|
322
|
+
/** The root's size in dp (React Native: `Dimensions.get("window")`). */
|
|
323
|
+
viewport: { width: number; height: number };
|
|
324
|
+
/** Defaults to the host given to `useHost`. */
|
|
325
|
+
host?: OrbitHost;
|
|
326
|
+
/**
|
|
327
|
+
* Told of what orbit recovered from instead of throwing: a scheduled
|
|
328
|
+
* commit the host refused (`E_ORBIT_COMMIT_FAILED`, `E_ORBIT_TREE_TOO_DEEP`;
|
|
329
|
+
* the batch is sent again with the next commit), an image address from data
|
|
330
|
+
* orbit will not load (`E_ORBIT_INSECURE_IMAGE_URL`, `E_ORBIT_IMAGE_SOURCE`;
|
|
331
|
+
* that image shows nothing), a value of a drawing the host left out
|
|
332
|
+
* (`E_ORBIT_SVG_VALUE`). Default: `console.warn` in a development build
|
|
333
|
+
* (`__DEV__`), nothing in production.
|
|
334
|
+
*/
|
|
335
|
+
onError?: ErrorListener;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/** Mount a native tree; returns its root (`flush()` commits synchronously). */
|
|
339
|
+
export function mountTree(tree: NativeNode, options: MountOptions): Root {
|
|
340
|
+
const host = options.host ?? currentHost();
|
|
341
|
+
const root = new Root(host, tree);
|
|
342
|
+
mountedHost = host;
|
|
343
|
+
setDirtyListener(() => root.schedule());
|
|
344
|
+
host.setViewport(options.viewport.width, options.viewport.height);
|
|
345
|
+
host.setEventHandler((tag, type, x, y, data) => {
|
|
346
|
+
if (type === "topScroll") {
|
|
347
|
+
for (const listener of byTag.get(tag)?.scrollListeners ?? []) listener(y);
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
if (type === "topViewport") {
|
|
351
|
+
// The window resized (the keyboard came or went): lay out again.
|
|
352
|
+
host.setViewport(x, y);
|
|
353
|
+
viewportSize({ width: x, height: y });
|
|
354
|
+
root.commitOrReport();
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
const payload = {
|
|
358
|
+
pageX: x,
|
|
359
|
+
pageY: y,
|
|
360
|
+
locationX: x,
|
|
361
|
+
locationY: y,
|
|
362
|
+
text: data,
|
|
363
|
+
};
|
|
364
|
+
for (let node = byTag.get(tag) ?? null; node; node = node.parent) {
|
|
365
|
+
if (node.onEvent) {
|
|
366
|
+
node.onEvent(type, payload);
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
root.commit();
|
|
372
|
+
return root;
|
|
373
|
+
}
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Orbit's errors, one per code. Every failure carries a stable
|
|
3
|
+
* `E_ORBIT_<REASON>` code, so a caller branches on `error.code` or on
|
|
4
|
+
* `instanceof errors.E_ORBIT_…` rather than on a message.
|
|
5
|
+
*/
|
|
6
|
+
import { createError } from "@c9up/flare";
|
|
7
|
+
|
|
8
|
+
/** `mount` ran before `useHost` gave orbit a native host, or the module is not orbit's host. */
|
|
9
|
+
export const E_ORBIT_NO_HOST = createError<[string]>(
|
|
10
|
+
"[orbit] %s",
|
|
11
|
+
"E_ORBIT_NO_HOST",
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
/** The native host refused a commit; the message carries its report. */
|
|
15
|
+
export const E_ORBIT_COMMIT_FAILED = createError<[unknown]>(
|
|
16
|
+
"[orbit] the host refused a commit: %j",
|
|
17
|
+
"E_ORBIT_COMMIT_FAILED",
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The views of a screen nest deeper than the host lays out (100 levels): the
|
|
22
|
+
* layout recurses once per level on the JS thread's stack, so the host refuses
|
|
23
|
+
* the commit instead of overflowing it.
|
|
24
|
+
*/
|
|
25
|
+
export const E_ORBIT_TREE_TOO_DEEP = createError<[string]>(
|
|
26
|
+
"[orbit] %s",
|
|
27
|
+
"E_ORBIT_TREE_TOO_DEEP",
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* A value of an `<svg>` the host cannot read (a `NaN` length, unreadable path
|
|
32
|
+
* data, `points` or `transform`): that part of the drawing is left out and the
|
|
33
|
+
* rest drawn. Reported to `mount`'s `onError`, never thrown.
|
|
34
|
+
*/
|
|
35
|
+
export const E_ORBIT_SVG_VALUE = createError<[string]>(
|
|
36
|
+
"[orbit] %s",
|
|
37
|
+
"E_ORBIT_SVG_VALUE",
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
/** An element with no native mapping yet (`<select>`, `<canvas>`, or `<text>` / a gradient inside an `<svg>`). */
|
|
41
|
+
export const E_ORBIT_UNSUPPORTED_ELEMENT = createError<[string, string]>(
|
|
42
|
+
"[orbit] <%s> %s",
|
|
43
|
+
"E_ORBIT_UNSUPPORTED_ELEMENT",
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
/** A block element inside text: native text holds only inline runs. */
|
|
47
|
+
export const E_ORBIT_BLOCK_IN_TEXT = createError<[string]>(
|
|
48
|
+
"[orbit] <%s> cannot sit inside text on native",
|
|
49
|
+
"E_ORBIT_BLOCK_IN_TEXT",
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
/** `motion=${…}` was not an object of motion values. The message is aurora's. */
|
|
53
|
+
export const E_ORBIT_INVALID_MOTION = createError<[string]>(
|
|
54
|
+
"%s",
|
|
55
|
+
"E_ORBIT_INVALID_MOTION",
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
/** `scroll-y=${…}` was not a motion value. */
|
|
59
|
+
export const E_ORBIT_INVALID_SCROLL_BINDING = createError(
|
|
60
|
+
"[orbit] scroll-y expects a motion value",
|
|
61
|
+
"E_ORBIT_INVALID_SCROLL_BINDING",
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
/** A colour orbit cannot read (named colours other than `transparent`, `hsl()`…). */
|
|
65
|
+
export const E_ORBIT_INVALID_COLOR = createError<[string]>(
|
|
66
|
+
'[orbit] cannot read the colour "%s"',
|
|
67
|
+
"E_ORBIT_INVALID_COLOR",
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
/** `<img src>` over plain http: images are fetched over https only. */
|
|
71
|
+
export const E_ORBIT_INSECURE_IMAGE_URL = createError<[string]>(
|
|
72
|
+
'[orbit] <img src="%s">: an image is fetched over https only',
|
|
73
|
+
"E_ORBIT_INSECURE_IMAGE_URL",
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
/** `<img src>` with a scheme that is not an image source (`data:`, `javascript:`…). */
|
|
77
|
+
export const E_ORBIT_IMAGE_SOURCE = createError<[string, string]>(
|
|
78
|
+
'[orbit] <img src="%s">: %s',
|
|
79
|
+
"E_ORBIT_IMAGE_SOURCE",
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
/** Two elements of an `each` list share a key: a key names one row. */
|
|
83
|
+
export const E_ORBIT_DUPLICATE_KEY = createError<[string]>(
|
|
84
|
+
"[orbit] each(): two elements share the key %s — a key names one row",
|
|
85
|
+
"E_ORBIT_DUPLICATE_KEY",
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
/** `useScreen()` in a component no screen of a stack built. */
|
|
89
|
+
export const E_ORBIT_NO_SCREEN = createError(
|
|
90
|
+
"[orbit] useScreen() outside a screen: it reads the screen a stack shows, from a component that screen built",
|
|
91
|
+
"E_ORBIT_NO_SCREEN",
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
/** One stack put in two places: a stack is shown once. */
|
|
95
|
+
export const E_ORBIT_STACK_SHOWN = createError(
|
|
96
|
+
"[orbit] a stack is shown in one place: this one is already in a template",
|
|
97
|
+
"E_ORBIT_STACK_SHOWN",
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
/** `useStyles` got something other than a table `ream css:compile` wrote. */
|
|
101
|
+
export const E_ORBIT_STYLE_TABLE = createError(
|
|
102
|
+
"[orbit] not a style table orbit reads (version 1, from ream css:compile)",
|
|
103
|
+
"E_ORBIT_STYLE_TABLE",
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
/** `serverPages({ server })` over plain http to a host that is not this machine or the emulator's. */
|
|
107
|
+
export const E_ORBIT_INSECURE_SERVER = createError<[string]>(
|
|
108
|
+
"[orbit] the server %s is not https (plain http only reaches this machine or the emulator's host)",
|
|
109
|
+
"E_ORBIT_INSECURE_SERVER",
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
/** The server named a page the app does not have (`serverPages({ pages })`). */
|
|
113
|
+
export const E_ORBIT_UNKNOWN_PAGE = createError<[string]>(
|
|
114
|
+
'[orbit] the server named the page "%s", which this app does not have',
|
|
115
|
+
"E_ORBIT_UNKNOWN_PAGE",
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
/** A server connection was asked for a path that resolves to another origin. */
|
|
119
|
+
export const E_ORBIT_FOREIGN_ORIGIN = createError<[string, string]>(
|
|
120
|
+
"[orbit] the connection to %s does not reach %s",
|
|
121
|
+
"E_ORBIT_FOREIGN_ORIGIN",
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
/** The server answered a JSON request with an empty non-2xx response. */
|
|
125
|
+
export const E_ORBIT_SERVER_REQUEST_FAILED = createError<[string, number]>(
|
|
126
|
+
"[orbit] %s answered HTTP %d",
|
|
127
|
+
"E_ORBIT_SERVER_REQUEST_FAILED",
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
/** Server events need an `XMLHttpRequest`, and the platform has none. */
|
|
131
|
+
export const E_ORBIT_NO_XHR = createError(
|
|
132
|
+
"[orbit] server events need XMLHttpRequest",
|
|
133
|
+
"E_ORBIT_NO_XHR",
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
/** An invariant inside orbit broke: a bug in orbit. The site says which one. */
|
|
137
|
+
export const E_ORBIT_INTERNAL = createError<[string]>(
|
|
138
|
+
"[orbit] %s",
|
|
139
|
+
"E_ORBIT_INTERNAL",
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
// The errors below are raised by orbit's stand-ins for the platform's `URL`
|
|
143
|
+
// and `TextDecoder`, which Hermes lacks. Code written against those APIs
|
|
144
|
+
// expects what the WHATWG specs raise — `new URL("bad")` throws a `TypeError`,
|
|
145
|
+
// an unknown encoding label a `RangeError` — so these stay native error
|
|
146
|
+
// classes, named like the platform's own (`name` is `TypeError` /
|
|
147
|
+
// `RangeError`), with the code added. They are not flare exceptions on
|
|
148
|
+
// purpose: a polyfill must not change what the API it replaces throws.
|
|
149
|
+
|
|
150
|
+
/** `URL.createObjectURL` with no platform `URL` to make blob URLs. */
|
|
151
|
+
export class E_ORBIT_OBJECT_URL_UNAVAILABLE extends TypeError {
|
|
152
|
+
static readonly code = "E_ORBIT_OBJECT_URL_UNAVAILABLE";
|
|
153
|
+
readonly code = "E_ORBIT_OBJECT_URL_UNAVAILABLE";
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** A URL (or its base) that does not parse. */
|
|
157
|
+
export class E_ORBIT_INVALID_URL extends TypeError {
|
|
158
|
+
static readonly code = "E_ORBIT_INVALID_URL";
|
|
159
|
+
readonly code = "E_ORBIT_INVALID_URL";
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** `TextDecoder` given an encoding label other than UTF-8's. */
|
|
163
|
+
export class E_ORBIT_UNSUPPORTED_ENCODING extends RangeError {
|
|
164
|
+
static readonly code = "E_ORBIT_UNSUPPORTED_ENCODING";
|
|
165
|
+
readonly code = "E_ORBIT_UNSUPPORTED_ENCODING";
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** `TextDecoder.decode` given something that is not an `ArrayBuffer` or a view on one. */
|
|
169
|
+
export class E_ORBIT_INVALID_DECODER_INPUT extends TypeError {
|
|
170
|
+
static readonly code = "E_ORBIT_INVALID_DECODER_INPUT";
|
|
171
|
+
readonly code = "E_ORBIT_INVALID_DECODER_INPUT";
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** A `fatal` `TextDecoder` met bytes that are not valid UTF-8. */
|
|
175
|
+
export class E_ORBIT_INVALID_ENCODED_DATA extends TypeError {
|
|
176
|
+
static readonly code = "E_ORBIT_INVALID_ENCODED_DATA";
|
|
177
|
+
readonly code = "E_ORBIT_INVALID_ENCODED_DATA";
|
|
178
|
+
}
|
package/src/host.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { E_ORBIT_NO_HOST } from "./errors.js";
|
|
2
|
+
import { useNativeMotion } from "./motion.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* What orbit needs from the native side: the Rust host behind JSI (the
|
|
6
|
+
* `NativeOrbit` TurboModule, see `specs/NativeOrbit.ts`), or a fake in tests.
|
|
7
|
+
* Structural on purpose: orbit's core imports nothing from React Native.
|
|
8
|
+
*/
|
|
9
|
+
export interface OrbitHost {
|
|
10
|
+
/** The root's size in dp. */
|
|
11
|
+
setViewport(width: number, height: number): void;
|
|
12
|
+
/** Apply a JSON batch of tree operations; returns the host's JSON report. */
|
|
13
|
+
commit(ops: string): string;
|
|
14
|
+
/**
|
|
15
|
+
* The bytes an image node shows (`<img src=${bytes}>`): copied into the
|
|
16
|
+
* host's memory, never to disk, and dropped (zeroed) with the node.
|
|
17
|
+
*/
|
|
18
|
+
imageBytes(tag: number, bytes: Uint8Array): void;
|
|
19
|
+
/**
|
|
20
|
+
* Where nodes were laid out: a JSON array of tags in, a JSON array of
|
|
21
|
+
* `[x, y, width, height]` (dp, the root's coordinates, scroll offsets left
|
|
22
|
+
* out) or null per tag out.
|
|
23
|
+
*/
|
|
24
|
+
layout(tags: string): string;
|
|
25
|
+
/** Scroll a scroll view by `by` dp, or to its end. */
|
|
26
|
+
scroll(tag: number, by: number, toEnd: boolean): void;
|
|
27
|
+
/**
|
|
28
|
+
* Events from host views: touches (React Native's touch event types, in dp),
|
|
29
|
+
* text fields (`topChange`, `topFocus`, `topBlur`, `topSubmit`, with the
|
|
30
|
+
* text in `data`), `topScroll` from a scroll view with its offset in `y`
|
|
31
|
+
* (dp), and `topViewport` on tag 0 when the window resized (the keyboard),
|
|
32
|
+
* with `x`/`y` the new size in dp.
|
|
33
|
+
*/
|
|
34
|
+
setEventHandler(
|
|
35
|
+
handler: (
|
|
36
|
+
tag: number,
|
|
37
|
+
type: string,
|
|
38
|
+
x: number,
|
|
39
|
+
y: number,
|
|
40
|
+
data: string,
|
|
41
|
+
) => void,
|
|
42
|
+
): void;
|
|
43
|
+
motionValue(tag: number, initial: number): void;
|
|
44
|
+
motionInterpolate(
|
|
45
|
+
tag: number,
|
|
46
|
+
input: number,
|
|
47
|
+
from: readonly number[],
|
|
48
|
+
to: readonly number[],
|
|
49
|
+
): void;
|
|
50
|
+
motionSet(tag: number, value: number): void;
|
|
51
|
+
motionStop(tag: number): void;
|
|
52
|
+
motionFrames(
|
|
53
|
+
tag: number,
|
|
54
|
+
frames: readonly number[],
|
|
55
|
+
to: number,
|
|
56
|
+
loop: boolean,
|
|
57
|
+
): void;
|
|
58
|
+
motionSpring(
|
|
59
|
+
tag: number,
|
|
60
|
+
to: number,
|
|
61
|
+
stiffness: number,
|
|
62
|
+
damping: number,
|
|
63
|
+
mass: number,
|
|
64
|
+
): void;
|
|
65
|
+
/** Forget a motion value or interpolation, its animation and its bindings. */
|
|
66
|
+
motionDispose(tag: number): void;
|
|
67
|
+
/** prop: 0 opacity, 1 translateX, 2 translateY, 3 scale, 4 visible (> 0.5). */
|
|
68
|
+
motionBind(view: number, prop: number, node: number): void;
|
|
69
|
+
motionBindScroll(scrollView: number, value: number): void;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The TurboModule name the native side registers. */
|
|
73
|
+
export const NATIVE_MODULE_NAME = "NativeOrbit";
|
|
74
|
+
|
|
75
|
+
let host: OrbitHost | null = null;
|
|
76
|
+
|
|
77
|
+
/** Give orbit its native host, once, before `mount`. */
|
|
78
|
+
export function useHost(next: OrbitHost): void {
|
|
79
|
+
host = next;
|
|
80
|
+
// aurora's motion values made from now on live in this host.
|
|
81
|
+
useNativeMotion();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function currentHost(): OrbitHost {
|
|
85
|
+
if (host === null) {
|
|
86
|
+
throw new E_ORBIT_NO_HOST([
|
|
87
|
+
`no native host: call useHost(TurboModuleRegistry.getEnforcing("${NATIVE_MODULE_NAME}")) before mount()`,
|
|
88
|
+
]);
|
|
89
|
+
}
|
|
90
|
+
return host;
|
|
91
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@c9up/orbit` — aurora components as native views on a phone.
|
|
3
|
+
*
|
|
4
|
+
* The JS side keeps a tree of native nodes that aurora's signals patch; each
|
|
5
|
+
* commit goes over JSI to a Rust host that lays it out (taffy), diffs it and
|
|
6
|
+
* hands mount operations to the platform's views. Animations and scroll-driven
|
|
7
|
+
* values run on the UI thread, without JS.
|
|
8
|
+
*
|
|
9
|
+
* useHost(TurboModuleRegistry.getEnforcing("NativeOrbit"))
|
|
10
|
+
* mount(App(), { viewport: Dimensions.get("window") })
|
|
11
|
+
*
|
|
12
|
+
* Screens: `createStack(Home)` in the root, `useScreen().push(Detail, props)`
|
|
13
|
+
* in a screen; the Android back button pops (see `@c9up/orbit/react-native`).
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export { colorToArgb } from "./color.js";
|
|
17
|
+
export {
|
|
18
|
+
afterCommit,
|
|
19
|
+
type MountOptions,
|
|
20
|
+
type OrbitReport,
|
|
21
|
+
type Root,
|
|
22
|
+
} from "./commit.js";
|
|
23
|
+
export * as errors from "./errors.js";
|
|
24
|
+
export { NATIVE_MODULE_NAME, type OrbitHost, useHost } from "./host.js";
|
|
25
|
+
export {
|
|
26
|
+
bindMotion,
|
|
27
|
+
bindScrollY,
|
|
28
|
+
MotionNode,
|
|
29
|
+
type MotionStyle,
|
|
30
|
+
MotionValue,
|
|
31
|
+
type SpringOptions,
|
|
32
|
+
type TimingOptions,
|
|
33
|
+
} from "./motion.js";
|
|
34
|
+
/** The app's ream server, one connection for pages, comet and relay. */
|
|
35
|
+
export {
|
|
36
|
+
type ConnectOptions,
|
|
37
|
+
connectServer,
|
|
38
|
+
type ServerConnection,
|
|
39
|
+
type ServerEvent,
|
|
40
|
+
ServerEventStream,
|
|
41
|
+
type TokenStore,
|
|
42
|
+
} from "./server.js";
|
|
43
|
+
export {
|
|
44
|
+
createStack,
|
|
45
|
+
/** What the back button does: pop the innermost shown stack (false: nothing to pop). */
|
|
46
|
+
goBack,
|
|
47
|
+
type Navigator,
|
|
48
|
+
type Screen,
|
|
49
|
+
type ScreenComponent,
|
|
50
|
+
type Stack,
|
|
51
|
+
type StackOptions,
|
|
52
|
+
useScreen,
|
|
53
|
+
} from "./stack.js";
|
|
54
|
+
export {
|
|
55
|
+
colorScheme,
|
|
56
|
+
type StyleTable,
|
|
57
|
+
unknownClasses,
|
|
58
|
+
useStyles,
|
|
59
|
+
} from "./styles.js";
|
|
60
|
+
export { type GestureEvent, mount } from "./template.js";
|
|
61
|
+
/** A standard UTF-8 `TextDecoder` where the engine has none (Hermes). */
|
|
62
|
+
export { installTextDecoder, StandardTextDecoder } from "./textDecoder.js";
|
|
63
|
+
/** A standard `URL` in place of the platform's, when it is not one (React Native's). */
|
|
64
|
+
export { installStandardUrl, StandardURL } from "./url.js";
|