refactor(core): CORE-18 type builtins and own prompt lifetimes

This commit is contained in:
Harvey Zhao committed 2026-09-11 12:31:01 +08:00
1 parent a842058136
commit 98a1800032
55 files changed
+26511 -650

No files matched your search

+13 -1
View File
@@ -9,6 +9,7 @@ interface LifecycleOwner {
interface State {
scope: ResourceScope
finalizers: ResourceScope
destroying: boolean
rollback?: () => void
releaseContainer?: () => void
@@ -18,7 +19,13 @@ const states = new WeakMap<object, State>()
const containers = new WeakMap<Element, object>()
export function beginLifecycle(owner: object): void {
states.set(owner, { scope: new ResourceScope(), destroying: false })
const state: State = { scope: new ResourceScope(), finalizers: new ResourceScope(), destroying: false }
states.set(owner, state)
state.scope.add(() => {
// Direct scope disposal has no destroy-event phase to wait for.
if (!state.destroying)
state.finalizers.dispose()
})
}
function stateOf(owner: object): State {
@@ -32,6 +39,10 @@ export function getScope(owner: object): ResourceScope {
return stateOf(owner).scope
}
export function getFinalizationScope(owner: object): ResourceScope {
return stateOf(owner).finalizers
}
export function isClosing(owner: object): boolean {
const state = stateOf(owner)
return state.destroying || state.scope.closed
@@ -83,6 +94,7 @@ export function destroyInstance(owner: LifecycleOwner, instances: LifecycleOwner
state.releaseContainer = undefined
}
attempt(() => owner.emit('destroy'))
attempt(() => state.finalizers.dispose())
if (failed && state.rollback)
attempt(state.rollback)
state.rollback = undefined