Skip to content

Commit

Permalink
Merge pull request #665 from smapiot/fix/#644
Browse files Browse the repository at this point in the history
Bind `this` In Pilet API Event Functions
  • Loading branch information
FlorianRappl authored Jan 17, 2024
2 parents e38bc99 + 029b6d7 commit 6429391
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
28 changes: 27 additions & 1 deletion src/framework/piral-base/src/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/**
* @vitest-environment jsdom
*/
import { describe, it, expect, vitest } from 'vitest';
import { initializeApi, mergeApis } from './api';
import { createListener } from './events';

const moduleMetadata = {
name: 'my-module',
Expand All @@ -14,6 +18,7 @@ describe('API Module', () => {
const container = {
on: vitest.fn(),
off: vitest.fn(),
once: vitest.fn(),
emit: vitest.fn(),
};
const api = initializeApi(moduleMetadata, container);
Expand All @@ -28,11 +33,32 @@ describe('API Module', () => {
const baseApi: any = {
a: 'foo',
};
const newApi = mergeApis(baseApi, [() => ({ b: 'bar' }) as any], {} as any);
const newApi = mergeApis(baseApi, [() => ({ b: 'bar' } as any)], {} as any);
expect(newApi).toEqual({
a: 'foo',
b: 'bar',
});
expect(newApi).toBe(baseApi);
});

it('calling event APIs without this context does not throw', () => {
// This test case covers GH issue #664: https://github.com/smapiot/piral/issues/664
const events = createListener(undefined);
const api = initializeApi(moduleMetadata, events);

// These calls should not throw:
const { on, off, once, emit } = api;
on('foo', () => {});
off('foo', () => {});
once('foo', () => {});
emit('foo', {});

// Chaining should also work as each event fn returns this (i.e., the event emitter):
expect(
on('foo', () => {})
.off('foo', () => {})
.once('foo', () => {})
.emit('foo', {}),
).not.toBeUndefined();
});
});
14 changes: 8 additions & 6 deletions src/framework/piral-base/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ function nameOf(type: string | number) {
export function createListener(state: any = {}): EventEmitter {
const eventListeners: EventListeners = [];

return {
const events = {
on(type, callback) {
const listener = ({ detail }: CustomEvent) => detail && detail.state === state && callback(detail.arg);
document.body.addEventListener(nameOf(type), listener);
eventListeners.push([callback, listener]);
return this;
return events;
},
once(type, callback) {
const cb = (ev: any) => {
this.off(type, cb);
events.off(type, cb);
callback(ev);
};
return this.on(type, cb);
return events.on(type, cb);
},
off(type, callback) {
const [listener] = eventListeners.filter((m) => m[0] === callback);
Expand All @@ -37,7 +37,7 @@ export function createListener(state: any = {}): EventEmitter {
eventListeners.splice(eventListeners.indexOf(listener), 1);
}

return this;
return events;
},
emit(type, arg) {
document.body.dispatchEvent(
Expand All @@ -50,7 +50,9 @@ export function createListener(state: any = {}): EventEmitter {
},
}),
);
return this;
return events;
},
};

return events;
}

0 comments on commit 6429391

Please sign in to comment.