From 4941436820e6c76a26e7576501a39aa671e79904 Mon Sep 17 00:00:00 2001 From: s-r-x Date: Wed, 20 Apr 2022 09:20:58 +0500 Subject: [PATCH] fix: handle nil events / don't ignore internal phx events --- src/index.spec.ts | 12 +----------- src/index.ts | 16 ++-------------- 2 files changed, 3 insertions(+), 25 deletions(-) diff --git a/src/index.spec.ts b/src/index.spec.ts index abbc547..91c2766 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -1,4 +1,4 @@ -import { Wildcard, INTERNAL_PHX_EVENTS, Glob } from './'; +import { Wildcard, Glob } from './'; import { Channel } from 'phoenix'; const createChannelMock = () => { @@ -70,16 +70,6 @@ it('should trigger subscribers on incoming events', () => { expect(sub2.cb).toHaveBeenCalledWith(...args); expect(sub3.cb).toHaveBeenCalledTimes(0); }); -it('should ignore internal phoenix events', () => { - const channel = createChannelMock(); - const wildcard = new Wildcard(channel); - const sub = createSub('*'); - wildcard.on(sub.glob, sub.cb); - [...Array.from(INTERNAL_PHX_EVENTS), 'event'].forEach(event => { - channel.onMessage(event, null, 1); - }); - expect(sub.cb).toBeCalledTimes(1); -}); it('should call initial onMessage callback', () => { const channel = createChannelMock(); const TRANSFORMED_PAYLOAD = 'new_payload'; diff --git a/src/index.ts b/src/index.ts index 5c7b176..29ab14e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ -import { Channel } from 'phoenix'; import { isMatch } from 'matcher'; +import type { Channel } from 'phoenix'; export type Glob = string | string[]; export type Callback = (event: string, payload: any, ref?: any) => any; @@ -8,15 +8,6 @@ type Subscriber = { cb: Callback; }; -export const INTERNAL_PHX_EVENTS = new Set([ - 'presence_diff', - 'phx_close', - 'phx_error', - 'phx_join', - 'phx_reply', - 'phx_leave', -]); - export class Wildcard { constructor(private _channel: Channel) { if (!_channel) { @@ -75,15 +66,12 @@ export class Wildcard { throw new Error('This Wildcard instance is destroyed. Create a new one.'); } } - private _isEventInternal(event: string) { - return INTERNAL_PHX_EVENTS.has(event) || event.startsWith('chan_reply'); - } private _onMessage = (event: string, payload: any, ref: any) => { let finalPayload = payload; if (this._initialOnMessageCallback) { finalPayload = this._initialOnMessageCallback(event, payload, ref); } - if (!this._isEventInternal(event)) { + if (event) { this._subscribers.forEach(sub => { if (isMatch(event, sub.glob)) { sub.cb(event, finalPayload, ref);