cbcore-ts 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.idea/misc.xml +6 -0
- package/.idea/modules.xml +8 -0
- package/.idea/uicore.iml +9 -0
- package/.idea/vcs.xml +6 -0
- package/.jshintrc +4 -0
- package/LICENSE +21 -0
- package/README.md +5 -0
- package/compiledScripts/CBCore.d.ts +54 -0
- package/compiledScripts/CBCore.js +182 -0
- package/compiledScripts/CBCore.js.map +7 -0
- package/compiledScripts/CBDataInterfaces.d.ts +156 -0
- package/compiledScripts/CBDataInterfaces.js +35 -0
- package/compiledScripts/CBDataInterfaces.js.map +7 -0
- package/compiledScripts/CBLanguageService.d.ts +43 -0
- package/compiledScripts/CBLanguageService.js +167 -0
- package/compiledScripts/CBLanguageService.js.map +7 -0
- package/compiledScripts/CBServerClient.d.ts +10 -0
- package/compiledScripts/CBServerClient.js +88 -0
- package/compiledScripts/CBServerClient.js.map +7 -0
- package/compiledScripts/CBSocketCallbackHolder.d.ts +65 -0
- package/compiledScripts/CBSocketCallbackHolder.js +343 -0
- package/compiledScripts/CBSocketCallbackHolder.js.map +7 -0
- package/compiledScripts/CBSocketClient.d.ts +70 -0
- package/compiledScripts/CBSocketClient.js +371 -0
- package/compiledScripts/CBSocketClient.js.map +7 -0
- package/compiledScripts/index.d.ts +6 -0
- package/compiledScripts/index.js +23 -0
- package/compiledScripts/index.js.map +7 -0
- package/etsc.config.mjs +23 -0
- package/package.json +51 -0
- package/rollup.config.js +49 -0
- package/scripts/CBCore.ts +381 -0
- package/scripts/CBDataInterfaces.ts +336 -0
- package/scripts/CBLanguageService.ts +371 -0
- package/scripts/CBServerClient.ts +147 -0
- package/scripts/CBSocketCallbackHolder.ts +872 -0
- package/scripts/CBSocketClient.ts +748 -0
- package/scripts/index.ts +18 -0
- package/tsconfig.json +69 -0
|
@@ -0,0 +1,872 @@
|
|
|
1
|
+
import objectHash from "object-hash"
|
|
2
|
+
import { FIRST, IS, IS_NOT, nil, NO, UIObject, YES } from "../../uicore-ts"
|
|
3
|
+
import {
|
|
4
|
+
CBSocketMessage,
|
|
5
|
+
CBSocketMessageCompletionFunction,
|
|
6
|
+
CBSocketMessageHandlerFunction, CBSocketMessageSendResponseFunction, CBSocketMultipleMessage,
|
|
7
|
+
CBSocketMultipleMessagecompletionFunction, CBSocketMultipleMessageObject
|
|
8
|
+
} from "./CBDataInterfaces"
|
|
9
|
+
import { CBSocketClient } from "./CBSocketClient"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
interface CBSocketCallbackHolderMessageDescriptor {
|
|
16
|
+
|
|
17
|
+
key: string;
|
|
18
|
+
message: {
|
|
19
|
+
identifier: string;
|
|
20
|
+
inResponseToIdentifier?: string;
|
|
21
|
+
keepWaitingForResponses?: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
sentAtTime: number;
|
|
26
|
+
|
|
27
|
+
//completionTriggered: boolean;
|
|
28
|
+
|
|
29
|
+
messageDataHash: string;
|
|
30
|
+
|
|
31
|
+
responseDataHash?: string;
|
|
32
|
+
|
|
33
|
+
mainResponseReceived: boolean;
|
|
34
|
+
|
|
35
|
+
anyMainResponseReceived: boolean;
|
|
36
|
+
|
|
37
|
+
completionPolicy: string;
|
|
38
|
+
completionFunction: CBSocketMessageCompletionFunction;
|
|
39
|
+
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
interface CBSocketCallbackHolderStoredResponseObject {
|
|
44
|
+
|
|
45
|
+
messageKey: string;
|
|
46
|
+
messageData: any;
|
|
47
|
+
messageDataHash: string;
|
|
48
|
+
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
export class CBSocketCallbackHolder extends UIObject {
|
|
55
|
+
|
|
56
|
+
messageDescriptors: {
|
|
57
|
+
|
|
58
|
+
[x: string]: CBSocketCallbackHolderMessageDescriptor[]
|
|
59
|
+
|
|
60
|
+
} = {}
|
|
61
|
+
|
|
62
|
+
handlers: {
|
|
63
|
+
[x: string]: CBSocketMessageHandlerFunction[]
|
|
64
|
+
} = {}
|
|
65
|
+
|
|
66
|
+
onetimeHandlers: {
|
|
67
|
+
[x: string]: CBSocketMessageHandlerFunction[]
|
|
68
|
+
} = {}
|
|
69
|
+
|
|
70
|
+
keysForIdentifiers: {
|
|
71
|
+
|
|
72
|
+
[x: string]: string
|
|
73
|
+
|
|
74
|
+
} = {}
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
isValid = YES
|
|
78
|
+
_storeableResponseKeys: string[]
|
|
79
|
+
_storedResponseHashesDictionary: {
|
|
80
|
+
|
|
81
|
+
[x: string]: {
|
|
82
|
+
|
|
83
|
+
hash: string,
|
|
84
|
+
validityDate: number
|
|
85
|
+
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
}
|
|
89
|
+
_verifiedResponseHashesDictionary: {
|
|
90
|
+
|
|
91
|
+
[x: string]: boolean
|
|
92
|
+
|
|
93
|
+
} = {}
|
|
94
|
+
|
|
95
|
+
_socketClient: CBSocketClient
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
constructor(socketClient: CBSocketClient, previousCallbackHolder?: CBSocketCallbackHolder) {
|
|
102
|
+
|
|
103
|
+
super()
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
this._socketClient = socketClient
|
|
107
|
+
|
|
108
|
+
if (IS(previousCallbackHolder)) {
|
|
109
|
+
|
|
110
|
+
this.handlers = previousCallbackHolder.handlers
|
|
111
|
+
this._verifiedResponseHashesDictionary = previousCallbackHolder._verifiedResponseHashesDictionary
|
|
112
|
+
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
triggerDisconnectHandlers() {
|
|
126
|
+
|
|
127
|
+
this.messageDescriptors.forEach(function (descriptor: CBSocketCallbackHolderMessageDescriptor, key: string) {
|
|
128
|
+
|
|
129
|
+
if (descriptor.mainResponseReceived) {
|
|
130
|
+
|
|
131
|
+
descriptor.completionFunction(CBSocketClient.disconnectionMessage, nil)
|
|
132
|
+
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
registerHandler(key: string, handlerFunction: CBSocketMessageHandlerFunction) {
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
if (!this.handlers[key]) {
|
|
147
|
+
|
|
148
|
+
this.handlers[key] = []
|
|
149
|
+
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
this.handlers[key].push(handlerFunction)
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
registerOnetimeHandler(key: string, handlerFunction: CBSocketMessageHandlerFunction) {
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
if (!this.onetimeHandlers[key]) {
|
|
162
|
+
|
|
163
|
+
this.onetimeHandlers[key] = []
|
|
164
|
+
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
this.onetimeHandlers[key].push(handlerFunction)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
get storedResponseHashesDictionary() {
|
|
178
|
+
|
|
179
|
+
if (IS_NOT(this._storedResponseHashesDictionary)) {
|
|
180
|
+
|
|
181
|
+
this._storedResponseHashesDictionary = JSON.parse(localStorage["CBSocketResponseHashesDictionary"] || "{}")
|
|
182
|
+
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return this._storedResponseHashesDictionary
|
|
186
|
+
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
storedResponseHashObjectForKey(requestKey: string, requestDataHash: string) {
|
|
190
|
+
|
|
191
|
+
const localStorageKey = this.keyForRequestKeyAndRequestDataHash(requestKey, requestDataHash)
|
|
192
|
+
|
|
193
|
+
const hashObject = this.storedResponseHashesDictionary[localStorageKey]
|
|
194
|
+
|
|
195
|
+
const result = FIRST(hashObject, {} as any)
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
return result
|
|
200
|
+
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
storedResponseForKey(requestKey: string, requestDataHash: string) {
|
|
204
|
+
|
|
205
|
+
const localStorageKey = this.keyForRequestKeyAndRequestDataHash(requestKey, requestDataHash)
|
|
206
|
+
|
|
207
|
+
const storedObject = JSON.parse(localStorage[localStorageKey] || "{}")
|
|
208
|
+
|
|
209
|
+
return storedObject.responseMessageData
|
|
210
|
+
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
keyForRequestKeyAndRequestDataHash(requestKey: string, requestDataHash: string) {
|
|
214
|
+
|
|
215
|
+
const result = "_CBSCH_LS_key_" + requestKey + "_" + requestDataHash
|
|
216
|
+
|
|
217
|
+
return result
|
|
218
|
+
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
storeResponse(
|
|
222
|
+
requestKey: string,
|
|
223
|
+
requestDataHash: string,
|
|
224
|
+
responseMessage: CBSocketMessage<any>,
|
|
225
|
+
responseDataHash: string
|
|
226
|
+
) {
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
if (!responseMessage.canBeStoredAsResponse ||
|
|
230
|
+
(IS_NOT(responseMessage.messageData) && IS_NOT(responseMessage.messageDataHash))) {
|
|
231
|
+
|
|
232
|
+
return
|
|
233
|
+
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
const localStorageKey = this.keyForRequestKeyAndRequestDataHash(requestKey, requestDataHash)
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
var validityDate: number
|
|
241
|
+
|
|
242
|
+
if (responseMessage.responseValidityDuration) {
|
|
243
|
+
|
|
244
|
+
validityDate = Date.now() + responseMessage.responseValidityDuration
|
|
245
|
+
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const storedResponseHashesDictionary = this.storedResponseHashesDictionary
|
|
249
|
+
storedResponseHashesDictionary[localStorageKey] = {
|
|
250
|
+
|
|
251
|
+
hash: responseDataHash,
|
|
252
|
+
validityDate: validityDate
|
|
253
|
+
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
this.saveInLocalStorage(localStorageKey, {
|
|
257
|
+
|
|
258
|
+
responseMessageData: responseMessage.messageData,
|
|
259
|
+
responseHash: responseDataHash
|
|
260
|
+
|
|
261
|
+
})
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
this.saveStoredResponseHashesDictionary(storedResponseHashesDictionary)
|
|
265
|
+
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
private saveStoredResponseHashesDictionary(storedResponseHashesDictionary: { [x: string]: { hash: string; validityDate: number; }; }) {
|
|
273
|
+
|
|
274
|
+
this.saveInLocalStorage("CBSocketResponseHashesDictionary", storedResponseHashesDictionary)
|
|
275
|
+
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
saveInLocalStorage(key: string, object: any) {
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
const stringToSave = JSON.stringify(object)
|
|
282
|
+
|
|
283
|
+
if (stringToSave != localStorage[key]) {
|
|
284
|
+
|
|
285
|
+
localStorage[key] = stringToSave
|
|
286
|
+
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
socketShouldSendMessage(
|
|
297
|
+
key: string,
|
|
298
|
+
message: CBSocketMessage<any>,
|
|
299
|
+
completionPolicy: string,
|
|
300
|
+
completionFunction: CBSocketMessageCompletionFunction
|
|
301
|
+
) {
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
var result = YES
|
|
306
|
+
|
|
307
|
+
var triggerStoredResponseImmediately = NO
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
const messageDataHash = objectHash(message.messageData || nil)
|
|
311
|
+
|
|
312
|
+
const descriptorKey = "socketMessageDescriptor_" + key + messageDataHash
|
|
313
|
+
|
|
314
|
+
this.messageDescriptors[descriptorKey] = (this.messageDescriptors[descriptorKey] || [])
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
const hashObject = this.storedResponseHashObjectForKey(key, messageDataHash)
|
|
318
|
+
message.storedResponseHash = hashObject.hash
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
if (completionPolicy == CBSocketClient.completionPolicy.first) {
|
|
323
|
+
|
|
324
|
+
const descriptorsForKey = (this.messageDescriptors[descriptorKey] || [])
|
|
325
|
+
|
|
326
|
+
const matchingDescriptor = descriptorsForKey.find(function (descriptor, index, array) {
|
|
327
|
+
return (descriptor.messageDataHash == messageDataHash)
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
if (matchingDescriptor) {
|
|
331
|
+
|
|
332
|
+
result = NO
|
|
333
|
+
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
if (completionPolicy == CBSocketClient.completionPolicy.storedOrFirst) {
|
|
339
|
+
|
|
340
|
+
const descriptorsForKey = (this.messageDescriptors[descriptorKey] || [])
|
|
341
|
+
|
|
342
|
+
const matchingDescriptor = descriptorsForKey.find(function (descriptor, index, array) {
|
|
343
|
+
return (descriptor.messageDataHash == messageDataHash)
|
|
344
|
+
})
|
|
345
|
+
|
|
346
|
+
const storedResponse = IS(message.storedResponseHash)
|
|
347
|
+
|
|
348
|
+
if (matchingDescriptor ||
|
|
349
|
+
(storedResponse && this._verifiedResponseHashesDictionary[message.storedResponseHash])) {
|
|
350
|
+
|
|
351
|
+
result = NO
|
|
352
|
+
|
|
353
|
+
triggerStoredResponseImmediately = YES
|
|
354
|
+
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
if (completionPolicy == CBSocketClient.completionPolicy.firstOnly) {
|
|
360
|
+
|
|
361
|
+
const descriptorsForKey = (this.messageDescriptors[descriptorKey] || [])
|
|
362
|
+
|
|
363
|
+
const matchingDescriptor = descriptorsForKey.find(function (descriptor, index, array) {
|
|
364
|
+
return (descriptor.messageDataHash == messageDataHash)
|
|
365
|
+
})
|
|
366
|
+
|
|
367
|
+
if (matchingDescriptor) {
|
|
368
|
+
|
|
369
|
+
return NO
|
|
370
|
+
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
if (hashObject && hashObject.hash && hashObject.validityDate && message.storedResponseHash &&
|
|
377
|
+
this._verifiedResponseHashesDictionary[message.storedResponseHash] && hashObject.validityDate >
|
|
378
|
+
Date.now()) {
|
|
379
|
+
|
|
380
|
+
result = NO
|
|
381
|
+
|
|
382
|
+
triggerStoredResponseImmediately = YES
|
|
383
|
+
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
if (IS(completionFunction)) {
|
|
388
|
+
|
|
389
|
+
this.messageDescriptors[descriptorKey].push({
|
|
390
|
+
|
|
391
|
+
key: key,
|
|
392
|
+
message: {
|
|
393
|
+
|
|
394
|
+
identifier: message.identifier,
|
|
395
|
+
inResponseToIdentifier: message.inResponseToIdentifier,
|
|
396
|
+
keepWaitingForResponses: message.keepWaitingForResponses
|
|
397
|
+
|
|
398
|
+
},
|
|
399
|
+
|
|
400
|
+
sentAtTime: Date.now(),
|
|
401
|
+
|
|
402
|
+
//completionTriggered: NO,
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
messageDataHash: messageDataHash,
|
|
406
|
+
|
|
407
|
+
mainResponseReceived: NO,
|
|
408
|
+
anyMainResponseReceived: NO,
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
completionPolicy: completionPolicy,
|
|
412
|
+
completionFunction: completionFunction
|
|
413
|
+
|
|
414
|
+
})
|
|
415
|
+
|
|
416
|
+
this.keysForIdentifiers[message.identifier] = descriptorKey
|
|
417
|
+
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
if (triggerStoredResponseImmediately) {
|
|
422
|
+
|
|
423
|
+
this.socketDidReceiveMessageForKey(
|
|
424
|
+
CBSocketClient.responseMessageKey,
|
|
425
|
+
{
|
|
426
|
+
|
|
427
|
+
identifier: nil,
|
|
428
|
+
messageData: nil,
|
|
429
|
+
|
|
430
|
+
inResponseToIdentifier: message.identifier,
|
|
431
|
+
|
|
432
|
+
useStoredResponse: YES
|
|
433
|
+
|
|
434
|
+
},
|
|
435
|
+
nil
|
|
436
|
+
)
|
|
437
|
+
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
return result
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
static defaultMultipleMessagecompletionFunction(responseMessages: any[], callcompletionFunctions: () => void) {
|
|
451
|
+
callcompletionFunctions()
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
socketWillSendMultipleMessage(
|
|
456
|
+
messageToSend: CBSocketMultipleMessage,
|
|
457
|
+
completionFunction: CBSocketMultipleMessagecompletionFunction = CBSocketCallbackHolder.defaultMultipleMessagecompletionFunction
|
|
458
|
+
) {
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
const key = CBSocketClient.multipleMessageKey
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
const messageDataHash = objectHash(messageToSend.messageData || nil)
|
|
466
|
+
|
|
467
|
+
const descriptorKey = "socketMessageDescriptor_" + key + messageDataHash
|
|
468
|
+
|
|
469
|
+
this.messageDescriptors[descriptorKey] = (this.messageDescriptors[descriptorKey] || [])
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
messageToSend.storedResponseHash = this.storedResponseHashObjectForKey(key, messageDataHash).hash
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
this.messageDescriptors[descriptorKey].push({
|
|
479
|
+
|
|
480
|
+
key: key,
|
|
481
|
+
message: {
|
|
482
|
+
|
|
483
|
+
identifier: messageToSend.identifier,
|
|
484
|
+
inResponseToIdentifier: messageToSend.inResponseToIdentifier,
|
|
485
|
+
keepWaitingForResponses: messageToSend.keepWaitingForResponses
|
|
486
|
+
|
|
487
|
+
},
|
|
488
|
+
|
|
489
|
+
sentAtTime: Date.now(),
|
|
490
|
+
|
|
491
|
+
//completionTriggered: NO,
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
messageDataHash: messageDataHash,
|
|
495
|
+
|
|
496
|
+
mainResponseReceived: NO,
|
|
497
|
+
anyMainResponseReceived: NO,
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
completionPolicy: CBSocketClient.completionPolicy.directOnly,
|
|
501
|
+
completionFunction: function (
|
|
502
|
+
this: CBSocketCallbackHolder,
|
|
503
|
+
responseMessage: CBSocketMultipleMessageObject[],
|
|
504
|
+
respondWithMessage
|
|
505
|
+
) {
|
|
506
|
+
|
|
507
|
+
completionFunction(
|
|
508
|
+
responseMessage.map(function (messageObject, index, array) {
|
|
509
|
+
|
|
510
|
+
return messageObject.message.messageData
|
|
511
|
+
|
|
512
|
+
}),
|
|
513
|
+
function (this: CBSocketCallbackHolder) {
|
|
514
|
+
|
|
515
|
+
//console.log("Received multiple message response with length of " + responseMessage.length + ".");
|
|
516
|
+
|
|
517
|
+
// Call all completion functions
|
|
518
|
+
responseMessage.forEach(function (
|
|
519
|
+
this: CBSocketCallbackHolder,
|
|
520
|
+
messageObject: CBSocketMultipleMessageObject,
|
|
521
|
+
index: number,
|
|
522
|
+
array: CBSocketMultipleMessageObject[]
|
|
523
|
+
) {
|
|
524
|
+
|
|
525
|
+
this._socketClient.didReceiveMessageForKey(messageObject.key, messageObject.message)
|
|
526
|
+
|
|
527
|
+
}.bind(this))
|
|
528
|
+
|
|
529
|
+
}.bind(this)
|
|
530
|
+
)
|
|
531
|
+
|
|
532
|
+
}.bind(this)
|
|
533
|
+
|
|
534
|
+
})
|
|
535
|
+
|
|
536
|
+
this.keysForIdentifiers[messageToSend.identifier] = descriptorKey
|
|
537
|
+
|
|
538
|
+
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
|
|
544
|
+
|
|
545
|
+
socketDidReceiveMessageForKey(
|
|
546
|
+
key: string,
|
|
547
|
+
message: CBSocketMessage<any>,
|
|
548
|
+
sendResponseFunction: CBSocketMessageSendResponseFunction
|
|
549
|
+
) {
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
if (!this.isValid) {
|
|
553
|
+
|
|
554
|
+
return
|
|
555
|
+
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
// Call static handlers
|
|
560
|
+
if (this.handlers[key]) {
|
|
561
|
+
|
|
562
|
+
this.handlers[key].forEach(function (
|
|
563
|
+
this: CBSocketCallbackHolder,
|
|
564
|
+
handler: CBSocketMessageHandlerFunction,
|
|
565
|
+
index,
|
|
566
|
+
array
|
|
567
|
+
) {
|
|
568
|
+
|
|
569
|
+
handler(message.messageData, sendResponseFunction)
|
|
570
|
+
|
|
571
|
+
}.bind(this))
|
|
572
|
+
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
if (this.onetimeHandlers[key]) {
|
|
576
|
+
|
|
577
|
+
this.onetimeHandlers[key].forEach(function (
|
|
578
|
+
this: CBSocketCallbackHolder,
|
|
579
|
+
handler: CBSocketMessageHandlerFunction,
|
|
580
|
+
index,
|
|
581
|
+
array
|
|
582
|
+
) {
|
|
583
|
+
|
|
584
|
+
handler(message.messageData, sendResponseFunction)
|
|
585
|
+
|
|
586
|
+
}.bind(this))
|
|
587
|
+
|
|
588
|
+
delete this.onetimeHandlers[key]
|
|
589
|
+
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
// Temporary response handlers are evaluated here
|
|
595
|
+
if (message.inResponseToIdentifier &&
|
|
596
|
+
(CBSocketClient.responseMessageKey == key || CBSocketClient.multipleMessageKey == key)) {
|
|
597
|
+
|
|
598
|
+
// Find descriptors for the key of the message that is being responded to
|
|
599
|
+
const descriptorKey = this.keysForIdentifiers[message.inResponseToIdentifier]
|
|
600
|
+
const descriptorsForKey = (this.messageDescriptors[descriptorKey] || [])
|
|
601
|
+
|
|
602
|
+
// Find response data hash to check for differences
|
|
603
|
+
const responseDataHash = message.messageDataHash
|
|
604
|
+
|
|
605
|
+
// Remove identifier from dictionary
|
|
606
|
+
if (!message.keepWaitingForResponses) {
|
|
607
|
+
|
|
608
|
+
delete this.keysForIdentifiers[message.inResponseToIdentifier]
|
|
609
|
+
|
|
610
|
+
delete this.messageDescriptors[descriptorKey]
|
|
611
|
+
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
// Function to call completion function
|
|
616
|
+
const callCompletionFunction = (descriptor, storedResponseCondition = NO) => {
|
|
617
|
+
|
|
618
|
+
var messageData = message.messageData
|
|
619
|
+
|
|
620
|
+
if (message.useStoredResponse && storedResponseCondition) {
|
|
621
|
+
|
|
622
|
+
messageData = this.storedResponseForKey(descriptor.key, descriptor.messageDataHash)
|
|
623
|
+
|
|
624
|
+
const responseHash = this.storedResponseHashObjectForKey(
|
|
625
|
+
descriptor.key,
|
|
626
|
+
descriptor.messageDataHash
|
|
627
|
+
).hash
|
|
628
|
+
|
|
629
|
+
const localStorageKey = this.keyForRequestKeyAndRequestDataHash(
|
|
630
|
+
descriptor.key,
|
|
631
|
+
descriptor.messageDataHash
|
|
632
|
+
)
|
|
633
|
+
|
|
634
|
+
if (message.responseValidityDuration && this.storedResponseHashesDictionary[localStorageKey]) {
|
|
635
|
+
|
|
636
|
+
this.storedResponseHashesDictionary[localStorageKey].validityDate = Date.now() +
|
|
637
|
+
message.responseValidityDuration
|
|
638
|
+
|
|
639
|
+
this.saveStoredResponseHashesDictionary(this.storedResponseHashesDictionary)
|
|
640
|
+
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
this._verifiedResponseHashesDictionary[responseHash] = YES
|
|
644
|
+
|
|
645
|
+
console.log("Using stored response.")
|
|
646
|
+
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
// Call completionFunction and set response data hash
|
|
650
|
+
descriptor.completionFunction(messageData, sendResponseFunction)
|
|
651
|
+
descriptor.responseDataHash = responseDataHash
|
|
652
|
+
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
|
|
656
|
+
descriptorsForKey.copy().forEach(function (
|
|
657
|
+
this: CBSocketCallbackHolder,
|
|
658
|
+
descriptor: CBSocketCallbackHolderMessageDescriptor,
|
|
659
|
+
index: number,
|
|
660
|
+
array: CBSocketCallbackHolderMessageDescriptor[]
|
|
661
|
+
) {
|
|
662
|
+
|
|
663
|
+
|
|
664
|
+
if ((descriptor.completionPolicy == CBSocketClient.completionPolicy.directOnly &&
|
|
665
|
+
descriptor.message.identifier == message.inResponseToIdentifier) || descriptor.completionPolicy ==
|
|
666
|
+
CBSocketClient.completionPolicy.first || descriptor.completionPolicy ==
|
|
667
|
+
CBSocketClient.completionPolicy.firstOnly || descriptor.completionPolicy ==
|
|
668
|
+
CBSocketClient.completionPolicy.storedOrFirst) {
|
|
669
|
+
|
|
670
|
+
// Calling completion function and removing descriptor
|
|
671
|
+
|
|
672
|
+
if (!message.keepWaitingForResponses) {
|
|
673
|
+
|
|
674
|
+
this.storeResponse(descriptor.key, descriptor.messageDataHash, message, responseDataHash)
|
|
675
|
+
|
|
676
|
+
descriptorsForKey.removeElement(descriptor)
|
|
677
|
+
|
|
678
|
+
sendResponseFunction.respondingToMainResponse = YES
|
|
679
|
+
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
callCompletionFunction(descriptor, !message.keepWaitingForResponses)
|
|
683
|
+
|
|
684
|
+
}
|
|
685
|
+
else if (descriptor.completionPolicy == CBSocketClient.completionPolicy.all) {
|
|
686
|
+
|
|
687
|
+
// Calling completion function
|
|
688
|
+
callCompletionFunction(descriptor, !message.keepWaitingForResponses)
|
|
689
|
+
|
|
690
|
+
// Marking descriptor as having been responded to
|
|
691
|
+
if (!message.keepWaitingForResponses) {
|
|
692
|
+
|
|
693
|
+
if (message.inResponseToIdentifier == descriptor.message.identifier) {
|
|
694
|
+
|
|
695
|
+
sendResponseFunction.respondingToMainResponse = YES
|
|
696
|
+
descriptor.mainResponseReceived = YES
|
|
697
|
+
descriptorsForKey.removeElement(descriptor)
|
|
698
|
+
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
descriptor.anyMainResponseReceived = YES
|
|
702
|
+
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
|
|
706
|
+
|
|
707
|
+
}
|
|
708
|
+
else if (descriptor.completionPolicy == CBSocketClient.completionPolicy.allDifferent) {
|
|
709
|
+
|
|
710
|
+
// Calling completionFunction if messageData is different from previous
|
|
711
|
+
if (descriptor.responseDataHash != responseDataHash) {
|
|
712
|
+
|
|
713
|
+
callCompletionFunction(descriptor, !message.keepWaitingForResponses)
|
|
714
|
+
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
// Marking descriptor as having been responded to
|
|
718
|
+
if (!message.keepWaitingForResponses) {
|
|
719
|
+
|
|
720
|
+
if (message.inResponseToIdentifier == descriptor.message.identifier) {
|
|
721
|
+
|
|
722
|
+
sendResponseFunction.respondingToMainResponse = YES
|
|
723
|
+
descriptor.mainResponseReceived = YES
|
|
724
|
+
descriptorsForKey.removeElement(descriptor)
|
|
725
|
+
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
descriptor.anyMainResponseReceived = YES
|
|
729
|
+
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
}
|
|
733
|
+
else if (descriptor.completionPolicy == CBSocketClient.completionPolicy.last &&
|
|
734
|
+
descriptor.message.identifier == message.inResponseToIdentifier) {
|
|
735
|
+
|
|
736
|
+
if (!message.keepWaitingForResponses) {
|
|
737
|
+
|
|
738
|
+
// Marking descriptor as having been responded to
|
|
739
|
+
descriptor.mainResponseReceived = YES
|
|
740
|
+
descriptor.anyMainResponseReceived = YES
|
|
741
|
+
|
|
742
|
+
sendResponseFunction.respondingToMainResponse = YES
|
|
743
|
+
|
|
744
|
+
}
|
|
745
|
+
else {
|
|
746
|
+
|
|
747
|
+
descriptor.completionFunction(message.messageData, sendResponseFunction)
|
|
748
|
+
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
}
|
|
752
|
+
else if (descriptor.completionPolicy == CBSocketClient.completionPolicy.firstAndLast ||
|
|
753
|
+
descriptor.completionPolicy == CBSocketClient.completionPolicy.firstAndLastIfDifferent) {
|
|
754
|
+
|
|
755
|
+
if (!message.keepWaitingForResponses) {
|
|
756
|
+
|
|
757
|
+
// Only calling completionFunction once as a first response call
|
|
758
|
+
if (!descriptor.anyMainResponseReceived) {
|
|
759
|
+
|
|
760
|
+
callCompletionFunction(descriptor, !message.keepWaitingForResponses)
|
|
761
|
+
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
// Marking descriptor as having been responded to
|
|
765
|
+
if (descriptor.message.identifier == message.inResponseToIdentifier) {
|
|
766
|
+
|
|
767
|
+
descriptor.mainResponseReceived = YES
|
|
768
|
+
sendResponseFunction.respondingToMainResponse = YES
|
|
769
|
+
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
descriptor.anyMainResponseReceived = YES
|
|
773
|
+
|
|
774
|
+
}
|
|
775
|
+
else if (descriptor.message.identifier == message.inResponseToIdentifier &&
|
|
776
|
+
message.keepWaitingForResponses) {
|
|
777
|
+
|
|
778
|
+
descriptor.completionFunction(message.messageData, sendResponseFunction)
|
|
779
|
+
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
}.bind(this))
|
|
785
|
+
|
|
786
|
+
|
|
787
|
+
|
|
788
|
+
|
|
789
|
+
|
|
790
|
+
// Last message completion policies
|
|
791
|
+
|
|
792
|
+
const allResponsesReceived = descriptorsForKey.allMatch(function (descriptorObject, index, array) {
|
|
793
|
+
return descriptorObject.mainResponseReceived
|
|
794
|
+
})
|
|
795
|
+
|
|
796
|
+
descriptorsForKey.copy().forEach(function (
|
|
797
|
+
this: CBSocketCallbackHolder,
|
|
798
|
+
descriptor: CBSocketCallbackHolderMessageDescriptor,
|
|
799
|
+
index: number,
|
|
800
|
+
array: CBSocketCallbackHolderMessageDescriptor[]
|
|
801
|
+
) {
|
|
802
|
+
|
|
803
|
+
if ((descriptor.completionPolicy == CBSocketClient.completionPolicy.last ||
|
|
804
|
+
descriptor.completionPolicy == CBSocketClient.completionPolicy.firstAndLast) &&
|
|
805
|
+
allResponsesReceived && !message.keepWaitingForResponses) {
|
|
806
|
+
|
|
807
|
+
// Calling completionFunction
|
|
808
|
+
callCompletionFunction(descriptor, !message.keepWaitingForResponses)
|
|
809
|
+
|
|
810
|
+
// Cleaning up
|
|
811
|
+
descriptorsForKey.removeElement(descriptor)
|
|
812
|
+
|
|
813
|
+
}
|
|
814
|
+
else if (descriptor.completionPolicy == CBSocketClient.completionPolicy.firstAndLastIfDifferent &&
|
|
815
|
+
allResponsesReceived && !message.keepWaitingForResponses) {
|
|
816
|
+
|
|
817
|
+
// Calling completionFunction if needed
|
|
818
|
+
if (descriptor.responseDataHash != responseDataHash) {
|
|
819
|
+
|
|
820
|
+
callCompletionFunction(descriptor, !message.keepWaitingForResponses)
|
|
821
|
+
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
// Cleaning up
|
|
825
|
+
descriptorsForKey.removeElement(descriptor)
|
|
826
|
+
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
}.bind(this))
|
|
830
|
+
|
|
831
|
+
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
|
|
835
|
+
|
|
836
|
+
|
|
837
|
+
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
|
|
841
|
+
|
|
842
|
+
|
|
843
|
+
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
|
|
847
|
+
|
|
848
|
+
|
|
849
|
+
|
|
850
|
+
|
|
851
|
+
|
|
852
|
+
|
|
853
|
+
|
|
854
|
+
|
|
855
|
+
|
|
856
|
+
|
|
857
|
+
|
|
858
|
+
|
|
859
|
+
|
|
860
|
+
|
|
861
|
+
|
|
862
|
+
|
|
863
|
+
|
|
864
|
+
|
|
865
|
+
|
|
866
|
+
|
|
867
|
+
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
|
|
872
|
+
|