@tempots/std 0.22.1 → 0.24.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/async-result.d.ts CHANGED
@@ -44,6 +44,16 @@ export type AsyncFailure<E> = {
44
44
  * @public
45
45
  */
46
46
  export type AsyncResult<V, E> = NotAsked | Loading<V> | AsyncSuccess<V> | AsyncFailure<E>;
47
+ /**
48
+ * Represents a settled state in an asynchronous result.
49
+ * @public
50
+ */
51
+ export type Settled<V, E> = AsyncSuccess<V> | AsyncFailure<E>;
52
+ /**
53
+ * Represents a state in an asynchronous result that is not loading.
54
+ * @public
55
+ */
56
+ export type NonLoading<V, E> = NotAsked | Settled<V, E>;
47
57
  /**
48
58
  * A set of utility functions for working with `AsyncResult`.
49
59
  * @public
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tempots/std",
3
- "version": "0.22.1",
3
+ "version": "0.24.0",
4
4
  "priority": 8,
5
5
  "description": "Std library for TypeScript. Natural complement to the Tempo libraries.",
6
6
  "keywords": [
package/timer.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=(n,e)=>{const t=setTimeout(n,e);return()=>clearTimeout(t)},b=(n,e)=>{const t=setInterval(n,e);return()=>clearInterval(t)},F=(n,e,t={})=>{const{noTrailing:o=!1,noLeading:l=!1,debounceMode:i}=t;let a,m=!1,c=0;function f(){a&&clearTimeout(a)}function T(u){const{upcomingOnly:s=!1}=u||{};f(),m=!s}function d(...u){if(m)return;const s=this,v=Date.now()-c;function r(){c=Date.now(),e.apply(s,u)}function A(){a=void 0}!l&&i&&!a&&r(),f(),i===void 0&&v>n?l?(c=Date.now(),o||(a=setTimeout(i?A:r,n))):r():o||(a=setTimeout(i?A:r,i===void 0?n-v:n))}return d.cancel=T,d},g=(n,e,{atBegin:t=!1}={})=>F(n,e,{debounceMode:t!==!1}),y=n=>{let e=null;const t=l=>{e=null,n(l)};return(()=>{e==null&&(e=requestAnimationFrame(t))})(),()=>{e!=null&&(cancelAnimationFrame(e),e=null)}},I=n=>{let e=null;const t=l=>{e=requestAnimationFrame(t),n(l)};return(()=>{e==null&&(e=requestAnimationFrame(t))})(),()=>{e!=null&&(cancelAnimationFrame(e),e=null)}};exports.debounce=g;exports.delayed=p;exports.delayedAnimationFrame=y;exports.interval=b;exports.intervalAnimationFrame=I;exports.throttle=F;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=(n,e)=>{const t=setTimeout(n,e);return()=>clearTimeout(t)},b=(n,e)=>{const t=setInterval(n,e);return()=>clearInterval(t)};function F(n,e,t={}){const{noTrailing:o=!1,noLeading:l=!1,debounceMode:i}=t;let a,f=!1,c=0;function m(){a&&clearTimeout(a)}function T(u){const{upcomingOnly:s=!1}=u||{};m(),f=!s}function d(...u){if(f)return;const s=this,v=Date.now()-c;function r(){c=Date.now(),e.apply(s,u)}function A(){a=void 0}!l&&i&&!a&&r(),m(),i===void 0&&v>n?l?(c=Date.now(),o||(a=setTimeout(i?A:r,n))):r():o||(a=setTimeout(i?A:r,i===void 0?Math.max(0,n-v):n))}return d.cancel=T,d}function g(n,e,{atBegin:t=!1}={}){return F(n,e,{debounceMode:t!==!1})}const y=n=>{let e=null;const t=l=>{e=null,n(l)};return(()=>{e==null&&(e=requestAnimationFrame(t))})(),()=>{e!=null&&(cancelAnimationFrame(e),e=null)}},I=n=>{let e=null;const t=l=>{e=requestAnimationFrame(t),n(l)};return(()=>{e==null&&(e=requestAnimationFrame(t))})(),()=>{e!=null&&(cancelAnimationFrame(e),e=null)}};exports.debounce=g;exports.delayed=p;exports.delayedAnimationFrame=y;exports.interval=b;exports.intervalAnimationFrame=I;exports.throttle=F;
package/timer.d.ts CHANGED
@@ -69,10 +69,26 @@ export interface CancelOptions {
69
69
  * Represents a throttled function with a cancel method.
70
70
  * @public
71
71
  */
72
- export interface ThrottledFunction<T extends unknown[]> {
73
- (...args: T): void;
72
+ export interface ThrottledFunction<TArgs extends any[], This = unknown> {
73
+ (this: This, ...args: TArgs): void;
74
74
  cancel: (options?: CancelOptions) => void;
75
75
  }
76
+ export interface ThrottleOptions {
77
+ noTrailing?: boolean;
78
+ noLeading?: boolean;
79
+ debounceMode?: boolean;
80
+ }
81
+ /**
82
+ * Options for debouncing a function.
83
+ * @public
84
+ */
85
+ export type DebounceOptions = {
86
+ /**
87
+ * If true, the callback is executed immediately before the delay.
88
+ * @default false
89
+ */
90
+ atBegin?: boolean;
91
+ };
76
92
  /**
77
93
  * Copyright (c) Ivan Nikolić http://ivannikolic.com
78
94
  *
@@ -117,18 +133,7 @@ export interface ThrottledFunction<T extends unknown[]> {
117
133
  * Common delay values are 100-250ms for UI updates and 1000ms+ for API calls.
118
134
  * @public
119
135
  */
120
- export declare const throttle: <FN extends (...args: unknown[]) => void>(delay: number, callback: FN, options?: ThrottleOptions) => ThrottledFunction<Parameters<FN>>;
121
- /**
122
- * Options for debouncing a function.
123
- * @public
124
- */
125
- export type DebounceOptions = {
126
- /**
127
- * If true, the callback is executed immediately before the delay.
128
- * @default false
129
- */
130
- atBegin?: boolean;
131
- };
136
+ export declare function throttle<This, F extends (this: This, ...args: any[]) => any>(delay: number, callback: F, options?: ThrottleOptions): ThrottledFunction<Parameters<F>, This>;
132
137
  /**
133
138
  * Copyright (c) Ivan Nikolić http://ivannikolic.com
134
139
  *
@@ -153,16 +158,16 @@ export type DebounceOptions = {
153
158
  *
154
159
  * @param {number} delay - A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
155
160
  * @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through, as-is,
156
- * to `callback` when the debounced-function is executed.
161
+ * to `callback` when the debounced-function is executed.
157
162
  * @param {object} [options] - An object to configure options.
158
163
  * @param {boolean} [options.atBegin] - Optional, defaults to false. If atBegin is false or unspecified, callback will only be executed `delay` milliseconds
159
- * after the last debounced-function call. If atBegin is true, callback will be executed only at the first debounced-function call.
160
- * (After the throttled-function has not been called for `delay` milliseconds, the internal counter is reset).
164
+ * after the last debounced-function call. If atBegin is true, callback will be executed only at the first debounced-function call.
165
+ * (After the throttled-function has not been called for `delay` milliseconds, the internal counter is reset).
161
166
  *
162
167
  * @returns {Function} A new, debounced function.
163
168
  * @public
164
169
  */
165
- export declare const debounce: <FN extends (...args: unknown[]) => void>(delay: number, callback: FN, { atBegin }?: DebounceOptions) => ThrottledFunction<Parameters<FN>>;
170
+ export declare function debounce<This, F extends (this: This, ...args: any[]) => any>(delay: number, callback: F, { atBegin }?: DebounceOptions): ThrottledFunction<Parameters<F>, This>;
166
171
  /**
167
172
  * Delays the execution of a function using requestAnimationFrame.
168
173
  *
package/timer.js CHANGED
@@ -1,14 +1,15 @@
1
1
  const T = (n, e) => {
2
2
  const t = setTimeout(n, e);
3
3
  return () => clearTimeout(t);
4
- }, I = (n, e) => {
4
+ }, x = (n, e) => {
5
5
  const t = setInterval(n, e);
6
6
  return () => clearInterval(t);
7
- }, F = (n, e, t = {}) => {
8
- const { noTrailing: a = !1, noLeading: l = !1, debounceMode: i } = t;
9
- let o, f = !1, c = 0;
7
+ };
8
+ function F(n, e, t = {}) {
9
+ const { noTrailing: a = !1, noLeading: l = !1, debounceMode: o } = t;
10
+ let i, f = !1, c = 0;
10
11
  function m() {
11
- o && clearTimeout(o);
12
+ i && clearTimeout(i);
12
13
  }
13
14
  function A(u) {
14
15
  const { upcomingOnly: s = !1 } = u || {};
@@ -21,15 +22,19 @@ const T = (n, e) => {
21
22
  c = Date.now(), e.apply(s, u);
22
23
  }
23
24
  function v() {
24
- o = void 0;
25
+ i = void 0;
25
26
  }
26
- !l && i && !o && r(), m(), i === void 0 && p > n ? l ? (c = Date.now(), a || (o = setTimeout(i ? v : r, n))) : r() : a || (o = setTimeout(
27
- i ? v : r,
28
- i === void 0 ? n - p : n
27
+ !l && o && !i && r(), m(), o === void 0 && p > n ? l ? (c = Date.now(), a || (i = setTimeout(o ? v : r, n))) : r() : a || (i = setTimeout(
28
+ o ? v : r,
29
+ o === void 0 ? Math.max(0, n - p) : n
29
30
  ));
30
31
  }
31
32
  return d.cancel = A, d;
32
- }, g = (n, e, { atBegin: t = !1 } = {}) => F(n, e, { debounceMode: t !== !1 }), w = (n) => {
33
+ }
34
+ function I(n, e, { atBegin: t = !1 } = {}) {
35
+ return F(n, e, { debounceMode: t !== !1 });
36
+ }
37
+ const g = (n) => {
33
38
  let e = null;
34
39
  const t = (l) => {
35
40
  e = null, n(l);
@@ -39,7 +44,7 @@ const T = (n, e) => {
39
44
  })(), () => {
40
45
  e != null && (cancelAnimationFrame(e), e = null);
41
46
  };
42
- }, x = (n) => {
47
+ }, w = (n) => {
43
48
  let e = null;
44
49
  const t = (l) => {
45
50
  e = requestAnimationFrame(t), n(l);
@@ -51,10 +56,10 @@ const T = (n, e) => {
51
56
  };
52
57
  };
53
58
  export {
54
- g as debounce,
59
+ I as debounce,
55
60
  T as delayed,
56
- w as delayedAnimationFrame,
57
- I as interval,
58
- x as intervalAnimationFrame,
61
+ g as delayedAnimationFrame,
62
+ x as interval,
63
+ w as intervalAnimationFrame,
59
64
  F as throttle
60
65
  };