@dr.pogodin/js-utils 0.0.1 → 0.0.2
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/README.md +3 -3
- package/js/time.d.ts +24 -1
- package/js/time.js +32 -16
- package/package.json +1 -1
- package/ts/time.ts +35 -6
package/README.md
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
# JS Utils
|
|
4
4
|
|
|
5
|
-
[
|
|
6
|
-
[
|
|
7
|
-
[
|
|
5
|
+
[](https://www.npmjs.com/package/@dr.pogodin/js-utils)
|
|
6
|
+
[](https://www.npmjs.com/package/@dr.pogodin/js-utils)
|
|
7
|
+
[](https://app.circleci.com/pipelines/github/birdofpreyru/js-utils)
|
|
8
8
|
[](https://github.com/birdofpreyru/js-utils)
|
|
9
9
|
|
|
10
10
|
The aim for this repo/package is to move in from the [React Utils] the pieces
|
package/js/time.d.ts
CHANGED
|
@@ -1,8 +1,31 @@
|
|
|
1
|
+
import Barrier, { type Executor } from './Barrier';
|
|
1
2
|
export declare const SEC_MS = 1000;
|
|
2
3
|
export declare const MIN_MS: number;
|
|
3
4
|
export declare const HOUR_MS: number;
|
|
4
5
|
export declare const DAY_MS: number;
|
|
5
6
|
export declare const YEAR_MS: number;
|
|
7
|
+
export declare class Timer<T> extends Barrier<void, T> {
|
|
8
|
+
private p_abort;
|
|
9
|
+
private p_timeout?;
|
|
10
|
+
get abort(): () => void;
|
|
11
|
+
get timeout(): number | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* Creates a new, non-initialized instance of Timer. Call .init() method
|
|
14
|
+
* to actually initialize and launch the timer.
|
|
15
|
+
*
|
|
16
|
+
* NOTE: Although it might be tempting to accept `timeout` value as
|
|
17
|
+
* a constructor's argument, it won't work well, because Timer is an
|
|
18
|
+
* extension of Promise (via Barrier), and the way Promises works (in
|
|
19
|
+
* particular their .then() method, which internally calls constructor()
|
|
20
|
+
* with special executor) does not play along with initalization depending
|
|
21
|
+
* on custom parameters done in constructor().
|
|
22
|
+
*
|
|
23
|
+
* @param executor
|
|
24
|
+
*/
|
|
25
|
+
constructor(executor?: Executor<T>);
|
|
26
|
+
init(timeout: number): Timer<T>;
|
|
27
|
+
then<TR1, TR2>(onFulfilled?: ((value: T) => TR1 | PromiseLike<TR1>) | null, onRejected?: ((reason: any) => TR2 | PromiseLike<TR2>) | null): Timer<TR1 | TR2>;
|
|
28
|
+
}
|
|
6
29
|
/**
|
|
7
30
|
* Creates a Promise, which resolves after the given timeout.
|
|
8
31
|
* @param {number} timeout Timeout [ms].
|
|
@@ -10,4 +33,4 @@ export declare const YEAR_MS: number;
|
|
|
10
33
|
* .abort() method attached, which cancels the pending timer resolution
|
|
11
34
|
* (without resolving or rejecting the barrier).
|
|
12
35
|
*/
|
|
13
|
-
export declare function timer(timeout: number):
|
|
36
|
+
export declare function timer(timeout: number): Timer<void>;
|
package/js/time.js
CHANGED
|
@@ -1,43 +1,60 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
14
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.timer = exports.YEAR_MS = exports.DAY_MS = exports.HOUR_MS = exports.MIN_MS = exports.SEC_MS = void 0;
|
|
6
|
+
exports.timer = exports.Timer = exports.YEAR_MS = exports.DAY_MS = exports.HOUR_MS = exports.MIN_MS = exports.SEC_MS = void 0;
|
|
16
7
|
const Barrier_1 = __importDefault(require("./Barrier"));
|
|
17
8
|
exports.SEC_MS = 1000;
|
|
18
9
|
exports.MIN_MS = 60 * exports.SEC_MS;
|
|
19
10
|
exports.HOUR_MS = 60 * exports.MIN_MS;
|
|
20
11
|
exports.DAY_MS = 24 * exports.HOUR_MS;
|
|
21
12
|
exports.YEAR_MS = 365 * exports.DAY_MS;
|
|
13
|
+
// TODO: Ok, as we have ended up with a Timer class, mostly to achieve a good
|
|
14
|
+
// TypeScript typing for timer() function, it makes sense to expose the class
|
|
15
|
+
// from the library as well, and it should be documented later.
|
|
22
16
|
class Timer extends Barrier_1.default {
|
|
23
17
|
get abort() { return this.p_abort; }
|
|
24
|
-
|
|
18
|
+
get timeout() { return this.p_timeout; }
|
|
19
|
+
/**
|
|
20
|
+
* Creates a new, non-initialized instance of Timer. Call .init() method
|
|
21
|
+
* to actually initialize and launch the timer.
|
|
22
|
+
*
|
|
23
|
+
* NOTE: Although it might be tempting to accept `timeout` value as
|
|
24
|
+
* a constructor's argument, it won't work well, because Timer is an
|
|
25
|
+
* extension of Promise (via Barrier), and the way Promises works (in
|
|
26
|
+
* particular their .then() method, which internally calls constructor()
|
|
27
|
+
* with special executor) does not play along with initalization depending
|
|
28
|
+
* on custom parameters done in constructor().
|
|
29
|
+
*
|
|
30
|
+
* @param executor
|
|
31
|
+
*/
|
|
32
|
+
constructor(executor) {
|
|
25
33
|
super(executor);
|
|
34
|
+
this.p_abort = () => { };
|
|
35
|
+
}
|
|
36
|
+
init(timeout) {
|
|
37
|
+
if (this.p_timeout !== undefined) {
|
|
38
|
+
throw Error('This Timer is initialized already');
|
|
39
|
+
}
|
|
40
|
+
this.p_timeout = timeout;
|
|
26
41
|
if (timeout > 0) {
|
|
27
42
|
const id = setTimeout(super.resolve.bind(this), timeout);
|
|
28
43
|
this.p_abort = () => clearTimeout(id);
|
|
29
44
|
}
|
|
30
45
|
else {
|
|
31
|
-
this.p_abort = () => { };
|
|
32
46
|
super.resolve();
|
|
33
47
|
}
|
|
48
|
+
return this;
|
|
34
49
|
}
|
|
35
50
|
then(onFulfilled, onRejected) {
|
|
36
51
|
const res = super.then(onFulfilled, onRejected);
|
|
37
|
-
|
|
52
|
+
if (this.timeout !== undefined)
|
|
53
|
+
res.init(this.timeout);
|
|
38
54
|
return res;
|
|
39
55
|
}
|
|
40
56
|
}
|
|
57
|
+
exports.Timer = Timer;
|
|
41
58
|
/**
|
|
42
59
|
* Creates a Promise, which resolves after the given timeout.
|
|
43
60
|
* @param {number} timeout Timeout [ms].
|
|
@@ -46,8 +63,7 @@ class Timer extends Barrier_1.default {
|
|
|
46
63
|
* (without resolving or rejecting the barrier).
|
|
47
64
|
*/
|
|
48
65
|
function timer(timeout) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
});
|
|
66
|
+
const t = new Timer();
|
|
67
|
+
return t.init(timeout);
|
|
52
68
|
}
|
|
53
69
|
exports.timer = timer;
|
package/package.json
CHANGED
package/ts/time.ts
CHANGED
|
@@ -6,20 +6,48 @@ export const HOUR_MS = 60 * MIN_MS;
|
|
|
6
6
|
export const DAY_MS = 24 * HOUR_MS;
|
|
7
7
|
export const YEAR_MS = 365 * DAY_MS;
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
// TODO: Ok, as we have ended up with a Timer class, mostly to achieve a good
|
|
10
|
+
// TypeScript typing for timer() function, it makes sense to expose the class
|
|
11
|
+
// from the library as well, and it should be documented later.
|
|
12
|
+
export class Timer<T> extends Barrier<void, T> {
|
|
10
13
|
private p_abort: () => void;
|
|
11
14
|
|
|
15
|
+
private p_timeout?: number;
|
|
16
|
+
|
|
12
17
|
get abort(): () => void { return this.p_abort; }
|
|
13
18
|
|
|
14
|
-
|
|
19
|
+
get timeout(): number | undefined { return this.p_timeout; }
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Creates a new, non-initialized instance of Timer. Call .init() method
|
|
23
|
+
* to actually initialize and launch the timer.
|
|
24
|
+
*
|
|
25
|
+
* NOTE: Although it might be tempting to accept `timeout` value as
|
|
26
|
+
* a constructor's argument, it won't work well, because Timer is an
|
|
27
|
+
* extension of Promise (via Barrier), and the way Promises works (in
|
|
28
|
+
* particular their .then() method, which internally calls constructor()
|
|
29
|
+
* with special executor) does not play along with initalization depending
|
|
30
|
+
* on custom parameters done in constructor().
|
|
31
|
+
*
|
|
32
|
+
* @param executor
|
|
33
|
+
*/
|
|
34
|
+
constructor(executor?: Executor<T>) {
|
|
15
35
|
super(executor);
|
|
36
|
+
this.p_abort = () => {};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
init(timeout: number): Timer<T> {
|
|
40
|
+
if (this.p_timeout !== undefined) {
|
|
41
|
+
throw Error('This Timer is initialized already');
|
|
42
|
+
}
|
|
43
|
+
this.p_timeout = timeout;
|
|
16
44
|
if (timeout > 0) {
|
|
17
45
|
const id = setTimeout(super.resolve.bind(this), timeout);
|
|
18
46
|
this.p_abort = () => clearTimeout(id);
|
|
19
47
|
} else {
|
|
20
|
-
this.p_abort = () => {};
|
|
21
48
|
super.resolve();
|
|
22
49
|
}
|
|
50
|
+
return this;
|
|
23
51
|
}
|
|
24
52
|
|
|
25
53
|
then<TR1, TR2>(
|
|
@@ -27,7 +55,7 @@ class Timer<T> extends Barrier<void, T> {
|
|
|
27
55
|
onRejected?: ((reason: any) => TR2 | PromiseLike<TR2>) | null,
|
|
28
56
|
): Timer<TR1 | TR2> {
|
|
29
57
|
const res = <Timer<TR1 | TR2>> super.then(onFulfilled, onRejected);
|
|
30
|
-
|
|
58
|
+
if (this.timeout !== undefined) res.init(this.timeout);
|
|
31
59
|
return res;
|
|
32
60
|
}
|
|
33
61
|
}
|
|
@@ -39,6 +67,7 @@ class Timer<T> extends Barrier<void, T> {
|
|
|
39
67
|
* .abort() method attached, which cancels the pending timer resolution
|
|
40
68
|
* (without resolving or rejecting the barrier).
|
|
41
69
|
*/
|
|
42
|
-
export
|
|
43
|
-
|
|
70
|
+
export function timer(timeout: number): Timer<void> {
|
|
71
|
+
const t = new Timer<void>();
|
|
72
|
+
return t.init(timeout);
|
|
44
73
|
}
|