mirror of
https://github.com/Effect-TS/effect.git
synced 2026-12-04 03:35:36 +08:00
Advance keep-alive write round on outermost closure renders (#11211)
Co-authored-by: Mathieu Post <5338582+m9tdev@users.noreply.github.com>
This commit is contained in:
co-authored by
Mathieu Post
parent
5a5b54bb0b
commit
22564884ba
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"effect": patch
|
||||
---
|
||||
|
||||
Fix a crash in `effect/ui` when a headless component's view closure repeatedly moves a keyed `h.keepAlive` child without re-rendering the parent. The child now keeps its instance across moves.
|
||||
@@ -75,7 +75,6 @@ export class Table {
|
||||
}
|
||||
}
|
||||
}
|
||||
this.round++
|
||||
}
|
||||
ask(
|
||||
alive: Alive,
|
||||
|
||||
@@ -342,6 +342,7 @@ export class Instance {
|
||||
declare reading: unknown
|
||||
declare kept:
|
||||
| {
|
||||
round: number
|
||||
readonly done: () => void
|
||||
readonly parked: () => ReadonlyArray<Rec>
|
||||
/** Whether a child the instance keeps is written by its view. */
|
||||
@@ -966,6 +967,11 @@ export const authorOf = (inst: Instance): Instance =>
|
||||
? inst.parent!
|
||||
: inst
|
||||
|
||||
// Nested renders by the same author share a round; independent closure renders start a new one.
|
||||
const write = (by: Instance): void => {
|
||||
if (by.writing++ === 0 && by.kept !== undefined) by.kept.round++
|
||||
}
|
||||
|
||||
const start = (inst: Instance): Rec | undefined => {
|
||||
let first: Next
|
||||
const component = inst.component
|
||||
@@ -1000,7 +1006,7 @@ const start = (inst: Instance): Rec | undefined => {
|
||||
const tree = viewOf(inst)
|
||||
if (tree === undefined) return undefined
|
||||
let rendered: Rec
|
||||
by.writing++
|
||||
write(by)
|
||||
try {
|
||||
rendered = create(by, tree)
|
||||
if (dev && shown !== fallbacks) checkShown(inst.path, [rendered])
|
||||
@@ -1033,7 +1039,7 @@ const render = (inst: Instance): void => {
|
||||
const by = authorOf(inst)
|
||||
slot = by === inst ? undefined : inst.path
|
||||
busy++
|
||||
by.writing++
|
||||
write(by)
|
||||
try {
|
||||
inst.component["~render"]?.(inst)
|
||||
const tree = viewOf(inst)
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
import { assert, describe, it } from "@effect/vitest"
|
||||
import { Context, Deferred, Effect, Layer, Schema, SchemaGetter, SchemaIssue } from "effect"
|
||||
import { AsyncResult } from "effect/reactivity"
|
||||
import { Component, Query } from "effect/ui"
|
||||
import { Component, Query, UiSchema } from "effect/ui"
|
||||
import { BrowserHost, Html, HtmlHydrate, HtmlServer } from "effect/ui/html"
|
||||
import { ssr } from "effect/ui/html/testing"
|
||||
import { TestHost } from "effect/ui/testing"
|
||||
import { HtmlPlay, ssr } from "effect/ui/html/testing"
|
||||
import { Locator, TestHost } from "effect/ui/testing"
|
||||
import { collectGarbage } from "../utils/gc.ts"
|
||||
|
||||
let inits = { avatar: 0, profile: 0, badge: 0 }
|
||||
@@ -1830,6 +1830,58 @@ describe("keep-alive children of a headless child", () => {
|
||||
])
|
||||
}).pipe(Effect.scoped)
|
||||
)
|
||||
|
||||
it.effect(
|
||||
"moves a kept child repeatedly when only the headless child reruns its view closure",
|
||||
() =>
|
||||
Effect.gen(function*() {
|
||||
let barInits = 0
|
||||
const ProviderMessage = UiSchema.TaggedEnum({ Bumped: {} })
|
||||
const Provider = Html.component({
|
||||
name: "Provider",
|
||||
model: Schema.Number,
|
||||
message: ProviderMessage,
|
||||
shares: { value: ({ model }) => ({ n: model }), receives: ["Bumped"] }
|
||||
}).make({
|
||||
init: () => 0,
|
||||
update: ($) => $.model + 1,
|
||||
parts: (_, model) => ({ n: model })
|
||||
})
|
||||
const Bar = Html.component({
|
||||
name: "Bar",
|
||||
model: Schema.Null,
|
||||
message: Schema.Null
|
||||
}).make({
|
||||
init: () => {
|
||||
barInits++
|
||||
return null
|
||||
},
|
||||
update: ($) => $.send(Provider, ProviderMessage.Bumped()),
|
||||
view: (h) => h.button.on("click", null).children("bump")
|
||||
})
|
||||
const App = Html.component({
|
||||
name: "App",
|
||||
model: Schema.Null,
|
||||
message: Schema.Never
|
||||
}).make({
|
||||
init: () => null,
|
||||
update: ($) => $.model,
|
||||
view: (h) =>
|
||||
h.main(Provider(h, {
|
||||
view: (p) =>
|
||||
p.n % 2 === 0
|
||||
? h.div.children(h.keepAlive((s) => Bar(s, { key: "bar" })))
|
||||
: h.section.children(h.keepAlive((s) => Bar(s, { key: "bar" })))
|
||||
}))
|
||||
})
|
||||
const play = yield* HtmlPlay.make(App, {})
|
||||
yield* play.click("bump")
|
||||
yield* play.expect(Locator.inside(Locator.tag("section"), Locator.tag("button"))).toExist()
|
||||
yield* play.click("bump")
|
||||
assert.strictEqual(barInits, 1)
|
||||
yield* play.expect(Locator.inside(Locator.tag("div"), Locator.tag("button"))).toExist()
|
||||
}).pipe(Effect.scoped, Effect.provide(TestHost.layer()))
|
||||
)
|
||||
})
|
||||
|
||||
let lateGate: Effect.Effect<void> = Effect.void
|
||||
|
||||
Reference in New Issue
Block a user