The monitors constructor
The arguments
The interval
The key aka identifier
The start timestamp if started, or -1
The timeout
true, if monitor was started and is running
true, if a started monitor timed out.
Callback function for fulfilled event. You can add multiple event listener if you want
The callback
Callback function for timeout event. You can add multiple event listener if you want
The callback
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
Starts the monitor
Stops the monitor
Deserializes a serialized monitor
The serialized monitor
If monitor was started on serialization the monitor starts automatically, if set true (default)
The monitor instance
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();