Skip to content

Instantly share code, notes, and snippets.

@CaptainJiNX
Created February 16, 2017 12:02
Show Gist options
  • Save CaptainJiNX/daef9d31402372c67342e6a05e1dd934 to your computer and use it in GitHub Desktop.
Save CaptainJiNX/daef9d31402372c67342e6a05e1dd934 to your computer and use it in GitHub Desktop.
From chrome://newtab/
'use strict';
const EventEmitter = require('events').EventEmitter;
const H = require('highland');
const emitter = new EventEmitter();
const delay = ({ key, value }) => {
return H((push, next) => {
H.log('----> REQUEST: ', key, value);
setTimeout(() => {
H.log('----> RESPONSE: ', key, value);
push(null, { key, value });
push(null, H.nil);
}, Math.random() * 1000);
});
};
const inFlight = new Set();
const updateStuff = ({ key, value }) => H.of({ key, value })
.doto((x) => H.log('START', x.key, x.value, Date.now()))
.flatMap(delay)
.flatMap((x) => {
if(x.value % 3 === 0) {
H.log('---> 3 <---', x.key);
return H([]);
}
return H.of(x);
})
.doto((x) => {
if(x.value % 2 === 0) {
throw new Error('BAM!');
}
})
.doto((x) => H.log('DONE', x.key, x.value, Date.now()))
.errors((err, push) => {
H.log('ERROR', err.message, key, value, Date.now());
push(null, { key, value });
})
.otherwise(() => {
H.log('-> (Empty) <-');
return H.of({ key, value });
});
H('update', emitter)
.map(updateStuff)
.parallel(2)
//.series()
.doto((x) => {
setTimeout(() => {
inFlight.delete(x.key);
H.log(`-> DELETED ${x.key}`);
}, 5000);
})
.each(() => H.log('..............'));
emitter.on('blahonga', ({ key, value }) => {
if (inFlight.has(key)) {
//H.log(`${key} debounced.`);
return;
}
inFlight.add(key);
H.log(`-> ADDED ${key}`);
setTimeout(() => {
emitter.emit('update', { key, value });
}, Math.random() * 1000);
});
setInterval(() => {
emitter.emit('blahonga', {
key: 'hello' + Math.floor(Math.random() * 3),
value: Math.floor(Math.random() * 100000)
});
}, 10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment