This repository was archived by the owner on Jun 18, 2025. It is now read-only.
File tree Expand file treeCollapse file tree 6 files changed+22
-4
lines changedFilter options
+22
-4
lines changed Original file line number Diff line number Diff line change
@@ -397,6 +397,7 @@ Here they are:
397
397
| `mutationPrefix` | `String` | `'SOCKET_'` | Prepend to event name while converting event to mutation. Empty string disables prefixing |
398
398
| `eventToMutationTransformer` | `Function` `string => string` | uppercase function | Determines how event name converted to mutation |
399
399
| `eventToActionTransformer` | `Function` `string => string` | camelcase function | Determines how event name converted to action |
400
+
| eventMapping | `Function` `socket => string` | | Map your event from socket event data
400
401
401
402
*FYI:* You can always access default plugin options if you need it (e.g. re-use default `eventToActionTransformer` function):
402
403
Original file line number Diff line number Diff line change
@@ -43,8 +43,14 @@ export default (Socket, { store, ...otherOptions } = {}) => {
43
43
function registerEventHandler() {
44
44
augmentMethod(Socket, 'onevent', (packet) => {
45
45
const [eventName, ...args] = packet.data;
46
-
GlobalEmitter.emit(eventName, ...args);
47
-
passToStore(eventName, args);
46
+
let mappedEventName = eventName;
47
+
48
+
if (otherOptions.eventMapping) {
49
+
mappedEventName = otherOptions.eventMapping(eventName, args);
50
+
}
51
+
52
+
GlobalEmitter.emit(mappedEventName, ...args);
53
+
passToStore(mappedEventName, args);
48
54
});
49
55
50
56
SYSTEM_EVENTS.forEach((eventName) => {
Original file line number Diff line number Diff line change
@@ -199,6 +199,18 @@ it('should apply custom event to mutation transformer', () => {
199
199
expect(fn).toHaveBeenLastCalledWith(store.state, expect.objectContaining(message));
200
200
});
201
201
202
+
it('should apply custom event name mapping', () => {
203
+
const fn = jest.fn();
204
+
const socket = io('wss://localhost');
205
+
Observe(socket, {
206
+
eventMapping: fn,
207
+
});
208
+
const message = { id: 15, body: 'Hi there' };
209
+
socket.fireServerEvent('new message', message);
210
+
expect(fn).toHaveBeenCalledTimes(1);
211
+
expect(fn).toHaveBeenLastCalledWith('new message', [message]);
212
+
});
213
+
202
214
it('should apply custom action prefix', () => {
203
215
const fn = jest.fn();
204
216
const store = new Store({
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ export interface SocketToVuexOptions {
9
9
mutationPrefix?: string;
10
10
eventToMutationTransformer?: (eventName: string) => string;
11
11
eventToActionTransformer?: (eventName: string) => string;
12
+
eventMapping? : (eventMapping : string, socketPayload : any[]) => string
12
13
}
13
14
14
15
export interface VueSocketIOExtOptions extends SocketToVuexOptions{
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4