From 34ff0d657d8db6481f36383b53fdc7eea51d0d91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Sat, 9 Apr 2016 00:39:17 +0200 Subject: [PATCH] Make KeyAction.assertKeyActiontype() and KeyAction.readAndAssertKeyActionId() only expect a single argument. --- .../config-items/DualRoleKeystrokeAction.ts | 4 ++-- config-serializer/config-items/KeyAction.ts | 13 ++++++++----- config-serializer/config-items/KeyActions.ts | 2 +- config-serializer/config-items/KeystrokeAction.ts | 4 ++-- .../config-items/KeystrokeModifiersAction.ts | 5 ++--- .../config-items/KeystrokeWithModifiersAction.ts | 5 ++--- config-serializer/config-items/MouseAction.ts | 4 ++-- config-serializer/config-items/NoneAction.ts | 4 ++-- config-serializer/config-items/PlayMacroAction.ts | 4 ++-- .../config-items/SwitchKeymapAction.ts | 4 ++-- config-serializer/config-items/SwitchLayerAction.ts | 4 ++-- 11 files changed, 27 insertions(+), 26 deletions(-) diff --git a/config-serializer/config-items/DualRoleKeystrokeAction.ts b/config-serializer/config-items/DualRoleKeystrokeAction.ts index 95494901..6f04c36d 100644 --- a/config-serializer/config-items/DualRoleKeystrokeAction.ts +++ b/config-serializer/config-items/DualRoleKeystrokeAction.ts @@ -21,14 +21,14 @@ class DualRoleKeystrokeAction extends KeyAction { private longPressAction: LongPressAction; _fromJsObject(jsObject: any): DualRoleKeystrokeAction { - this.assertKeyActionType(jsObject, keyActionType.DualRoleKeystrokeAction, 'DualRoleKeystrokeAction'); + this.assertKeyActionType(jsObject); this.scancode = jsObject.scancode; this.longPressAction = LongPressAction[ jsObject.longPressAction]; return this; } _fromBinary(buffer: UhkBuffer): DualRoleKeystrokeAction { - this.readAndAssertKeyActionId(buffer, KeyActionId.DualRoleKeystrokeAction, 'DualRoleKeystrokeAction'); + this.readAndAssertKeyActionId(buffer); this.scancode = buffer.readUInt8(); this.longPressAction = buffer.readUInt8(); return this; diff --git a/config-serializer/config-items/KeyAction.ts b/config-serializer/config-items/KeyAction.ts index 4f66255f..e43e2f61 100644 --- a/config-serializer/config-items/KeyAction.ts +++ b/config-serializer/config-items/KeyAction.ts @@ -26,16 +26,19 @@ let keyActionType = { }; abstract class KeyAction extends Serializable { - assertKeyActionType(jsObject: any, keyActionTypeString: string, classname: string) { + assertKeyActionType(jsObject: any) { + let keyActionClassname = this.constructor.name; + let keyActionTypeString = keyActionType[keyActionClassname] if (jsObject.keyActionType !== keyActionTypeString) { - console.log(arguments.callee.prototype.name); - throw `Invalid ${classname}.keyActionType: ${jsObject.keyActionType}`; + throw `Invalid ${keyActionClassname}.keyActionType: ${jsObject.keyActionType}`; } } - readAndAssertKeyActionId(buffer: UhkBuffer, keyActionIdParam: KeyActionId, classname: string) { + readAndAssertKeyActionId(buffer: UhkBuffer) { + let classname = this.constructor.name; let readKeyActionId = buffer.readUInt8(); - if (readKeyActionId !== keyActionIdParam) { + let keyActionId = KeyActionId[ classname]; + if (readKeyActionId !== keyActionId) { throw `Invalid ${classname} first byte: ${readKeyActionId}`; } } diff --git a/config-serializer/config-items/KeyActions.ts b/config-serializer/config-items/KeyActions.ts index 965842af..21e19b7c 100644 --- a/config-serializer/config-items/KeyActions.ts +++ b/config-serializer/config-items/KeyActions.ts @@ -21,7 +21,7 @@ class KeyActions extends ClassArray { case keyActionType.PlayMacroAction: return new PlayMacroAction().fromJsObject(jsObject); default: - throw `Invalid KeyAction.keyActionType: "${jsObject.actionType}"`; + throw `Invalid KeyAction.keyActionType: "${jsObject.keyActionType}"`; } } diff --git a/config-serializer/config-items/KeystrokeAction.ts b/config-serializer/config-items/KeystrokeAction.ts index 255b26c3..9468fe35 100644 --- a/config-serializer/config-items/KeystrokeAction.ts +++ b/config-serializer/config-items/KeystrokeAction.ts @@ -4,13 +4,13 @@ class KeystrokeAction extends KeyAction { scancode: number; _fromJsObject(jsObject: any): KeystrokeAction { - this.assertKeyActionType(jsObject, keyActionType.KeystrokeAction, 'KeystrokeAction'); + this.assertKeyActionType(jsObject); this.scancode = jsObject.scancode; return this; } _fromBinary(buffer: UhkBuffer): KeystrokeAction { - this.readAndAssertKeyActionId(buffer, KeyActionId.KeystrokeAction, 'KeystrokeAction'); + this.readAndAssertKeyActionId(buffer); this.scancode = buffer.readUInt8(); return this; } diff --git a/config-serializer/config-items/KeystrokeModifiersAction.ts b/config-serializer/config-items/KeystrokeModifiersAction.ts index 9a7013fe..b40e009a 100644 --- a/config-serializer/config-items/KeystrokeModifiersAction.ts +++ b/config-serializer/config-items/KeystrokeModifiersAction.ts @@ -4,14 +4,13 @@ class KeystrokeModifiersAction extends KeyAction { modifierMask: number; _fromJsObject(jsObject: any): KeystrokeModifiersAction { - this.assertKeyActionType( - jsObject, keyActionType.KeystrokeModifiersAction, 'KeystrokeModifiersAction'); + this.assertKeyActionType(jsObject); this.modifierMask = jsObject.modifierMask; return this; } _fromBinary(buffer: UhkBuffer): KeystrokeModifiersAction { - this.readAndAssertKeyActionId(buffer, KeyActionId.KeystrokeModifiersAction, 'KeystrokeModifiersAction'); + this.readAndAssertKeyActionId(buffer); this.modifierMask = buffer.readUInt8(); return this; } diff --git a/config-serializer/config-items/KeystrokeWithModifiersAction.ts b/config-serializer/config-items/KeystrokeWithModifiersAction.ts index fb3b91a4..393bbd0b 100644 --- a/config-serializer/config-items/KeystrokeWithModifiersAction.ts +++ b/config-serializer/config-items/KeystrokeWithModifiersAction.ts @@ -7,15 +7,14 @@ class KeystrokeWithModifiersAction extends KeyAction { scancode: number; _fromJsObject(jsObject: any): KeystrokeWithModifiersAction { - this.assertKeyActionType( - jsObject, keyActionType.KeystrokeWithModifiersAction, 'KeystrokeWithModifiersAction'); + this.assertKeyActionType(jsObject); this.scancode = jsObject.scancode; this.modifierMask = jsObject.modifierMask; return this; } _fromBinary(buffer: UhkBuffer): KeystrokeWithModifiersAction { - this.readAndAssertKeyActionId(buffer, KeyActionId.KeystrokeWithModifiersAction, 'KeystrokeWithModifiersAction'); + this.readAndAssertKeyActionId(buffer); this.scancode = buffer.readUInt8(); this.modifierMask = buffer.readUInt8(); return this; diff --git a/config-serializer/config-items/MouseAction.ts b/config-serializer/config-items/MouseAction.ts index 345dc948..88eade07 100644 --- a/config-serializer/config-items/MouseAction.ts +++ b/config-serializer/config-items/MouseAction.ts @@ -20,13 +20,13 @@ class MouseAction extends KeyAction { mouseAction: MouseActionParam; _fromJsObject(jsObject: any): MouseAction { - this.assertKeyActionType(jsObject, keyActionType.MouseAction, 'MouseAction'); + this.assertKeyActionType(jsObject); this.mouseAction = MouseActionParam[ jsObject.mouseAction]; return this; } _fromBinary(buffer: UhkBuffer): MouseAction { - this.readAndAssertKeyActionId(buffer, KeyActionId.MouseAction, 'MouseAction'); + this.readAndAssertKeyActionId(buffer); this.mouseAction = buffer.readUInt8(); return this; } diff --git a/config-serializer/config-items/NoneAction.ts b/config-serializer/config-items/NoneAction.ts index 3c18699a..77c27286 100644 --- a/config-serializer/config-items/NoneAction.ts +++ b/config-serializer/config-items/NoneAction.ts @@ -1,12 +1,12 @@ class NoneAction extends KeyAction { _fromJsObject(jsObject: any): NoneAction { - this.assertKeyActionType(jsObject, keyActionType.NoneAction, 'NoneAction'); + this.assertKeyActionType(jsObject); return this; } _fromBinary(buffer: UhkBuffer): NoneAction { - this.readAndAssertKeyActionId(buffer, KeyActionId.NoneAction, 'NoneAction'); + this.readAndAssertKeyActionId(buffer); return this; } diff --git a/config-serializer/config-items/PlayMacroAction.ts b/config-serializer/config-items/PlayMacroAction.ts index c0c9e0f5..784f57b7 100644 --- a/config-serializer/config-items/PlayMacroAction.ts +++ b/config-serializer/config-items/PlayMacroAction.ts @@ -4,13 +4,13 @@ class PlayMacroAction extends KeyAction { macroId: number; _fromJsObject(jsObject: any): PlayMacroAction { - this.assertKeyActionType(jsObject, keyActionType.PlayMacroAction, 'PlayMacroAction'); + this.assertKeyActionType(jsObject); this.macroId = jsObject.macroId; return this; } _fromBinary(buffer: UhkBuffer): PlayMacroAction { - this.readAndAssertKeyActionId(buffer, KeyActionId.PlayMacroAction, 'PlayMacroAction'); + this.readAndAssertKeyActionId(buffer); this.macroId = buffer.readUInt8(); return this; } diff --git a/config-serializer/config-items/SwitchKeymapAction.ts b/config-serializer/config-items/SwitchKeymapAction.ts index 3156e7ff..ea09fe8e 100644 --- a/config-serializer/config-items/SwitchKeymapAction.ts +++ b/config-serializer/config-items/SwitchKeymapAction.ts @@ -4,13 +4,13 @@ class SwitchKeymapAction extends KeyAction { keymapId: number; _fromJsObject(jsObject: any): SwitchKeymapAction { - this.assertKeyActionType(jsObject, keyActionType.SwitchKeymapAction, 'SwitchKeymapAction'); + this.assertKeyActionType(jsObject); this.keymapId = jsObject.keymapId; return this; } _fromBinary(buffer: UhkBuffer): SwitchKeymapAction { - this.readAndAssertKeyActionId(buffer, KeyActionId.SwitchKeymapAction, 'SwitchKeymapAction'); + this.readAndAssertKeyActionId(buffer); this.keymapId = buffer.readUInt8(); return this; } diff --git a/config-serializer/config-items/SwitchLayerAction.ts b/config-serializer/config-items/SwitchLayerAction.ts index 0c8d4a3d..65965952 100644 --- a/config-serializer/config-items/SwitchLayerAction.ts +++ b/config-serializer/config-items/SwitchLayerAction.ts @@ -18,14 +18,14 @@ class SwitchLayerAction extends KeyAction { } _fromJsObject(jsObject: any): SwitchLayerAction { - this.assertKeyActionType(jsObject, keyActionType.SwitchLayerAction, 'SwitchLayerAction'); + this.assertKeyActionType(jsObject); this.layer = Layer[ jsObject.layer]; this.isLayerToggleable = jsObject.toggle; return this; } _fromBinary(buffer: UhkBuffer): SwitchLayerAction { - this.readAndAssertKeyActionId(buffer, KeyActionId.SwitchLayerAction, 'SwitchLayerAction'); + this.readAndAssertKeyActionId(buffer); let layer = buffer.readUInt8(); this.isLayerToggleable = (layer & SwitchLayerAction.toggleFlag) !== 0; layer &= ~SwitchLayerAction.toggleFlag; // Clear toggle bit.