Aliaksei Chapyzhenka a4a0c9892c kill method
2020-11-28 18:37:51 -08:00

62 lines
1.3 KiB
JavaScript

'use strict';
const stream = require('stream');
const EventEmitter = require('events').EventEmitter;
let lib = require('bindings')('vcd.node');
module.exports = () => {
const wires = {};
const info = {stack: [wires], wires: wires};
const s = new stream.Writable();
// const lifee = new EventEmitter();
// gets called by c with 1 argument, a number
const lifemit = s.emit.bind(s);
const triee = new EventEmitter();
// gets called by c with 5 arguments
// string eventName
// bigint state->time
// int command
// bigint state->value
// bigint state->mask
const triemit = triee.emit.bind(triee);
let triemit2 = triemit;
const cxt = lib.init(lifemit, triemit, info);
s._write = function (chunk, encoding, callback) {
lib.execute(cxt, lifemit, triemit2, info, chunk);
callback();
};
s.change = {
on: (id, fn) => {
triemit2 = triemit;
triee.on(id, fn);
const triggerString = triee.eventNames().join(' ') + ' ';
lib.setTrigger(cxt, triggerString);
},
any: fn => {
triemit2 = fn;
lib.setTrigger(cxt, '\0');
}
};
s.info = info;
s.getTime = () => lib.getTime(cxt);
s.kill = () => {
lib.done(cxt, lifemit, triemit2, info);
s.end();
};
return s;
};