@burstjs/monitor v0.6.0-beta.6
@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)
Param | Description |
---|---|
args | The 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
Param | Description |
---|---|
fn | The callback |
monitor.onFulfilled(fn)
Kind: instance method of Monitor
Param | Description |
---|---|
fn | The callback |
Monitor.deserialize(serializedMonitor, autoStart) ⇒
Kind: static method of Monitor
Returns: The monitor instance
See: [Monitor.serialize]
Param | Default | Description |
---|---|---|
serializedMonitor | The serialized monitor | |
autoStart | true | If monitor was started on serialization the monitor starts automatically, if set true (default) |
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago