3.0.0 • Published 7 years ago
@motorcycle/types v3.0.0
@motorcycle/types -- 2.1.0
A shared-kernel of types for Motorcycle
Get it
yarn add @motorcycle/types
# or
npm install --save @motorcycle/types
API Documentation
All functions are curried!
Component
Component function type signature
export type Component<
Sources extends { readonly [key: string]: any },
Sinks extends { readonly [key: string]: Stream<any> }
> = (sources: Sources) => Sinks
Delay
Delay time offset
export type Delay = number
Disposable
Interface of a resource that can be disposed
export interface Disposable {
dispose(): void;
}
Handle
Opaque handle type produced by functions like setTimeout
export type Handle = any
IOComponent
An IO Component function signature.
export type IOComponent<
Sinks extends { readonly [key: string]: Stream<any> },
Sources extends { readonly [key: string]: any }
> = (sinks: Sinks) => Sources
Offset
Relative offset between to Clocks / schedulers
export type Offset = number
Period
Span of time between time instants
export type Period = number
ScheduledTask
A Task scheduled for a particular time
export interface ScheduledTask {
task: Task;
run(): void;
error(err: Error): void;
dispose(): void;
}
Scheduler
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;
}
Sink\<A>
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;
}
Stream\<A>
Motorcycle Stream type
export interface Stream<A> {
run(sink: Sink<A>, scheduler: Scheduler): Disposable
}
Task
A generic Task type
export interface Task {
run(time: Time): void;
error(time: Time, e: Error): void;
dispose(): void;
}
TaskRunner
Runs a ScheduledTask
export type TaskRunner = (st: ScheduledTask) => any;
Time
An instant in time.
export type Time = number
Timeline
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;
}
Timer
A Timer interface
export interface Timer {
now(): Time;
setTimer(f: () => any, delayTime: Delay): Handle;
clearTimer(timerHandle: Handle): void;
}