forked from alm-tools/alm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocketLibClient.ts
More file actions
69 lines (58 loc) · 2.54 KB
/
Copy pathsocketLibClient.ts
File metadata and controls
69 lines (58 loc) · 2.54 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
import {RequesterResponder, Message, anycastMessageName, TypedEvent, CastMessage} from "./socketLib";
let socketIo = io;
let origin = `${window.location.protocol}//${window.location.hostname}${(window.location.port ? ':' + window.location.port : '') }`;
export var resolve: typeof Promise.resolve = Promise.resolve.bind(Promise);
/** This is your main function to launch the client */
export function run<TServer, TCast>(config: {
clientImplementation: any;
serverContract: TServer;
cast: TCast;
}): {
client: Client;
server: TServer;
cast: TCast;
pendingRequestsChanged: TypedEvent<{pending:string[]}>;
connectionStatusChanged: TypedEvent<{connected:boolean}>;
} {
let client = new Client(config.clientImplementation);
let server = client.sendAllToSocket(config.serverContract);
let cast = client.setupAllCast(config.cast);
let pendingRequestsChanged = new TypedEvent<{pending:string[]}>();
client.pendingRequestsChanged = pending => pendingRequestsChanged.emit({pending});
return { client, server, cast, pendingRequestsChanged, connectionStatusChanged: client.connectionStatusChanged };
}
export class Client extends RequesterResponder {
protected getSocket = () => this.socket;
private socket: SocketIOClient.Socket;
public connectionStatusChanged = new TypedEvent<{connected:boolean}>();
constructor(clientImplementation: any) {
super();
this.socket = io.connect(origin);
// Also provide the following services to the server
this.registerAllFunctionsExportedFromAsResponders(clientImplementation);
this.startListening();
this.socket.on(anycastMessageName,(msg:CastMessage<any>)=>{
this.typedEvents[msg.message].emit(msg.data);
});
let connected = false;
setInterval(() => {
let newConnected = this.socket.connected;
if (newConnected != connected) {
connected = newConnected;
this.connectionStatusChanged.emit({ connected });
}
}, 2000);
}
private typedEvents:{[key:string]:TypedEvent<any>} = {};
/**
* Each member of `instance` must be a typed event
* we wire these up to be emitted in the client if an emit is called on the server
*/
setupAllCast<T>(instance: T): T {
Object.keys(instance).forEach(name => {
// Override the actual emit function with one that sends it on to the server
this.typedEvents[name] = instance[name];
});
return instance;
}
}