import { EVENT } from './enums'; export function create(dataview) { return { 'offset': 0, 'size': dataview.byteLength, 'view': dataview, }; } export function u8(d) { const value = d.view.getUint8(d.offset); d.offset += 1; return value; } export function u16(d) { const value = d.view.getUint16(d.offset, true); d.offset += 2; return value; } export function u32(d) { const value = d.view.getUint32(d.offset, true); d.offset += 4; return value; } function u16array(d, count) { const array = new Uint16Array(d.view.buffer, d.offset, count); d.offset += count * 2; return array; } export function event(d) { const event = {}; event.type = u8(d); u8(d); // padding switch (event.type) { case EVENT.PREDRAW: { event.x = u16(d); event.y = u16(d); break; } case EVENT.STROKE: { const point_count = u16(d); event.points = u16array(d, point_count * 2); break; } case EVENT.IMAGE: { event.image_id = u32(d); event.x = u16(d); event.y = u16(d); break; } case EVENT.UNDO: case EVENT.REDO: { break; } default: { console.error('fuck'); console.trace(); process.exit(1); } } return event; }