@typed/template 0.12.0 → 0.13.0
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/compiler-tools/package.json +6 -0
- package/dist/cjs/Html.js +1 -1
- package/dist/cjs/Html.js.map +1 -1
- package/dist/cjs/Hydrate.js +0 -14
- package/dist/cjs/Hydrate.js.map +1 -1
- package/dist/cjs/Template.js.map +1 -1
- package/dist/cjs/Test.js +1 -1
- package/dist/cjs/Test.js.map +1 -1
- package/dist/cjs/compiler-tools.js +100 -0
- package/dist/cjs/compiler-tools.js.map +1 -0
- package/dist/cjs/internal/HydrateContext.js.map +1 -1
- package/dist/cjs/internal/v2/render.js +241 -66
- package/dist/cjs/internal/v2/render.js.map +1 -1
- package/dist/dts/Hydrate.d.ts +2 -9
- package/dist/dts/Hydrate.d.ts.map +1 -1
- package/dist/dts/Template.d.ts +3 -3
- package/dist/dts/Template.d.ts.map +1 -1
- package/dist/dts/compiler-tools.d.ts +143 -0
- package/dist/dts/compiler-tools.d.ts.map +1 -0
- package/dist/dts/internal/v2/render.d.ts +31 -10
- package/dist/dts/internal/v2/render.d.ts.map +1 -1
- package/dist/esm/Html.js +2 -2
- package/dist/esm/Html.js.map +1 -1
- package/dist/esm/Hydrate.js +0 -12
- package/dist/esm/Hydrate.js.map +1 -1
- package/dist/esm/Template.js.map +1 -1
- package/dist/esm/Test.js +2 -2
- package/dist/esm/Test.js.map +1 -1
- package/dist/esm/compiler-tools.js +91 -0
- package/dist/esm/compiler-tools.js.map +1 -0
- package/dist/esm/internal/HydrateContext.js.map +1 -1
- package/dist/esm/internal/v2/render.js +231 -63
- package/dist/esm/internal/v2/render.js.map +1 -1
- package/package.json +16 -8
- package/src/Html.ts +2 -2
- package/src/Hydrate.ts +2 -37
- package/src/Template.ts +4 -2
- package/src/Test.ts +2 -2
- package/src/compiler-tools.ts +250 -0
- package/src/internal/HydrateContext.ts +0 -2
- package/src/internal/v2/render.ts +279 -54
- package/dist/cjs/internal/v2/hydrate.js +0 -202
- package/dist/cjs/internal/v2/hydrate.js.map +0 -1
- package/dist/dts/internal/v2/hydrate.d.ts +0 -7
- package/dist/dts/internal/v2/hydrate.d.ts.map +0 -1
- package/dist/esm/internal/v2/hydrate.js +0 -195
- package/dist/esm/internal/v2/hydrate.js.map +0 -1
- package/src/internal/v2/hydrate.ts +0 -289
|
@@ -1,289 +0,0 @@
|
|
|
1
|
-
import * as Fx from "@typed/fx"
|
|
2
|
-
import { isText } from "@typed/wire"
|
|
3
|
-
import type * as Chunk from "effect/Chunk"
|
|
4
|
-
import { unsafeGet } from "effect/Context"
|
|
5
|
-
import * as Effect from "effect/Effect"
|
|
6
|
-
import { flow } from "effect/Function"
|
|
7
|
-
import * as Scope from "effect/Scope"
|
|
8
|
-
import type { BrowserEntry } from "../../Entry.js"
|
|
9
|
-
import type { Placeholder } from "../../Placeholder.js"
|
|
10
|
-
import type { Renderable } from "../../Renderable.js"
|
|
11
|
-
import type { RenderContext } from "../../RenderContext.js"
|
|
12
|
-
import { DomRenderEvent, type RenderEvent } from "../../RenderEvent.js"
|
|
13
|
-
import type { RenderQueue } from "../../RenderQueue.js"
|
|
14
|
-
import type { RenderTemplate } from "../../RenderTemplate.js"
|
|
15
|
-
import type * as Template from "../../Template.js"
|
|
16
|
-
import { CouldNotFindCommentError, isHydrationError } from "../errors.js"
|
|
17
|
-
import { HydrateContext } from "../HydrateContext.js"
|
|
18
|
-
import { findHydratePath, isCommentWithValue, type ParentChildNodes } from "../utils.js"
|
|
19
|
-
import type { HydrationHole, HydrationNode, HydrationTemplate } from "./hydration-template.js"
|
|
20
|
-
import {
|
|
21
|
-
findHydrationHole,
|
|
22
|
-
findHydrationMany,
|
|
23
|
-
findHydrationTemplate,
|
|
24
|
-
getChildNodes,
|
|
25
|
-
getNodes,
|
|
26
|
-
getPreviousNodes
|
|
27
|
-
} from "./hydration-template.js"
|
|
28
|
-
import { getBrowserEntry } from "./render-entry.js"
|
|
29
|
-
import type { TemplateContext } from "./render.js"
|
|
30
|
-
import {
|
|
31
|
-
makeTemplateContext,
|
|
32
|
-
renderTemplate,
|
|
33
|
-
setupAttrPart,
|
|
34
|
-
setupBooleanPart,
|
|
35
|
-
setupClassNamePart,
|
|
36
|
-
setupCommentPart,
|
|
37
|
-
setupDataPart,
|
|
38
|
-
setupEventPart,
|
|
39
|
-
setupNodePart,
|
|
40
|
-
setupPropertiesPart,
|
|
41
|
-
setupPropertyPart,
|
|
42
|
-
setupRefPart,
|
|
43
|
-
setupSparseAttrPart,
|
|
44
|
-
setupSparseClassNamePart,
|
|
45
|
-
setupSparseCommentPart,
|
|
46
|
-
setupTextPart
|
|
47
|
-
} from "./render.js"
|
|
48
|
-
|
|
49
|
-
type HydrateTemplateContext = TemplateContext & {
|
|
50
|
-
readonly where: HydrationNode
|
|
51
|
-
readonly manyKey: string | undefined
|
|
52
|
-
readonly makeHydrateContext: (where: HydrationNode, index: number) => HydrateContext
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function findWhere(hydrateCtx: HydrateContext, entry: BrowserEntry): HydrationTemplate | null {
|
|
56
|
-
// If there is not a manyKey, we can just find the template by its hash
|
|
57
|
-
if (hydrateCtx.manyKey === undefined) {
|
|
58
|
-
return findHydrationTemplate(getChildNodes(hydrateCtx.where), entry.template.hash)
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// If there is a manyKey, we need to find the many node first
|
|
62
|
-
const many = findHydrationMany(getChildNodes(hydrateCtx.where), hydrateCtx.manyKey)
|
|
63
|
-
|
|
64
|
-
if (many === null) return null
|
|
65
|
-
|
|
66
|
-
// Then we can find the template by its hash
|
|
67
|
-
return findHydrationTemplate(getChildNodes(many), entry.template.hash)
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export const hydrateTemplate: (document: Document, ctx: RenderContext) => RenderTemplate = (
|
|
71
|
-
document,
|
|
72
|
-
renderContext
|
|
73
|
-
) => {
|
|
74
|
-
const render_ = renderTemplate(document, renderContext)
|
|
75
|
-
|
|
76
|
-
return <Values extends ReadonlyArray<Renderable<any, any>>>(
|
|
77
|
-
templateStrings: TemplateStringsArray,
|
|
78
|
-
values: Values
|
|
79
|
-
): Fx.Fx<
|
|
80
|
-
RenderEvent,
|
|
81
|
-
Placeholder.Error<Values[number]>,
|
|
82
|
-
Scope.Scope | RenderQueue | Placeholder.Context<Values[number]>
|
|
83
|
-
> => {
|
|
84
|
-
const entry = getBrowserEntry(document, renderContext, templateStrings)
|
|
85
|
-
|
|
86
|
-
return Fx.make((sink) =>
|
|
87
|
-
Effect.catchAllDefect(
|
|
88
|
-
Effect.gen(function*() {
|
|
89
|
-
const ctx = yield* makeTemplateContext(entry.content, document, renderContext, values, sink.onFailure)
|
|
90
|
-
const hydrateCtx = unsafeGet(ctx.context, HydrateContext)
|
|
91
|
-
|
|
92
|
-
// If we're not longer hydrating, just render normally
|
|
93
|
-
if (hydrateCtx.hydrate === false) {
|
|
94
|
-
return yield* render_(templateStrings, values).run(sink)
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const where: HydrationTemplate | null = findWhere(hydrateCtx, entry)
|
|
98
|
-
|
|
99
|
-
if (where === null) {
|
|
100
|
-
hydrateCtx.hydrate = false
|
|
101
|
-
return yield* render_(templateStrings, values).run(sink)
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const wire = getWire(where)
|
|
105
|
-
if (entry.template.parts.length === 0) return yield* sink.onSuccess(DomRenderEvent(wire))
|
|
106
|
-
|
|
107
|
-
const makeHydrateContext = (where: HydrationNode): HydrateContext => ({
|
|
108
|
-
where,
|
|
109
|
-
parentTemplate: entry.template,
|
|
110
|
-
hydrate: true
|
|
111
|
-
})
|
|
112
|
-
|
|
113
|
-
const effects = setupParts(entry.template.parts, {
|
|
114
|
-
...ctx,
|
|
115
|
-
where,
|
|
116
|
-
manyKey: hydrateCtx.manyKey,
|
|
117
|
-
makeHydrateContext
|
|
118
|
-
})
|
|
119
|
-
if (effects.length > 0) {
|
|
120
|
-
yield* Effect.forEach(effects, flow(Effect.catchAllCause(ctx.onCause), Effect.forkIn(ctx.scope)))
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// Setup our event listeners for our wire.
|
|
124
|
-
// We use the parentScope to allow event listeners to exist
|
|
125
|
-
// beyond the lifetime of the current Fiber, but no further than its parent template.
|
|
126
|
-
yield* ctx.eventSource.setup(wire, ctx.parentScope)
|
|
127
|
-
|
|
128
|
-
// We're done setting up so we can resume in "regular" rendering mode when
|
|
129
|
-
// and new templates come in.
|
|
130
|
-
hydrateCtx.hydrate = false
|
|
131
|
-
|
|
132
|
-
// Emit our DomRenderEvent
|
|
133
|
-
yield* sink.onSuccess(DomRenderEvent(wire)).pipe(
|
|
134
|
-
// Ensure our templates last forever in the DOM environment
|
|
135
|
-
// so event listeners are kept attached to the current Scope.
|
|
136
|
-
Effect.zipRight(Effect.never),
|
|
137
|
-
// Close our scope whenever the current Fiber is interrupted
|
|
138
|
-
Effect.onExit((exit) => Scope.close(ctx.scope, exit))
|
|
139
|
-
)
|
|
140
|
-
}),
|
|
141
|
-
(defect) =>
|
|
142
|
-
Effect.gen(function*(_) {
|
|
143
|
-
if (isHydrationError(defect)) {
|
|
144
|
-
yield* _(Effect.logError(defect))
|
|
145
|
-
const hydrateCtx = yield* _(HydrateContext)
|
|
146
|
-
hydrateCtx.hydrate = false
|
|
147
|
-
return yield* _(render_(templateStrings, values).run(sink))
|
|
148
|
-
} else {
|
|
149
|
-
return yield* _(Effect.die(defect))
|
|
150
|
-
}
|
|
151
|
-
})
|
|
152
|
-
)
|
|
153
|
-
)
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
function setupParts(parts: Template.Template["parts"], ctx: HydrateTemplateContext) {
|
|
158
|
-
const effects: Array<Effect.Effect<void, any, any>> = []
|
|
159
|
-
|
|
160
|
-
for (const [part, path] of parts) {
|
|
161
|
-
const effect = setupPart(part, path, ctx)
|
|
162
|
-
if (effect) {
|
|
163
|
-
effects.push(effect)
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
return effects
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
function setupPart(
|
|
171
|
-
part: Template.PartNode | Template.SparsePartNode,
|
|
172
|
-
path: Chunk.Chunk<number>,
|
|
173
|
-
ctx: HydrateTemplateContext
|
|
174
|
-
) {
|
|
175
|
-
switch (part._tag) {
|
|
176
|
-
case "attr":
|
|
177
|
-
return setupAttrPart(part, findHydratePath(ctx.where, path) as any, ctx, ctx.values[part.index])
|
|
178
|
-
case "boolean-part":
|
|
179
|
-
return setupBooleanPart(part, findHydratePath(ctx.where, path) as any, ctx, ctx.values[part.index])
|
|
180
|
-
case "className-part":
|
|
181
|
-
return setupClassNamePart(part, findHydratePath(ctx.where, path) as any, ctx, ctx.values[part.index])
|
|
182
|
-
case "comment-part":
|
|
183
|
-
return setupCommentPart(part, findHydratePath(ctx.where, path) as any, ctx)
|
|
184
|
-
case "data":
|
|
185
|
-
return setupDataPart(part, findHydratePath(ctx.where, path) as any, ctx, ctx.values[part.index])
|
|
186
|
-
case "event":
|
|
187
|
-
return setupEventPart(part, findHydratePath(ctx.where, path) as any, ctx, ctx.values[part.index])
|
|
188
|
-
case "node": {
|
|
189
|
-
const hole = findHydrationHole(getChildNodes(ctx.where), part.index)
|
|
190
|
-
if (hole === null) {
|
|
191
|
-
throw new CouldNotFindCommentError(part.index)
|
|
192
|
-
}
|
|
193
|
-
return setupHydratedNodePart(part, hole, ctx)
|
|
194
|
-
}
|
|
195
|
-
case "properties":
|
|
196
|
-
return setupPropertiesPart(part, findHydratePath(ctx.where, path) as any, ctx)
|
|
197
|
-
case "property":
|
|
198
|
-
return setupPropertyPart(part, findHydratePath(ctx.where, path) as any, ctx, ctx.values[part.index])
|
|
199
|
-
case "ref":
|
|
200
|
-
return setupRefPart(part, findHydratePath(ctx.where, path) as any, ctx)
|
|
201
|
-
case "sparse-attr":
|
|
202
|
-
return setupSparseAttrPart(part, findHydratePath(ctx.where, path) as any, ctx)
|
|
203
|
-
case "sparse-class-name":
|
|
204
|
-
return setupSparseClassNamePart(part, findHydratePath(ctx.where, path) as any, ctx)
|
|
205
|
-
case "sparse-comment":
|
|
206
|
-
return setupSparseCommentPart(part, findHydratePath(ctx.where, path) as any, ctx)
|
|
207
|
-
case "text-part": {
|
|
208
|
-
const hole = findHydrationHole(getChildNodes(ctx.where), part.index)
|
|
209
|
-
if (hole === null) throw new CouldNotFindCommentError(part.index)
|
|
210
|
-
return setupTextPart(part, hole.endComment, ctx)
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
function setupHydratedNodePart(
|
|
216
|
-
part: Template.NodePart,
|
|
217
|
-
hole: HydrationHole,
|
|
218
|
-
ctx: HydrateTemplateContext
|
|
219
|
-
) {
|
|
220
|
-
const nestedCtx = ctx.makeHydrateContext(hole, part.index)
|
|
221
|
-
const previousNodes = getPreviousNodes(hole)
|
|
222
|
-
const text = previousNodes.length === 2 && isCommentWithValue(previousNodes[0], "text") && isText(previousNodes[1])
|
|
223
|
-
? previousNodes[1]
|
|
224
|
-
: null
|
|
225
|
-
const effect = setupNodePart(part, hole.endComment, ctx, text, text === null ? previousNodes : [text])
|
|
226
|
-
if (effect === null) return null
|
|
227
|
-
return Effect.provideService(effect, HydrateContext, nestedCtx)
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
export function findRootParentChildNodes(where: HTMLElement): ParentChildNodes {
|
|
231
|
-
const childNodes = findRootChildNodes(where)
|
|
232
|
-
|
|
233
|
-
return {
|
|
234
|
-
parentNode: where,
|
|
235
|
-
childNodes
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
const START = "typed-start"
|
|
240
|
-
const END = "typed-end"
|
|
241
|
-
|
|
242
|
-
// Finds all of the childNodes between the "typed-start" and "typed-end" comments
|
|
243
|
-
export function findRootChildNodes(where: HTMLElement): Array<Node> {
|
|
244
|
-
let start = -1
|
|
245
|
-
let end = -1
|
|
246
|
-
|
|
247
|
-
const { childNodes } = where
|
|
248
|
-
const length = childNodes.length
|
|
249
|
-
|
|
250
|
-
for (let i = 0; i < length; i++) {
|
|
251
|
-
const node = childNodes[i]
|
|
252
|
-
|
|
253
|
-
if (node.nodeType === node.COMMENT_NODE && node.nodeValue === START) {
|
|
254
|
-
start = i
|
|
255
|
-
break
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
for (let i = length - 1; i >= Math.max(start, 0); i--) {
|
|
260
|
-
const node = childNodes[i]
|
|
261
|
-
|
|
262
|
-
if (node.nodeType === node.COMMENT_NODE && node.nodeValue === END) {
|
|
263
|
-
end = i
|
|
264
|
-
break
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
// If we can't find the start and end comments, just return all childNodes
|
|
269
|
-
if (start === -1 && end === -1) {
|
|
270
|
-
return Array.from(childNodes)
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
start = start === -1 ? 0 : start
|
|
274
|
-
end = end === -1 ? length - 1 : end
|
|
275
|
-
|
|
276
|
-
const rootChildNodes: Array<Node> = Array(end - start)
|
|
277
|
-
|
|
278
|
-
for (let i = start + 1, j = 0; i <= end; i++) {
|
|
279
|
-
rootChildNodes[j++] = childNodes[i]
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
return rootChildNodes
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
function getWire(where: HydrationNode) {
|
|
286
|
-
const nodes = getNodes(where)
|
|
287
|
-
if (nodes.length === 1) return nodes[0]
|
|
288
|
-
return nodes
|
|
289
|
-
}
|