diff --git a/packages/context-state2/src/internal/state-instance.ts b/packages/context-state2/src/internal/state-instance.ts index 7e96027..5fada2f 100644 --- a/packages/context-state2/src/internal/state-instance.ts +++ b/packages/context-state2/src/internal/state-instance.ts @@ -16,6 +16,7 @@ export interface Instance { export function createInstance( key: K, observable: Observable, + onAfterChange: () => void, ): Instance { let subject = new BehaviorSubject(EMPTY_VALUE) @@ -35,6 +36,7 @@ export function createInstance( next: (v) => { deferred.res(v) subject.next(v) + onAfterChange() }, error: (e) => { deferred.rej(e) @@ -69,6 +71,7 @@ export function createInstance( } }, reset() { + // TODO how to reset without activating straight away with this flow? if (error !== EMPTY_VALUE || subject.getValue() !== EMPTY_VALUE) { // If the new subscription returns the same value synchronously, do not complete the previous result. // TODO the child nodes should also reset... are they resetting? diff --git a/packages/context-state2/src/internal/state-node.ts b/packages/context-state2/src/internal/state-node.ts index fa7f85c..7a6ea24 100644 --- a/packages/context-state2/src/internal/state-node.ts +++ b/packages/context-state2/src/internal/state-node.ts @@ -15,6 +15,12 @@ import { NestedMap, Wildcard } from "./nested-map" import { StatePromise } from "./promises" import { Instance, createInstance } from "./state-instance" +export type InstanceEvent = + | "added" + | "ready" + | "removed" + | "reset" + | "postchange" export interface InternalStateNode { keysOrder: Array getInstances: ( @@ -26,7 +32,7 @@ export interface InternalStateNode { removeInstance: (key: K) => void resetInstance: (key: K) => void instanceChange$: Observable<{ - type: "added" | "ready" | "removed" | "reset" + type: InstanceEvent key: K }> getContext: ( @@ -98,7 +104,7 @@ export function createStateNode( throw inactiveContext() } const instanceChange$ = new Subject<{ - type: "added" | "ready" | "removed" | "reset" + type: InstanceEvent key: K }>() const addInstance = (key: K) => { @@ -147,6 +153,11 @@ export function createStateNode( } }), ), + () => + instanceChange$.next({ + type: "postchange", + key, + }), ), ) @@ -178,7 +189,6 @@ export function createStateNode( if (!instance) { // TODO is this a valid path? Maybe throw error instead! addInstance(key) - activateInstance(key) return } // Only kill + readd if it was already active.