@jigs.design/runtime 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.
@@ -0,0 +1,132 @@
1
+ import { PickleErrorData, ProcedureEntry } from "./types.js";
2
+ //#region src/protocol.d.ts
3
+ /**
4
+ * Prop value on the wire. Functions never leave the worker; they travel as
5
+ * `{ fn }` ids. Lists of plain objects travel as `{ rows }`.
6
+ */
7
+ export type WireValue = boolean | number | string | {
8
+ fn: string;
9
+ } | {
10
+ rows: ReadonlyArray<Row>;
11
+ };
12
+ /** One row of a list prop: flat fields only. */
13
+ export type Row = Readonly<Record<string, boolean | number | string | null>>;
14
+ export type WireArg = boolean | number | string;
15
+ /** The props of one node on the wire. */
16
+ export type WireProps = Record<string, WireValue>;
17
+ /** UI operations produced by the worker's reconciler. The root container has id 0. */
18
+ export type Op = {
19
+ id: number;
20
+ op: "create";
21
+ props: WireProps;
22
+ type: string;
23
+ } | {
24
+ id: number;
25
+ op: "createText";
26
+ text: string;
27
+ } | {
28
+ id: number;
29
+ index: number;
30
+ op: "place";
31
+ parent: number;
32
+ } | {
33
+ id: number;
34
+ op: "remove";
35
+ } | {
36
+ id: number;
37
+ op: "set";
38
+ props: Record<string, WireValue | null>;
39
+ } | {
40
+ id: number;
41
+ op: "text";
42
+ text: string;
43
+ };
44
+ export interface RuntimeError {
45
+ line: number | null;
46
+ message: string;
47
+ }
48
+ /** A query as the program sees it from `useQuery`. */
49
+ export type QueryResult = {
50
+ data: null;
51
+ error: null;
52
+ status: "pending";
53
+ } | {
54
+ data: unknown;
55
+ error: null;
56
+ status: "success";
57
+ } | {
58
+ data: null;
59
+ error: PickleErrorData;
60
+ status: "error";
61
+ };
62
+ /** How a mutation settled. */
63
+ export type MutationResult = {
64
+ data: unknown;
65
+ status: "success";
66
+ } | {
67
+ error: PickleErrorData;
68
+ status: "error";
69
+ };
70
+ /** A query the program read during a render. `key` is stable for a name and input. */
71
+ export interface QueryRead {
72
+ input: unknown;
73
+ key: string;
74
+ name: string;
75
+ }
76
+ /** A `mutate()` call made inside a handler. */
77
+ export interface MutationCall {
78
+ id: number;
79
+ input: unknown;
80
+ name: string;
81
+ }
82
+ /** Page to worker. */
83
+ export type HostMessage = {
84
+ args: ReadonlyArray<WireArg>;
85
+ fn: string;
86
+ seq: number;
87
+ type: "call";
88
+ } | {
89
+ id: number;
90
+ result: MutationResult;
91
+ seq: number;
92
+ type: "mutation";
93
+ } | {
94
+ key: string;
95
+ result: QueryResult;
96
+ seq: number;
97
+ type: "query";
98
+ } | {
99
+ code: string;
100
+ /** Drop the state and mutation statuses of the program before. */
101
+ fresh: boolean;
102
+ procedures: Readonly<Record<string, ProcedureEntry>>;
103
+ prose: Readonly<Record<string, string>>;
104
+ seq: number;
105
+ type: "update";
106
+ };
107
+ /** Worker to page. */
108
+ export type WorkerMessage = {
109
+ errors: Array<RuntimeError>;
110
+ mutations: Array<MutationCall>;
111
+ ops: Array<Op>;
112
+ queries: Array<QueryRead>;
113
+ seq: number;
114
+ type: "result";
115
+ } | {
116
+ message: string;
117
+ seq: number;
118
+ type: "failed";
119
+ };
120
+ /** What the frame sends to the page. */
121
+ export type FrameMessage = WorkerMessage | {
122
+ seq: number;
123
+ type: "quarantined";
124
+ } | {
125
+ type: "ready";
126
+ } |
127
+ /** The frame's own security policy is not in force, so it will not run. */
128
+ {
129
+ type: "unprotected";
130
+ };
131
+ export declare const ROOT_ID = 0;
132
+ //#endregion
@@ -0,0 +1,4 @@
1
+ //#region src/protocol.ts
2
+ const ROOT_ID = 0;
3
+ //#endregion
4
+ export { ROOT_ID };
@@ -0,0 +1,21 @@
1
+ import { HostMessage, WorkerMessage } from "./protocol.js";
2
+ //#region src/runtime-core.d.ts
3
+ export interface Runtime {
4
+ handle: (message: HostMessage) => void;
5
+ }
6
+ /**
7
+ * The runtime that executes compiled programs inside the sandbox worker.
8
+ *
9
+ * It is shipped to the worker with `createRuntime.toString()`, so the body
10
+ * must not reference anything declared outside it (type imports are erased
11
+ * and fine). `FunctionCtor` is passed in because the worker removes dynamic
12
+ * code evaluation from its globals before any program runs.
13
+ *
14
+ * A small React-style reconciler: programs build a tree with `__pickle.jsx`,
15
+ * the runtime diffs it against the previous render by position and type, and
16
+ * posts the differences as `Op`s. Each render also reports the queries it read
17
+ * and the mutations its handlers called; the page runs those through the
18
+ * client and sends the results back.
19
+ */
20
+ export declare function createRuntime(post: (message: WorkerMessage) => void, FunctionCtor: FunctionConstructor): Runtime;
21
+ //#endregion