diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2019-04-01 16:00:22 +0200 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2019-04-01 16:00:22 +0200 |
| commit | 33c7666cc6d03b393b4e003c04c4ca1d3a4ff5cc (patch) | |
| tree | 6a155396bedff004b0f733ad7fc304b8cc215336 /gui/src/main | |
| parent | bcf731749bddebb2eb25ccc6a5b83b45de8fdb0e (diff) | |
| parent | c54a8e8af8ed5e53151ded7f66d9eea7cf7a5d20 (diff) | |
| download | mullvadvpn-33c7666cc6d03b393b4e003c04c4ca1d3a4ff5cc.tar.xz mullvadvpn-33c7666cc6d03b393b4e003c04c4ca1d3a4ff5cc.zip | |
Merge branch 'fix-typescript-tests'
Diffstat (limited to 'gui/src/main')
| -rw-r--r-- | gui/src/main/jsonrpc-client.ts | 70 | ||||
| -rw-r--r-- | gui/src/main/keyframe-animation.ts | 28 |
2 files changed, 25 insertions, 73 deletions
diff --git a/gui/src/main/jsonrpc-client.ts b/gui/src/main/jsonrpc-client.ts index 2d66aad50d..0a6ada62d8 100644 --- a/gui/src/main/jsonrpc-client.ts +++ b/gui/src/main/jsonrpc-client.ts @@ -246,9 +246,8 @@ export default class JsonRpcClient<T> extends EventEmitter { } private onMessage(obj: object) { - let message: any; + let message: ReturnType<typeof jsonrpc.parseObject>; try { - // @ts-ignore message = jsonrpc.parseObject(obj); } catch (error) { log.error(`Failed to parse JSON-RPC message: ${error} for object`); @@ -256,9 +255,9 @@ export default class JsonRpcClient<T> extends EventEmitter { } if (message.type === 'notification') { - this.onNotification(message); + this.onNotification(message as IJsonRpcNotification); } else { - this.onReply(message); + this.onReply(message as (IJsonRpcErrorResponse | IJsonRpcSuccess)); } } @@ -297,7 +296,7 @@ export default class JsonRpcClient<T> extends EventEmitter { } } -interface ITransport<T> { +export interface ITransport<T> { onOpen: () => void; onMessage: (data: object) => void; onClose: (error?: Error) => void; @@ -306,67 +305,6 @@ interface ITransport<T> { connect(params: T): void; } -export class WebsocketTransport implements ITransport<string> { - public ws?: WebSocket; - - constructor(ws?: WebSocket) { - this.ws = ws; - } - public onOpen = () => { - // no-op - }; - public onMessage = (_message: object) => { - // no-op - }; - public onClose = (_error?: Error) => { - // no-op - }; - - public close() { - if (this.ws) { - this.ws.close(); - } - } - - public send(msg: string) { - if (this.ws) { - this.ws.send(msg); - } - } - - public connect(params: string): void { - if (this.ws) { - this.ws.close(); - } - this.ws = new WebSocket(params); - this.ws.onopen = (_event) => { - this.onOpen(); - }; - this.ws.onmessage = (event) => { - try { - const data = event.data; - if (typeof data === 'string') { - const msg = JSON.parse(data); - this.onMessage(msg); - } else { - throw event; - } - } catch (error) { - log.error('Got invalid reply from server: ', error); - } - }; - - this.ws.onclose = (event) => { - log.info(`The websocket connection closed with code: ${event.code}`); - if (event.code === 1000) { - this.onClose(); - } else { - this.onClose(new WebSocketError(event.code)); - } - }; - } -} - // Given the correct parameters, this transport supports named pipes/unix // domain sockets, and also TCP/UDP sockets export class SocketTransport implements ITransport<{ path: string }> { diff --git a/gui/src/main/keyframe-animation.ts b/gui/src/main/keyframe-animation.ts index 25cd83a5db..1a95e1a106 100644 --- a/gui/src/main/keyframe-animation.ts +++ b/gui/src/main/keyframe-animation.ts @@ -13,7 +13,7 @@ export default class KeyframeAnimation { private onFrameValue?: OnFrameFn; private onFinishValue?: OnFinishFn; - private currentFrame: number = 0; + private currentFrameValue: number = 0; private targetFrame: number = 0; private isRunningValue: boolean = false; @@ -21,6 +21,20 @@ export default class KeyframeAnimation { private timeout?: NodeJS.Timeout; + get currentFrame(): number { + return this.currentFrameValue; + } + + // This setter is only meant to be used when running tests + // @internal + set currentFrame(newValue: number) { + if (process.env.NODE_ENV === 'test') { + this.currentFrameValue = newValue; + } else { + throw new Error('The setter for currentFrame is only available in test environment.'); + } + } + set onFrame(newValue: OnFrameFn | undefined) { this.onFrameValue = newValue; } @@ -56,7 +70,7 @@ export default class KeyframeAnimation { const { start, end } = options; if (start !== undefined) { - this.currentFrame = start; + this.currentFrameValue = start; } this.targetFrame = end; @@ -88,7 +102,7 @@ export default class KeyframeAnimation { private render() { if (this.onFrameValue) { - this.onFrameValue(this.currentFrame); + this.onFrameValue(this.currentFrameValue); } } @@ -119,12 +133,12 @@ export default class KeyframeAnimation { return; } - if (this.currentFrame === this.targetFrame) { + if (this.currentFrameValue === this.targetFrame) { this.didFinish(); - } else if (this.currentFrame < this.targetFrame) { - this.currentFrame += 1; + } else if (this.currentFrameValue < this.targetFrame) { + this.currentFrameValue += 1; } else { - this.currentFrame -= 1; + this.currentFrameValue -= 1; } } } |
