1.1.3 • Published 12 months ago

@fruitsjs/monitor v1.1.3

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
12 months ago

@fruitsjs/monitor

A monitor to watch for specific changes on the the Fruits Eco-Blockchain platform

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.

Installation

@fruitsjs/monitor can be used with NodeJS or Web.

Using with NodeJS and/or modern web frameworks

Install using npm:

npm install @fruitsjs/monitor

or using yarn:

yarn add @fruitsjs/monitor

Example

import {Monitor} from '@fruitsjs/monitor'
import {composeApi} from "@fruitsjs/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 api = composeApi({ nodeHost: 'https://testnet.fwallet.net/'})
    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({
    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');
});
1.1.3

12 months ago

1.1.2

1 year ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0-rc.4

3 years ago

1.0.0-rc.3

3 years ago