Switch to ordered maps of monitor records for monitor state

This commit is contained in:
DD Liu 2017-05-24 15:42:29 -04:00
parent acdc783f27
commit 1902848ea4
3 changed files with 16 additions and 15 deletions

View file

@ -1,7 +1,7 @@
const adapter = require('./adapter');
const mutationAdapter = require('./mutation-adapter');
const xmlEscape = require('../util/xml-escape');
const {Map} = require('immutable');
const MonitorRecord = require('./records');
/**
* @fileoverview
@ -294,7 +294,7 @@ class Blocks {
if (optRuntime && wasMonitored && !block.isMonitored) {
optRuntime.requestRemoveMonitor(block.id);
} else if (optRuntime && !wasMonitored && block.isMonitored) {
optRuntime.requestAddMonitor(Map({
optRuntime.requestAddMonitor(MonitorRecord({
// @todo(vm#564) this will collide if multiple sprites use same block
id: block.id,
category: 'data',

View file

@ -2,7 +2,7 @@ const EventEmitter = require('events');
const Sequencer = require('./sequencer');
const Blocks = require('./blocks');
const Thread = require('./thread');
const {Map} = require('immutable');
const {OrderedMap} = require('immutable');
// Virtual I/O devices.
const Clock = require('../io/clock');
@ -113,14 +113,14 @@ class Runtime extends EventEmitter {
this._refreshTargets = false;
/**
* List of all monitors.
* Ordered map of all monitors, which are MonitorReporter objects.
*/
this._monitorState = Map({});
this._monitorState = OrderedMap({});
/**
* Monitor state from last tick
*/
this._prevMonitorState = Map({});
this._prevMonitorState = OrderedMap({});
/**
* Whether the project is in "turbo mode."
@ -880,7 +880,7 @@ class Runtime extends EventEmitter {
/**
* Add a monitor to the state. If the monitor already exists in the state,
* overwrites it.
* @param {!Map} monitor Monitor to add.
* @param {!MonitorRecord} monitor Monitor to add.
*/
requestAddMonitor (monitor) {
this._monitorState = this._monitorState.set(monitor.get('id'), monitor);
@ -889,7 +889,9 @@ class Runtime extends EventEmitter {
/**
* Update a monitor in the state. Does nothing if the monitor does not already
* exist in the state.
* @param {!Map} monitor Monitor to update.
* @param {!Map} monitor Monitor values to update. Values on the monitor with overwrite
* values on the old monitor with the same ID. If a value isn't defined on the new monitor,
* the old monitor will keep its old value.
*/
requestUpdateMonitor (monitor) {
if (this._monitorState.has(monitor.get('id'))) {

View file

@ -1,5 +1,6 @@
const test = require('tap').test;
const Runtime = require('../../src/engine/runtime');
const MonitorRecord = require('../../src/engine/records');
const {Map} = require('immutable');
test('spec', t => {
@ -15,7 +16,7 @@ test('spec', t => {
test('monitorStateEquals', t => {
const r = new Runtime();
const id = 'xklj4#!';
const prevMonitorState = Map({
const prevMonitorState = MonitorRecord({
id,
category: 'data',
label: 'turtle whereabouts',
@ -38,13 +39,11 @@ test('monitorStateEquals', t => {
test('monitorStateDoesNotEqual', t => {
const r = new Runtime();
const id = 'xklj4#!';
const prevMonitorState = Map({
const prevMonitorState = MonitorRecord({
id,
category: 'data',
label: 'turtle whereabouts',
value: '25',
x: 0,
y: 0
value: '25'
});
// Value change
@ -61,13 +60,13 @@ test('monitorStateDoesNotEqual', t => {
// Prop change
newMonitorDelta = Map({
id: 'xklj4#!',
moose: 7
x: 7
});
r.requestUpdateMonitor(newMonitorDelta);
t.equals(false, prevMonitorState.equals(r._monitorState.get(id)));
t.equals(String(24), r._monitorState.get(id).get('value'));
t.equals(7, r._monitorState.get(id).get('moose'));
t.equals(7, r._monitorState.get(id).get('x'));
t.end();
});