This repository has been archived by the owner on Oct 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathsocketio.common.ts
76 lines (60 loc) · 1.9 KB
/
socketio.common.ts
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
export interface SocketOptions {
query?: string | Object;
forceWebsockets?: boolean,
android?: any;
ios?: any;
}
export abstract class Common {
protected _listeners = new WeakMap();
abstract get connected(): boolean;
get disconnected() {
return !this.connected;
}
abstract connect();
abstract disconnect();
open() {
this.connect();
}
close() {
this.disconnect();
}
abstract on(event: string, callback: (...payload: Array<any> /*, ack?: Function */) => any) : this;
abstract once(event: string, callback: (...payload: Array<any> /*, ack?: Function */) => any) : this;
abstract off(event: string, callback?: Function): this;
abstract emit(event: string, ...payload: Array<any>): this;
addEventListener(event: string, callback: (...payload: Array<any> /*, ack?: Function */) => any) : this {
return this.on(event, callback);
}
removeListener(event: string, callback?: Function): this {
return this.off(event, callback);
}
removeEventListener(event: string, callback?: Function): this {
return this.off(event, callback);
}
}
export const debugNop = function(...args: Array<any>): void { };
export function debugDefault(...args: Array<any>) {
args = args.map((value) => {
if (typeof value === 'object' && value) {
try {
value = JSON.stringify(value);
} catch (e) {
value = value.toString();
}
}
return value;
});
args.unshift('nativescript-socket.io');
// @ts-ignore
console.log.apply(console, args);
}
let _debug = debugNop;
export function debug(...args: Array<any>){
_debug.apply(null, args);
}
export function enableDebug(debugFn: ((...args: Array<any>) => any) = debugDefault): void {
_debug = debugFn;
}
export function disableDebug(): void {
_debug = debugNop;
}