Skip to content

Commit

Permalink
Merge pull request #2 from Dashlane/expose-transportmessage
Browse files Browse the repository at this point in the history
Expose transport message interfaces
  • Loading branch information
lguychard authored Apr 11, 2018
2 parents a2a22e5 + e115155 commit 56898f7
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 18 deletions.
8 changes: 5 additions & 3 deletions src/Channel.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) {
Expand All @@ -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))
}

Expand Down
10 changes: 10 additions & 0 deletions src/Message.ts
Original file line number Diff line number Diff line change
@@ -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
17 changes: 7 additions & 10 deletions src/Transport.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Handler, callHandlers } from './Handler'
import { Channel } from './Channel'
import {
TransportRegistrationMessage,
TransportError,
TransportRequest,
TransportResponse,
TransportMessage
} from './Message'

let _ID = 0

Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { slot, Slot } from './Slot'
export { EventDeclaration, combineEvents, createEventBus } from './Events'
export { Channel, GenericChannel } from './Channel'
export { TransportMessage } from './Message'
2 changes: 1 addition & 1 deletion test/Event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
10 changes: 7 additions & 3 deletions test/TestChannel.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion test/Transport.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down

0 comments on commit 56898f7

Please sign in to comment.