avocado-old/packages/input/packet/input.packet.js

37 lines
717 B
JavaScript
Raw Normal View History

2019-04-15 10:03:02 -05:00
import * as I from 'immutable';
2019-04-24 18:01:17 -05:00
import {Packet} from '@avocado/net';
2019-04-15 10:03:02 -05:00
export class InputPacket extends Packet {
static get schema() {
return {
...super.schema,
data: [
['string'],
['varint'],
],
};
}
static fromState(state) {
const data = [[], []];
const [actions, values] = data;
for (const [action, value] of state.entries()) {
actions.push(action);
values.push(parseInt(value));
}
return new InputPacket(data);
}
toState() {
const [actions, values] = this.data;
return I.Map().withMutations((state) => {
for (let i = 0; i < actions.length; ++i) {
state.set(actions[i], values[i]);
}
});
}
}