1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const readable_stream_1 = require("readable-stream");
|
4 | const streamsOpts = { objectMode: true };
|
5 | const defaultStoreOptions = {
|
6 | clean: true,
|
7 | };
|
8 | class Store {
|
9 | constructor(options) {
|
10 | this.options = options || {};
|
11 | this.options = Object.assign(Object.assign({}, defaultStoreOptions), options);
|
12 | this._inflights = new Map();
|
13 | }
|
14 | put(packet, cb) {
|
15 | this._inflights.set(packet.messageId, packet);
|
16 | if (cb) {
|
17 | cb();
|
18 | }
|
19 | return this;
|
20 | }
|
21 | createStream() {
|
22 | const stream = new readable_stream_1.Readable(streamsOpts);
|
23 | const values = [];
|
24 | let destroyed = false;
|
25 | let i = 0;
|
26 | this._inflights.forEach((value, key) => {
|
27 | values.push(value);
|
28 | });
|
29 | stream._read = () => {
|
30 | if (!destroyed && i < values.length) {
|
31 | stream.push(values[i++]);
|
32 | }
|
33 | else {
|
34 | stream.push(null);
|
35 | }
|
36 | };
|
37 | stream.destroy = (err) => {
|
38 | if (destroyed) {
|
39 | return;
|
40 | }
|
41 | destroyed = true;
|
42 | setTimeout(() => {
|
43 | stream.emit('close');
|
44 | }, 0);
|
45 | return stream;
|
46 | };
|
47 | return stream;
|
48 | }
|
49 | del(packet, cb) {
|
50 | const toDelete = this._inflights.get(packet.messageId);
|
51 | if (toDelete) {
|
52 | this._inflights.delete(packet.messageId);
|
53 | cb(null, toDelete);
|
54 | }
|
55 | else if (cb) {
|
56 | cb(new Error('missing packet'));
|
57 | }
|
58 | return this;
|
59 | }
|
60 | get(packet, cb) {
|
61 | const storedPacket = this._inflights.get(packet.messageId);
|
62 | if (storedPacket) {
|
63 | cb(null, storedPacket);
|
64 | }
|
65 | else if (cb) {
|
66 | cb(new Error('missing packet'));
|
67 | }
|
68 | return this;
|
69 | }
|
70 | close(cb) {
|
71 | if (this.options.clean) {
|
72 | this._inflights = null;
|
73 | }
|
74 | if (cb) {
|
75 | cb();
|
76 | }
|
77 | }
|
78 | }
|
79 | exports.default = Store;
|
80 |
|
\ | No newline at end of file |