-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathWikiSyncer.ts
More file actions
136 lines (108 loc) · 3.44 KB
/
Copy pathWikiSyncer.ts
File metadata and controls
136 lines (108 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
'use client';
import { makeAutoObservable } from 'mobx';
import { ImportableData } from '@/types/State';
import { GetPlayerRequest, WikiSyncerRequestType, WikiSyncerResponsesUnion } from './WikiSyncerTypes';
import parseWikiSyncImportableData from './ParseWikiSync';
const minimumPort = 37767;
const maximumPort = 37776;
// const maximumPort = 37768; // small port range for testing
interface InFlightRequest<T> {
resolve: (value: T) => void;
reject: (error: Error) => void;
}
enum ConnectionState {
Disconnected,
Connected,
}
export class WikiSyncer {
port: number;
private _connectionState = ConnectionState.Disconnected;
public get connectionState(): ConnectionState {
return this._connectionState;
}
public set connectionState(value: ConnectionState) {
this._connectionState = value;
}
private _username?: string;
public get username(): string | undefined {
return this._username;
}
public set username(value: string | undefined) {
this._username = value;
}
private ws?: WebSocket;
private reconnectionJobId?: ReturnType<typeof setTimeout>;
private nextSequenceID = 0;
private inFlightRequests = { getPlayer: new Map<number, InFlightRequest<ImportableData>>() };
constructor(port: number) {
makeAutoObservable(this);
this.port = port;
this.connect();
}
private onMessage(message: MessageEvent) {
const response: WikiSyncerResponsesUnion = JSON.parse(message.data);
switch (response._wsType) {
case WikiSyncerRequestType.USERNAME_CHANGED:
this.username = response.username;
break;
case WikiSyncerRequestType.GET_PLAYER:
if (response.error) {
this.inFlightRequests.getPlayer.get(response.sequenceId)?.reject(new Error(response.error));
} else {
this.inFlightRequests.getPlayer.get(response.sequenceId)?.resolve(parseWikiSyncImportableData(response.payload));
}
break;
default:
break;
}
}
connect() {
if (this.ws) {
// There is already a connection.
return;
}
this.ws = new WebSocket(`ws://localhost:${this.port}`);
this.ws.onopen = () => { this.connectionState = ConnectionState.Connected; };
this.ws.onmessage = (message) => this.onMessage(message);
this.ws.onclose = () => {
this.ws = undefined;
this.username = undefined;
this.connectionState = ConnectionState.Disconnected;
if (this.reconnectionJobId) {
return;
}
this.reconnectionJobId = setTimeout(() => {
this.reconnectionJobId = undefined;
console.debug('Reconnecting', this.port);
this.connect();
}, 10000);
};
}
getPlayer() {
const p = new Promise<ImportableData>(((resolve, reject) => {
if (this.ws) {
const req: GetPlayerRequest = {
_wsType: WikiSyncerRequestType.GET_PLAYER,
sequenceId: this.nextSequenceID,
data: {},
};
this.ws.send(JSON.stringify(req));
this.inFlightRequests.getPlayer.set(this.nextSequenceID, { resolve, reject });
this.nextSequenceID += 1;
} else {
reject(new Error('Not connected'));
}
}));
return p;
}
}
const syncers = new Map();
export const startPollingForRuneLite = () => {
if (typeof window === 'undefined' || syncers.size > 0) {
return syncers;
}
for (let port = minimumPort; port <= maximumPort; port++) {
syncers.set(port, new WikiSyncer(port));
}
return syncers;
};