Options
All
  • Public
  • Public/Protected
  • All
Menu

Class WaitFor

Utility to poll a resolver in order to determine if a function should fire. If the resolver does not pass before a timeout then the intended method will be undefined.

import WaitFor from 'waitfor.ts';

let user;

function resolver() {
 return user !== undefined;
}

function WelcomeMessage(greet){
 console.log(`${greet} ${user}!`);
}

const Wait = new WaitFor(resolver, WelcomeMessage);
Wait.fire('Welcome'); // nothing yet

// Pretend Login after some validation and stuff
user = `Ben`;

// Welcome Ben!

Hierarchy

  • WaitFor

Index

Constructors

constructor

  • Parameters

    • resolveFn: Resolver

      Method that must return true in order for the intended function to fire.

    • invokedFn: Function

      Expected function. This method will fire after the resolver returns true.

    • Default value options: WaitForOptions = {timeout: 1000}

      Options that dictate timeout how the waitfor class behaves.

    Returns WaitFor

Properties

Private firstInvoked

firstInvoked: Date | undefined

Sets the time the method was originally attempted. This is used to determine timeout.

Private frequency

frequency: number

How often should we poll and check the resolver if it didn't immediately return true.

Private invokedFn

invokedFn: Function

Expected function. This method will fire after the resolver returns true.

Private options

Options that dictate timeout how the waitfor class behaves.

Private resolveFn

resolveFn: Resolver

Method that must return true in order for the intended function to fire.

Private silent

silent: boolean

If set to true then rather than throwing an error at timeout the class will simply stop trying to resolve the resolver.

Private timeout

timeout: number

After this value has been exceeded the check will fail and stop trying.

Private timerId

timerId: Timer | undefined

Reference for the timer method, unless the request is immediately resolved

Accessors

Private sinceLast

  • get sinceLast(): number
  • Returns the amount of time, in milliseconds, that has elapsed since the original request.

    Returns number

Private timedOut

  • get timedOut(): boolean
  • Takes the amount of time that has elapsed since originally requested and determines if the call should time out.

    Returns boolean

Methods

Private clearTimer

  • clearTimer(): void
  • Clears the internal polling timer.

    Returns void

fire

  • fire(...args: any[]): void
  • Starts the request to the resolver, and if needed the polling timer.

    Parameters

    • Rest ...args: any[]

    Returns void

Private setTimer

  • setTimer(...args: any[]): void
  • Utility method used to create the polling timer. It subsequently invokes, and clears the timer, or clears the timer and times out if the request has timed out.

    Parameters

    • Rest ...args: any[]

    Returns void

Static create

  • Helper method to require less 'boilerplate'.

    import { WaitFor } from 'waitfor.ts';
    
    function resolverFn(): boolean {
     // after something happens to set it true
     return true;
    }
    
    function delayedFn(name) {
     console.log(`Hello ${name}!`);
    }
    
    const DelayedFn = WaitFor.create(resolveFn, delayedFn);
    
    DelayedFn('John') // "Hello John!" after resolver passes

    Parameters

    Returns any

Generated using TypeDoc