Please read the code comments in the files below. Examples are taken from our Github repo.
// import the Feature superclass from the trac-peer package
import {Feature} from 'trac-peer';
export class Timer extends Feature {
/**
* Setup your Feature
*
* Instances of Features are passed to Peer instances.
* See the Deployment section for more details.
*/
constructor(peer, options = {}) {
super(peer, options);
this.update_interval = options.update_interval !== undefined &&
false === isNaN(parseInt(options.update_interval)) &&
parseInt(options.update_interval) > 0 ? parseInt(options.update_interval) : 60_000;
}
/**
* start() is supposed to trigger the actual Feature execution.
*
* In the case of timers, an infinite event loop reads the latest
* time every 10th second and appends it into the contract.
*
+ The contract will then read this value as showcased in the previous
* section.
*
* Appends from Features don't go through transactions but straight into
* the contract.
*/
async start(options = {}) {
while(true){
await this.append('currentTime', Date.now());
await this.sleep(this.update_interval);
}
}
// stop helps to shutdown and may be triggered by a custom Feature-handler
async stop(options = {}) { }
}
export default Timer;