Skip to content

Commit

Permalink
Merge pull request #160 from steemit/stream-irreversible-block
Browse files Browse the repository at this point in the history
Add `mode` param on streaming method to load head or irreversible block
  • Loading branch information
Fabien authored Jun 9, 2017
2 parents 2955a6a + f72e069 commit ce9739f
Showing 1 changed file with 41 additions and 10 deletions.
51 changes: 41 additions & 10 deletions src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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(() => {
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit ce9739f

Please sign in to comment.