Make Serializable a generic. Make fromJson() and fromBinary() return the generic type.
This commit is contained in:
@@ -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}"';
|
||||
}
|
||||
|
||||
@@ -1,18 +1,19 @@
|
||||
/// <reference path="KeyAction.ts" />
|
||||
/// <reference path="Serializable.ts" />
|
||||
|
||||
class KeyActionNone extends KeyAction implements Serializable {
|
||||
class KeyActionNone extends KeyAction implements Serializable<KeyActionNone> {
|
||||
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 {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
/// <reference path="KeyAction.ts" />
|
||||
/// <reference path="Serializable.ts" />
|
||||
|
||||
class KeystrokeAction extends KeyAction implements Serializable {
|
||||
class KeystrokeAction extends KeyAction implements Serializable<KeystrokeAction> {
|
||||
|
||||
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) {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
/// <reference path="UhkBuffer.ts" />
|
||||
|
||||
interface Serializable {
|
||||
fromJsObject(jsObject: any);
|
||||
fromBinary(buffer: UhkBuffer);
|
||||
interface Serializable<T> {
|
||||
fromJsObject(jsObject: any): T;
|
||||
fromBinary(buffer: UhkBuffer): T;
|
||||
toJsObject(): any;
|
||||
toBinary(buffer: UhkBuffer);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user