-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathAckExtension.js
96 lines (89 loc) · 3.62 KB
/
AckExtension.js
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
/*
* Copyright (c) 2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {Extension} from "./Extension.js";
/**
* This client-side extension enables the client to acknowledge to the server
* the messages that the client has received.
* For the acknowledgement to work, the server must be configured with the
* correspondent server-side ack extension. If both client and server support
* the ack extension, then the ack functionality will take place automatically.
* By enabling this extension, all messages arriving from the server will arrive
* via /meta/connect, so the comet communication will be slightly chattier.
* The fact that all messages will return via /meta/connect means also that the
* messages will arrive with total order, which is not guaranteed if messages
* can arrive via both /meta/connect and normal response.
* Messages are not acknowledged one by one, but instead a batch of messages is
* acknowledged when the /meta/connect returns.
*/
export class AckExtension extends Extension {
#serverSupportsAcks = false;
#batch = 0;
#debug(text, args) {
this.cometd._debug(text, args);
}
registered(name, cometd) {
super.registered(name, cometd);
this.#debug("AckExtension: executing registration callback");
};
unregistered() {
this.#debug("AckExtension: executing unregistration callback");
super.unregistered();
};
incoming(message) {
const channel = message.channel;
const ext = message.ext;
if (channel === "/meta/handshake") {
if (ext) {
const ackField = ext.ack;
if (typeof ackField === "object") {
// New format.
this.#serverSupportsAcks = ackField.enabled === true;
const batch = ackField.batch;
if (typeof batch === "number") {
this.#batch = batch;
}
} else {
// Old format.
this.#serverSupportsAcks = ackField === true;
}
}
this.#debug("AckExtension: server supports acknowledgements", this.#serverSupportsAcks);
} else if (channel === "/meta/connect" && message.successful && this.#serverSupportsAcks) {
if (ext && typeof ext.ack === "number") {
this.#batch = ext.ack;
this.#debug("AckExtension: server sent batch", this.#batch);
}
}
return message;
};
outgoing(message) {
const channel = message.channel;
if (!message.ext) {
message.ext = {};
}
if (channel === "/meta/handshake") {
message.ext.ack = this.cometd && this.cometd.ackEnabled !== false;
this.#serverSupportsAcks = false;
this.#batch = 0;
} else if (channel === "/meta/connect") {
if (this.#serverSupportsAcks) {
message.ext.ack = this.#batch;
this.#debug("AckExtension: client sending batch", this.#batch);
}
}
return message;
};
}