Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Monitor<T>

The generic monitor class.

A monitor can be used to check periodically for a certain situation, e.g. confirmation of a transaction, activation on an account, or even something completely different.

Example: (checking for the existence of an account aka account activation)

// A method that checks if an account exists
// > IMPORTANT: Do not use closures, when you need to serialize the monitor
async function tryFetchAccount() {
   const Api = composeApi({ nodeHost: 'https://testnet.signum.network:6876/'})
   try{
       const {account} = await Api.account.getAccount('1234')
       return account;
   }catch (e){
       // ignore error
       return null;
   }
}

// A comparing function to check if a certain condition for the returned data from fetch function
// is true. If it's true the monitor stops
function checkIfAccountExists(account) {
   return account !== null;
}

// Create your monitor
const monitor = new Monitor<Account>({
   asyncFetcherFn: tryFetchAccount,
   compareFn: checkIfAccountExists,
   intervalSecs: 10, // polling interval in seconds
   key: 'monitor-account',
   timeoutSecs: 2 * 240 // when reached timeout the monitor stops
})
.onFulfilled(() => {
   // called when `checkIfAccountExists` returns true
   console.log('Yay, account active');
})
.onTimeout(() => {
   // called when `timeoutSecs` is reached
   console.log('Hmm, something went wrong');
}).start();

Type parameters

  • T

Hierarchy

  • Monitor

Index

Constructors

constructor

Accessors

intervalSecs

  • get intervalSecs(): number

key

  • get key(): string

startTime

  • get startTime(): number

timeoutSecs

  • get timeoutSecs(): number

Methods

_debug

  • _debug(msg: any): void

_resetStartTime

  • _resetStartTime(): void

hasStarted

  • hasStarted(): boolean

isExpired

  • isExpired(): boolean

onFulfilled

  • onFulfilled(fn: FulfilledFunction<T>): Monitor<T>

onTimeout

  • onTimeout(fn: TimeoutFunction): Monitor<T>

serialize

  • serialize(): string
  • Serializes the monitor, such it can be stored. This serializes also the asyncFetcher and compareFn It is important that these functions are not closures, i.e. the must not reference outer data/variables, otherwise the behavior on deserialization is not deterministic

    Returns string

start

stop

  • stop(): void

Static deserialize

  • deserialize<T>(serializedMonitor: string, autoStart?: boolean): Monitor<T>
  • Deserializes a serialized monitor

    see

    Monitor.serialize

    Type parameters

    • T

    Parameters

    • serializedMonitor: string

      The serialized monitor

    • Default value autoStart: boolean = true

      If monitor was started on serialization the monitor starts automatically, if set true (default)

    Returns Monitor<T>

    The monitor instance