Skip to content

Commit 31c6635

Browse files
committed
new sub model
1 parent 32f1b9f commit 31c6635

File tree

2 files changed

+15
-32
lines changed

2 files changed

+15
-32
lines changed

src/client.js

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,6 @@ class RPCClient extends EventEmitter {
8585
*/
8686
this._expecting = new Map();
8787

88-
/**
89-
* Map of current subscriptions
90-
* @type {Map}
91-
* @private
92-
*/
93-
this._subscriptions = new Map();
94-
9588
this._connectPromise = undefined;
9689
}
9790

@@ -191,11 +184,7 @@ class RPCClient extends EventEmitter {
191184
}
192185
this._expecting.delete(message.nonce);
193186
} else {
194-
const subid = subKey(message.evt, message.args);
195-
if (!this._subscriptions.has(subid)) {
196-
return;
197-
}
198-
this._subscriptions.get(subid)(message.data);
187+
this.emit(message.evt, message.data);
199188
}
200189
}
201190

@@ -650,22 +639,13 @@ class RPCClient extends EventEmitter {
650639
* Subscribe to an event
651640
* @param {string} event Name of event e.g. `MESSAGE_CREATE`
652641
* @param {Object} [args] Args for event e.g. `{ channel_id: '1234' }`
653-
* @param {Function} callback Callback when an event for the subscription is triggered
654642
* @returns {Promise<Object>}
655643
*/
656-
subscribe(event, args, callback) {
657-
if (!callback && typeof args === 'function') {
658-
callback = args;
659-
args = undefined;
660-
}
661-
return this.request(RPCCommands.SUBSCRIBE, args, event).then(() => {
662-
const subid = subKey(event, args);
663-
this._subscriptions.set(subid, callback);
664-
return {
665-
unsubscribe: () => this.request(RPCCommands.UNSUBSCRIBE, args, event)
666-
.then(() => this._subscriptions.delete(subid)),
667-
};
668-
});
644+
async subscribe(event, args) {
645+
await this.request(RPCCommands.SUBSCRIBE, args, event);
646+
return {
647+
unsubscribe: () => this.request(RPCCommands.UNSUBSCRIBE, args, event),
648+
};
669649
}
670650

671651
/**

test/rp.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ const client = new Client({
1212
transport: 'ipc',
1313
});
1414

15+
client.on('VOICE_CHANNEL_SELECT', ({ channel_id }) => {
16+
client.subscribe('VOICE_STATE_UPDATE', { channel_id });
17+
});
18+
19+
client.on('VOICE_STATE_UPDATE', (args) => {
20+
console.log(args);
21+
});
22+
1523
client.on('ready', async () => {
16-
await client.setActivity({
17-
buttons: [
18-
{ label: 'B1', url: 'https://snek.dev/b1' },
19-
{ label: 'B2', url: 'https://snek.dev/b2' },
20-
],
21-
});
24+
client.subscribe('VOICE_CHANNEL_SELECT');
2225
});
2326

2427
client.login(require('./auth')).catch(console.error);

0 commit comments

Comments
 (0)