From 90e93ba432e3a239a608418cdbff69a067742480 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Herv=C3=A9?= Date: Thu, 26 Mar 2020 10:44:56 +0100 Subject: [PATCH] Fix notConnectedSlot config assignment I had used the same object everytime, and configs were getting overriden. --- src/Slot.ts | 37 +++++++++++++++++++++---------------- test/Slot.test.ts | 20 +++++++++++++++++++- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/Slot.ts b/src/Slot.ts index 2f47368..56adbc5 100644 --- a/src/Slot.ts +++ b/src/Slot.ts @@ -4,14 +4,24 @@ import { DEFAULT_PARAM } from './Constants' const signalNotConnected = () => { throw new Error('Slot not connected') } -const notConnectedSlot: Slot = 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 => + Object.assign( + () => signalNotConnected(), + { + config, + lazy: () => signalNotConnected, + on: () => signalNotConnected, + slotName: 'Not connected' + } + ) export type LazyCallback = (param: string) => void export type Unsubscribe = () => void @@ -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. * @@ -81,15 +85,16 @@ export interface Slot { 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( - config: SlotConfig = { noBuffer: false } + config: SlotConfig = defaultSlotConfig ): Slot { - return Object.assign(notConnectedSlot, config) + return getNotConnectedSlot(config) } export function connectSlot( diff --git a/test/Slot.test.ts b/test/Slot.test.ts index ad8ba3e..7ca7d6c 100644 --- a/test/Slot.test.ts +++ b/test/Slot.test.ts @@ -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' @@ -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', () => {