Refactor: remove Serializable

This commit is contained in:
Farkas József
2016-12-14 19:01:15 +01:00
committed by József Farkas
parent 13d91ad26b
commit e40dbb83b0
23 changed files with 52 additions and 118 deletions

View File

@@ -1,51 +0,0 @@
/// <references path="Function.d.ts">
import { UhkBuffer } from './UhkBuffer';
export abstract class Serializable<T> {
private static depth = 0;
private static maxDisplayedJsonLength = 160;
private static enableDump = false;
toJsonObject(): any {
this.dump(`${this.getIndentation()}${this.constructor.name}.toJsObject: ${this}\n`);
Serializable.depth++;
let value = this._toJsonObject();
Serializable.depth--;
this.dump(`${this.getIndentation()}=> ${this.stringifyJsonObject(value)}\n`);
return value;
}
// TODO: remove parameter and return the buffer
toBinary(buffer: UhkBuffer): void {
this.dump(`\n${this.getIndentation()}${this.constructor.name}.toBinary: ${this} [`);
Serializable.depth++;
buffer.enableDump = Serializable.enableDump;
let value = this._toBinary(buffer);
buffer.enableDump = false;
Serializable.depth--;
this.dump(`]`);
return value;
}
abstract _toJsonObject(): any;
abstract _toBinary(buffer: UhkBuffer): void;
private dump(value: any) {
if (Serializable.enableDump) {
process.stdout.write(value);
}
}
private getIndentation() {
return new Array(Serializable.depth + 1).join(' ');
}
private stringifyJsonObject(jsonObject: any): string {
let json = JSON.stringify(jsonObject);
return json.length > Serializable.maxDisplayedJsonLength
? json.substr(0, Serializable.maxDisplayedJsonLength) + '...'
: json;
}
}

View File

