-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathshared.tsx
More file actions
135 lines (119 loc) · 2.74 KB
/
shared.tsx
File metadata and controls
135 lines (119 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { createComputed, $PROXY } from "solid-js";
import { createStore, produce } from "solid-js/store";
export type WsMessage<T> = T & { id: string };
export type WsMessageUp<I = any> =
| {
type: "create";
name: string;
input?: I;
}
| {
type: "subscribe";
ref: SerializedMemo;
path?: undefined;
}
| {
type: "subscribe";
ref: SerializedProjection | SerializedStoreAccessor;
path: string;
}
| {
type: "dispose";
}
| {
type: "invoke";
ref: SerializedRef;
input?: I;
}
| {
type: "value";
value: I;
};
export type WsMessageDown<T> =
| {
type: "value";
value: T;
}
| {
type: "subscribe";
ref: SerializedMemo;
}
| {
type: "subscribe";
ref: SerializedProjection;
path: string;
};
export type SerializedRef<I = any, O = any> = {
__type: "ref";
name: string;
scope: string;
};
export type SerializedMemo<O = any> = {
__type: "memo";
name: string;
scope: string;
initial?: O;
};
export type SerializedProjection<O = any> = {
__type: "projection";
name: string;
scope: string;
initial?: O;
};
export type SerializedStoreAccessor<O = any> = {
__type: "store-accessor";
name: string;
scope: string;
initial?: O;
};
export type SerializedReactiveThing<T = any> =
| SerializedMemo<T>
| SerializedProjection<T>
| SerializedStoreAccessor<T>;
export type SerializedThing<T = any> =
| SerializedRef<T>
| SerializedReactiveThing<T>;
export type SerializedStream<O = any> = {
__type: "stream";
name: string;
scope: string;
value: O;
};
export function createSeriazliedMemo(
opts: Omit<SerializedMemo, "__type">
): SerializedMemo {
return { ...opts, __type: "memo" };
}
export function createSeriazliedProjection(
opts: Omit<SerializedProjection, "__type">
): SerializedProjection {
return { ...opts, __type: "projection" };
}
export function createSeriazliedStore(
opts: Omit<SerializedStoreAccessor, "__type">
): SerializedStoreAccessor {
return { ...opts, __type: "store-accessor" };
}
export function createSocketMemo<T>(source: () => T): () => T | undefined {
// @ts-expect-error
source.type = "memo";
return source;
}
export function createSocketProjection<T extends object = {}>(
storeOrMutation: (draft: T) => void,
init?: T
): T | undefined {
// @ts-expect-error
const [store, setStore] = createStore<T>(init || {});
createComputed(() => setStore(produce(storeOrMutation)));
// @ts-expect-error
store.type = "projection";
return store;
}
export function createSocketStore<T extends object = {}>(
storeAccessor: () => T
): T | undefined {
// @ts-expect-error
storeAccessor.type = "store-accessor";
return storeAccessor as any;
}