Browse Source

chore(core): Remove deprecated EventBus.subscribe() method

Michael Bromley 4 years ago
parent
commit
9d57c1a184

+ 0 - 105
packages/core/src/event-bus/event-bus.spec.ts

@@ -14,111 +14,6 @@ describe('EventBus', () => {
         expect(() => eventBus.publish(event)).not.toThrow();
     });
 
-    it('single handler is called once', () => {
-        const handler = jest.fn();
-        const event = new TestEvent('foo');
-        eventBus.subscribe(TestEvent, handler);
-
-        eventBus.publish(event);
-
-        expect(handler).toHaveBeenCalledTimes(1);
-        expect(handler).toHaveBeenCalledWith(event);
-    });
-
-    it('single handler is called on multiple events', () => {
-        const handler = jest.fn();
-        const event1 = new TestEvent('foo');
-        const event2 = new TestEvent('bar');
-        const event3 = new TestEvent('baz');
-        eventBus.subscribe(TestEvent, handler);
-
-        eventBus.publish(event1);
-        eventBus.publish(event2);
-        eventBus.publish(event3);
-
-        expect(handler).toHaveBeenCalledTimes(3);
-        expect(handler).toHaveBeenCalledWith(event1);
-        expect(handler).toHaveBeenCalledWith(event2);
-        expect(handler).toHaveBeenCalledWith(event3);
-    });
-
-    it('subscribing same handler multiple times does not result in multiple dispatch of event', () => {
-        const handler = jest.fn();
-        const event = new TestEvent('foo');
-        eventBus.subscribe(TestEvent, handler);
-        eventBus.subscribe(TestEvent, handler);
-        eventBus.subscribe(TestEvent, handler);
-
-        eventBus.publish(event);
-
-        expect(handler).toHaveBeenCalledTimes(1);
-        expect(handler).toHaveBeenCalledWith(event);
-    });
-
-    it('multiple handlers are called', () => {
-        const handler1 = jest.fn();
-        const handler2 = jest.fn();
-        const handler3 = jest.fn();
-        const event = new TestEvent('foo');
-        eventBus.subscribe(TestEvent, handler1);
-        eventBus.subscribe(TestEvent, handler2);
-        eventBus.subscribe(TestEvent, handler3);
-
-        eventBus.publish(event);
-
-        expect(handler1).toHaveBeenCalledWith(event);
-        expect(handler2).toHaveBeenCalledWith(event);
-        expect(handler3).toHaveBeenCalledWith(event);
-    });
-
-    it('handler is not called for other events', () => {
-        const handler = jest.fn();
-        const event = new OtherTestEvent('foo');
-        eventBus.subscribe(TestEvent, handler);
-
-        eventBus.publish(event);
-
-        expect(handler).not.toHaveBeenCalled();
-    });
-
-    it('subscribe() returns an unsubscribe() funtion', () => {
-        const handler = jest.fn();
-        const event = new TestEvent('foo');
-        const unsubscribe = eventBus.subscribe(TestEvent, handler);
-
-        eventBus.publish(event);
-
-        expect(handler).toHaveBeenCalledTimes(1);
-
-        unsubscribe();
-
-        eventBus.publish(event);
-        eventBus.publish(event);
-
-        expect(handler).toHaveBeenCalledTimes(1);
-    });
-
-    it('unsubscribe() only unsubscribes own handler', () => {
-        const handler1 = jest.fn();
-        const handler2 = jest.fn();
-        const event = new TestEvent('foo');
-        const unsubscribe1 = eventBus.subscribe(TestEvent, handler1);
-        const unsubscribe2 = eventBus.subscribe(TestEvent, handler2);
-
-        eventBus.publish(event);
-
-        expect(handler1).toHaveBeenCalledTimes(1);
-        expect(handler2).toHaveBeenCalledTimes(1);
-
-        unsubscribe1();
-
-        eventBus.publish(event);
-        eventBus.publish(event);
-
-        expect(handler1).toHaveBeenCalledTimes(1);
-        expect(handler2).toHaveBeenCalledTimes(3);
-    });
-
     describe('ofType()', () => {
         it('single handler is called once', () => {
             const handler = jest.fn();

+ 0 - 34
packages/core/src/event-bus/event-bus.ts

@@ -8,9 +8,6 @@ import { TRANSACTION_MANAGER_KEY } from '../common/constants';
 
 import { VendureEvent } from './vendure-event';
 
-export type EventHandler<T extends VendureEvent> = (event: T) => void;
-export type UnsubscribeFn = () => void;
-
 /**
  * @description
  * The EventBus is used to globally publish events which can then be subscribed to.
@@ -19,7 +16,6 @@ export type UnsubscribeFn = () => void;
  * */
 @Injectable()
 export class EventBus implements OnModuleDestroy {
-    private subscriberMap = new Map<Type<VendureEvent>, Array<EventHandler<any>>>();
     private eventStream = new Subject<VendureEvent>();
     private destroy$ = new Subject();
 
@@ -28,14 +24,6 @@ export class EventBus implements OnModuleDestroy {
      * Publish an event which any subscribers can react to.
      */
     publish<T extends VendureEvent>(event: T): void {
-        const eventType = (event as any).constructor;
-        const handlers = this.subscriberMap.get(eventType);
-        if (handlers) {
-            const length = handlers.length;
-            for (let i = 0; i < length; i++) {
-                handlers[i](event);
-            }
-        }
         this.eventStream.next(this.prepareRequestContext(event));
     }
 
@@ -50,28 +38,6 @@ export class EventBus implements OnModuleDestroy {
         ) as Observable<T>;
     }
 
-    /**
-     * @description
-     * Deprecated: use `ofType()` instead.
-     *
-     * Subscribe to the given event type. Returns an unsubscribe function which can be used
-     * to unsubscribe the handler from the event.
-     *
-     * @deprecated
-     */
-    subscribe<T extends VendureEvent>(type: Type<T>, handler: EventHandler<T>): UnsubscribeFn {
-        const handlers = this.subscriberMap.get(type) || [];
-        if (!handlers.includes(handler)) {
-            handlers.push(handler);
-        }
-        this.subscriberMap.set(type, handlers);
-        return () =>
-            this.subscriberMap.set(
-                type,
-                handlers.filter(h => h !== handler),
-            );
-    }
-
     /** @internal */
     onModuleDestroy(): any {
         this.destroy$.next();