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 {
|
static fromJsObject(jsObject: any): KeyAction {
|
||||||
switch (jsObject.keyActionType) {
|
switch (jsObject.keyActionType) {
|
||||||
case KeystrokeAction.actionTypeString:
|
case KeystrokeAction.actionTypeString:
|
||||||
let keyActionNone = new KeyActionNone();
|
return new KeyActionNone().fromJsObject(jsObject);
|
||||||
keyActionNone.fromJsObject(jsObject);
|
|
||||||
return keyActionNone;
|
|
||||||
case KeystrokeAction.actionTypeString:
|
case KeystrokeAction.actionTypeString:
|
||||||
let keystrokeAction = new KeystrokeAction();
|
return new KeystrokeAction().fromJsObject(jsObject);
|
||||||
keystrokeAction.fromJsObject(jsObject);
|
|
||||||
return keystrokeAction;
|
|
||||||
default:
|
default:
|
||||||
throw 'Unknown KeyAction actionType "${jsObject.actionType}"';
|
throw 'Unknown KeyAction actionType "${jsObject.actionType}"';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static fromBinary(buffer: UhkBuffer) {
|
static fromBinary(buffer: UhkBuffer): KeyAction {
|
||||||
let keyActionFirstByte = buffer.readUInt8();
|
let keyActionFirstByte = buffer.readUInt8();
|
||||||
buffer.backtrack();
|
buffer.backtrack();
|
||||||
|
|
||||||
if (KeystrokeAction.isScancodeValid(keyActionFirstByte)) {
|
if (KeystrokeAction.isScancodeValid(keyActionFirstByte)) {
|
||||||
let keystrokeAction = new KeystrokeAction();
|
return new KeystrokeAction().fromBinary(buffer);
|
||||||
keystrokeAction.fromBinary(buffer);
|
|
||||||
return keystrokeAction;
|
|
||||||
} else if (keyActionFirstByte === KeyActionNone.keyActionNoneId) {
|
} else if (keyActionFirstByte === KeyActionNone.keyActionNoneId) {
|
||||||
let keyActionNone = new KeyActionNone();
|
return new KeyActionNone().fromBinary(buffer);
|
||||||
keyActionNone.fromBinary(buffer);
|
|
||||||
return keyActionNone;
|
|
||||||
} else {
|
} else {
|
||||||
throw 'Unknown KeyAction first byte "${keyActionFirstByte}"';
|
throw 'Unknown KeyAction first byte "${keyActionFirstByte}"';
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,19 @@
|
|||||||
/// <reference path="KeyAction.ts" />
|
/// <reference path="KeyAction.ts" />
|
||||||
/// <reference path="Serializable.ts" />
|
/// <reference path="Serializable.ts" />
|
||||||
|
|
||||||
class KeyActionNone extends KeyAction implements Serializable {
|
class KeyActionNone extends KeyAction implements Serializable<KeyActionNone> {
|
||||||
static actionTypeString = 'none';
|
static actionTypeString = 'none';
|
||||||
static keyActionNoneId = 0;
|
static keyActionNoneId = 0;
|
||||||
static keyActionNoneParam = 0;
|
static keyActionNoneParam = 0;
|
||||||
|
|
||||||
fromJsObject(jsObject: any) {
|
fromJsObject(jsObject: any): KeyActionNone {
|
||||||
if (jsObject.actionType !== 'none') {
|
if (jsObject.actionType !== 'none') {
|
||||||
throw 'KeyActionNone: The actionType is not "none"';
|
throw 'KeyActionNone: The actionType is not "none"';
|
||||||
}
|
}
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
fromBinary(buffer: UhkBuffer) {
|
fromBinary(buffer: UhkBuffer): KeyActionNone {
|
||||||
let keyActionId = buffer.readUInt8();
|
let keyActionId = buffer.readUInt8();
|
||||||
if (keyActionId !== KeyActionNone.keyActionNoneId) {
|
if (keyActionId !== KeyActionNone.keyActionNoneId) {
|
||||||
throw 'KeyActionNone: id is ${keyActionId} instead of ${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) {
|
if (keyActionParam !== KeyActionNone.keyActionNoneParam) {
|
||||||
throw 'KeyActionNone: The param is ${keyActionParam} instead of ${KeyActionNone.keyActionNoneParam}';
|
throw 'KeyActionNone: The param is ${keyActionParam} instead of ${KeyActionNone.keyActionNoneParam}';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
toJsObject(): any {
|
toJsObject(): any {
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
/// <reference path="KeyAction.ts" />
|
/// <reference path="KeyAction.ts" />
|
||||||
/// <reference path="Serializable.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 firstValidScancode = 1;
|
||||||
static lastValidScancode = 231;
|
static lastValidScancode = 231;
|
||||||
|
|
||||||
@@ -26,22 +26,24 @@ class KeystrokeAction extends KeyAction implements Serializable {
|
|||||||
scancode <= KeystrokeAction.lastValidScancode;
|
scancode <= KeystrokeAction.lastValidScancode;
|
||||||
}
|
}
|
||||||
|
|
||||||
fromJsObject(jsObject: any) {
|
fromJsObject(jsObject: any): KeystrokeAction {
|
||||||
this.scancode = jsObject.scancode;
|
this.scancode = jsObject.scancode;
|
||||||
this.modifierMask = jsObject.modifierMask;
|
this.modifierMask = jsObject.modifierMask;
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
toJsObject(): any {
|
toJsObject(): any {
|
||||||
return {
|
return {
|
||||||
keyActionType: 'keystroke',
|
keyActionType: KeystrokeAction.actionTypeString,
|
||||||
scancode: this.scancode,
|
scancode: this.scancode,
|
||||||
modifierMask: this.modifierMask
|
modifierMask: this.modifierMask
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fromBinary(buffer: UhkBuffer) {
|
fromBinary(buffer: UhkBuffer): KeystrokeAction {
|
||||||
this.scancode = buffer.readUInt8();
|
this.scancode = buffer.readUInt8();
|
||||||
this.modifierMask = buffer.readUInt8();
|
this.modifierMask = buffer.readUInt8();
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
toBinary(buffer: UhkBuffer) {
|
toBinary(buffer: UhkBuffer) {
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
/// <reference path="UhkBuffer.ts" />
|
/// <reference path="UhkBuffer.ts" />
|
||||||
|
|
||||||
interface Serializable {
|
interface Serializable<T> {
|
||||||
fromJsObject(jsObject: any);
|
fromJsObject(jsObject: any): T;
|
||||||
fromBinary(buffer: UhkBuffer);
|
fromBinary(buffer: UhkBuffer): T;
|
||||||
toJsObject(): any;
|
toJsObject(): any;
|
||||||
toBinary(buffer: UhkBuffer);
|
toBinary(buffer: UhkBuffer);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user