0.5.1 • Published 5 years ago

waitfor.ts v0.5.1

Weekly downloads
2
License
ISC
Repository
github
Last release
5 years ago

Waitfor.ts

A typescript utility to give a sequential feel to asyncronous/reactive programming patterns.

API Documentation

Installation

$ npm install waitfor.ts

What it does

You provide the class with a resolver function and another function that you wish to fire once resolved, the class will periodically attempt to resolve this function and, once resolved, will fire the intended function. You supply your arguments to the Class's fire method, which are passed back to your entended function.

Constructor Example

import Waiter from 'waitfor.ts';
import EventDrivenClass from 'eventLib'; // 100% pseudo code

let ready = false;

function resolver() { ready = true; }

function notifier(user) {
  console.log(`Hey ${user}, the application is ready!`);
}

const Wait = new Waiter(resolver, notifier);

Wait.fire('Frank'); // will log out "Hey Frank, the application is ready!" after the listener event resets the `ready` variable to true.

const listener = new EventDrivenClass();

listener.on('ready', () => {
  ready = true;
});

Static helper Example

There exists a static method that makes it feel a bit cleaner. Here is the same example as above, but using the static helper.

import Waiter from 'waitfor.ts';
import EventDrivenClass from 'eventLib'; // 100% pseudo code

let ready = false;

function resolver() { ready = true; }

function notifier(user) {
  console.log(`Hey ${user}, the application is ready!`);
}

const Greet = Waiter.create(resolver, notifier);

Greet('Frank'); // will log out "Hey Frank, the application is ready!" after the listener event resets the `ready` variable to true.

const listener = new EventDrivenClass();

listener.on('ready', () => {
  ready = true;
});

Decorator Example

Also included is a decorator that can be used to allow you to wait for resolvers on your own class methods

import { WaitFor } from 'waitfor.ts';

class Demo {

  private _user:string;

  constructor(){
    this.logUser();
  }

  @WaitFor(() => this.user !== undefined)
  private logUser(): void {
    console.log(`Hey ${this.user}, the application is ready!`);
  }

  public set user(user:string) {
    this._user = user;
  }

  public get user(): string {
    return this._user;
  }

}

// ...

const DecoratorDemo = new Demo(); // no log

DecoratorDemo.user = 'Frank';

// "Hey ${this.user}, the application is ready!"

Available configuration options

OptionTypeDefaultDescription
timeoutnumber1000Number of milliseconds before the Class throws a timeout error.
silentbooleanfalseIf true then the class wont throw an error at timeout, it will just stop attempting to resolve.
frequencynumber50Number of milliseconds between resolver attempts.