From 0aeec88cb57bdbadfd43d6b9afcffb97d69fe057 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Tue, 29 Mar 2016 00:17:11 +0200 Subject: [PATCH] Make Serializable a generic. Make fromJson() and fromBinary() return the generic type. --- model/KeyAction.ts | 18 +++++------------- model/KeyActionNone.ts | 9 ++++++--- model/KeystrokeAction.ts | 12 +++++++----- model/Serializable.ts | 6 +++--- 4 files changed, 21 insertions(+), 24 deletions(-) diff --git a/model/KeyAction.ts b/model/KeyAction.ts index 0634546e..229eb16b 100644 --- a/model/KeyAction.ts +++ b/model/KeyAction.ts @@ -6,30 +6,22 @@ class KeyAction { static fromJsObject(jsObject: any): KeyAction { switch (jsObject.keyActionType) { case KeystrokeAction.actionTypeString: - let keyActionNone = new KeyActionNone(); - keyActionNone.fromJsObject(jsObject); - return keyActionNone; + return new KeyActionNone().fromJsObject(jsObject); case KeystrokeAction.actionTypeString: - let keystrokeAction = new KeystrokeAction(); - keystrokeAction.fromJsObject(jsObject); - return keystrokeAction; + return new KeystrokeAction().fromJsObject(jsObject); default: throw 'Unknown KeyAction actionType "${jsObject.actionType}"'; } } - static fromBinary(buffer: UhkBuffer) { + static fromBinary(buffer: UhkBuffer): KeyAction { let keyActionFirstByte = buffer.readUInt8(); buffer.backtrack(); if (KeystrokeAction.isScancodeValid(keyActionFirstByte)) { - let keystrokeAction = new KeystrokeAction(); - keystrokeAction.fromBinary(buffer); - return keystrokeAction; + return new KeystrokeAction().fromBinary(buffer); } else if (keyActionFirstByte === KeyActionNone.keyActionNoneId) { - let keyActionNone = new KeyActionNone(); - keyActionNone.fromBinary(buffer); - return keyActionNone; + return new KeyActionNone().fromBinary(buffer); } else { throw 'Unknown KeyAction first byte "${keyActionFirstByte}"'; } diff --git a/model/KeyActionNone.ts b/model/KeyActionNone.ts index 78a55625..6993182b 100644 --- a/model/KeyActionNone.ts +++ b/model/KeyActionNone.ts @@ -1,18 +1,19 @@ /// /// -class KeyActionNone extends KeyAction implements Serializable { +class KeyActionNone extends KeyAction implements Serializable { static actionTypeString = 'none'; static keyActionNoneId = 0; static keyActionNoneParam = 0; - fromJsObject(jsObject: any) { + fromJsObject(jsObject: any): KeyActionNone { if (jsObject.actionType !== 'none') { throw 'KeyActionNone: The actionType is not "none"'; } + return this; } - fromBinary(buffer: UhkBuffer) { + fromBinary(buffer: UhkBuffer): KeyActionNone { let keyActionId = buffer.readUInt8(); if (keyActionId !== KeyActionNone.keyActionNoneId) { throw 'KeyActionNone: id is ${keyActionId} instead of ${KeyActionNone.keyActionNoneId}'; @@ -22,6 +23,8 @@ class KeyActionNone extends KeyAction implements Serializable { if (keyActionParam !== KeyActionNone.keyActionNoneParam) { throw 'KeyActionNone: The param is ${keyActionParam} instead of ${KeyActionNone.keyActionNoneParam}'; } + + return this; } toJsObject(): any { diff --git a/model/KeystrokeAction.ts b/model/KeystrokeAction.ts index b34d53e9..b9592264 100644 --- a/model/KeystrokeAction.ts +++ b/model/KeystrokeAction.ts @@ -1,9 +1,9 @@ /// /// -class KeystrokeAction extends KeyAction implements Serializable { +class KeystrokeAction extends KeyAction implements Serializable { - static actionTypeString = 'keyStroke'; + static actionTypeString = 'keystroke'; static firstValidScancode = 1; static lastValidScancode = 231; @@ -26,22 +26,24 @@ class KeystrokeAction extends KeyAction implements Serializable { scancode <= KeystrokeAction.lastValidScancode; } - fromJsObject(jsObject: any) { + fromJsObject(jsObject: any): KeystrokeAction { this.scancode = jsObject.scancode; this.modifierMask = jsObject.modifierMask; + return this; } toJsObject(): any { return { - keyActionType: 'keystroke', + keyActionType: KeystrokeAction.actionTypeString, scancode: this.scancode, modifierMask: this.modifierMask }; } - fromBinary(buffer: UhkBuffer) { + fromBinary(buffer: UhkBuffer): KeystrokeAction { this.scancode = buffer.readUInt8(); this.modifierMask = buffer.readUInt8(); + return this; } toBinary(buffer: UhkBuffer) { diff --git a/model/Serializable.ts b/model/Serializable.ts index 48259cd9..59c77891 100644 --- a/model/Serializable.ts +++ b/model/Serializable.ts @@ -1,8 +1,8 @@ /// -interface Serializable { - fromJsObject(jsObject: any); - fromBinary(buffer: UhkBuffer); +interface Serializable { + fromJsObject(jsObject: any): T; + fromBinary(buffer: UhkBuffer): T; toJsObject(): any; toBinary(buffer: UhkBuffer); }