Skip to content

Commit

Permalink
Merge pull request #16 from Alexandre-Herve/bugfix/fix-slot-config
Browse files Browse the repository at this point in the history
Fix notConnectedSlot config assignment
  • Loading branch information
Alexandre-Herve authored Mar 26, 2020
2 parents dd2aa46 + 90e93ba commit 68f9526
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
37 changes: 21 additions & 16 deletions src/Slot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@ import { DEFAULT_PARAM } from './Constants'

const signalNotConnected = () => { throw new Error('Slot not connected') }

const notConnectedSlot: Slot<any, any> = Object.assign(
() => signalNotConnected(),
{
on: signalNotConnected,
lazy: signalNotConnected,
slotName: 'Not connected'
}
)
interface SlotConfig {
// This option will prevent the slot from buffering the
// requests if no remote handlers are set for some transports.
noBuffer?: boolean
}

export const defaultSlotConfig = { noBuffer: false }

const getNotConnectedSlot = (config: SlotConfig): Slot<any, any> =>
Object.assign(
() => signalNotConnected(),
{
config,
lazy: () => signalNotConnected,
on: () => signalNotConnected,
slotName: 'Not connected'
}
)

export type LazyCallback = (param: string) => void
export type Unsubscribe = () => void
Expand Down Expand Up @@ -41,12 +51,6 @@ const findAllUsedParams = (handlers: Handlers): string[] =>
return paramsUniq
}, [] as string[])

interface SlotConfig {
// This option will prevent the slot from buffering the
// requests if no remote handlers are set for some transports.
noBuffer?: boolean
}

/**
* Represents an event shared by two modules.
*
Expand Down Expand Up @@ -81,15 +85,16 @@ export interface Slot<RequestData=null, ResponseData=void> {
slotName: string
}


/**
* A shorthand function used to declare slots in event bus object literals
* It returns a fake slot, that will throw if triggered or subscribed to.
* Slots need to be connected in order to be functional.
*/
export function slot<RequestData=void, ResponseData=void>(
config: SlotConfig = { noBuffer: false }
config: SlotConfig = defaultSlotConfig
): Slot<RequestData, ResponseData> {
return Object.assign(notConnectedSlot, config)
return getNotConnectedSlot(config)
}

export function connectSlot<T=void, T2=void>(
Expand Down
20 changes: 19 additions & 1 deletion test/Slot.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'should'
import { spy } from 'sinon'

import { connectSlot } from './../src/Slot'
import { connectSlot, slot, defaultSlotConfig } from './../src/Slot'
import { TestChannel } from './TestChannel'
import { Transport } from './../src/Transport'
import { DEFAULT_PARAM } from './../src/Constants'
Expand All @@ -14,6 +14,24 @@ const makeTestTransport = () => {
return { channel, transport }
}

describe('slot', () => {
it('should have a default config', () => {
const testSlot = slot()
if (!testSlot.config) {
throw new Error('testSlot should have a config')
}
testSlot.config.should.match(defaultSlotConfig)
})
it('should set config passed as argument', () => {
const config = { noBuffer: true }
const testSlot = slot(config)
if (!testSlot.config) {
throw new Error('testSlot should have a config')
}
testSlot.config.should.match(config)
})
})

describe('connectSlot', () => {

context('without parameter', () => {
Expand Down

0 comments on commit 68f9526

Please sign in to comment.