cbvirtua 1.0.23 → 1.0.24
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/package.json +1 -1
- package/queue.js +124 -0
- package/vue-i18n-dev.zip +0 -0
package/package.json
CHANGED
package/queue.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
function promiseWithResolvers() {
|
|
2
|
+
let resolve;
|
|
3
|
+
let reject;
|
|
4
|
+
const promise = new Promise(
|
|
5
|
+
(res, rej) => {
|
|
6
|
+
// Executed synchronously!
|
|
7
|
+
resolve = res;
|
|
8
|
+
reject = rej;
|
|
9
|
+
});
|
|
10
|
+
return { promise, resolve, reject };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
class PromiseQueue {
|
|
14
|
+
constructor() {
|
|
15
|
+
const { promise, resolve } = Promise.withResolvers();
|
|
16
|
+
this.frontPromise = promise;
|
|
17
|
+
this.backResolve = resolve;
|
|
18
|
+
this.i = 0
|
|
19
|
+
}
|
|
20
|
+
put() {
|
|
21
|
+
const { resolve, promise } = Promise.withResolvers();
|
|
22
|
+
// By resolving, we add another (pending) element
|
|
23
|
+
// to the end of the queue
|
|
24
|
+
this.i++
|
|
25
|
+
const value = this.i
|
|
26
|
+
this.backResolve({ value, promise });
|
|
27
|
+
this.backResolve = resolve;
|
|
28
|
+
}
|
|
29
|
+
get() {
|
|
30
|
+
return this.frontPromise.then(
|
|
31
|
+
(next) => {
|
|
32
|
+
this.frontPromise = next.promise;
|
|
33
|
+
return next.value;
|
|
34
|
+
}
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
let test
|
|
41
|
+
async function aa() {
|
|
42
|
+
const queue = new PromiseQueue();
|
|
43
|
+
setTimeout(() => {
|
|
44
|
+
queue.put()
|
|
45
|
+
}, 1000)
|
|
46
|
+
let i = await queue.get()
|
|
47
|
+
console.log('i1', i)
|
|
48
|
+
test = queue
|
|
49
|
+
let i2 = await queue.get()
|
|
50
|
+
console.log('i2', i2)
|
|
51
|
+
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
aa()
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
setTimeout(() => {
|
|
58
|
+
test.put()
|
|
59
|
+
}, 3000)
|
|
60
|
+
|
|
61
|
+
class AsyncIterQueue {
|
|
62
|
+
#frontPromise;
|
|
63
|
+
#backResolve;
|
|
64
|
+
constructor() {
|
|
65
|
+
const { promise, resolve } = Promise.withResolvers();
|
|
66
|
+
this.#frontPromise = promise;
|
|
67
|
+
this.#backResolve = resolve;
|
|
68
|
+
}
|
|
69
|
+
put(value) {
|
|
70
|
+
if (this.#backResolve === null) {
|
|
71
|
+
throw new Error('Queue is closed');
|
|
72
|
+
}
|
|
73
|
+
const { resolve, promise } = Promise.withResolvers();
|
|
74
|
+
this.#backResolve({ done: false, value, promise });
|
|
75
|
+
this.#backResolve = resolve;
|
|
76
|
+
}
|
|
77
|
+
close() {
|
|
78
|
+
this.#backResolve(
|
|
79
|
+
{ done: true, value: undefined, promise: null }
|
|
80
|
+
);
|
|
81
|
+
this.#backResolve = null;
|
|
82
|
+
}
|
|
83
|
+
next() {
|
|
84
|
+
if (this.#frontPromise === null) {
|
|
85
|
+
return Promise.resolve({ done: true });
|
|
86
|
+
}
|
|
87
|
+
return this.#frontPromise.then(
|
|
88
|
+
(next) => {
|
|
89
|
+
this.#frontPromise = next.promise;
|
|
90
|
+
return { value: next.value, done: next.done };
|
|
91
|
+
}
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
[Symbol.asyncIterator]() {
|
|
95
|
+
return this;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
{ // Putting before async iteration
|
|
100
|
+
const queue = new AsyncIterQueue();
|
|
101
|
+
queue.put('one');
|
|
102
|
+
queue.put('two');
|
|
103
|
+
queue.close();
|
|
104
|
+
assert.deepEqual(
|
|
105
|
+
await Array.fromAsync(queue),
|
|
106
|
+
['one', 'two']
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
{ // Async iteration before putting
|
|
110
|
+
const queue = new AsyncIterQueue();
|
|
111
|
+
setTimeout(
|
|
112
|
+
// Runs after `await` pauses the current execution context
|
|
113
|
+
() => {
|
|
114
|
+
queue.put('one');
|
|
115
|
+
queue.put('two');
|
|
116
|
+
queue.close();
|
|
117
|
+
},
|
|
118
|
+
0
|
|
119
|
+
);
|
|
120
|
+
assert.deepEqual(
|
|
121
|
+
await Array.fromAsync(queue),
|
|
122
|
+
['one', 'two']
|
|
123
|
+
);
|
|
124
|
+
}
|
package/vue-i18n-dev.zip
DELETED
|
Binary file
|