import { Subscription } from './subscription' ; export interface Scheduler { readonly now : number ; delay ( time ) : Promise < void > ; schedule ( action , dueTime ) : Subscription ; } class ActionSubscription implements Subscription { public isUnsubscribed = false ; private _action : ( ) => void ; constructor ( action ) { this . _action = action ; } unsubscribe ( ) { if ( ! this . isUnsubscribed ) { this . isUnsubscribed = true ; this . _action ( ) ; } } } export class DefaultScheduler implements Scheduler { get now ( ) { return Date . now ( ) ; } delay ( dueTime ) { return new Promise < void > ( res => setTimeout ( res , dueTime ) ) ; } schedule ( action , dueTime ) { const id = setTimeout ( ( ) => action ( ) , dueTime ) ; return new ActionSubscription ( ( ) => clearTimeout ( id ) ) ; } }