From e115155032c89f0a16cb32b62ae94a8065084f80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Guychard?= Date: Mon, 26 Mar 2018 14:38:50 +0200 Subject: [PATCH] Expose transport message interfaces so that channel developers can develop richer channels --- src/Channel.ts | 8 +++++--- src/Message.ts | 10 ++++++++++ src/Transport.ts | 17 +++++++---------- src/index.ts | 1 + test/Event.test.ts | 2 +- test/TestChannel.ts | 10 +++++++--- test/Transport.test.ts | 3 ++- 7 files changed, 33 insertions(+), 18 deletions(-) create mode 100644 src/Message.ts diff --git a/src/Channel.ts b/src/Channel.ts index a7fb3e4..ad409d2 100644 --- a/src/Channel.ts +++ b/src/Channel.ts @@ -1,8 +1,10 @@ +import { TransportMessage } from './Message' + export type OnMessageCallback = (message: {}) => void export interface Channel { timeout?: number - send: (message: {}) => void + send: (message: TransportMessage) => void onData: (cb: OnMessageCallback) => void onConnect: (cb: () => void) => void onDisconnect: (cb: () => void) => void @@ -18,7 +20,7 @@ export abstract class GenericChannel implements Channel { private _onDisconnectCallbacks: Function[] = [] private _onErrorCallbacks: Function[] = [] private _ready = false - public abstract send(message: {}): void + public abstract send(message: TransportMessage): void public onData(cb: OnMessageCallback): void { if (this._onMessageCallbacks.indexOf(cb) === -1) { @@ -41,7 +43,7 @@ export abstract class GenericChannel implements Channel { this._onErrorCallbacks.push(cb) } - protected _messageReceived(message: {}) { + protected _messageReceived(message: TransportMessage) { this._onMessageCallbacks.forEach(cb => cb(message)) } diff --git a/src/Message.ts b/src/Message.ts new file mode 100644 index 0000000..6e0b1bb --- /dev/null +++ b/src/Message.ts @@ -0,0 +1,10 @@ + +export type TransportRequest = { type: 'request', slotName: string, id: string, data: any } +export type TransportResponse = { type: 'response', slotName: string, id: string, data: any } +export type TransportError = { type: 'error', slotName: string, id: string, message: string, stack?: string } +export type TransportRegistrationMessage = { type: 'handler_registered', slotName: string } +export type TransportMessage = + TransportRegistrationMessage + | TransportRequest + | TransportResponse + | TransportError diff --git a/src/Transport.ts b/src/Transport.ts index 52512e4..edc276f 100644 --- a/src/Transport.ts +++ b/src/Transport.ts @@ -1,5 +1,12 @@ import { Handler, callHandlers } from './Handler' import { Channel } from './Channel' +import { + TransportRegistrationMessage, + TransportError, + TransportRequest, + TransportResponse, + TransportMessage +} from './Message' let _ID = 0 @@ -17,16 +24,6 @@ const ERRORS = { CHANNEL_NOT_READY: 'CHANNEL_NOT_READY' } -export type TransportRequest = { type: 'request', slotName: string, id: string, data: any } -export type TransportResponse = { type: 'response', slotName: string, id: string, data: any } -export type TransportError = { type: 'error', slotName: string, id: string, message: string, stack?: string } -export type TransportRegistrationMessage = { type: 'handler_registered', slotName: string } -export type TransportMessage = - TransportRegistrationMessage - | TransportRequest - | TransportResponse - | TransportError - export type PendingRequest = { resolve: (data?: any) => void; reject: (e: Error) => void; diff --git a/src/index.ts b/src/index.ts index c7f87d5..65bdfe7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ export { slot, Slot } from './Slot' export { EventDeclaration, combineEvents, createEventBus } from './Events' export { Channel, GenericChannel } from './Channel' +export { TransportMessage } from './Message' diff --git a/test/Event.test.ts b/test/Event.test.ts index 958e3b0..ead44a8 100644 --- a/test/Event.test.ts +++ b/test/Event.test.ts @@ -3,7 +3,7 @@ import 'should' import {slot} from './../src/Slot' import {combineEvents, createEventBus} from './../src/Events' import {GenericChannel} from './../src/Channel' -import {TransportMessage} from './../src/Transport' +import {TransportMessage} from './../src/Message' import {TestChannel} from './TestChannel' import * as sinon from 'sinon' diff --git a/test/TestChannel.ts b/test/TestChannel.ts index 0d81d9a..80cfcba 100644 --- a/test/TestChannel.ts +++ b/test/TestChannel.ts @@ -1,5 +1,4 @@ -import {GenericChannel} from './../src/Channel' -import {TransportMessage} from './../src/Transport' +import {TransportMessage, GenericChannel} from './../src/' import * as sinon from 'sinon' export class TestChannel extends GenericChannel { @@ -23,7 +22,12 @@ export class TestChannel extends GenericChannel { } public callMessageReceived() { - this._messageReceived({ type: 'test' }) + this._messageReceived({ + type: 'error', + slotName: 'test', + id: '1', + message: 'error' + }) } public callError() { diff --git a/test/Transport.test.ts b/test/Transport.test.ts index 9e8612f..2f52350 100644 --- a/test/Transport.test.ts +++ b/test/Transport.test.ts @@ -1,6 +1,7 @@ import 'should' -import { Transport, TransportMessage } from './../src/Transport' +import { Transport } from './../src/Transport' +import { TransportMessage } from './../src/Message' import { TestChannel } from './TestChannel' import * as sinon from 'sinon' import { createEventBus } from '../src/Events'