dpaw-monorail-plugin
Version:
JS library to link the brocket network to the QGIS/monorail server
70 lines (58 loc) • 1.56 kB
JavaScript
/**
* @fileOverview
* @name logs.js
* @author Gavin Coombes
* @license BSD-3-Clause
*
* Display incoming and outgoing messages
*
*/
let Cycle = require('@cycle/core');
let CycleDOM = require('@cycle/dom');
let Rx = require('rx');
let u = require('dpaw-brocket-utility');
let h = CycleDOM.h;
let Obs = Rx.Observable;
let log = u.log;
const DEFAULT_OPTS = {title: 'incoming', id: null};
function Log(DOM, message$, _opts) {
let opts = defaults(_opts);
let actions = DOM.select(`#${opts.id}`).events('click');
let messageArr$ = message$
.pluck('payload')
.filter(msg => !u.isUndefined(msg))
.scan(myAggregate, [])
.startWith([{tag: 'init', payload: 'in the beginning'}]);
let vtree$ = Obs.combineLatest(messageArr$, renderArray);
return { vtree$ };
function renderArray(ma) {
log('LogComponent.render: got array', ma);
let view = h('div.log-container', [
h('h4', opts.title),
h('ul', [ma.map(renderItem)])
]);
return view;
}
function renderItem(message) {
let li = h('li', JSON.stringify(message));
return li;
}
};
function DummyLog() {
return {vtree$: Obs.just(h('div', 'incoming'))};
}
module.exports = Log;
function myAggregate(acc, val, ix, src) {
log('myAggregate got val', val);
acc.push(val);
log('myAggregate has acc', acc);
return acc.slice(-10);
}
function defaults(_opts) {
let ix = 0;
let opts = u.defaults({}, _opts, DEFAULT_OPTS);
if (u.isNull(opts.id)) {
opts.id = 'log-${opts.title}-${ix++}';
}
return opts;
}