@neovici/cosmoz-utils 3.26.0 → 3.27.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.
Files changed (2) hide show
  1. package/lib/promise.js +40 -17
  2. package/package.json +1 -1
package/lib/promise.js CHANGED
@@ -1,19 +1,22 @@
1
- export const
2
- timeout$ = ms => new Promise(res => setTimeout(res, ms)),
3
- event$ = (target, type, predicate = () => true, timeout = 300000) => new Promise((resolve, reject) => {
4
- let handler;
5
- const tid = setTimeout(() => {
6
- target.removeEventListener(type, handler);
7
- reject(new Error('Timeout out'));
8
- }, timeout);
9
- target.addEventListener(type, handler = e => {
10
- if (predicate(e)) {
1
+ export const timeout$ = ms => new Promise(res => setTimeout(res, ms)),
2
+ event$ = (target, type, predicate = () => true, timeout = 300000) =>
3
+ new Promise((resolve, reject) => {
4
+ let handler;
5
+ const tid = setTimeout(() => {
11
6
  target.removeEventListener(type, handler);
12
- clearTimeout(tid);
13
- resolve(e);
14
- }
15
- });
16
- }),
7
+ reject(new Error('Timeout out'));
8
+ }, timeout);
9
+ target.addEventListener(
10
+ type,
11
+ handler = e => {
12
+ if (predicate(e)) {
13
+ target.removeEventListener(type, handler);
14
+ clearTimeout(tid);
15
+ resolve(e);
16
+ }
17
+ }
18
+ );
19
+ }),
17
20
  limit$ = (fn, limit) => {
18
21
  const state = {
19
22
  queue: [],
@@ -30,7 +33,7 @@ export const
30
33
 
31
34
  const task = state.queue.shift();
32
35
  state.pending.push(task);
33
- fn(...task.args)
36
+ Promise.resolve(fn(...task.args))
34
37
  .then(task.resolve, task.reject)
35
38
  .then(() => {
36
39
  state.pending.splice(state.pending.indexOf(task), 1);
@@ -43,5 +46,25 @@ export const
43
46
  state.queue.push({ args, resolve, reject });
44
47
  process();
45
48
  });
49
+ },
50
+ debounce$ = (fn, ms) => {
51
+ let timeoutId;
52
+ const pending = [];
53
+ return (...args) =>
54
+ new Promise((res, rej) => {
55
+ clearTimeout(timeoutId);
56
+ timeoutId = setTimeout(() => {
57
+ const currentPending = [...pending];
58
+ pending.length = 0;
59
+ Promise.resolve(fn(...args)).then(
60
+ data => {
61
+ currentPending.forEach(({ resolve }) => resolve(data));
62
+ },
63
+ error => {
64
+ currentPending.forEach(({ reject }) => reject(error));
65
+ }
66
+ );
67
+ }, ms);
68
+ pending.push({ resolve: res, reject: rej });
69
+ });
46
70
  };
47
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neovici/cosmoz-utils",
3
- "version": "3.26.0",
3
+ "version": "3.27.0",
4
4
  "description": "Date, money and template management functions commonly needed in Cosmoz views.",
5
5
  "keywords": [
6
6
  "polymer",