import { get } from '@typed/effects' ; import { Arity1 , Arity2 , id , IO } from '@typed/lambda' ; import { Channel } from './Channel' ; import { ChannelEffects , HookEnv , InitialState , UseChannelState , UseChannelStateOptions , } from './types' ; import { useMemo } from './useMemo' ; export function * useChannelState < E , A , B = A > ( options ) { const { hooksManager : { useChannelState } , hookEnvironment , } = yield * get ( ) ; return yield * useChannelState < E , A , B > ( options , hookEnvironment ) ; } /**
 * Used to consume and update the Channels value by a provider
 * higher up in the tree or in the case of no parents the provided
 * environment.
 */ export function * useChannel < E , A > ( channel ) { return yield * useChannelState ( { channel , selector : id } ) ; } /**
 * Used to provided the values of a Channel to itself and all consumers
 * lower in the tree, if any.
 */ export function * provideChannel < E , A > ( channel , initialState = channel . defaultValue ) { return yield * useChannelState ( { channel , initialState } ) ; } /**
 * Used to consume and map over the values in a channel. Will only be marked
 * as updated if the return value of the selector has changed.
 */ export function * useMapChannel < E , A , B > ( fn , channel ) { const [ getValue ] = yield * useChannelState ( { channel , selector : fn } ) ; return yield * getValue ( ) ; } export const useChannelValue = < E , A > ( channel ) : ChannelEffects < HookEnv & E , A > => useMapChannel ( id , channel ) ; export function * useReduceChannel < E , A , B > ( reducer , channel ) { const [ getState , updateState ] = yield * provideChannel ( channel ) ; const deps = [ reducer , updateState ] as const ; const dispatch = yield * useMemo < E , typeof deps , Arity1 < B , ChannelEffects < E , A > > > ( createDispatch , deps ) ; return [ getState , dispatch ] as const ; } function createDispatch < E , A , B > ( reducer , updateState ) { return ( event ) : ChannelEffects < E , A > => updateState ( ( state ) => reducer ( state , event ) ) ; }