gritty 8.0.0 → 8.1.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/ChangeLog +5 -0
- package/dist/gritty.js +63 -19
- package/dist/gritty.js.map +1 -1
- package/dist-dev/gritty.js +1 -1
- package/package.json +7 -6
- package/server/gritty.js +3 -1
package/ChangeLog
CHANGED
package/dist/gritty.js
CHANGED
|
@@ -4447,6 +4447,29 @@ class Socket extends component_emitter_1.Emitter {
|
|
|
4447
4447
|
*/
|
|
4448
4448
|
this._queueSeq = 0;
|
|
4449
4449
|
this.ids = 0;
|
|
4450
|
+
/**
|
|
4451
|
+
* A map containing acknowledgement handlers.
|
|
4452
|
+
*
|
|
4453
|
+
* The `withError` attribute is used to differentiate handlers that accept an error as first argument:
|
|
4454
|
+
*
|
|
4455
|
+
* - `socket.emit("test", (err, value) => { ... })` with `ackTimeout` option
|
|
4456
|
+
* - `socket.timeout(5000).emit("test", (err, value) => { ... })`
|
|
4457
|
+
* - `const value = await socket.emitWithAck("test")`
|
|
4458
|
+
*
|
|
4459
|
+
* From those that don't:
|
|
4460
|
+
*
|
|
4461
|
+
* - `socket.emit("test", (value) => { ... });`
|
|
4462
|
+
*
|
|
4463
|
+
* In the first case, the handlers will be called with an error when:
|
|
4464
|
+
*
|
|
4465
|
+
* - the timeout is reached
|
|
4466
|
+
* - the socket gets disconnected
|
|
4467
|
+
*
|
|
4468
|
+
* In the second case, the handlers will be simply discarded upon disconnection, since the client will never receive
|
|
4469
|
+
* an acknowledgement from the server.
|
|
4470
|
+
*
|
|
4471
|
+
* @private
|
|
4472
|
+
*/
|
|
4450
4473
|
this.acks = {};
|
|
4451
4474
|
this.flags = {};
|
|
4452
4475
|
this.io = io;
|
|
@@ -4636,11 +4659,13 @@ class Socket extends component_emitter_1.Emitter {
|
|
|
4636
4659
|
debug("event with ack id %d has timed out after %d ms", id, timeout);
|
|
4637
4660
|
ack.call(this, new Error("operation has timed out"));
|
|
4638
4661
|
}, timeout);
|
|
4639
|
-
|
|
4662
|
+
const fn = (...args) => {
|
|
4640
4663
|
// @ts-ignore
|
|
4641
4664
|
this.io.clearTimeoutFn(timer);
|
|
4642
|
-
ack.apply(this,
|
|
4665
|
+
ack.apply(this, args);
|
|
4643
4666
|
};
|
|
4667
|
+
fn.withError = true;
|
|
4668
|
+
this.acks[id] = fn;
|
|
4644
4669
|
}
|
|
4645
4670
|
/**
|
|
4646
4671
|
* Emits an event and waits for an acknowledgement
|
|
@@ -4659,17 +4684,12 @@ class Socket extends component_emitter_1.Emitter {
|
|
|
4659
4684
|
* @return a Promise that will be fulfilled when the server acknowledges the event
|
|
4660
4685
|
*/
|
|
4661
4686
|
emitWithAck(ev, ...args) {
|
|
4662
|
-
// the timeout flag is optional
|
|
4663
|
-
const withErr = this.flags.timeout !== undefined || this._opts.ackTimeout !== undefined;
|
|
4664
4687
|
return new Promise((resolve, reject) => {
|
|
4665
|
-
|
|
4666
|
-
|
|
4667
|
-
|
|
4668
|
-
|
|
4669
|
-
|
|
4670
|
-
return resolve(arg1);
|
|
4671
|
-
}
|
|
4672
|
-
});
|
|
4688
|
+
const fn = (arg1, arg2) => {
|
|
4689
|
+
return arg1 ? reject(arg1) : resolve(arg2);
|
|
4690
|
+
};
|
|
4691
|
+
fn.withError = true;
|
|
4692
|
+
args.push(fn);
|
|
4673
4693
|
this.emit(ev, ...args);
|
|
4674
4694
|
});
|
|
4675
4695
|
}
|
|
@@ -4803,6 +4823,26 @@ class Socket extends component_emitter_1.Emitter {
|
|
|
4803
4823
|
this.connected = false;
|
|
4804
4824
|
delete this.id;
|
|
4805
4825
|
this.emitReserved("disconnect", reason, description);
|
|
4826
|
+
this._clearAcks();
|
|
4827
|
+
}
|
|
4828
|
+
/**
|
|
4829
|
+
* Clears the acknowledgement handlers upon disconnection, since the client will never receive an acknowledgement from
|
|
4830
|
+
* the server.
|
|
4831
|
+
*
|
|
4832
|
+
* @private
|
|
4833
|
+
*/
|
|
4834
|
+
_clearAcks() {
|
|
4835
|
+
Object.keys(this.acks).forEach((id) => {
|
|
4836
|
+
const isBuffered = this.sendBuffer.some((packet) => String(packet.id) === id);
|
|
4837
|
+
if (!isBuffered) {
|
|
4838
|
+
// note: handlers that do not accept an error as first argument are ignored here
|
|
4839
|
+
const ack = this.acks[id];
|
|
4840
|
+
delete this.acks[id];
|
|
4841
|
+
if (ack.withError) {
|
|
4842
|
+
ack.call(this, new Error("socket has been disconnected"));
|
|
4843
|
+
}
|
|
4844
|
+
}
|
|
4845
|
+
});
|
|
4806
4846
|
}
|
|
4807
4847
|
/**
|
|
4808
4848
|
* Called with socket packet.
|
|
@@ -4897,21 +4937,25 @@ class Socket extends component_emitter_1.Emitter {
|
|
|
4897
4937
|
};
|
|
4898
4938
|
}
|
|
4899
4939
|
/**
|
|
4900
|
-
* Called upon a server
|
|
4940
|
+
* Called upon a server acknowledgement.
|
|
4901
4941
|
*
|
|
4902
4942
|
* @param packet
|
|
4903
4943
|
* @private
|
|
4904
4944
|
*/
|
|
4905
4945
|
onack(packet) {
|
|
4906
4946
|
const ack = this.acks[packet.id];
|
|
4907
|
-
if ("function"
|
|
4908
|
-
debug("calling ack %s with %j", packet.id, packet.data);
|
|
4909
|
-
ack.apply(this, packet.data);
|
|
4910
|
-
delete this.acks[packet.id];
|
|
4911
|
-
}
|
|
4912
|
-
else {
|
|
4947
|
+
if (typeof ack !== "function") {
|
|
4913
4948
|
debug("bad ack %s", packet.id);
|
|
4949
|
+
return;
|
|
4950
|
+
}
|
|
4951
|
+
delete this.acks[packet.id];
|
|
4952
|
+
debug("calling ack %s with %j", packet.id, packet.data);
|
|
4953
|
+
// @ts-ignore FIXME ack is incorrectly inferred as 'never'
|
|
4954
|
+
if (ack.withError) {
|
|
4955
|
+
packet.data.unshift(null);
|
|
4914
4956
|
}
|
|
4957
|
+
// @ts-ignore
|
|
4958
|
+
ack.apply(this, packet.data);
|
|
4915
4959
|
}
|
|
4916
4960
|
/**
|
|
4917
4961
|
* Called upon server connect.
|