@keplr-wallet/common 0.12.313 → 0.13.1
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/async/event-bus.d.ts +19 -0
- package/build/async/event-bus.js +88 -0
- package/build/async/event-bus.js.map +1 -0
- package/build/async/index.d.ts +1 -0
- package/build/async/index.js +18 -0
- package/build/async/index.js.map +1 -0
- package/build/index.d.ts +1 -0
- package/build/index.js +1 -0
- package/build/index.js.map +1 -1
- package/package.json +6 -5
- package/src/async/event-bus.ts +79 -0
- package/src/async/index.ts +1 -0
- package/src/index.ts +1 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface EventBusCore<T = unknown> {
|
|
2
|
+
enqueue(message: T): void;
|
|
3
|
+
subscribe(handler: (msg: T) => Promise<void> | void): void;
|
|
4
|
+
flush(): void;
|
|
5
|
+
}
|
|
6
|
+
export declare class EventBusPublisher<T = unknown> {
|
|
7
|
+
private core;
|
|
8
|
+
constructor(core: EventBusCore<T>);
|
|
9
|
+
publish(message: T): void;
|
|
10
|
+
}
|
|
11
|
+
export declare class EventBusSubscriber<T = unknown> {
|
|
12
|
+
private core;
|
|
13
|
+
constructor(core: EventBusCore<T>);
|
|
14
|
+
subscribe(handler: (msg: T) => Promise<void> | void): void;
|
|
15
|
+
}
|
|
16
|
+
export declare function createSingleChannelEventBus<T = unknown>(concurrency?: number): {
|
|
17
|
+
publisher: EventBusPublisher<T>;
|
|
18
|
+
subscriber: EventBusSubscriber<T>;
|
|
19
|
+
};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.createSingleChannelEventBus = exports.EventBusSubscriber = exports.EventBusPublisher = void 0;
|
|
16
|
+
const p_queue_1 = __importDefault(require("p-queue"));
|
|
17
|
+
class SingleChannelEventBusCore {
|
|
18
|
+
constructor(concurrency = 1) {
|
|
19
|
+
this.subscriber = null;
|
|
20
|
+
this.buffer = [];
|
|
21
|
+
this.isFlushing = false;
|
|
22
|
+
this.queue = new p_queue_1.default({ concurrency });
|
|
23
|
+
}
|
|
24
|
+
enqueue(message) {
|
|
25
|
+
this.buffer.push(message);
|
|
26
|
+
this.flush();
|
|
27
|
+
}
|
|
28
|
+
subscribe(handler) {
|
|
29
|
+
this.subscriber = handler;
|
|
30
|
+
this.flush();
|
|
31
|
+
}
|
|
32
|
+
flush() {
|
|
33
|
+
if (this.isFlushing)
|
|
34
|
+
return;
|
|
35
|
+
const subscriber = this.subscriber;
|
|
36
|
+
if (!subscriber)
|
|
37
|
+
return;
|
|
38
|
+
if (this.buffer.length === 0)
|
|
39
|
+
return;
|
|
40
|
+
this.isFlushing = true;
|
|
41
|
+
try {
|
|
42
|
+
while (this.buffer.length > 0) {
|
|
43
|
+
const msg = this.buffer.shift();
|
|
44
|
+
if (msg === undefined) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
this.queue.add(() => __awaiter(this, void 0, void 0, function* () {
|
|
48
|
+
try {
|
|
49
|
+
yield subscriber(msg);
|
|
50
|
+
}
|
|
51
|
+
catch (e) {
|
|
52
|
+
console.error("error:", e);
|
|
53
|
+
}
|
|
54
|
+
}));
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
finally {
|
|
58
|
+
this.isFlushing = false;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
class EventBusPublisher {
|
|
63
|
+
constructor(core) {
|
|
64
|
+
this.core = core;
|
|
65
|
+
}
|
|
66
|
+
publish(message) {
|
|
67
|
+
this.core.enqueue(message);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
exports.EventBusPublisher = EventBusPublisher;
|
|
71
|
+
class EventBusSubscriber {
|
|
72
|
+
constructor(core) {
|
|
73
|
+
this.core = core;
|
|
74
|
+
}
|
|
75
|
+
subscribe(handler) {
|
|
76
|
+
this.core.subscribe(handler);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
exports.EventBusSubscriber = EventBusSubscriber;
|
|
80
|
+
function createSingleChannelEventBus(concurrency = 1) {
|
|
81
|
+
const core = new SingleChannelEventBusCore(concurrency);
|
|
82
|
+
return {
|
|
83
|
+
publisher: new EventBusPublisher(core),
|
|
84
|
+
subscriber: new EventBusSubscriber(core),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
exports.createSingleChannelEventBus = createSingleChannelEventBus;
|
|
88
|
+
//# sourceMappingURL=event-bus.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event-bus.js","sourceRoot":"","sources":["../../src/async/event-bus.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,sDAA6B;AAQ7B,MAAM,yBAAyB;IAM7B,YAAY,WAAW,GAAG,CAAC;QALpB,eAAU,GAA8C,IAAI,CAAC;QAC7D,WAAM,GAAQ,EAAE,CAAC;QAEhB,eAAU,GAAG,KAAK,CAAC;QAGzB,IAAI,CAAC,KAAK,GAAG,IAAI,iBAAM,CAAC,EAAE,WAAW,EAAE,CAAC,CAAC;IAC3C,CAAC;IAED,OAAO,CAAC,OAAU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AArB,SAAI,GAAJ,IAAI,CAAiB;IAAG,CAAC;IAE7C,SAAS,CAAC,OAAyC;QACjD,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;IAC/B,CAAC;CACF;AAND,gDAMC;AAED,SAAgB,2BAA2B,CAAc,WAAW,GAAG,CAAC;IACtE,MAAM,IAAI,GAAG,IAAI,yBAAyB,CAAI,WAAW,CAAC,CAAC;IAC3D,OAAO;QACL,SAAS,EAAE,IAAI,iBAAiB,CAAI,IAAI,CAAC;QACzC,UAAU,EAAE,IAAI,kBAAkB,CAAI,IAAI,CAAC;KAC5C,CAAC;AACJ,CAAC;AAND,kEAMC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./event-bus";
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./event-bus"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/async/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,8CAA4B"}
|
package/build/index.d.ts
CHANGED
package/build/index.js
CHANGED
|
@@ -26,5 +26,6 @@ __exportStar(require("./sleep"), exports);
|
|
|
26
26
|
__exportStar(require("./mnemonic"), exports);
|
|
27
27
|
__exportStar(require("./coin"), exports);
|
|
28
28
|
__exportStar(require("./service-worker"), exports);
|
|
29
|
+
__exportStar(require("./async"), exports);
|
|
29
30
|
__exportStar(require("./bigint"), exports);
|
|
30
31
|
//# sourceMappingURL=index.js.map
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6CAA2B;AAC3B,0CAAwB;AACxB,yCAAuB;AACvB,0CAAwB;AACxB,2CAAyB;AACzB,yCAAuB;AACvB,yCAAuB;AACvB,0CAAwB;AACxB,0CAAwB;AACxB,6CAA2B;AAC3B,yCAAuB;AACvB,mDAAiC;AACjC,2CAAyB"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6CAA2B;AAC3B,0CAAwB;AACxB,yCAAuB;AACvB,0CAAwB;AACxB,2CAAyB;AACzB,yCAAuB;AACvB,yCAAuB;AACvB,0CAAwB;AACxB,0CAAwB;AACxB,6CAA2B;AAC3B,yCAAuB;AACvB,mDAAiC;AACjC,0CAAwB;AACxB,2CAAyB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@keplr-wallet/common",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.1",
|
|
4
4
|
"main": "build/index.js",
|
|
5
5
|
"author": "chainapsis",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -20,10 +20,11 @@
|
|
|
20
20
|
"lint-fix": "npx eslint --fix \"src/**/*\" && npx prettier --write \"src/**/*\""
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@keplr-wallet/crypto": "0.
|
|
24
|
-
"@keplr-wallet/types": "0.
|
|
23
|
+
"@keplr-wallet/crypto": "0.13.1",
|
|
24
|
+
"@keplr-wallet/types": "0.13.1",
|
|
25
25
|
"buffer": "^6.0.3",
|
|
26
|
-
"delay": "^4.4.0"
|
|
26
|
+
"delay": "^4.4.0",
|
|
27
|
+
"p-queue": "^6.6.2"
|
|
27
28
|
},
|
|
28
|
-
"gitHead": "
|
|
29
|
+
"gitHead": "005bf5131f8612e3a773b864b673361ca9d9ab85"
|
|
29
30
|
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import PQueue from "p-queue";
|
|
2
|
+
|
|
3
|
+
export interface EventBusCore<T = unknown> {
|
|
4
|
+
enqueue(message: T): void;
|
|
5
|
+
subscribe(handler: (msg: T) => Promise<void> | void): void;
|
|
6
|
+
flush(): void;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
class SingleChannelEventBusCore<T = unknown> implements EventBusCore<T> {
|
|
10
|
+
public subscriber: ((msg: T) => Promise<void> | void) | null = null;
|
|
11
|
+
public buffer: T[] = [];
|
|
12
|
+
public queue: PQueue;
|
|
13
|
+
private isFlushing = false;
|
|
14
|
+
|
|
15
|
+
constructor(concurrency = 1) {
|
|
16
|
+
this.queue = new PQueue({ concurrency });
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
enqueue(message: T) {
|
|
20
|
+
this.buffer.push(message);
|
|
21
|
+
this.flush();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
subscribe(handler: (msg: T) => Promise<void> | void) {
|
|
25
|
+
this.subscriber = handler;
|
|
26
|
+
this.flush();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
flush() {
|
|
30
|
+
if (this.isFlushing) return;
|
|
31
|
+
const subscriber = this.subscriber;
|
|
32
|
+
if (!subscriber) return;
|
|
33
|
+
if (this.buffer.length === 0) return;
|
|
34
|
+
|
|
35
|
+
this.isFlushing = true;
|
|
36
|
+
|
|
37
|
+
try {
|
|
38
|
+
while (this.buffer.length > 0) {
|
|
39
|
+
const msg = this.buffer.shift();
|
|
40
|
+
if (msg === undefined) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
this.queue.add(async () => {
|
|
44
|
+
try {
|
|
45
|
+
await subscriber(msg);
|
|
46
|
+
} catch (e) {
|
|
47
|
+
console.error("error:", e);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
} finally {
|
|
52
|
+
this.isFlushing = false;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export class EventBusPublisher<T = unknown> {
|
|
58
|
+
constructor(private core: EventBusCore<T>) {}
|
|
59
|
+
|
|
60
|
+
publish(message: T) {
|
|
61
|
+
this.core.enqueue(message);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export class EventBusSubscriber<T = unknown> {
|
|
66
|
+
constructor(private core: EventBusCore<T>) {}
|
|
67
|
+
|
|
68
|
+
subscribe(handler: (msg: T) => Promise<void> | void) {
|
|
69
|
+
this.core.subscribe(handler);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function createSingleChannelEventBus<T = unknown>(concurrency = 1) {
|
|
74
|
+
const core = new SingleChannelEventBusCore<T>(concurrency);
|
|
75
|
+
return {
|
|
76
|
+
publisher: new EventBusPublisher<T>(core),
|
|
77
|
+
subscriber: new EventBusSubscriber<T>(core),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./event-bus";
|