Skip to content

Commit

Permalink
Feature/event name mapping (#480)
Browse files Browse the repository at this point in the history
feat(observer): introduce `eventMapping` option, allowing to extract even name from event data. Especially useful for 3rd party APIs. Kudos @amiceli for the feature

* Update readme.

* Update types/index.d.ts

Co-authored-by: Max Lyashuk <[email protected]>
  • Loading branch information
amiceli and probil authored Mar 24, 2021
1 parent 33976fc commit ad6e309
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ Here they are:
| `mutationPrefix` | `String` | `'SOCKET_'` | Prepend to event name while converting event to mutation. Empty string disables prefixing |
| `eventToMutationTransformer` | `Function` `string => string` | uppercase function | Determines how event name converted to mutation |
| `eventToActionTransformer` | `Function` `string => string` | camelcase function | Determines how event name converted to action |
| eventMapping | `Function` `socket => string` | | Map your event from socket event data
*FYI:* You can always access default plugin options if you need it (e.g. re-use default `eventToActionTransformer` function):
Expand Down
1 change: 0 additions & 1 deletion dist/vue-socket.io-ext.esm.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/vue-socket.io-ext.min.js

This file was deleted.

10 changes: 8 additions & 2 deletions src/Observe.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,14 @@ export default (Socket, { store, ...otherOptions } = {}) => {
function registerEventHandler() {
augmentMethod(Socket, 'onevent', (packet) => {
const [eventName, ...args] = packet.data;
GlobalEmitter.emit(eventName, ...args);
passToStore(eventName, args);
let mappedEventName = eventName;

if (otherOptions.eventMapping) {
mappedEventName = otherOptions.eventMapping(eventName, args);
}

GlobalEmitter.emit(mappedEventName, ...args);
passToStore(mappedEventName, args);
});

SYSTEM_EVENTS.forEach((eventName) => {
Expand Down
12 changes: 12 additions & 0 deletions src/__tests__/Observe.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,18 @@ it('should apply custom event to mutation transformer', () => {
expect(fn).toHaveBeenLastCalledWith(store.state, expect.objectContaining(message));
});

it('should apply custom event name mapping', () => {
const fn = jest.fn();
const socket = io('wss://localhost');
Observe(socket, {
eventMapping: fn,
});
const message = { id: 15, body: 'Hi there' };
socket.fireServerEvent('new message', message);
expect(fn).toHaveBeenCalledTimes(1);
expect(fn).toHaveBeenLastCalledWith('new message', [message]);
});

it('should apply custom action prefix', () => {
const fn = jest.fn();
const store = new Store({
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface SocketToVuexOptions {
mutationPrefix?: string;
eventToMutationTransformer?: (eventName: string) => string;
eventToActionTransformer?: (eventName: string) => string;
eventMapping? : (eventMapping : string, socketPayload : any[]) => string
}

export interface VueSocketIOExtOptions extends SocketToVuexOptions{
Expand Down

0 comments on commit ad6e309

Please sign in to comment.