import { MonoTypeOperatorFunction, Subscription } from 'rxjs'; import { createStore, InternalStoreOptions, StateUpdates, Store, StoreOptions, StoreQuery, StoreWithUpdates, withStoreUpdates, } from './store'; /** * Creates a deferred or transformed view of the store. */ export function pipeStore( store: Store, operator: MonoTypeOperatorFunction, ): StoreQuery { let subscription: Subscription | undefined; const clone = createStore(store.get(), { internal: true, onDestroy: () => { if (subscription) { subscription.unsubscribe(); subscription = undefined; } }, } as InternalStoreOptions); subscription = store.value$.pipe(operator).subscribe({ next: (state) => { clone.set(state); }, complete: () => { clone.destroy(); }, }); return clone; } /** * @deprecated Use `declareStore()` */ export function declareStoreWithUpdates< State, Updates extends StateUpdates, >( initialState: State, updates: Updates, baseOptions?: StoreOptions, ): ( state?: State, options?: StoreOptions, ) => StoreWithUpdates { return (state = initialState, options?: StoreOptions) => { return withStoreUpdates( createStore(state, { ...baseOptions, ...options }), updates, ); }; }