function buildState ({ api, minsPerStep, scale }) { // build localPeers, remotePeers const localPeers = throttle(api.sbot.obs.localPeers(), 1000) const remotePeers = computed([localPeers, throttle(api.sbot.obs.connectedPeers(), 1000)], (local, connected) => { return connected.filter(peer => !local.includes(peer)) }) // build seq, replication (my current state, and replicated state) const seq = Value() const replication = Dict({}) onceTrue(api.sbot.obs.connection, server => { setInterval(() => { // TODO check ebt docs if this is best method server.ebt.peerStatus(server.id, (err, data) => { if (err) return console.error(err) seq.set(data.seq) for (var peer in data.peers) { replication.put(peer, data.peers[peer]) } }) }, 5e3) }) // build data, range const data = Dict({ [toTimeBlock(Date.now(), minsPerStep) + minsPerStep * MINUTE]: 0, [toTimeBlock(Date.now(), minsPerStep) + minsPerStep * MINUTE - scale]: 0 }) onceTrue(api.sbot.obs.connection, server => { getData({ data, server, minsPerStep, scale }) }) const latest = Value(toTimeBlock(Date.now(), minsPerStep)) // start of the most recent bar setInterval(() => { latest.set(toTimeBlock(Date.now(), minsPerStep)) }, minsPerStep / 4 * MINUTE) const range = computed([latest], (latest) => { return { upper: latest + minsPerStep * MINUTE, lower: latest + minsPerStep * MINUTE - scale } }) return { localPeers, remotePeers, seq, replication, data, // TODO rename this !! range, invite: Value(), inviteProcessing: Value(false), inviteResult: Value(null) } }
function initialiseChart ({ canvas, state: { data, range } }) { var chart = new Chart(canvas.getContext('2d'), chartConfig(range)) watch(range, ({ lower, upper }) => { // set horizontal scale chart.options.scales.xAxes[0].time.min = lower chart.options.scales.xAxes[0].time.max = upper chart.update() }) watchAll([throttle(data, 300), range], (data, { lower, upper }) => { const _data = Object.keys(data) .sort((a, b) => a < b ? -1 : +1) .map(ts => { return { t: Number(ts), // NOTE - might need to offset by a half-step ? y: data[ts] } }) // update chard data chart.data.datasets[0].data = _data // scales the height of the graph (to the visible data)! const slice = _data .filter(d => d.t >= lower && d.t < upper) .map(d => d.y) .sort((a, b) => a > b ? -1 : +1) var h = slice[0] if (!h || h < GRAPH_Y_MIN) h = GRAPH_Y_MIN // min-height else h = h + (GRAPH_Y_STEP - h % GRAPH_Y_STEP) // round height to multiples of GRAPH_Y_STEP chart.options.scales.yAxes[0].ticks.max = h chart.update() }) }