Skip to content

Instantly share code, notes, and snippets.

@axefrog
Last active June 6, 2017 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save axefrog/7811cc25e2542947b8aee308456ec78c to your computer and use it in GitHub Desktop.
Save axefrog/7811cc25e2542947b8aee308456ec78c to your computer and use it in GitHub Desktop.
Most.js introspection concept
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);
}
/** @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