Skip to content

Commit ceb68a7

Browse files
authored
feat: Replace socket.io-client (#99)
1 parent efbc16f commit ceb68a7

14 files changed

+2778
-4726
lines changed

.eslintignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
lib/
2-
socket.io-websocket-only.js
32
doc/resource
43
coverage/
54
build/

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,4 @@ package.tgz
5959
!.yarn/releases
6060
!.yarn/sdks
6161
!.yarn/versions
62+
.DS_Store

package.json

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@
2323
"dist"
2424
],
2525
"devDependencies": {
26-
"@rollup/plugin-commonjs": "^25.0.0",
27-
"@types/uuid": "^9.0.0",
28-
"cross-fetch": "^3.1.5",
26+
"@types/uuid": "^9.0.1",
27+
"cross-fetch": "^3.1.6",
2928
"dayjs": "^1.11.7",
30-
"eslint": "^8.33.0",
29+
"eslint": "^8.41.0",
3130
"eslint-config-react-app": "^7.0.1",
32-
"jsdom": "^21.1.0",
33-
"lint-staged": "^13.1.0",
34-
"msw": "^1.0.0",
35-
"prettier": "^2.8.3",
31+
"jsdom": "^22.0.0",
32+
"lint-staged": "^13.2.2",
33+
"msw": "^1.2.1",
34+
"prettier": "^2.8.8",
3635
"typescript": "^5.0.4",
3736
"vite": "^4.3.8",
3837
"vite-plugin-dts": "^2.3.0",
39-
"vitest": "^0.31.1"
38+
"vitest": "^0.31.1",
39+
"ws": "^8.13.0"
4040
},
4141
"repository": {
4242
"type": "git",
@@ -54,10 +54,19 @@
5454
},
5555
"homepage": "http://ftrack.com",
5656
"dependencies": {
57+
"isomorphic-ws": "^5.0.0",
5758
"loglevel": "^1.8.1",
5859
"moment": "^2.29.4",
5960
"uuid": "^9.0.0"
6061
},
62+
"peerDependencies": {
63+
"ws": "^8.13.0"
64+
},
65+
"peerDependenciesMeta": {
66+
"ws": {
67+
"optional": true
68+
}
69+
},
6170
"lint-staged": {
6271
"*.js": "eslint --cache --fix",
6372
"*.{js,css,md,json,jsx,scss,yml}": "prettier --write"

source/event.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// :copyright: Copyright (c) 2016 ftrack
22
import { v4 as uuidV4 } from "uuid";
3-
3+
import { EventSource } from "./event_hub.js";
44
/**
55
* ftrack API Event class.
66
*/
@@ -11,7 +11,7 @@ export class Event {
1111
target: string;
1212
inReplyToEvent: string | null;
1313
id: string;
14-
source?: any;
14+
source?: EventSource;
1515
};
1616

1717
/**
@@ -37,12 +37,12 @@ export class Event {
3737
}
3838

3939
/** Return event data. */
40-
getData(): { [key: string]: any } {
40+
getData() {
4141
return this._data;
4242
}
4343

4444
/** Add source to event data. */
45-
addSource(source: any): void {
45+
addSource(source: EventSource): void {
4646
this._data.source = source;
4747
}
4848
}

source/event_hub.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// :copyright: Copyright (c) 2016 ftrack
22
import { v4 as uuidV4 } from "uuid";
33
import loglevel from "loglevel";
4-
import * as io from "./socket.io-websocket-only.js";
4+
import io from "./simple_socketio.js";
55
import { Event } from "./event.js";
66
import {
77
EventServerConnectionTimeoutError,
@@ -72,11 +72,11 @@ export type EventPayload =
7272
| UpdateEventPayload;
7373

7474
export interface EventSource {
75-
clientToken: string;
75+
clientToken?: string;
7676
applicationId: string;
7777
user: {
7878
username: string;
79-
id: string;
79+
id?: string;
8080
};
8181
id: string;
8282
}
@@ -132,7 +132,7 @@ export class EventHub {
132132
};
133133
private _unsentEvents: ConnectionCallback[];
134134
private _subscribers: Subscriber[];
135-
private _socketIo: io.SocketIO | null;
135+
private _socketIo: io | null;
136136

137137
/**
138138
* Construct EventHub instance with API credentials.
@@ -177,17 +177,7 @@ export class EventHub {
177177

178178
/** Connect to the event server. */
179179
connect(): void {
180-
this._socketIo = io.connect(this._serverUrl, {
181-
"max reconnection attempts": Infinity,
182-
"reconnection limit": 10000,
183-
"reconnection delay": 5000,
184-
transports: ["websocket"],
185-
query: new URLSearchParams({
186-
api_user: this._apiUser,
187-
api_key: this._apiKey,
188-
}).toString(),
189-
});
190-
180+
this._socketIo = io.connect(this._serverUrl, this._apiUser, this._apiKey);
191181
this._socketIo.on("connect", this._onSocketConnected);
192182
this._socketIo.on("ftrack.event", this._handle);
193183
}
@@ -197,7 +187,7 @@ export class EventHub {
197187
* @return {Boolean}
198188
*/
199189
isConnected(): boolean {
200-
return (this._socketIo && this._socketIo.socket.connected) || false;
190+
return this._socketIo?.isConnected() || false;
201191
}
202192

203193
/**
@@ -247,7 +237,7 @@ export class EventHub {
247237
*
248238
* If timeout is non-zero, the promise will be rejected if the event is not
249239
* sent before the timeout is reached. Should be specified as seconds and
250-
* will default to 10.
240+
* will default to 30.
251241
*
252242
* @param {Event} event Event instance to publish
253243
* @param {Function} [options.onReply] Function to be invoked when a reply
@@ -368,7 +358,7 @@ export class EventHub {
368358
// Force reconnect socket if not automatically reconnected. This
369359
// happens for example in Adobe After Effects when rendering a
370360
// sequence takes longer than ~30s and the JS thread is blocked.
371-
this._socketIo.socket.reconnect();
361+
this._socketIo.reconnect();
372362
}
373363
} else {
374364
callback();

0 commit comments

Comments
 (0)