0.6.0-beta.6 • Published 3 years ago

@burstjs/monitor v0.6.0-beta.6

Weekly downloads
1
License
Apache-2.0
Repository
github
Last release
3 years ago

@burstjs/monitor

A monitor to watch for specific changes on the Burst blockchain

Due to average blocktime of 240 seconds, transactions stay pending for a certain time. It is a repeating pattern to watch for such changes and waiting for confirmation. This package simplifies this task.

As additional feature, a monitor is serializable, that means it can be stored e restored. This is especially useful for web applications, as it allows reloading pages without losing the ability to check whether a transaction is still pending or already concluded.

Installation

@burstjs/monitor can be used with NodeJS or Web. Two formats are available

Using with NodeJS and/or modern web frameworks

Install using npm:

npm install @burstjs/monitor

or using yarn:

yarn add @burstjs/monitor

Example

import {Monitor} from '@burstjs/monitor'
import {composeApi} from "@burstjs/core";

// A method that checks if an account exists
// > IMPORTANT: Do not use closures, when you need to serialize the monitor
async function tryFetchAccount() {
    const BurstApi = composeApi({ nodeHost: 'https://testnet.burstcoin.network:6876/'})
    try{
        const {account} = await BurstApi.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({
    asyncFetcherFn: tryFetchAccount,
    compareFn: checkIfAccountExists,
    intervalSecs: 10, // polling interval in seconds
    key: 'monitor-account',
    timeoutSecs: 2 * 240 // when reached timeout the monitor stops
});
// starts monitor
monitor.start();

// called when `checkIfAccountExists` returns true
monitor.onFulfilled(() => {
    console.log('Yay, account active');
});

// called when `timeoutSecs` is reached
monitor.onTimeout(() => {
    console.log('Hmm, something went wrong');
});

Using in classic <script>

Each package is available as bundled standalone library using IIFE. This way burstJS can be used also within <script>-Tags. This might be useful for Wordpress and/or other PHP applications.

Just import the package using the HTML <script> tag.

<script src='https://cdn.jsdelivr.net/npm/@burstjs/monitor/dist/burstjs.monitor.min.js'></script>

Example

const monitor = new b$monitor.Monitor({
    //...
});
monitor.start()
monitor.onFulFilled(() => {
    //...
})

Monitor Serialization

TO DO


See more here:

@burstjs/monitor Online Documentation


API Reference

monitor

// 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();

monitor~Monitor

Kind: inner class of monitor

new Monitor(args)

ParamDescription
argsThe arguments

monitor.startTime

Kind: instance property of Monitor

monitor.intervalSecs

Kind: instance property of Monitor

monitor.key

Kind: instance property of Monitor

monitor.timeoutSecs

Kind: instance property of Monitor

monitor.serialize()

Kind: instance method of Monitor

monitor.hasStarted() ⇒

Kind: instance method of Monitor
Returns: true, if monitor was started and is running

monitor.isExpired() ⇒

Kind: instance method of Monitor
Returns: true, if a started monitor timed out.

monitor.start()

Kind: instance method of Monitor

monitor.stop()

Kind: instance method of Monitor

monitor.onTimeout(fn)

Kind: instance method of Monitor

ParamDescription
fnThe callback

monitor.onFulfilled(fn)

Kind: instance method of Monitor

ParamDescription
fnThe callback

Monitor.deserialize(serializedMonitor, autoStart) ⇒

Kind: static method of Monitor
Returns: The monitor instance
See: [Monitor.serialize]

ParamDefaultDescription
serializedMonitorThe serialized monitor
autoStarttrueIf monitor was started on serialization the monitor starts automatically, if set true (default)
0.6.0-beta.6

3 years ago

0.6.0-beta.5

3 years ago

0.6.0-beta.4

3 years ago

0.6.0-beta.3

3 years ago

0.6.0-beta.2

3 years ago

0.6.0-beta.1

3 years ago

0.6.0-alpha.9

3 years ago

0.6.0-alpha.8

3 years ago

0.6.0-alpha.7

3 years ago

0.6.0-alpha.6

3 years ago

0.6.0-alpha.5

3 years ago

0.6.0-alpha.4

3 years ago

0.6.0-alpha.2

3 years ago

0.6.0-alpha.1

3 years ago