dpaw-qweb-app
Version:
Simple viewer app to interface with QGIS/monorail
47 lines (37 loc) • 1.06 kB
JavaScript
/**
* @fileOverview
* @name model.js
* @author Gavin Coombes
* @license BSD-3-Clause
*/
let Rx = require('rx');
let u = require('dpaw-brocket-utility');
let log = u.log;
function calculateBMI(weight, height) {
let heightMeters = height * 0.01;
return Math.round(weight / (heightMeters * heightMeters));
}
// NEW!
// UPDATED!
function model(actions, incoming$) {
let bmi$ = Rx.Observable.combineLatest(
actions.changeWeight$.startWith(70), actions.changeHeight$.startWith(170),
(weight, height) => ({weight, height, bmi: calculateBMI(weight, height)})
);
let messageArr$ = incoming$
.do(v => log('prescan got val ', v))
// .map(v => JSON.parse(v))
.startWith([{tag: 'init', payload: 'in the beginning'}])
.pluck('payload')
.scan(myAggregate, []);
let state$ = Rx.Observable.combineLatest(
messageArr$, bmi$,
(messageArr, bmi) => ({messageArr, bmi})
);
return state$;
}
module.exports = model;
function myAggregate(acc, val, ix, src) {
acc.push(val);
return acc.slice(-10);
}