A shared-kernel of types for Motorcycle
yarn add @motorcycle/types
# or
npm install --save @motorcycle/types
All functions are curried!
Component function type signature
export type Component<
Sources extends { readonly [key: string]: any },
Sinks extends { readonly [key: string]: Stream<any> }
> = (sources: Sources) => Sinks
Delay time offset
export type Delay = number
Interface of a resource that can be disposed
export interface Disposable {
dispose(): void;
}
Opaque handle type produced by functions like setTimeout
export type Handle = any
An IO Component function signature.
export type IOComponent<
Sinks extends { readonly [key: string]: Stream<any> },
Sources extends { readonly [key: string]: any }
> = (sinks: Sinks) => Sources
Relative offset between to Clocks / schedulers
export type Offset = number
Span of time between time instants
export type Period = number
A Task scheduled for a particular time
export interface ScheduledTask {
task: Task;
run(): void;
error(err: Error): void;
dispose(): void;
}
A Scheduler
export interface Scheduler {
now(): Time;
asap (task: Task): ScheduledTask;
delay (delay: Delay, task: Task): ScheduledTask;
periodic (period: Period, task: Task): ScheduledTask;
schedule (delay: Delay, period: Period, task: Task): ScheduledTask;
scheduleTask (offset: Offset, delay: Delay, period: Period, task: Task): ScheduledTask;
relative(offset: Offset): Scheduler;
cancel(task: ScheduledTask): void;
cancelAll(predicate: (task: ScheduledTask) => boolean): void;
}
Motorcycle Sink type. Used to propagate events.
export interface Sink<A> {
event(time: Time, value: A): void;
end(time: Time): void;
error(time: Time, err: Error): void;
}
Motorcycle Stream type
export interface Stream<A> {
run(sink: Sink<A>, scheduler: Scheduler): Disposable
}
A generic Task type
export interface Task {
run(time: Time): void;
error(time: Time, e: Error): void;
dispose(): void;
}
Runs a ScheduledTask
export type TaskRunner = (st: ScheduledTask) => any;
An instant in time.
export type Time = number
Keeps track of time
export interface Timeline {
add(scheduledTask: ScheduledTask): void;
remove(scheduledTask: ScheduledTask): boolean;
removeAll(f: (scheduledTask: ScheduledTask) => boolean): void;
isEmpty(): boolean;
nextArrival(): Time;
runTasks(time: Time, runTask: TaskRunner): void;
}
A Timer interface
export interface Timer {
now(): Time;
setTimer(f: () => any, delayTime: Delay): Handle;
clearTimer(timerHandle: Handle): void;
}