11import { IO , runIO } from "@funkia/io" ;
22import { placeholder } from "./placeholder" ;
3- import { Time , SListener } from "./common" ;
4- import { Future , fromPromise , mapCbFuture } from "./future" ;
5- import { Node } from "./datastructures" ;
3+ import { Time } from "./common" ;
4+ import { Future , fromPromise , mapCbFuture , sinkFuture } from "./future" ;
65import { Behavior } from "./behavior" ;
7- import { ActiveStream , Stream , mapCbStream , isStream } from "./stream" ;
6+ import { Stream , mapCbStream , isStream } from "./stream" ;
87import { tick } from "./clock" ;
98
109export type MapNowTuple < A > = { [ K in keyof A ] : Now < A [ K ] > } ;
@@ -104,11 +103,11 @@ export function sample<A>(b: Behavior<A>): Now<A> {
104103}
105104
106105export class PerformNow < A > extends Now < A > {
107- constructor ( private cb : ( ) => A ) {
106+ constructor ( private _run : ( ) => A ) {
108107 super ( ) ;
109108 }
110109 run ( ) : A {
111- return this . cb ( ) ;
110+ return this . _run ( ) ;
112111 }
113112}
114113
@@ -124,9 +123,9 @@ export function performIO<A>(comp: IO<A>): Now<Future<A>> {
124123 return perform ( ( ) => fromPromise ( runIO ( comp ) ) ) ;
125124}
126125
127- export function performStream < A > ( s : Stream < IO < A > > ) : Now < Stream < A > > {
126+ export function performStream < A > ( s : Stream < IO < A > > ) : Now < Stream < Future < A > > > {
128127 return perform ( ( ) =>
129- mapCbStream < IO < A > , A > ( ( io , cb ) => runIO ( io ) . then ( cb ) , s )
128+ mapCbStream < IO < A > , Future < A > > ( ( io , cb ) => cb ( fromPromise ( runIO ( io ) ) ) , s )
130129 ) ;
131130}
132131
@@ -157,91 +156,6 @@ export function performMap<A, B>(
157156 ) ;
158157}
159158
160- class PerformIOLatestStream < A > extends ActiveStream < A >
161- implements SListener < IO < A > > {
162- private node : Node < this> = new Node ( this ) ;
163- constructor ( s : Stream < IO < A > > ) {
164- super ( ) ;
165- s . addListener ( this . node , tick ( ) ) ;
166- }
167- next : number = 0 ;
168- newest : number = 0 ;
169- running : number = 0 ;
170- pushS ( _t : number , io : IO < A > ) : void {
171- const time = ++ this . next ;
172- this . running ++ ;
173- runIO ( io ) . then ( ( a : A ) => {
174- this . running -- ;
175- if ( time > this . newest ) {
176- const t = tick ( ) ;
177- if ( this . running === 0 ) {
178- this . next = 0 ;
179- this . newest = 0 ;
180- } else {
181- this . newest = time ;
182- }
183- this . pushSToChildren ( t , a ) ;
184- }
185- } ) ;
186- }
187- }
188-
189- export class PerformStreamLatestNow < A > extends Now < Stream < A > > {
190- constructor ( private s : Stream < IO < A > > ) {
191- super ( ) ;
192- }
193- run ( ) : Stream < A > {
194- return new PerformIOLatestStream ( this . s ) ;
195- }
196- }
197-
198- export function performStreamLatest < A > ( s : Stream < IO < A > > ) : Now < Stream < A > > {
199- return perform ( ( ) => new PerformIOLatestStream ( s ) ) ;
200- }
201-
202- class PerformIOStreamOrdered < A > extends ActiveStream < A > {
203- private node : Node < this> = new Node ( this ) ;
204- constructor ( s : Stream < IO < A > > ) {
205- super ( ) ;
206- s . addListener ( this . node , tick ( ) ) ;
207- }
208- nextId : number = 0 ;
209- next : number = 0 ;
210- buffer : { value : A } [ ] = [ ] ; // Object-wrapper to support a result as undefined
211- pushS ( _t : number , io : IO < A > ) : void {
212- const id = this . nextId ++ ;
213- runIO ( io ) . then ( ( a : A ) => {
214- if ( id === this . next ) {
215- this . buffer [ 0 ] = { value : a } ;
216- this . pushFromBuffer ( ) ;
217- } else {
218- this . buffer [ id - this . next ] = { value : a } ;
219- }
220- } ) ;
221- }
222- pushFromBuffer ( ) : void {
223- while ( this . buffer [ 0 ] !== undefined ) {
224- const t = tick ( ) ;
225- const { value } = this . buffer . shift ( ) ;
226- this . pushSToChildren ( t , value ) ;
227- this . next ++ ;
228- }
229- }
230- }
231-
232- export class PerformStreamOrderedNow < A > extends Now < Stream < A > > {
233- constructor ( private s : Stream < IO < A > > ) {
234- super ( ) ;
235- }
236- run ( ) : Stream < A > {
237- return new PerformIOStreamOrdered ( this . s ) ;
238- }
239- }
240-
241- export function performStreamOrdered < A > ( s : Stream < IO < A > > ) : Now < Stream < A > > {
242- return new PerformStreamOrderedNow ( s ) ;
243- }
244-
245159export function plan < A > ( future : Future < Now < A > > ) : Now < Future < A > > {
246160 return performMap < Now < A > , A > ( runNow , future ) ;
247161}
0 commit comments