@neovici/cosmoz-utils 4.0.0-beta.2 → 5.1.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/dist/array.d.ts +1 -0
- package/dist/array.js +24 -0
- package/dist/date.d.ts +39 -0
- package/dist/date.js +147 -0
- package/dist/directives/lazy-until.d.ts +43 -0
- package/dist/directives/lazy-until.js +142 -0
- package/dist/directives/measure.d.ts +12 -0
- package/dist/directives/measure.js +24 -0
- package/dist/directives/portal.d.ts +15 -0
- package/dist/directives/portal.js +58 -0
- package/dist/directives/spread-props.d.ts +9 -0
- package/dist/directives/spread-props.js +22 -0
- package/dist/elements/cz-icon.d.ts +8 -0
- package/dist/elements/cz-icon.js +15 -0
- package/dist/elements/cz-spinner.d.ts +1 -0
- package/dist/elements/cz-spinner.js +22 -0
- package/dist/elements/index.d.ts +2 -0
- package/dist/elements/index.js +2 -0
- package/dist/function.d.ts +5 -0
- package/dist/function.js +4 -0
- package/dist/haunted-polymer.d.ts +341 -0
- package/dist/haunted-polymer.js +83 -0
- package/dist/hooks/use-debounce-raf.d.ts +16 -0
- package/dist/hooks/use-debounce-raf.js +36 -0
- package/dist/hooks/use-dropped-files.d.ts +1 -0
- package/dist/hooks/use-dropped-files.js +7 -0
- package/dist/hooks/use-handle-drop.d.ts +1 -0
- package/dist/hooks/use-handle-drop.js +18 -0
- package/dist/hooks/use-host-bounds.d.ts +2 -0
- package/dist/hooks/use-host-bounds.js +12 -0
- package/dist/hooks/use-host.d.ts +3 -0
- package/dist/hooks/use-host.js +6 -0
- package/dist/hooks/use-imperative-api.d.ts +1 -0
- package/dist/hooks/use-imperative-api.js +18 -0
- package/dist/hooks/use-meta.d.ts +8 -0
- package/{lib → dist}/hooks/use-meta.js +3 -5
- package/dist/hooks/use-notify-property.d.ts +2 -0
- package/dist/hooks/use-notify-property.js +19 -0
- package/dist/hooks/use-promise.d.ts +1 -0
- package/dist/hooks/use-promise.js +42 -0
- package/dist/index.d.ts +26 -0
- package/{index.js → dist/index.js} +14 -24
- package/dist/money.d.ts +47 -0
- package/dist/money.js +86 -0
- package/dist/object.d.ts +21 -0
- package/dist/object.js +56 -0
- package/dist/promise.d.ts +10 -0
- package/dist/promise.js +66 -0
- package/dist/reduce/action.d.ts +13 -0
- package/dist/reduce/action.js +10 -0
- package/{lib/reduce/index.js → dist/reduce/index.d.ts} +0 -0
- package/dist/reduce/index.js +2 -0
- package/dist/reduce/reduce.d.ts +1 -0
- package/dist/reduce/reduce.js +12 -0
- package/dist/tag.d.ts +23 -0
- package/dist/tag.js +28 -0
- package/dist/tagged.d.ts +1 -0
- package/{lib → dist}/tagged.js +0 -0
- package/dist/template.d.ts +47 -0
- package/dist/template.js +66 -0
- package/package.json +20 -6
- package/lib/array.js +0 -27
- package/lib/date.js +0 -176
- package/lib/directives/lazy-until.js +0 -163
- package/lib/directives/measure.js +0 -32
- package/lib/directives/portal.js +0 -65
- package/lib/directives/spread-props.js +0 -31
- package/lib/function.js +0 -21
- package/lib/haunted-polymer.js +0 -93
- package/lib/hooks/use-debounce-raf.js +0 -49
- package/lib/hooks/use-dropped-files.js +0 -12
- package/lib/hooks/use-handle-drop.js +0 -22
- package/lib/hooks/use-host-bounds.js +0 -21
- package/lib/hooks/use-host.js +0 -9
- package/lib/hooks/use-imperative-api.js +0 -22
- package/lib/hooks/use-notify-property.js +0 -26
- package/lib/hooks/use-promise.js +0 -74
- package/lib/money.js +0 -105
- package/lib/object.js +0 -26
- package/lib/promise.js +0 -88
- package/lib/reduce/action.js +0 -16
- package/lib/reduce/reduce.js +0 -16
- package/lib/tag.js +0 -34
- package/lib/template.js +0 -89
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
useState,
|
|
3
|
-
useEffect
|
|
4
|
-
} from 'haunted';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Debounces a value by updating it via a double requestAnimationFrame call.
|
|
8
|
-
*
|
|
9
|
-
* Double RAF is useful for ensuring that animations start before expensive rendering is done.
|
|
10
|
-
* It helps provide smoother user experience by making animations feel reactive.
|
|
11
|
-
* Normal rendering would block the animation from starting.
|
|
12
|
-
*
|
|
13
|
-
* In the context of haunted, the double RAF call makes sure the value is updated *after* all
|
|
14
|
-
* hooks have been processed and the rendering has been done. Effectively all rapid, intermediary
|
|
15
|
-
* state changes will be skipped.
|
|
16
|
-
*
|
|
17
|
-
* @template T
|
|
18
|
-
* @param {T} value the value to debounce
|
|
19
|
-
* @returns {T} the debounced value
|
|
20
|
-
*/
|
|
21
|
-
export const useDebounceRaf = value => {
|
|
22
|
-
// State and setters for debounced value
|
|
23
|
-
const [debouncedValue, setDebouncedValue] = useState(value);
|
|
24
|
-
|
|
25
|
-
useEffect(
|
|
26
|
-
() => {
|
|
27
|
-
/**
|
|
28
|
-
* @type {?number}
|
|
29
|
-
*/
|
|
30
|
-
let double;
|
|
31
|
-
|
|
32
|
-
// Update debounced value after double RAF
|
|
33
|
-
const handler = requestAnimationFrame(() => {
|
|
34
|
-
double = requestAnimationFrame(() => setDebouncedValue(value));
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
// Cancel the animation frames if value changes (also on unmount)
|
|
38
|
-
// This is how we prevent debounced value from updating if value is changed.
|
|
39
|
-
// RAF gets cleared and restarted.
|
|
40
|
-
return () => {
|
|
41
|
-
cancelAnimationFrame(handler);
|
|
42
|
-
cancelAnimationFrame(double);
|
|
43
|
-
};
|
|
44
|
-
},
|
|
45
|
-
[value] // Only re-call effect if value changes
|
|
46
|
-
);
|
|
47
|
-
|
|
48
|
-
return debouncedValue;
|
|
49
|
-
};
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
useState,
|
|
3
|
-
useCallback
|
|
4
|
-
} from 'haunted';
|
|
5
|
-
import { useHandleDrop } from './use-handle-drop';
|
|
6
|
-
|
|
7
|
-
export const useDroppedFiles = el => {
|
|
8
|
-
const [files, setFiles] = useState([]),
|
|
9
|
-
handleDrop = useCallback(event => setFiles(event.dataTransfer.files), [setFiles]);
|
|
10
|
-
useHandleDrop(el, handleDrop);
|
|
11
|
-
return files;
|
|
12
|
-
};
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { useEffect } from 'haunted';
|
|
2
|
-
|
|
3
|
-
export const useHandleDrop = (el, callback) =>
|
|
4
|
-
useEffect(() => {
|
|
5
|
-
const dragStop = event => {
|
|
6
|
-
event.stopPropagation();
|
|
7
|
-
event.preventDefault();
|
|
8
|
-
},
|
|
9
|
-
handleDrop = event => {
|
|
10
|
-
dragStop(event);
|
|
11
|
-
callback(event);
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
el.addEventListener('dragenter', dragStop);
|
|
15
|
-
el.addEventListener('dragover', dragStop);
|
|
16
|
-
el.addEventListener('drop', handleDrop);
|
|
17
|
-
return () => {
|
|
18
|
-
el.removeEventListener('dragenter', dragStop);
|
|
19
|
-
el.removeEventListener('dragover', dragStop);
|
|
20
|
-
el.removeEventListener('drop', handleDrop);
|
|
21
|
-
};
|
|
22
|
-
}, [el, callback]);
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
useLayoutEffect,
|
|
3
|
-
useState
|
|
4
|
-
} from 'haunted';
|
|
5
|
-
|
|
6
|
-
import { useHost } from './use-host';
|
|
7
|
-
|
|
8
|
-
const useHostBounds = () => {
|
|
9
|
-
const host = useHost(),
|
|
10
|
-
[bounds, setBounds] = useState();
|
|
11
|
-
|
|
12
|
-
useLayoutEffect(() => {
|
|
13
|
-
const observer = new ResizeObserver(entries => requestAnimationFrame(() => setBounds(entries[0]?.contentRect)));
|
|
14
|
-
observer.observe(host);
|
|
15
|
-
return () => observer.unobserve(host);
|
|
16
|
-
}, []);
|
|
17
|
-
|
|
18
|
-
return bounds;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export { useHostBounds };
|
package/lib/hooks/use-host.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Hook, hook
|
|
3
|
-
} from 'haunted';
|
|
4
|
-
|
|
5
|
-
export const useImperativeApi = hook(class extends Hook {
|
|
6
|
-
constructor(id, state, api, values) {
|
|
7
|
-
super(id, state);
|
|
8
|
-
Object.assign(state.host, api);
|
|
9
|
-
this.values = values;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
update(api, values) {
|
|
13
|
-
if (this.hasChanged(values)) {
|
|
14
|
-
this.values = values;
|
|
15
|
-
Object.assign(this.state.host, api);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
hasChanged(values = []) {
|
|
20
|
-
return values.some((value, i) => this.values[i] !== value);
|
|
21
|
-
}
|
|
22
|
-
});
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { useEffect } from 'haunted';
|
|
2
|
-
import { useHost } from './use-host';
|
|
3
|
-
|
|
4
|
-
const UPPER = /([A-Z])/gu,
|
|
5
|
-
|
|
6
|
-
/* Emulate polymer notify props */
|
|
7
|
-
notifyProperty = (host, name, value) => {
|
|
8
|
-
// this is required to make polymer double-binding recognize the change
|
|
9
|
-
// @see https://github.com/Polymer/polymer/blob/76c71e186ecc605294c3575dd31ac7983a8b3ae3/lib/mixins/property-effects.js#L382
|
|
10
|
-
host[name] = value;
|
|
11
|
-
|
|
12
|
-
// emulate polymer notify event
|
|
13
|
-
host.dispatchEvent(new CustomEvent(name.replace(UPPER, '-$1').toLowerCase() + '-changed', { detail: { value }}));
|
|
14
|
-
},
|
|
15
|
-
|
|
16
|
-
useNotifyProperty = (name, value) => {
|
|
17
|
-
const host = useHost();
|
|
18
|
-
useEffect(() => {
|
|
19
|
-
notifyProperty(host, name, value);
|
|
20
|
-
}, [value]);
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export {
|
|
24
|
-
notifyProperty,
|
|
25
|
-
useNotifyProperty
|
|
26
|
-
};
|
package/lib/hooks/use-promise.js
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
useReducer,
|
|
3
|
-
useEffect
|
|
4
|
-
} from 'haunted';
|
|
5
|
-
import {
|
|
6
|
-
reduce,
|
|
7
|
-
action
|
|
8
|
-
} from '../reduce';
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
states = {
|
|
12
|
-
pending: 'pending',
|
|
13
|
-
rejected: 'rejected',
|
|
14
|
-
resolved: 'resolved'
|
|
15
|
-
},
|
|
16
|
-
initial = {
|
|
17
|
-
error: undefined,
|
|
18
|
-
result: undefined,
|
|
19
|
-
state: states.pending
|
|
20
|
-
},
|
|
21
|
-
pending = action(states.pending),
|
|
22
|
-
resolved = action(states.resolved, result => ({ result })),
|
|
23
|
-
rejected = action(states.rejected, error => ({ error })),
|
|
24
|
-
reducer = reduce(initial, [
|
|
25
|
-
[pending, () => ({
|
|
26
|
-
error: undefined,
|
|
27
|
-
result: undefined,
|
|
28
|
-
state: states.pending
|
|
29
|
-
})],
|
|
30
|
-
|
|
31
|
-
[resolved, (state, { result }) => ({
|
|
32
|
-
error: undefined,
|
|
33
|
-
result,
|
|
34
|
-
state: states.resolved
|
|
35
|
-
})],
|
|
36
|
-
|
|
37
|
-
[rejected, (state, { error }) => ({
|
|
38
|
-
error,
|
|
39
|
-
result: undefined,
|
|
40
|
-
state: states.rejected
|
|
41
|
-
})]
|
|
42
|
-
]);
|
|
43
|
-
|
|
44
|
-
export const usePromise = promise => {
|
|
45
|
-
const [
|
|
46
|
-
{
|
|
47
|
-
error,
|
|
48
|
-
result,
|
|
49
|
-
state
|
|
50
|
-
},
|
|
51
|
-
dispatch
|
|
52
|
-
] = useReducer(reducer, initial);
|
|
53
|
-
|
|
54
|
-
useEffect(() => {
|
|
55
|
-
if (!promise) {
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
let canceled = false;
|
|
60
|
-
|
|
61
|
-
dispatch(pending());
|
|
62
|
-
|
|
63
|
-
promise.then(
|
|
64
|
-
result => !canceled && dispatch(resolved(result)),
|
|
65
|
-
error => !canceled && dispatch(rejected(error))
|
|
66
|
-
);
|
|
67
|
-
|
|
68
|
-
return () => {
|
|
69
|
-
canceled = true;
|
|
70
|
-
};
|
|
71
|
-
}, [promise]);
|
|
72
|
-
|
|
73
|
-
return [result, error, state];
|
|
74
|
-
};
|
package/lib/money.js
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
const CURRENCY_FORMATTERS = {},
|
|
2
|
-
NUMBER_FORMATTERS = {},
|
|
3
|
-
_getCurrencyFormatter = function (currency, locale, currencyDisplay = 'code') {
|
|
4
|
-
if (currency == null) {
|
|
5
|
-
return;
|
|
6
|
-
}
|
|
7
|
-
const key = currency.toUpperCase() + (locale || '');
|
|
8
|
-
if (CURRENCY_FORMATTERS[key] == null) {
|
|
9
|
-
CURRENCY_FORMATTERS[key] = new Intl.NumberFormat(locale, {
|
|
10
|
-
style: 'currency',
|
|
11
|
-
currency,
|
|
12
|
-
currencyDisplay
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
return CURRENCY_FORMATTERS[key];
|
|
16
|
-
},
|
|
17
|
-
/**
|
|
18
|
-
* Determine if a constant or a variable is an amount.
|
|
19
|
-
* @param {object} potentialAmount Potential amount.
|
|
20
|
-
* @returns {boolean} Whether the potential amount is a valid amount object with amount and currency.
|
|
21
|
-
*/
|
|
22
|
-
isAmount = function (potentialAmount) {
|
|
23
|
-
return (
|
|
24
|
-
potentialAmount != null &&
|
|
25
|
-
potentialAmount.amount != null &&
|
|
26
|
-
potentialAmount.currency != null &&
|
|
27
|
-
typeof potentialAmount.amount === 'number' &&
|
|
28
|
-
typeof potentialAmount.currency === 'string' &&
|
|
29
|
-
potentialAmount.currency.length === 3
|
|
30
|
-
);
|
|
31
|
-
},
|
|
32
|
-
/**
|
|
33
|
-
* Render an amount with decimal separator and currency symbol.
|
|
34
|
-
* @param {object} money Money with amount property and optionally currency property.
|
|
35
|
-
* @param {void|string} locale Locale to format the amount in.
|
|
36
|
-
* @return {string} Formatted amount.
|
|
37
|
-
*/
|
|
38
|
-
renderAmount = function (money, locale) {
|
|
39
|
-
if (money?.amount == null) {
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
const formatter = _getCurrencyFormatter(money.currency, locale);
|
|
43
|
-
if (formatter == null) {
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
return formatter.format(money.amount);
|
|
47
|
-
},
|
|
48
|
-
/**
|
|
49
|
-
* Alias for renderAmount(money). Render an amount with decimal separator and currency symbol.
|
|
50
|
-
* @param {object} money Money with amount property and optionally currency property.
|
|
51
|
-
* @return {string} Formatted amount.
|
|
52
|
-
*/
|
|
53
|
-
renderMoney = renderAmount,
|
|
54
|
-
/**
|
|
55
|
-
* Render an amount with decimal separator but without currency symbol.
|
|
56
|
-
* @param {object} money Money with amount property and optionally currency property.
|
|
57
|
-
* @param {void|string} locale Locale to format the amount in.
|
|
58
|
-
* @return {string} Formatted number.
|
|
59
|
-
*/
|
|
60
|
-
renderNumberAmount = function (money, locale) {
|
|
61
|
-
const key = locale || '0';
|
|
62
|
-
if (NUMBER_FORMATTERS[key] == null) {
|
|
63
|
-
NUMBER_FORMATTERS[key] = new Intl.NumberFormat(locale, {
|
|
64
|
-
minimumFractionDigits: 2,
|
|
65
|
-
maximumFractionDigits: 2
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
return NUMBER_FORMATTERS[key].format(money.amount);
|
|
69
|
-
},
|
|
70
|
-
/**
|
|
71
|
-
* Round a number to a given precision.
|
|
72
|
-
* @param {string} number Number with decimals to round.
|
|
73
|
-
* @param {number} precision Number of decimals to round amount to.
|
|
74
|
-
* @return {number} Rounded number.
|
|
75
|
-
*/
|
|
76
|
-
round = function (number, precision) {
|
|
77
|
-
const fixed = Number(
|
|
78
|
-
Math.round(number + 'e' + precision) + 'e-' + precision
|
|
79
|
-
).toFixed(precision);
|
|
80
|
-
return parseFloat(fixed);
|
|
81
|
-
},
|
|
82
|
-
/**
|
|
83
|
-
* Compare amounts.
|
|
84
|
-
* @param {object} amount1 Amount 1.
|
|
85
|
-
* @param {object} amount2 Amount 2.
|
|
86
|
-
* @param {number} precision Decimal precision, defaults to 2 decimals.
|
|
87
|
-
* @returns {boolean} Whether the amounts are equal regarding amount to the specified precision and the currency.
|
|
88
|
-
*/
|
|
89
|
-
amountEquals = function (amount1, amount2, precision = 2) {
|
|
90
|
-
return (
|
|
91
|
-
isAmount(amount1) &&
|
|
92
|
-
isAmount(amount2) &&
|
|
93
|
-
round(amount1.amount, precision) === round(amount2.amount, precision) &&
|
|
94
|
-
amount1.currency === amount2.currency
|
|
95
|
-
);
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
export {
|
|
99
|
-
isAmount,
|
|
100
|
-
renderAmount,
|
|
101
|
-
renderMoney,
|
|
102
|
-
renderNumberAmount,
|
|
103
|
-
round,
|
|
104
|
-
amountEquals
|
|
105
|
-
};
|
package/lib/object.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { identity } from './function';
|
|
2
|
-
|
|
3
|
-
export const prop = key => key && (object => object[key]) || identity,
|
|
4
|
-
strProp = key => {
|
|
5
|
-
const p = prop(key);
|
|
6
|
-
return o => typeof o === 'string' ? o : p(o)?.toString() || '';
|
|
7
|
-
},
|
|
8
|
-
isObject = obj => Object.prototype.toString.call(obj) === '[object Object]',
|
|
9
|
-
merge = (...objs) => objs.reduce((acc, obj) => {
|
|
10
|
-
if (obj == null) {
|
|
11
|
-
return acc;
|
|
12
|
-
}
|
|
13
|
-
for (const key of Object.keys(obj)) {
|
|
14
|
-
if (isObject(obj[key]) && isObject(acc[key])) {
|
|
15
|
-
Object.assign(acc[key], merge(acc[key], obj[key]));
|
|
16
|
-
} else if (Array.isArray(acc[key]) && Array.isArray(obj[key])) {
|
|
17
|
-
acc[key] = acc[key].concat(obj[key]);
|
|
18
|
-
} else {
|
|
19
|
-
acc[key] = obj[key];
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
return acc;
|
|
23
|
-
}, {}),
|
|
24
|
-
transform = predicate => obj => Object.fromEntries(predicate(Object.entries(obj))),
|
|
25
|
-
props = keys => transform(e => e.filter(([k]) => keys.includes(k))),
|
|
26
|
-
omit = keys => transform(e => e.filter(([k]) => !keys.includes(k)));
|
package/lib/promise.js
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
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(() => {
|
|
16
|
-
target.removeEventListener(type, handler);
|
|
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
|
-
};
|
package/lib/reduce/action.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
const action = (type, create = () => ({})) => {
|
|
2
|
-
const common = {
|
|
3
|
-
type,
|
|
4
|
-
toString() {
|
|
5
|
-
return type;
|
|
6
|
-
}
|
|
7
|
-
},
|
|
8
|
-
callable = (...args) => Object.assign(create(...args), common);
|
|
9
|
-
return Object.assign(callable, common);
|
|
10
|
-
},
|
|
11
|
-
type = action => action.type || action.toString();
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export {
|
|
15
|
-
action, type
|
|
16
|
-
};
|
package/lib/reduce/reduce.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { type } from './action';
|
|
2
|
-
|
|
3
|
-
const ensureArray = x => Array.isArray(x) ? x : [x],
|
|
4
|
-
reduce = (initial, handle) => {
|
|
5
|
-
const handles = ensureArray(handle),
|
|
6
|
-
handlers = (handles.every(Array.isArray) ? handles : [handles]).map(([actions, handle]) => ({
|
|
7
|
-
actions: ensureArray(actions).map(type),
|
|
8
|
-
handle
|
|
9
|
-
}));
|
|
10
|
-
return (state = initial, action) => {
|
|
11
|
-
const handler = handlers.find(h => h.actions.includes(type(action)));
|
|
12
|
-
return handler ? handler.handle(state, action) : state;
|
|
13
|
-
};
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export { reduce };
|
package/lib/tag.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { html as htm } from 'haunted';
|
|
2
|
-
|
|
3
|
-
const html = (arr, ...thru) =>
|
|
4
|
-
htm(
|
|
5
|
-
Object.assign(arr, { raw: true }),
|
|
6
|
-
...thru
|
|
7
|
-
);
|
|
8
|
-
|
|
9
|
-
export const
|
|
10
|
-
/**
|
|
11
|
-
* Dynamic component support for lit-html.
|
|
12
|
-
*
|
|
13
|
-
* Normally lit-html does not support interpolating the component tag, but we can trick it
|
|
14
|
-
* by calling the template function manually.
|
|
15
|
-
*
|
|
16
|
-
* NOTE: You can only use this function for interpolating a single tag and it must be the
|
|
17
|
-
* first interpolated value. If you need multiple components, you can do multiple interpolations.
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* tag`<${name} some=${param}/>`
|
|
21
|
-
*
|
|
22
|
-
* @example
|
|
23
|
-
* html`A: ${ tag`<${ aTag }/>` } - B: ${ tag`${ bTag }` }`
|
|
24
|
-
*
|
|
25
|
-
* @param {string[]} strings The static strings.
|
|
26
|
-
* @param {string} component The interpolated component name.
|
|
27
|
-
* @param {any[]} values The interpolated values.
|
|
28
|
-
*
|
|
29
|
-
* @return {TemplateResult} The lit template result.
|
|
30
|
-
*/
|
|
31
|
-
tag = (strings, component, ...values) => html([
|
|
32
|
-
strings[0] + component + strings[1],
|
|
33
|
-
...strings.slice(2)
|
|
34
|
-
], ...values);
|
package/lib/template.js
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
const abs = Math.abs,
|
|
2
|
-
/**
|
|
3
|
-
* Check if any of the arguments are true.
|
|
4
|
-
* @return {Boolean} Whether any of the function arguments are true or not.
|
|
5
|
-
*/
|
|
6
|
-
anyTrue = function () {
|
|
7
|
-
const argumentArray = Array.prototype.slice.call(arguments);
|
|
8
|
-
return argumentArray.some(arg => !!arg);
|
|
9
|
-
},
|
|
10
|
-
/**
|
|
11
|
-
* Concatenate all arguments to a string.
|
|
12
|
-
* @return {string} Concatenated arguments.
|
|
13
|
-
*/
|
|
14
|
-
concat = function () {
|
|
15
|
-
return Array.prototype.join.call(arguments, '');
|
|
16
|
-
},
|
|
17
|
-
/**
|
|
18
|
-
* If iftrue argument is true, return result argument, otherwise return elseresult argument.
|
|
19
|
-
* @param {boolean} iftrue Codition for result
|
|
20
|
-
* @param {*} result Result when iftrue is true.
|
|
21
|
-
* @param {*} elseresult Result when iftrue is false.
|
|
22
|
-
* @return {boolean} result or elseresult depending on iftrue evaluation.
|
|
23
|
-
*/
|
|
24
|
-
ifElse = function (iftrue, result, elseresult) {
|
|
25
|
-
return iftrue ? result : elseresult;
|
|
26
|
-
},
|
|
27
|
-
/**
|
|
28
|
-
* Check if item argument exists in array argument.
|
|
29
|
-
* @param {*} item Item to search for in the array.
|
|
30
|
-
* @param {array} array Array to search.
|
|
31
|
-
* @return {boolean} Whether the item was found in array or not.
|
|
32
|
-
*/
|
|
33
|
-
inArray = function (item, array) {
|
|
34
|
-
return array.indexOf(item) > -1;
|
|
35
|
-
},
|
|
36
|
-
/**
|
|
37
|
-
* Check equality of the arguments.
|
|
38
|
-
* @param {*} arg1 First argument to compare.
|
|
39
|
-
* @param {*} arg2 Second argument to compare
|
|
40
|
-
* @return {boolean} Whether the first and second arguments are equal or not.
|
|
41
|
-
*/
|
|
42
|
-
isEqual = function (arg1, arg2) {
|
|
43
|
-
return arg1 === arg2;
|
|
44
|
-
},
|
|
45
|
-
/**
|
|
46
|
-
* Check if argument is undefined, null, empty Array list,
|
|
47
|
-
* empty String or 0 number (like length).
|
|
48
|
-
* @param {object} obj Argument to evaluate.
|
|
49
|
-
* @return {boolean} Whether the argument is empty or not.
|
|
50
|
-
*/
|
|
51
|
-
isEmpty = function (obj) {
|
|
52
|
-
if (obj === undefined || obj === null) {
|
|
53
|
-
return true;
|
|
54
|
-
}
|
|
55
|
-
if (obj instanceof Array && obj.length === 0) {
|
|
56
|
-
return true;
|
|
57
|
-
}
|
|
58
|
-
const objType = typeof obj;
|
|
59
|
-
if (objType === 'string' && obj.length === 0) {
|
|
60
|
-
return true;
|
|
61
|
-
}
|
|
62
|
-
if (objType === 'number' && obj === 0) {
|
|
63
|
-
return true;
|
|
64
|
-
}
|
|
65
|
-
return false;
|
|
66
|
-
},
|
|
67
|
-
/**
|
|
68
|
-
* Formats a number using fixed-point notation.
|
|
69
|
-
* @param {number} number Number with decimals to be formatted.
|
|
70
|
-
* @param {number} fixval Number of decimals to use.
|
|
71
|
-
* @return {String} The number converted to string keep only fixval number of decimals, if number empty, returns empty string
|
|
72
|
-
*/
|
|
73
|
-
toFixed = function (number, fixval) {
|
|
74
|
-
if (typeof number !== 'number') {
|
|
75
|
-
return '';
|
|
76
|
-
}
|
|
77
|
-
return number.toFixed(fixval);
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
export {
|
|
81
|
-
abs,
|
|
82
|
-
anyTrue,
|
|
83
|
-
concat,
|
|
84
|
-
ifElse,
|
|
85
|
-
inArray,
|
|
86
|
-
isEqual,
|
|
87
|
-
isEmpty,
|
|
88
|
-
toFixed
|
|
89
|
-
};
|