import { Stream , Time } from '@most/types' ; import { snapshot as snapshotStream , map as mapS , startWith } from '@most/core' ; import { snapshotTime } from './snapshotTime' ; export type Behavior < A > = < B > ( s : Stream < B > ) => Stream < [ A , B ] > ; export const snapshot = < A , B > ( b , s ) : Stream < [ A , B ] > => b ( s ) ; export const sample = < A , B > ( b , s ) : Stream < A > => mapS ( ( ab ) => ab [ 0 ] , snapshot ( b , s ) ) ; export { snapshotTime as time } ; export const always = < A > ( a ) : Behavior < A > => < B > ( sb ) : Stream < [ A , B ] > => mapS ( ( b ) => [ a , b ] , sb ) ; export const fromTime = < A > ( f ) : Behavior < A > => map ( f , snapshotTime ) ; export const step = < A > ( a , sa ) : Behavior < A > => < B > ( sb ) : Stream < [ A , B ] > => snapshotStream ( ( a , b ) => [ a , b ] , startWith ( a , sa ) , sb ) ; export const map = < A , B > ( f , ba ) : Behavior < B > => < C > ( sc ) : Stream < [ B , C ] > => mapS ( ( [ a , c ] ) => [ f ( a ) , c ] , snapshot ( ba , sc ) ) ; export const apply = < A , B > ( bf , ba ) : Behavior < B > => liftA2 ( ( f , a ) => f ( a ) , bf , ba ) ; export const liftA2 = < A , B , C > ( f , ba , bb ) : Behavior < C > => < D > ( sd ) : Stream < [ C , D ] > => mapS ( ( [ a , [ b , d ] ] ) => [ f ( a , b ) , d ] , snapshot ( ba , snapshot ( bb , sd ) ) ) ;