@@ -1,5 +1,3 @@
import { Serializable } from './Serializable';
export class UhkBuffer {
private static eepromSize = 32 * 1024;
@@ -153,7 +151,7 @@ export class UhkBuffer {
this.writeUInt8(bool ? 1 : 0);
}
readArray<T extends Serializable<T>>(elementReader: (buffer: UhkBuffer) => T): T[] {
readArray<T>(elementReader: (buffer: UhkBuffer) => T): T[] {
let array: T[] = [];
let length = this.readCompactLength();
for (let i = 0; i < length; ++i) {
@@ -162,10 +160,10 @@ export class UhkBuffer {
return array;
}
writeArray<T extends Serializable<T>>(array: T[]): void {
writeArray<T>(array: T[]): void {
this.writeCompactLength(array.length);
for (let element of array) {
element.toBinary(this);
(<any>element).toBinary(this); // TODO: Remove any
}
}

View File

@@ -1,9 +1,8 @@
import { Serializable } from '../Serializable';
import { UhkBuffer } from '../UhkBuffer';
import { Layer } from './Layer';
import { Macro } from './Macro';
export class Keymap extends Serializable<Keymap> {
export class Keymap {
name: string;
@@ -16,7 +15,6 @@ export class Keymap extends Serializable<Keymap> {
layers: Layer[];
constructor(keymap?: Keymap) {
super();
if (!keymap) {
return;
}
@@ -48,7 +46,7 @@ export class Keymap extends Serializable<Keymap> {
return this;
}
_toJsonObject(): any {
toJsonObject(): any {
return {
isDefault: this.isDefault,
abbreviation: this.abbreviation,
@@ -58,7 +56,7 @@ export class Keymap extends Serializable<Keymap> {
};
}
_toBinary(buffer: UhkBuffer): void {
toBinary(buffer: UhkBuffer): void {
buffer.writeString(this.abbreviation);
buffer.writeBoolean(this.isDefault);
buffer.writeString(this.name);

View File

@@ -1,15 +1,13 @@
import { Serializable } from '../Serializable';
import { UhkBuffer } from '../UhkBuffer';
import { Keymap } from './Keymap';
import { Macro } from './Macro';
import { Module } from './Module';
export class Layer extends Serializable<Layer> {
export class Layer {
modules: Module[];
constructor(layers?: Layer) {
super();
if (!layers) {
return;
}
@@ -28,13 +26,13 @@ export class Layer extends Serializable<Layer> {
return this;
}
_toJsonObject(): any {
toJsonObject(): any {
return {
modules: this.modules.map(module => module.toJsonObject())
};
}
_toBinary(buffer: UhkBuffer): void {
toBinary(buffer: UhkBuffer): void {
buffer.writeArray(this.modules);
}

View File

@@ -1,9 +1,8 @@
import { assertUInt8 } from '../assert';
import { Serializable } from '../Serializable';
import { UhkBuffer } from '../UhkBuffer';
import { Helper as MacroActionHelper, MacroAction } from './macro-action';
export class Macro extends Serializable<Macro> {
export class Macro {
@assertUInt8
id: number;
@@ -17,7 +16,6 @@ export class Macro extends Serializable<Macro> {
macroActions: MacroAction[];
constructor(other?: Macro) {
super();
if (!other) {
return;
}
@@ -50,7 +48,7 @@ export class Macro extends Serializable<Macro> {
return this;
}
_toJsonObject(): any {
toJsonObject(): any {
return {
id: this.id,
isLooped: this.isLooped,
@@ -60,7 +58,7 @@ export class Macro extends Serializable<Macro> {
};
}
_toBinary(buffer: UhkBuffer): void {
toBinary(buffer: UhkBuffer): void {
buffer.writeUInt8(this.id);
buffer.writeBoolean(this.isLooped);
buffer.writeBoolean(this.isPrivate);

View File

@@ -1,5 +1,4 @@
import { assertEnum, assertUInt8 } from '../assert';
import { Serializable } from '../Serializable';
import { UhkBuffer } from '../UhkBuffer';
import { Helper as KeyActionHelper, KeyAction, NoneAction } from './key-action';
import { Keymap } from './Keymap';
@@ -11,7 +10,7 @@ enum PointerRole {
scroll
}
export class Module extends Serializable<Module> {
export class Module {
@assertUInt8
id: number;
@@ -22,7 +21,6 @@ export class Module extends Serializable<Module> {
pointerRole: PointerRole;
constructor(other?: Module) {
super();
if (!other) {
return;
}
@@ -51,7 +49,7 @@ export class Module extends Serializable<Module> {
return this;
}
_toJsonObject(): any {
toJsonObject(): any {
return {
id: this.id,
pointerRole: PointerRole[this.pointerRole],
@@ -63,7 +61,7 @@ export class Module extends Serializable<Module> {
};
}
_toBinary(buffer: UhkBuffer): void {
toBinary(buffer: UhkBuffer): void {
buffer.writeUInt8(this.id);
buffer.writeUInt8(this.pointerRole);

View File

@@ -1,8 +1,7 @@
import { assertUInt8 } from '../assert';
import { Serializable } from '../Serializable';
import { UhkBuffer } from '../UhkBuffer';
export class ModuleConfiguration extends Serializable<ModuleConfiguration> {
export class ModuleConfiguration {
/*
* module id enumeration is a separate story
@@ -36,7 +35,7 @@ export class ModuleConfiguration extends Serializable<ModuleConfiguration> {
return this;
}
_toJsonObject(): any {
toJsonObject(): any {
return {
id: this.id,
initialPointerSpeed: this.initialPointerSpeed,
@@ -45,7 +44,7 @@ export class ModuleConfiguration extends Serializable<ModuleConfiguration> {
};
}
_toBinary(buffer: UhkBuffer): void {
toBinary(buffer: UhkBuffer): void {
buffer.writeUInt8(this.id);
buffer.writeUInt8(this.initialPointerSpeed);
buffer.writeUInt8(this.pointerAcceleration);

View File

@@ -1,11 +1,10 @@
import { assertUInt16, assertUInt32, assertUInt8 } from '../assert';
import { Serializable } from '../Serializable';
import { UhkBuffer } from '../UhkBuffer';
import { Keymap } from './Keymap';
import { Macro } from './Macro';
import { ModuleConfiguration } from './ModuleConfiguration';
export class UhkConfiguration extends Serializable<UhkConfiguration> {
export class UhkConfiguration {
signature: string;
@@ -60,7 +59,7 @@ export class UhkConfiguration extends Serializable<UhkConfiguration> {
return this;
}
_toJsonObject(): any {
toJsonObject(): any {
return {
signature: this.signature,
dataModelVersion: this.dataModelVersion,
@@ -74,7 +73,7 @@ export class UhkConfiguration extends Serializable<UhkConfiguration> {
};
}
_toBinary(buffer: UhkBuffer): void {
toBinary(buffer: UhkBuffer): void {
buffer.writeString(this.signature);
buffer.writeUInt16(this.dataModelVersion);
buffer.writeUInt32(this.prologue);

View File

@@ -1,6 +1,5 @@
/// <reference path="../../Function.d.ts" />
import { Serializable } from '../../Serializable';
import { UhkBuffer } from '../../UhkBuffer';
export enum KeyActionId {
@@ -29,7 +28,7 @@ export let keyActionType = {
PlayMacroAction : 'playMacro'
};
export abstract class KeyAction extends Serializable<KeyAction> {
export abstract class KeyAction {
assertKeyActionType(jsObject: any): void {
let keyActionClassname: string = this.constructor.name;
@@ -53,6 +52,6 @@ export abstract class KeyAction extends Serializable<KeyAction> {
return readKeyActionId;
}
abstract _toJsonObject(): any;
abstract _toBinary(buffer: UhkBuffer): void;
abstract toJsonObject(): any;
abstract toBinary(buffer: UhkBuffer): void;
}

View File

@@ -63,7 +63,7 @@ export class KeystrokeAction extends KeyAction {
return this;
}
_toJsonObject(): JsonObjectKeystrokeAction {
toJsonObject(): JsonObjectKeystrokeAction {
let jsonObject: JsonObjectKeystrokeAction = {
keyActionType: keyActionType.KeystrokeAction
};
@@ -83,7 +83,7 @@ export class KeystrokeAction extends KeyAction {
return jsonObject;
}
_toBinary(buffer: UhkBuffer) {
toBinary(buffer: UhkBuffer) {
let flags = 0;
let bufferData: number[] = [];

View File

@@ -43,14 +43,14 @@ export class MouseAction extends KeyAction {
return this;
}
_toJsonObject(): any {
toJsonObject(): any {
return {
keyActionType: keyActionType.MouseAction,
mouseAction: MouseActionParam[this.mouseAction]
};
}
_toBinary(buffer: UhkBuffer) {
toBinary(buffer: UhkBuffer) {
buffer.writeUInt8(KeyActionId.MouseAction);
buffer.writeUInt8(this.mouseAction);
}

View File

@@ -19,13 +19,13 @@ export class NoneAction extends KeyAction {
return this;
}
_toJsonObject(): any {
toJsonObject(): any {
return {
keyActionType: keyActionType.NoneAction
};
}
_toBinary(buffer: UhkBuffer) {
toBinary(buffer: UhkBuffer) {
buffer.writeUInt8(KeyActionId.NoneAction);
}

View File

@@ -32,14 +32,14 @@ export class PlayMacroAction extends KeyAction {
return this;
}
_toJsonObject(): any {
toJsonObject(): any {
return {
keyActionType: keyActionType.PlayMacroAction,
macroId: this.macroId
};
}
_toBinary(buffer: UhkBuffer) {
toBinary(buffer: UhkBuffer) {
buffer.writeUInt8(KeyActionId.PlayMacroAction);
buffer.writeUInt8(this.macroId);
}

View File

@@ -30,14 +30,14 @@ export class SwitchKeymapAction extends KeyAction {
return this;
}
_toJsonObject(): any {
toJsonObject(): any {
return {
keyActionType: keyActionType.SwitchKeymapAction,
keymapAbbreviation: this.keymapAbbreviation
};
}
_toBinary(buffer: UhkBuffer) {
toBinary(buffer: UhkBuffer) {
buffer.writeUInt8(KeyActionId.SwitchKeymapAction);
buffer.writeString(this.keymapAbbreviation);
}

View File

@@ -38,7 +38,7 @@ export class SwitchLayerAction extends KeyAction {
return this;
}
_toJsonObject(): any {
toJsonObject(): any {
return {
keyActionType: keyActionType.SwitchLayerAction,
layer: LayerName[this.layer],
@@ -46,7 +46,7 @@ export class SwitchLayerAction extends KeyAction {
};
}
_toBinary(buffer: UhkBuffer) {
toBinary(buffer: UhkBuffer) {
buffer.writeUInt8(KeyActionId.SwitchLayerAction);
buffer.writeUInt8(this.layer);
buffer.writeBoolean(this.isLayerToggleable);

View File

@@ -27,14 +27,14 @@ export class DelayMacroAction extends MacroAction {
return this;
}
_toJsonObject(): any {
toJsonObject(): any {
return {
macroActionType: macroActionType.DelayMacroAction,
delay: this.delay
};
}
_toBinary(buffer: UhkBuffer) {
toBinary(buffer: UhkBuffer) {
buffer.writeUInt8(MacroActionId.DelayMacroAction);
buffer.writeUInt16(this.delay);
}

View File

@@ -55,7 +55,7 @@ export class KeyMacroAction extends MacroAction {
return this;
}
_toJsonObject(): any {
toJsonObject(): any {
let jsObject: JsObjectKeyMacroAction = {
macroActionType: macroActionType.KeyMacroAction,
action: MacroSubAction[this.action]
@@ -72,7 +72,7 @@ export class KeyMacroAction extends MacroAction {
return jsObject;
}
_toBinary(buffer: UhkBuffer) {
toBinary(buffer: UhkBuffer) {
let keyMacroType: number = MacroActionId.KeyMacroAction;
keyMacroType += NUM_OF_COMBINATIONS * this.action;

View File

@@ -1,4 +1,3 @@
import { Serializable } from '../../Serializable';
import { UhkBuffer } from '../../UhkBuffer';
export enum MacroActionId {
@@ -45,7 +44,7 @@ export let macroActionType = {
TextMacroAction : 'text'
};
export abstract class MacroAction extends Serializable<MacroAction> {
export abstract class MacroAction {
assertMacroActionType(jsObject: any) {
let macroActionClassname = this.constructor.name;
let macroActionTypeString = macroActionType[macroActionClassname];
@@ -73,6 +72,6 @@ export abstract class MacroAction extends Serializable<MacroAction> {
return readMacroActionId;
}
abstract _toJsonObject(): any;
abstract _toBinary(buffer: UhkBuffer): void;
abstract toJsonObject(): any;
abstract toBinary(buffer: UhkBuffer): void;
}

View File

@@ -44,7 +44,7 @@ export class MouseButtonMacroAction extends MacroAction {
return this;
}
_toJsonObject(): any {
toJsonObject(): any {
return {
macroActionType: macroActionType.MouseButtonMacroAction,
action: MacroSubAction[this.action],
@@ -52,7 +52,7 @@ export class MouseButtonMacroAction extends MacroAction {
};
}
_toBinary(buffer: UhkBuffer): void {
toBinary(buffer: UhkBuffer): void {
buffer.writeUInt8(MacroActionId.MouseButtonMacroAction + this.action);
buffer.writeUInt8(this.mouseButtonsMask);
}

View File

@@ -33,7 +33,7 @@ export class MoveMouseMacroAction extends MacroAction {
return this;
}
_toJsonObject(): any {
toJsonObject(): any {
return {
macroActionType: macroActionType.MoveMouseMacroAction,
x: this.x,
@@ -41,7 +41,7 @@ export class MoveMouseMacroAction extends MacroAction {
};
}
_toBinary(buffer: UhkBuffer) {
toBinary(buffer: UhkBuffer) {
buffer.writeUInt8(MacroActionId.MoveMouseMacroAction);
buffer.writeInt16(this.x);
buffer.writeInt16(this.y);

View File

@@ -33,7 +33,7 @@ export class ScrollMouseMacroAction extends MacroAction {
return this;
}
_toJsonObject(): any {
toJsonObject(): any {
return {
macroActionType: macroActionType.ScrollMouseMacroAction,
x: this.x,
@@ -41,7 +41,7 @@ export class ScrollMouseMacroAction extends MacroAction {
};
}
_toBinary(buffer: UhkBuffer) {
toBinary(buffer: UhkBuffer) {
buffer.writeUInt8(MacroActionId.ScrollMouseMacroAction);
buffer.writeInt16(this.x);
buffer.writeInt16(this.y);

View File

@@ -25,14 +25,14 @@ export class TextMacroAction extends MacroAction {
return this;
}
_toJsonObject(): any {
toJsonObject(): any {
return {
macroActionType: macroActionType.TextMacroAction,
text: this.text
};
}
_toBinary(buffer: UhkBuffer) {
toBinary(buffer: UhkBuffer) {
buffer.writeUInt8(MacroActionId.TextMacroAction);
buffer.writeString(this.text);
}

View File

@@ -1,5 +1,4 @@
import { UhkConfiguration } from '../src/config-serializer/config-items/UhkConfiguration';
import { Serializable } from '../src/config-serializer/Serializable';
import { UhkBuffer } from '../src/config-serializer/UhkBuffer';
let assert = require('assert');
@@ -8,7 +7,7 @@ let fs = require('fs');
let uhkConfig = JSON.parse(fs.readFileSync('../src/config-serializer/uhk-config.json'));
let config1Js = uhkConfig;
let config1Ts: Serializable<UhkConfiguration> = new UhkConfiguration().fromJsonObject(config1Js);
let config1Ts: UhkConfiguration = new UhkConfiguration().fromJsonObject(config1Js);
let config1Buffer = new UhkBuffer();
config1Ts.toBinary(config1Buffer);
let config1BufferContent = config1Buffer.getBufferContent();