@neovici/cosmoz-utils 3.25.0 → 3.28.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/lib/haunted-polymer.js +1 -1
- package/lib/promise.js +87 -15
- package/package.json +1 -1
package/lib/haunted-polymer.js
CHANGED
|
@@ -87,7 +87,7 @@ export const hauntedPolymer = (outputPath, hook) => base => {
|
|
|
87
87
|
// render in the next animation frame to allow the haunted scheduler to finish processing the current state update
|
|
88
88
|
// otherwise hooks might be updated twice
|
|
89
89
|
cancelAnimationFrame(outlet.__renderAF);
|
|
90
|
-
outlet.__renderAF = requestAnimationFrame(() => render(part, outlet));
|
|
90
|
+
outlet.__renderAF = requestAnimationFrame(() => render(typeof part === 'function' ? part() : part, outlet));
|
|
91
91
|
}
|
|
92
92
|
};
|
|
93
93
|
};
|
package/lib/promise.js
CHANGED
|
@@ -1,16 +1,88 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
class ManagedPromise extends Promise {
|
|
2
|
+
constructor(callback) {
|
|
3
|
+
const handles = {};
|
|
4
|
+
super((resolve, reject) => Object.assign(handles, { resolve, reject }));
|
|
5
|
+
Object.assign(this, handles);
|
|
6
|
+
callback?.(handles.resolve, handles.reject);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
const timeout$ = ms => new Promise(res => setTimeout(res, ms)),
|
|
12
|
+
event$ = (target, type, predicate = () => true, timeout = 300000) =>
|
|
13
|
+
new Promise((resolve, reject) => {
|
|
14
|
+
let handler;
|
|
15
|
+
const tid = setTimeout(() => {
|
|
11
16
|
target.removeEventListener(type, handler);
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
+
reject(new Error('Timeout out'));
|
|
18
|
+
}, timeout);
|
|
19
|
+
target.addEventListener(
|
|
20
|
+
type,
|
|
21
|
+
handler = e => {
|
|
22
|
+
if (predicate(e)) {
|
|
23
|
+
target.removeEventListener(type, handler);
|
|
24
|
+
clearTimeout(tid);
|
|
25
|
+
resolve(e);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
);
|
|
29
|
+
}),
|
|
30
|
+
limit$ = (fn, limit) => {
|
|
31
|
+
const state = {
|
|
32
|
+
queue: [],
|
|
33
|
+
pending: []
|
|
34
|
+
},
|
|
35
|
+
process = () => {
|
|
36
|
+
if (state.queue.length === 0) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (state.pending.length >= limit) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const task = state.queue.shift();
|
|
45
|
+
state.pending.push(task);
|
|
46
|
+
Promise.resolve(fn(...task.args))
|
|
47
|
+
.then(task.resolve, task.reject)
|
|
48
|
+
.then(() => {
|
|
49
|
+
state.pending.splice(state.pending.indexOf(task), 1);
|
|
50
|
+
process();
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
return (...args) =>
|
|
55
|
+
new Promise((resolve, reject) => {
|
|
56
|
+
state.queue.push({ args, resolve, reject });
|
|
57
|
+
process();
|
|
58
|
+
});
|
|
59
|
+
},
|
|
60
|
+
debounce$ = (fn, ms) => {
|
|
61
|
+
let timeoutId;
|
|
62
|
+
const pending = [];
|
|
63
|
+
return (...args) =>
|
|
64
|
+
new Promise((res, rej) => {
|
|
65
|
+
clearTimeout(timeoutId);
|
|
66
|
+
timeoutId = setTimeout(() => {
|
|
67
|
+
const currentPending = [...pending];
|
|
68
|
+
pending.length = 0;
|
|
69
|
+
Promise.resolve(fn(...args)).then(
|
|
70
|
+
data => {
|
|
71
|
+
currentPending.forEach(({ resolve }) => resolve(data));
|
|
72
|
+
},
|
|
73
|
+
error => {
|
|
74
|
+
currentPending.forEach(({ reject }) => reject(error));
|
|
75
|
+
}
|
|
76
|
+
);
|
|
77
|
+
}, ms);
|
|
78
|
+
pending.push({ resolve: res, reject: rej });
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export {
|
|
83
|
+
ManagedPromise,
|
|
84
|
+
timeout$,
|
|
85
|
+
event$,
|
|
86
|
+
limit$,
|
|
87
|
+
debounce$
|
|
88
|
+
};
|