Last active
June 6, 2017 19:09
-
-
Save axefrog/7811cc25e2542947b8aee308456ec78c to your computer and use it in GitHub Desktop.
Most.js introspection concept
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function identity<T>(value: T): T { | |
return value; | |
} | |
let wrap = identity; | |
export function inspect<T>(fn: (value: T) => T): void { | |
wrap = fn; | |
} | |
export function trace<T>(value: T): T { | |
return wrap(value); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @license MIT License (c) copyright 2010-2016 original author or authors */ | |
/** @author Brian Cavalier */ | |
/** @author John Hann */ | |
import { trace } from '../inspect' | |
import Pipe from '../sink/Pipe' | |
import { disposeBoth } from '@most/disposable' | |
import { propagateEventTask, propagateEndTask } from '../scheduler/PropagateTask' | |
/** | |
* @param {Number} delayTime milliseconds to delay each item | |
* @param {Stream} stream | |
* @returns {Stream} new stream containing the same items, but delayed by ms | |
*/ | |
export const delay = (delayTime, stream) => | |
delayTime <= 0 ? stream : trace(new Delay(delayTime, stream)) | |
class Delay { | |
constructor (dt, source) { | |
this.dt = dt | |
this.source = source | |
} | |
run (sink, scheduler) { | |
const delaySink = trace(new DelaySink(this.dt, sink, scheduler)) | |
return disposeBoth(delaySink, this.source.run(delaySink, scheduler)) | |
} | |
} | |
class DelaySink extends Pipe { | |
constructor (dt, sink, scheduler) { | |
super(sink) | |
this.dt = dt | |
this.scheduler = scheduler | |
} | |
dispose () { | |
this.scheduler.cancelAll(task => task.sink === this.sink) | |
} | |
event (t, x) { | |
this.scheduler.delay(this.dt, propagateEventTask(x, this.sink)) | |
} | |
end (t) { | |
this.scheduler.delay(this.dt, propagateEndTask(this.sink)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment