Make KeyAction.assertKeyActiontype() and KeyAction.readAndAssertKeyActionId() only expect a single argument.
This commit is contained in:
@@ -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[<string> 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;
|
||||
|
||||
@@ -26,16 +26,19 @@ let keyActionType = {
|
||||
};
|
||||
|
||||
abstract class KeyAction extends Serializable<KeyAction> {
|
||||
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[<string> classname];
|
||||
if (readKeyActionId !== keyActionId) {
|
||||
throw `Invalid ${classname} first byte: ${readKeyActionId}`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ class KeyActions extends ClassArray<KeyAction> {
|
||||
case keyActionType.PlayMacroAction:
|
||||
return new PlayMacroAction().fromJsObject(jsObject);
|
||||
default:
|
||||
throw `Invalid KeyAction.keyActionType: "${jsObject.actionType}"`;
|
||||
throw `Invalid KeyAction.keyActionType: "${jsObject.keyActionType}"`;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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[<string> jsObject.mouseAction];
|
||||
return this;
|
||||
}
|
||||
|
||||
_fromBinary(buffer: UhkBuffer): MouseAction {
|
||||
this.readAndAssertKeyActionId(buffer, KeyActionId.MouseAction, 'MouseAction');
|
||||
this.readAndAssertKeyActionId(buffer);
|
||||
this.mouseAction = buffer.readUInt8();
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -18,14 +18,14 @@ class SwitchLayerAction extends KeyAction {
|
||||
}
|
||||
|
||||
_fromJsObject(jsObject: any): SwitchLayerAction {
|
||||
this.assertKeyActionType(jsObject, keyActionType.SwitchLayerAction, 'SwitchLayerAction');
|
||||
this.assertKeyActionType(jsObject);
|
||||
this.layer = Layer[<string> 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.
|
||||
|
||||
Reference in New Issue
Block a user