Skip to content

Commit

Permalink
patch: add shared bus module
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 10, 2021
1 parent b3e2714 commit 2038399
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
81 changes: 81 additions & 0 deletions packages/shared/src/__tests__/bus.test.ts
@@ -0,0 +1,81 @@
import { createBus } from 'bus';

describe('bus', () => {
it('should be a function', () => {
expect(createBus).toBeInstanceOf(Function);
});

it('should return a bus', () => {
const bus = createBus();
expect(bus).toBeInstanceOf(Object);
expect(bus.emit).toBeInstanceOf(Function);
expect(bus.on).toBeInstanceOf(Function);
});

it('should emit events', () => {
const bus = createBus();
const spy = jest.fn();
bus.on('test', spy);
bus.emit('test');
expect(spy).toHaveBeenCalled();
});

it('should emit events with data', () => {
const bus = createBus();
const spy = jest.fn();
bus.on('test', spy);
bus.emit('test', 'testData');
expect(spy).toHaveBeenCalledWith('testData');
});

it('should emit events with multiple listeners', () => {
const bus = createBus();
const spy1 = jest.fn();
const spy2 = jest.fn();
bus.on('test', spy1);
bus.on('test', spy2);
bus.emit('test');
expect(spy1).toHaveBeenCalled();
expect(spy2).toHaveBeenCalled();
});

it('should emit events with multiple listeners and data', () => {
const bus = createBus();
const spy1 = jest.fn();
const spy2 = jest.fn();
bus.on('test', spy1);
bus.on('test', spy2);
bus.emit('test', 'test1');
expect(spy1).toHaveBeenCalledWith('test1');
expect(spy2).toHaveBeenCalledWith('test1');
});

test('on returns an object with an `off` function', () => {
const bus = createBus();
const spy = jest.fn();
const off = bus.on('test', spy);
expect(off).toBeInstanceOf(Object);
expect(off.off).toBeInstanceOf(Function);
});

test('off should remove a listener', () => {
const bus = createBus();
const spy = jest.fn();
const off = bus.on('test', spy);
off.off();
bus.emit('test');
expect(spy).not.toHaveBeenCalled();
});

test('off should only remove specific handler', () => {
const bus = createBus();
const spy1 = jest.fn();
const spy2 = jest.fn();
const off = bus.on('test', spy1);
bus.on('test', spy2);
off.off();
bus.emit('test');
expect(spy1).not.toHaveBeenCalled();
expect(spy2).toHaveBeenCalled();
});
});
32 changes: 32 additions & 0 deletions packages/shared/src/bus.ts
@@ -0,0 +1,32 @@
export function createBus(): {
on: (event: string, handler: (...args: any[]) => void) => { off: () => void };
emit: (event: string, ...args: any[]) => void;
} {
const listeners: Record<string, ((...args: any[]) => void)[]> = {};

return {
emit(event: string, data: any) {
if (!listeners[event]) {
return;
}

listeners[event].forEach(listener => {
listener(data);
});
},

on(event: string, handler: (...args: any[]) => void): { off: () => void } {
if (!listeners[event]) {
listeners[event] = [];
}

listeners[event].push(handler);

return {
off() {
listeners[event] = listeners[event].filter(h => h !== handler);
},
};
},
};
}
1 change: 1 addition & 0 deletions tsconfig.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2038399

Please sign in to comment.