@super-protocol/sdk-js 0.15.0 → 0.15.2-beta.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/build/TIIGenerator.js +129 -239
- package/build/connectors/BaseConnector.js +17 -47
- package/build/connectors/BlockchainConnector.js +222 -348
- package/build/connectors/BlockchainEventsListener.js +38 -92
- package/build/contracts/app.json +466 -35
- package/build/crypto/Crypto.js +63 -135
- package/build/crypto/index.js +1 -1
- package/build/crypto/nodejs/AES.js +62 -119
- package/build/crypto/nodejs/ARIA.js +61 -118
- package/build/crypto/nodejs/ECIES.js +51 -87
- package/build/crypto/nodejs/NativeCrypto.js +64 -118
- package/build/crypto/nodejs/RSA-Hybrid.js +54 -113
- package/build/index.d.ts +5 -0
- package/build/index.js +14 -2
- package/build/logger.d.ts +3 -1
- package/build/logger.js +2 -2
- package/build/models/EtlModel.js +65 -114
- package/build/models/Offer.js +227 -462
- package/build/models/Order.d.ts +4 -0
- package/build/models/Order.js +326 -559
- package/build/models/Provider.d.ts +2 -0
- package/build/models/Provider.js +74 -153
- package/build/models/TCB.js +67 -171
- package/build/models/TeeOffer.d.ts +1 -0
- package/build/models/TeeOffer.js +389 -731
- package/build/proto/Compression.js +20 -21
- package/build/proto/TRI.js +46 -50
- package/build/proto/TeeProperties.js +60 -66
- package/build/providers/storage/ChunksDownloadDecorator.d.ts +1 -1
- package/build/providers/storage/ChunksDownloadDecorator.js +125 -235
- package/build/providers/storage/IStorageProvider.d.ts +1 -1
- package/build/providers/storage/S3StorageProvider.js +173 -298
- package/build/providers/storage/StorageAdapter.d.ts +60 -0
- package/build/providers/storage/StorageAdapter.js +322 -0
- package/build/providers/storage/StorageContentWriter.d.ts +46 -0
- package/build/providers/storage/StorageContentWriter.js +182 -0
- package/build/providers/storage/StorageKeyValueAdapter.d.ts +23 -0
- package/build/providers/storage/StorageKeyValueAdapter.js +157 -0
- package/build/providers/storage/StorageMetadataReader.d.ts +20 -0
- package/build/providers/storage/StorageMetadataReader.js +67 -0
- package/build/providers/storage/StorjAdapter.d.ts +19 -0
- package/build/providers/storage/StorjAdapter.js +73 -0
- package/build/providers/storage/StorjStorageProvider.js +138 -315
- package/build/providers/storage/getStorageProvider.js +7 -7
- package/build/providers/storage/types.d.ts +7 -0
- package/build/providers/storage/types.js +2 -0
- package/build/staticModels/ActiveOffers.js +32 -100
- package/build/staticModels/ActiveOrders.js +42 -120
- package/build/staticModels/BaseStaticModel.js +7 -10
- package/build/staticModels/Consensus.d.ts +4 -1
- package/build/staticModels/Consensus.js +164 -220
- package/build/staticModels/Deposits.js +109 -201
- package/build/staticModels/Marks.js +21 -82
- package/build/staticModels/ModelPackager.js +34 -84
- package/build/staticModels/Offers.js +140 -266
- package/build/staticModels/Orders.d.ts +1 -0
- package/build/staticModels/Orders.js +286 -429
- package/build/staticModels/ProviderRegistry.js +114 -225
- package/build/staticModels/Superpro.js +17 -63
- package/build/staticModels/SuperproToken.js +90 -169
- package/build/staticModels/TeeOffers.js +220 -416
- package/build/store.js +2 -2
- package/build/types/HardwareInfo.js +2 -2
- package/build/types/Order.js +4 -4
- package/build/types/Provider.js +1 -1
- package/build/types/TeeOfferInfo.js +1 -1
- package/build/types/TeeOfferOption.js +2 -2
- package/build/types/TeeOfferSlot.js +2 -2
- package/build/types/ValueOfferSlot.js +3 -3
- package/build/utils/Monitoring.js +24 -28
- package/build/utils/NonceTracker.js +56 -121
- package/build/utils/PubSub.d.ts +9 -0
- package/build/utils/PubSub.js +36 -0
- package/build/utils/TxManager.js +121 -215
- package/build/utils/compressors/GzipCompressor.js +13 -50
- package/build/utils/compressors/UncompressedCompressor.js +10 -13
- package/build/utils/helpers/tryWithInterval.js +34 -78
- package/build/utils/resourceLoaders/BaseResourceLoader.js +26 -78
- package/build/utils/resourceLoaders/StorageProviderLoader.js +33 -97
- package/build/utils/resourceLoaders/UrlResourceLoader.js +29 -93
- package/build/utils/resourceLoaders/getResourceLoader.js +6 -6
- package/build/utils.js +60 -105
- package/package.json +4 -1
package/build/store.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
const constants_1 = require("./constants");
|
|
4
|
+
const store = {
|
|
5
5
|
web3Wss: undefined,
|
|
6
6
|
web3Https: undefined,
|
|
7
7
|
actionAccount: undefined,
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.HardwareInfoStructure = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
const OptionInfo_1 = require("./OptionInfo");
|
|
5
|
+
const SlotInfo_1 = require("./SlotInfo");
|
|
6
6
|
// Order of keys and type conversion functions for this object in blockchain contract
|
|
7
7
|
exports.HardwareInfoStructure = {
|
|
8
8
|
slotInfo: SlotInfo_1.SlotInfoStructure,
|
package/build/types/Order.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.OrderUsageStructure = exports.OrderResultStructure = exports.OrderInfoStructureArray = exports.OrderInfoStructure = exports.OrderSlotsStructure = exports.OrderArgsStructure = exports.OrderStatus = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
const utils_1 = require("ethers/lib/utils");
|
|
5
|
+
const SlotUsage_1 = require("./SlotUsage");
|
|
6
|
+
const SlotInfo_1 = require("./SlotInfo");
|
|
7
|
+
const OptionInfo_1 = require("./OptionInfo");
|
|
8
8
|
var OrderStatus;
|
|
9
9
|
(function (OrderStatus) {
|
|
10
10
|
OrderStatus["New"] = "0";
|
package/build/types/Provider.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ProviderInfoStructureV2 = exports.ProviderInfoStructure = void 0;
|
|
4
|
-
|
|
4
|
+
const utils_1 = require("ethers/lib/utils");
|
|
5
5
|
// Order of keys and type conversion functions for this object in blockchain contract
|
|
6
6
|
exports.ProviderInfoStructure = {
|
|
7
7
|
tokenReceiver: String,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TeeOfferInfoStructure = void 0;
|
|
4
|
-
|
|
4
|
+
const HardwareInfo_1 = require("./HardwareInfo");
|
|
5
5
|
// Order of keys and type conversion functions for this object in blockchain contract
|
|
6
6
|
exports.TeeOfferInfoStructure = {
|
|
7
7
|
name: String,
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TeeOfferOptionStructure = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
const OptionInfo_1 = require("./OptionInfo");
|
|
5
|
+
const SlotUsage_1 = require("./SlotUsage");
|
|
6
6
|
// Order of keys and type conversion functions for this object in blockchain contract
|
|
7
7
|
exports.TeeOfferOptionStructure = {
|
|
8
8
|
id: String,
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TeeOfferSlotStructure = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
const SlotInfo_1 = require("./SlotInfo");
|
|
5
|
+
const SlotUsage_1 = require("./SlotUsage");
|
|
6
6
|
// Order of keys and type conversion functions for this object in blockchain contract
|
|
7
7
|
exports.TeeOfferSlotStructure = {
|
|
8
8
|
id: String,
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ValueOfferSlotStructure = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
const OptionInfo_1 = require("./OptionInfo");
|
|
5
|
+
const SlotInfo_1 = require("./SlotInfo");
|
|
6
|
+
const SlotUsage_1 = require("./SlotUsage");
|
|
7
7
|
// Order of keys and type conversion functions for this object in blockchain contract
|
|
8
8
|
exports.ValueOfferSlotStructure = {
|
|
9
9
|
id: String,
|
|
@@ -4,45 +4,41 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.Monitoring = void 0;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
const logger_1 = __importDefault(require("../logger"));
|
|
8
|
+
class Monitoring {
|
|
9
|
+
constructor() {
|
|
10
10
|
this.logger = logger_1.default.child({ className: "Monitoring" });
|
|
11
11
|
this.contractMethodCalls = new Map();
|
|
12
12
|
}
|
|
13
|
-
|
|
13
|
+
static getInstance() {
|
|
14
14
|
if (!Monitoring.instance) {
|
|
15
15
|
Monitoring.instance = new Monitoring();
|
|
16
16
|
}
|
|
17
17
|
return Monitoring.instance;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
var checkInterval = process.env.PRINT_CONTRACT_CALLS_INTERVAL || 300000;
|
|
18
|
+
}
|
|
19
|
+
initializeLogging() {
|
|
20
|
+
const checkInterval = process.env.PRINT_CONTRACT_CALLS_INTERVAL || 300000;
|
|
22
21
|
this.shutdownLogging();
|
|
23
|
-
|
|
24
|
-
this.interval = setInterval(
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
var timeSpend = Math.floor((Date.now() - startTs) / (60 * 1000));
|
|
33
|
-
_this.logger.debug({
|
|
22
|
+
const startTs = Date.now();
|
|
23
|
+
this.interval = setInterval(() => {
|
|
24
|
+
this.contractMethodCalls.forEach((value, key) => this.logger.debug({
|
|
25
|
+
methodName: key,
|
|
26
|
+
calledTimes: value,
|
|
27
|
+
}));
|
|
28
|
+
const totalCalls = Array.from(this.contractMethodCalls.values()).reduce((acc, curr) => acc + curr, 0);
|
|
29
|
+
const timeSpend = Math.floor((Date.now() - startTs) / (60 * 1000));
|
|
30
|
+
this.logger.debug({
|
|
34
31
|
timeSpend: timeSpend + " min",
|
|
35
|
-
totalCalls
|
|
32
|
+
totalCalls,
|
|
36
33
|
}, "Contract methods calls");
|
|
37
34
|
}, +checkInterval);
|
|
38
|
-
}
|
|
39
|
-
|
|
35
|
+
}
|
|
36
|
+
shutdownLogging() {
|
|
40
37
|
clearInterval(this.interval);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
|
|
38
|
+
}
|
|
39
|
+
incrementCall(methodName) {
|
|
40
|
+
const prevValue = this.contractMethodCalls.get(methodName) || 0;
|
|
44
41
|
this.contractMethodCalls.set(methodName, prevValue + 1);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
}());
|
|
42
|
+
}
|
|
43
|
+
}
|
|
48
44
|
exports.Monitoring = Monitoring;
|
|
@@ -8,148 +8,83 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
11
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
13
|
};
|
|
41
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
15
|
+
const logger_1 = __importDefault(require("../logger"));
|
|
16
|
+
class NonceTracker {
|
|
17
|
+
constructor(web3, address) {
|
|
45
18
|
this.web3 = web3;
|
|
46
19
|
this.address = address;
|
|
47
20
|
this.countOfPendingTransactions = 0;
|
|
48
|
-
this.logger = logger_1.default.child({ className: "NonceTracker", address
|
|
21
|
+
this.logger = logger_1.default.child({ className: "NonceTracker", address });
|
|
49
22
|
this.logger.trace("Created NonceTracker");
|
|
50
23
|
}
|
|
51
|
-
|
|
52
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
switch (_b.label) {
|
|
56
|
-
case 0:
|
|
57
|
-
_a = this;
|
|
58
|
-
return [4 /*yield*/, this.web3.eth.getTransactionCount(this.address)];
|
|
59
|
-
case 1:
|
|
60
|
-
_a.txCount = _b.sent();
|
|
61
|
-
this.logger.trace("Initialized ".concat(this.address, " account with nonce: ").concat(this.txCount));
|
|
62
|
-
return [2 /*return*/];
|
|
63
|
-
}
|
|
64
|
-
});
|
|
24
|
+
initAccount() {
|
|
25
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
+
this.txCount = yield this.web3.eth.getTransactionCount(this.address);
|
|
27
|
+
this.logger.trace(`Initialized ${this.address} account with nonce: ${this.txCount}`);
|
|
65
28
|
});
|
|
66
|
-
}
|
|
67
|
-
|
|
29
|
+
}
|
|
30
|
+
getNonce() {
|
|
68
31
|
if (this.txCount === undefined)
|
|
69
|
-
throw Error(
|
|
70
|
-
this.logger.trace(
|
|
32
|
+
throw Error(`NonceTracker for address ${this.address} is not initialized`);
|
|
33
|
+
this.logger.trace(`Get nonce: ${this.txCount}`);
|
|
71
34
|
return this.txCount;
|
|
72
|
-
}
|
|
73
|
-
|
|
35
|
+
}
|
|
36
|
+
consumeNonce() {
|
|
74
37
|
if (this.txCount === undefined)
|
|
75
|
-
throw Error(
|
|
76
|
-
this.logger.trace(
|
|
38
|
+
throw Error(`NonceTracker for address ${this.address} is not initialized`);
|
|
39
|
+
this.logger.trace(`Consume nonce: ${this.txCount + 1}`);
|
|
77
40
|
return this.txCount++;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
return [4 /*yield*/, this.waitForPendingTransactions()];
|
|
86
|
-
case 1:
|
|
87
|
-
_a.sent();
|
|
88
|
-
_a.label = 2;
|
|
89
|
-
case 2:
|
|
90
|
-
this.countOfPendingTransactions++;
|
|
91
|
-
return [2 /*return*/];
|
|
92
|
-
}
|
|
93
|
-
});
|
|
41
|
+
}
|
|
42
|
+
onTransactionStartPublishing() {
|
|
43
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
+
if (this.transactionsOnHold) {
|
|
45
|
+
yield this.waitForPendingTransactions();
|
|
46
|
+
}
|
|
47
|
+
this.countOfPendingTransactions++;
|
|
94
48
|
});
|
|
95
|
-
}
|
|
96
|
-
|
|
49
|
+
}
|
|
50
|
+
onTransactionPublished() {
|
|
97
51
|
this.countOfPendingTransactions--;
|
|
98
52
|
if (this.countOfPendingTransactions === 0) {
|
|
99
53
|
this.sendHoldTransactions();
|
|
100
54
|
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
case 1: return [4 /*yield*/, this.waitForPendingTransactions()];
|
|
114
|
-
case 2:
|
|
115
|
-
_a.sent();
|
|
116
|
-
_a.label = 3;
|
|
117
|
-
case 3: return [2 /*return*/];
|
|
118
|
-
}
|
|
119
|
-
});
|
|
55
|
+
}
|
|
56
|
+
onTransactionError() {
|
|
57
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
58
|
+
this.countOfPendingTransactions--;
|
|
59
|
+
if (!this.transactionsOnHold)
|
|
60
|
+
this.transactionsOnHold = [];
|
|
61
|
+
if (this.countOfPendingTransactions === 0) {
|
|
62
|
+
this.sendHoldTransactions();
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
yield this.waitForPendingTransactions();
|
|
66
|
+
}
|
|
120
67
|
});
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
resolve();
|
|
131
|
-
});
|
|
132
|
-
})];
|
|
68
|
+
}
|
|
69
|
+
waitForPendingTransactions() {
|
|
70
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
71
|
+
return new Promise((resolve) => {
|
|
72
|
+
if (!this.transactionsOnHold)
|
|
73
|
+
return resolve();
|
|
74
|
+
this.transactionsOnHold.push(() => {
|
|
75
|
+
resolve();
|
|
76
|
+
});
|
|
133
77
|
});
|
|
134
78
|
});
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
return [4 /*yield*/, this.initAccount()];
|
|
144
|
-
case 1:
|
|
145
|
-
_a.sent();
|
|
146
|
-
this.transactionsOnHold.forEach(function (callback) { return callback(); });
|
|
147
|
-
this.transactionsOnHold = undefined;
|
|
148
|
-
return [2 /*return*/];
|
|
149
|
-
}
|
|
150
|
-
});
|
|
79
|
+
}
|
|
80
|
+
sendHoldTransactions() {
|
|
81
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
82
|
+
if (!this.transactionsOnHold)
|
|
83
|
+
return;
|
|
84
|
+
yield this.initAccount();
|
|
85
|
+
this.transactionsOnHold.forEach((callback) => callback());
|
|
86
|
+
this.transactionsOnHold = undefined;
|
|
151
87
|
});
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
}());
|
|
88
|
+
}
|
|
89
|
+
}
|
|
155
90
|
exports.default = NonceTracker;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
type Listener<Message> = (message: Message) => void;
|
|
2
|
+
export default class PubSub<Event, Message> {
|
|
3
|
+
private subscriptions;
|
|
4
|
+
subscribe(event: Event, listener: Listener<Message>): () => void;
|
|
5
|
+
unsubscribe(event: Event, listener: Listener<Message>): void;
|
|
6
|
+
publish(event: Event, message: Message): void;
|
|
7
|
+
clear(event?: Event): void;
|
|
8
|
+
}
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class PubSub {
|
|
4
|
+
constructor() {
|
|
5
|
+
this.subscriptions = new Map();
|
|
6
|
+
}
|
|
7
|
+
subscribe(event, listener) {
|
|
8
|
+
this.subscriptions.set(event, (this.subscriptions.get(event) || []).concat(listener));
|
|
9
|
+
return () => {
|
|
10
|
+
this.unsubscribe(event, listener);
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
unsubscribe(event, listener) {
|
|
14
|
+
const eventListeners = this.subscriptions.get(event);
|
|
15
|
+
if (eventListeners) {
|
|
16
|
+
this.subscriptions.set(event, eventListeners.filter((l) => l !== listener));
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
publish(event, message) {
|
|
20
|
+
const eventListeners = this.subscriptions.get(event);
|
|
21
|
+
if (eventListeners) {
|
|
22
|
+
eventListeners.forEach((listener) => {
|
|
23
|
+
listener(message);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
clear(event) {
|
|
28
|
+
if (event) {
|
|
29
|
+
this.subscriptions.delete(event);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
this.subscriptions.clear();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.default = PubSub;
|