Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Merge pull request #176 from apollographql/feat/string-key
Browse files Browse the repository at this point in the history
feat(string-key): change subscription key to be string instead of number
  • Loading branch information
Urigo authored Jun 12, 2017
2 parents c29294a + d99fd9f commit 6446163
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

### vNEXT
- Operation key is now `string` instead of `number` [PR #176](https://github.com/apollographql/subscriptions-transport-ws/pull/176)
- ...

### 0.7.1
Expand Down
19 changes: 10 additions & 9 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export class SubscriptionClient {
return this.on('reconnecting', callback, context);
}

public unsubscribe(opId: number) {
public unsubscribe(opId: string) {
if (this.operations[opId]) {
delete this.operations[opId];
this.sendMessage(opId, MessageTypes.GQL_STOP, undefined);
Expand All @@ -237,7 +237,7 @@ export class SubscriptionClient {

public unsubscribeAll() {
Object.keys(this.operations).forEach( subId => {
this.unsubscribe(parseInt(subId, 10));
this.unsubscribe(subId);
});
}

Expand Down Expand Up @@ -304,7 +304,7 @@ export class SubscriptionClient {
}
}

private executeOperation(options: OperationOptions, handler: (error: Error[], result?: any) => void): number {
private executeOperation(options: OperationOptions, handler: (error: Error[], result?: any) => void): string {
const opId = this.generateOperationId();
this.operations[opId] = { options: options, handler };

Expand All @@ -324,7 +324,7 @@ export class SubscriptionClient {
return opId;
}

private buildMessage(id: number, type: string, payload: any) {
private buildMessage(id: string, type: string, payload: any) {
const payloadToReturn = payload && payload.query ?
{
...payload,
Expand Down Expand Up @@ -362,7 +362,7 @@ export class SubscriptionClient {
}];
}

private sendMessage(id: number, type: string, payload: any) {
private sendMessage(id: string, type: string, payload: any) {
this.sendMessageRaw(this.buildMessage(id, type, payload));
}

Expand Down Expand Up @@ -392,8 +392,8 @@ export class SubscriptionClient {
}
}

private generateOperationId() {
return ++this.nextOperationId;
private generateOperationId(): string {
return String(++this.nextOperationId);
}

private tryReconnect() {
Expand All @@ -404,7 +404,7 @@ export class SubscriptionClient {
if (!this.reconnecting) {
Object.keys(this.operations).forEach((key) => {
this.unsentMessagesQueue.push(
this.buildMessage(parseInt(key, 10), MessageTypes.GQL_START, this.operations[key].options),
this.buildMessage(key, MessageTypes.GQL_START, this.operations[key].options),
);
});
this.reconnecting = true;
Expand Down Expand Up @@ -462,7 +462,7 @@ export class SubscriptionClient {

private processReceivedData(receivedData: any) {
let parsedMessage: any;
let opId: number;
let opId: string;

try {
parsedMessage = JSON.parse(receivedData);
Expand All @@ -478,6 +478,7 @@ export class SubscriptionClient {
].indexOf(parsedMessage.type) !== -1 && !this.operations[opId]
) {
this.unsubscribe(opId);

return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ export function addGraphQLSubscriptions(networkInterface: any, wsClient: Subscri
}

return assign(networkInterface, {
subscribe(request: any, handler: any): number {
subscribe(request: any, handler: any): string {
return wsClient.subscribe(request, handler);
},
unsubscribe(id: number): void {
unsubscribe(id: string): void {
wsClient.unsubscribe(id);
},
});
Expand Down
2 changes: 1 addition & 1 deletion src/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1727,7 +1727,7 @@ describe('Server', function () {

it('does not send more subscription data after client unsubscribes', function (done) {
const client4 = new SubscriptionClient(`ws://localhost:${TEST_PORT}/`);
let subId: number;
let subId: string;
setTimeout(() => {
client4.unsubscribe(subId);
}, 50);
Expand Down

0 comments on commit 6446163

Please sign in to comment.