This repository was archived by the owner on Jan 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvnode.ts
More file actions
185 lines (173 loc) · 5.23 KB
/
vnode.ts
File metadata and controls
185 lines (173 loc) · 5.23 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { isSourceReference, SourceReference, MarshalledSourceReference } from "./source-reference";
import { isAsyncIterable, AsyncIterableLike, isIterable } from "iterable";
import { Fragment } from "./fragment";
/**
* Generic VNode, represents a virtual node within a state tree
*
* The VNode can be used to hydrate native state or external sources
*/
export interface VNode {
/**
* A unique reference to this {@link VNode}, this could be a globally unique symbol like {@link Fragment}
*/
reference: SourceReference;
/**
* An `AsyncIterable` that will return a `VNode[]` that represents a group of children updates
* Each iteration represents an update to the {@link VNode}'s children state
*/
children?: AsyncIterable<VNode[]>;
/**
* The resolved source for the {@link VNode}
*
* A {@link VContext} may choose to utilise an external value to represent the source
*/
source?: unknown;
/**
* The options provided to the {@link VNode} from the {@link createNode} function
*/
options?: object;
/**
* See {@link ScalarVNode}
*/
scalar?: boolean;
/**
* See {@link NativeVNode}
*/
native?: boolean;
}
/**
* A {@link VNode} that has been marshalled, allowing for transmission or storage of the VNode state
*
* Children can be represented as a synchronous iterable such as an array, which then utilises synchronous iterables
* for each update list of children
*/
export type MarshalledVNode = Omit<VNode, "children" | "reference"> & {
reference?: MarshalledSourceReference;
children: Iterable<Iterable<MarshalledVNode | MarshalledSourceReference>>;
};
/**
* A {@link VNode} that has a scalar {@link SourceReference} {@link VNode.source}
*
* A {@link ScalarVNode} can still have both {@link VNode.options} and {@link VNode.children}, so they should not be
* disregarded
*
* A {@link ScalarVNode} can be used to represent a {@link VNode} with no backing {@link VContext}, which can be
* picked up in a later external process
*/
export interface ScalarVNode extends VNode {
source: SourceReference;
scalar: true;
}
/**
* {@link VContext} should utilise {@link NativeVNode} to indicate that the {@link VNode} is backed by a native
* representation
*/
export interface NativeVNode extends VNode {
native: true;
}
/**
* A {@link FragmentVNode} indicates a {@link VNode} where the {@link FragmentVNode} should be ignored and its
* {@link VNode.children} should take its place
*
* A {@link FragmentVNode} may have no children, in which case, it should be ignored completely
*/
export interface FragmentVNode extends VNode {
reference: typeof Fragment;
}
/**
* A {@link VNode} that requires asynchronous resolution
*/
export type AsyncVNodeRepresentation = Promise<VNode> | AsyncIterable<VNode>;
/**
* A {@link VNode} that can be resolved synchronously
*/
export type SyncVNodeRepresentation = MarshalledVNode | SourceReference | VNode | Iterable<VNode>;
/**
* A {@link VNode} with requiring _either_ synchronous or asynchronous resolution
*/
export type VNodeRepresentation = AsyncVNodeRepresentation | SyncVNodeRepresentation;
/**
* A value that represents a {@link VNode}
*/
export type BasicVNodeRepresentation = VNodeRepresentation | AsyncIterableLike<VNodeRepresentation>;
/**
* A value that represents a {@link VNode}
*/
export type VNodeRepresentationSource = BasicVNodeRepresentation | AsyncIterableLike<BasicVNodeRepresentation>;
function isVNodeLike(value: unknown): value is Partial<VNode> {
return typeof value === "object" || typeof value === "function";
}
/**
* Indicates if a value is a {@link VNode}
* @param value
*/
export function isVNode(value: unknown): value is VNode {
return !!(
isVNodeLike(value) &&
isSourceReference(value.reference) &&
(
!value.children ||
isAsyncIterable(value.children)
) &&
(
!value.options ||
typeof value.options === "object"
)
);
}
/**
* Indicates if a value is a {@link NativeVNode}
* @param value
*/
export function isNativeVNode(value: unknown): value is NativeVNode {
function isNativeVNodeLike(value: unknown): value is VNode & { native?: unknown, source?: unknown } {
return isVNode(value);
}
return (
isNativeVNodeLike(value) &&
value.native === true
);
}
/**
* Indicates if a value is a {@link ScalarVNode}
* @param value
*/
export function isScalarVNode(value: unknown): value is ScalarVNode {
function isScalarVNodeLike(value: unknown): value is VNode & { scalar?: unknown } {
return isVNode(value);
}
return (
isScalarVNodeLike(value) &&
isSourceReference(value.source) &&
value.scalar === true
);
}
/**
* Indicates if a value is a {@link FragmentVNode}
* @param value
*/
export function isFragmentVNode(value: unknown): value is FragmentVNode {
return (
isVNode(value) &&
value.reference === Fragment
);
}
/**
* Indicates if a valid is a {@link MarshalledVNode}
* @param value
*/
export function isMarshalledVNode(value: unknown): value is MarshalledVNode {
return (
isVNodeLike(value) &&
(
!value.reference ||
isSourceReference(value.reference)
) &&
// If we don't have children, then we have a normal VNode
isIterable(value.children) &&
(
!value.options ||
typeof value.options === "object"
)
);
}