Commit ce9739f7 authored by Fabien's avatar Fabien Committed by GitHub

Merge pull request #160 from steemit/stream-irreversible-block

Add `mode` param on streaming method to load head or irreversible block
parents 2955a6a6 f72e069d
......@@ -265,7 +265,11 @@ class Steem extends EventEmitter {
return this.currentP;
}
streamBlockNumber(callback, ts = 200) {
streamBlockNumber(mode = 'head', callback, ts = 200) {
if (typeof mode === 'function') {
callback = mode;
mode = 'head';
}
let current = '';
let running = true;
......@@ -274,10 +278,22 @@ class Steem extends EventEmitter {
this.getDynamicGlobalPropertiesAsync()
.then((result) => {
const blockId = result.head_block_number;
const blockId = mode === 'irreversible'
? result.last_irreversible_block_num
: result.head_block_number;
if (blockId !== current) {
current = blockId;
callback(null, current);
if (current) {
for (let i = current; i < blockId; i++) {
if (i !== current) {
callback(null, i);
}
current = i;
}
} else {
current = blockId;
callback(null, blockId);
}
}
Promise.delay(ts).then(() => {
......@@ -295,11 +311,16 @@ class Steem extends EventEmitter {
};
}
streamBlock(callback) {
streamBlock(mode = 'head', callback) {
if (typeof mode === 'function') {
callback = mode;
mode = 'head';
}
let current = '';
let last = '';
const release = this.streamBlockNumber((err, id) => {
const release = this.streamBlockNumber(mode, (err, id) => {
if (err) {
release();
callback(err);
......@@ -316,8 +337,13 @@ class Steem extends EventEmitter {
return release;
}
streamTransactions(callback) {
const release = this.streamBlock((err, result) => {
streamTransactions(mode = 'head', callback) {
if (typeof mode === 'function') {
callback = mode;
mode = 'head';
}
const release = this.streamBlock(mode, (err, result) => {
if (err) {
release();
callback(err);
......@@ -334,8 +360,13 @@ class Steem extends EventEmitter {
return release;
}
streamOperations(callback) {
const release = this.streamTransactions((err, transaction) => {
streamOperations(mode = 'head', callback) {
if (typeof mode === 'function') {
callback = mode;
mode = 'head';
}
const release = this.streamTransactions(mode, (err, transaction) => {
if (err) {
release();
callback(err);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment