forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-websocket.d.ts
More file actions
157 lines (133 loc) · 4.88 KB
/
angular-websocket.d.ts
File metadata and controls
157 lines (133 loc) · 4.88 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Type definitions for angular-websocket v2.0
// Project: https://github.com/AngularClass/angular-websocket
// Definitions by: Nick Veys <https://github.com/nickveys>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path='../angularjs/angular.d.ts' />
declare namespace angular.websocket {
/**
* Options available to be specified for IWebSocketProvider.
*/
type IWebSocketConfigOptions = {
scope?: ng.IScope;
rootScopeFailOver?: boolean;
useApplyAsync?: boolean;
initialTimeout?: number;
maxTimeout?: number;
binaryType?: "blob" | "arraybuffer";
reconnectIfNotNormalClose?: boolean;
}
interface IWebSocketProvider {
/**
* Creates and opens an IWebSocket instance.
*
* @param url url to connect to
* @return websocket instance
*/
(url: string, protocols?: string | string[] | IWebSocketConfigOptions, options?: IWebSocketConfigOptions): IWebSocket;
}
/** Options available to be specified for IWebSocket.onMessage */
type IWebSocketMessageOptions = {
/**
* If specified, only messages that match the filter will cause the message event
* to be fired.
*/
filter?: string | RegExp;
/** If true, each message handled will safely call `$rootScope.$digest()`. */
autoApply?: boolean;
}
/** Type corresponding to onMessage callbaks stored in $Websocket#onMessageCallbacks instance. */
type IWebSocketMessageHandler = {
fn: (evt: MessageEvent) => void;
pattern: string | RegExp;
autoApply: boolean;
}
/** Type corresponding to items stored in $WebSocket#sendQueue instance. */
type IWebSocketQueueItem = {
message: any;
defered: ng.IPromise<void>;
}
interface IWebSocket {
/**
* Adds a callback to be executed each time a socket connection is opened for
* this instance.
*
* @param event event object
* @returns this instance, for method chaining
*/
onOpen(callback: (event: Event) => void): IWebSocket;
/**
* Adds a callback to be executed each time a socket connection is closed for
* this instance.
*
* @param event event object
* @returns this instance, for method chaining
*/
onClose(callback: (event: CloseEvent) => void): IWebSocket;
/**
* Adds a callback to be executed each time a socket connection is closed for
* this instance.
*
* @param event event object
* @returns this instance, for method chaining
*/
onError(callback: (event: Event) => void): IWebSocket;
/**
* Adds a callback to be executed each time a socket connection has an error for
* this instance.
*
* @param event event object
* @returns this instance, for method chaining
*/
onMessage(callback: (event: MessageEvent) => void, options?: IWebSocketMessageOptions): IWebSocket;
/**
* Closes the underlying socket, as long as no data is still being sent from the client.
*
* @param force if `true`, force close even if data is still being sent
* @returns this instance, for method chaining
*/
close(force?: boolean): IWebSocket;
/**
* Adds data to a queue, and attempts to send if the socket is ready.
*
* @param data data to send, if this is an object, it will be stringified before sending
*/
send(data: string | {}): ng.IPromise<any>;
/**
* WebSocket instance.
*/
socket: WebSocket;
/**
* Queue of send calls to be made on socket when socket is able to receive data.
*/
sendQueue: IWebSocketQueueItem[];
/**
* List of callbacks to be executed when the socket is opened.
*/
onOpenCallbacks: ((evt: Event) => void)[];
/**
* List of callbacks to be executed when a message is received from the socket.
*/
onMessageCallbacks: IWebSocketMessageHandler[];
/**
* List of callbacks to be executed when an error is received from the socket.
*/
onErrorCallbacks: ((evt: Event) => void)[];
/**
* List of callbacks to be executed when the socket is closed.
*/
onCloseCallbacks: ((evt: CloseEvent) => void)[];
/**
* Returns either the readyState value from the underlying WebSocket instance
* or a proprietary value representing the internal state
*/
readyState: number;
/**
* The initial timeout.
*/
initialTimeout: number;
/**
* Maximun timeout used to determine reconnection delay.
*/
maxTimeout: number;
}
}