This commit is contained in:
Víctor Oliva 2024-01-16 14:53:45 +01:00
parent 76a4f0c17e
commit a26a2cfaf7
No known key found for this signature in database
GPG Key ID: 358CE314C2A5C999
3 changed files with 18 additions and 5 deletions

View File

@ -12,7 +12,7 @@ describe("subtree", () => {
const otherRoot = createRoot().withTypes<{ value: string }>()
subtree(subnode, (ctx) => otherRoot.run(null, { value: ctx(subnode) }))
subtree(subnode, otherRoot, (ctx) => [null, { value: ctx(subnode) }])
mainRoot.run()
@ -31,7 +31,7 @@ describe("subtree", () => {
const otherRootCopy = substate(subnode, () => activeObs$(otherRoot))
const otherRoot = createRoot().withTypes<{ value: string }>()
subtree(subnode, (ctx) => otherRoot.run(null, { value: ctx(subnode) }))
subtree(subnode, otherRoot, (ctx) => [null, { value: ctx(subnode) }])
mainRoot.run()

View File

@ -7,17 +7,29 @@ import {
StateNode,
isSignal,
} from "./types"
import { RootNode } from "./create-root"
export function subtree<K extends KeysBaseType>(
export function subtree<K extends KeysBaseType, CtxValue, KeyValue>(
parent: StateNode<unknown, K>,
createInstance: (
node: RootNode<CtxValue, string, KeyValue>,
instanceSelector: (
ctxValue: GetValueFn,
ctxObservable: GetObservableFn<K>,
key: K,
) => () => void,
) => [KeyValue] | [KeyValue, CtxValue],
): void {
const internalParent = getInternals(parent)
const createInstance = (
ctxValue: GetValueFn,
ctxObservable: GetObservableFn<K>,
key: K,
) => {
const [keyValue, context] = instanceSelector(ctxValue, ctxObservable, key)
return node.run(keyValue, context!)
}
const teardowns = new NestedMap<K[keyof K], () => void>()
const nestedMapKey = (key: K) => internalParent.keysOrder.map((k) => key[k])
const teardown = (key: K) => teardowns.get(nestedMapKey(key))?.()

View File

@ -2,6 +2,7 @@ import { ObservableInput, from, Observable } from "rxjs"
import { SUSPENSE } from "@react-rxjs/core"
import { defaultStart } from "./internal-utils"
//
/**
* A RxJS creation operator that prepends a SUSPENSE on the source observable.
*