@zajno/common 1.4.14 → 1.4.17
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/coverage/clover.xml +38 -22
- package/coverage/coverage-final.json +3 -2
- package/coverage/lcov-report/index.html +25 -25
- package/coverage/lcov-report/src/__tests__/helpers/index.html +1 -1
- package/coverage/lcov-report/src/__tests__/helpers/main.ts.html +1 -1
- package/coverage/lcov-report/src/async/arrays.ts.html +1 -1
- package/coverage/lcov-report/src/async/index.html +1 -1
- package/coverage/lcov-report/src/async/timeout.ts.html +12 -12
- package/coverage/lcov-report/src/cache.ts.html +1 -1
- package/coverage/lcov-report/src/dates/calc.ts.html +1 -1
- package/coverage/lcov-report/src/dates/convert.ts.html +1 -1
- package/coverage/lcov-report/src/dates/datex.ts.html +1 -1
- package/coverage/lcov-report/src/dates/format.ts.html +1 -1
- package/coverage/lcov-report/src/dates/index.html +1 -1
- package/coverage/lcov-report/src/dates/index.ts.html +1 -1
- package/coverage/lcov-report/src/dates/parse.ts.html +1 -1
- package/coverage/lcov-report/src/dates/period.ts.html +1 -1
- package/coverage/lcov-report/src/dates/shift.ts.html +1 -1
- package/coverage/lcov-report/src/dates/types.ts.html +1 -1
- package/coverage/lcov-report/src/dates/yearDate.ts.html +1 -1
- package/coverage/lcov-report/src/disposer.ts.html +1 -1
- package/coverage/lcov-report/src/enumHelper.ts.html +1 -1
- package/coverage/lcov-report/src/event.ts.html +1 -1
- package/coverage/lcov-report/src/fields/index.html +1 -1
- package/coverage/lcov-report/src/fields/update.ts.html +1 -1
- package/coverage/lcov-report/src/functions.ts.html +1 -1
- package/coverage/lcov-report/src/index.html +24 -9
- package/coverage/lcov-report/src/lazy.light.ts.html +1 -1
- package/coverage/lcov-report/src/logger/batch.ts.html +1 -1
- package/coverage/lcov-report/src/logger/console.ts.html +1 -1
- package/coverage/lcov-report/src/logger/index.html +1 -1
- package/coverage/lcov-report/src/logger/index.ts.html +1 -1
- package/coverage/lcov-report/src/logger/named.ts.html +1 -1
- package/coverage/lcov-report/src/logger/proxy.ts.html +1 -1
- package/coverage/lcov-report/src/math/arrays.ts.html +1 -1
- package/coverage/lcov-report/src/math/calc.ts.html +1 -1
- package/coverage/lcov-report/src/math/distribution.ts.html +1 -1
- package/coverage/lcov-report/src/math/index.html +1 -1
- package/coverage/lcov-report/src/math/index.ts.html +1 -1
- package/coverage/lcov-report/src/observersMap.ts.html +1 -1
- package/coverage/lcov-report/src/observingCache.ts.html +1 -1
- package/coverage/lcov-report/src/tempoCache.ts.html +134 -0
- package/coverage/lcov-report/src/transitionObserver.ts.html +1 -1
- package/coverage/lcov-report/src/types.ts.html +1 -1
- package/coverage/lcov-report/src/validation/ValidationErrors.ts.html +1 -1
- package/coverage/lcov-report/src/validation/creditCard.ts.html +1 -1
- package/coverage/lcov-report/src/validation/helpers.ts.html +1 -1
- package/coverage/lcov-report/src/validation/index.html +1 -1
- package/coverage/lcov-report/src/validation/index.ts.html +1 -1
- package/coverage/lcov-report/src/validation/types.ts.html +1 -1
- package/coverage/lcov-report/src/validation/validators.ts.html +1 -1
- package/coverage/lcov-report/src/validation/wrappers.ts.html +1 -1
- package/coverage/lcov-report/src/viewModels/CommonModel.ts.html +1 -1
- package/coverage/lcov-report/src/viewModels/FlagModel.ts.html +1 -1
- package/coverage/lcov-report/src/viewModels/LoadingModel.ts.html +47 -11
- package/coverage/lcov-report/src/viewModels/MultiSelectModel.ts.html +1 -1
- package/coverage/lcov-report/src/viewModels/NumberModel.ts.html +1 -1
- package/coverage/lcov-report/src/viewModels/SelectModel.ts.html +1 -1
- package/coverage/lcov-report/src/viewModels/TextModel.ts.html +1 -1
- package/coverage/lcov-report/src/viewModels/Validatable.ts.html +1 -1
- package/coverage/lcov-report/src/viewModels/index.html +16 -16
- package/coverage/lcov-report/src/viewModels/wrappers.ts.html +1 -1
- package/coverage/lcov-report/tempoCache.ts.html +134 -0
- package/coverage/lcov-report/timer.ts.html +200 -0
- package/coverage/lcov.info +64 -27
- package/lib/tempoCache.d.ts +10 -0
- package/lib/tempoCache.d.ts.map +1 -0
- package/lib/tempoCache.js +21 -0
- package/lib/tempoCache.js.map +1 -0
- package/lib/timeHelper.d.ts +0 -7
- package/lib/timeHelper.d.ts.map +1 -1
- package/lib/timeHelper.js +3 -19
- package/lib/timeHelper.js.map +1 -1
- package/lib/timer.d.ts +15 -0
- package/lib/timer.d.ts.map +1 -0
- package/lib/timer.js +36 -0
- package/lib/timer.js.map +1 -0
- package/lib/viewModels/LoadingModel.d.ts +2 -0
- package/lib/viewModels/LoadingModel.d.ts.map +1 -1
- package/lib/viewModels/LoadingModel.js +8 -2
- package/lib/viewModels/LoadingModel.js.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/tempoCache.test.ts +31 -0
- package/src/tempoCache.ts +18 -0
- package/src/timeHelper.ts +3 -18
- package/src/timer.ts +40 -0
- package/src/viewModels/LoadingModel.ts +14 -2
- package/yarn.lock +3 -3
|
@@ -4,9 +4,15 @@ exports.LoadingModel = void 0;
|
|
|
4
4
|
const NumberModel_1 = require("./NumberModel");
|
|
5
5
|
class LoadingModel extends NumberModel_1.NumberModel {
|
|
6
6
|
get isLoading() { return this.value > 0; }
|
|
7
|
-
async useLoading(cb) {
|
|
7
|
+
async useLoading(cb, exclusive = false) {
|
|
8
|
+
if (exclusive && this.isLoading) {
|
|
9
|
+
if (exclusive === 'throw') {
|
|
10
|
+
throw new Error('Operation cannot be started because another one is in progress already.');
|
|
11
|
+
}
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
this.increment();
|
|
8
15
|
try {
|
|
9
|
-
this.increment();
|
|
10
16
|
const res = await cb();
|
|
11
17
|
return res;
|
|
12
18
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LoadingModel.js","sourceRoot":"","sources":["../../src/viewModels/LoadingModel.ts"],"names":[],"mappings":";;;AAAA,+CAA4C;AAE5C,MAAa,YAAa,SAAQ,yBAAW;IAEzC,IAAW,SAAS,KAAK,OAAO,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"LoadingModel.js","sourceRoot":"","sources":["../../src/viewModels/LoadingModel.ts"],"names":[],"mappings":";;;AAAA,+CAA4C;AAE5C,MAAa,YAAa,SAAQ,yBAAW;IAEzC,IAAW,SAAS,KAAK,OAAO,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC;IAM1C,KAAK,CAAC,UAAU,CAAI,EAA0B,EAAE,YAA+B,KAAK;QACvF,IAAI,SAAS,IAAI,IAAI,CAAC,SAAS,EAAE;YAC7B,IAAI,SAAS,KAAK,OAAO,EAAE;gBACvB,MAAM,IAAI,KAAK,CAAC,yEAAyE,CAAC,CAAC;aAC9F;YACD,OAAO,KAAK,CAAC;SAChB;QAED,IAAI,CAAC,SAAS,EAAE,CAAC;QAEjB,IAAI;YACA,MAAM,GAAG,GAAG,MAAM,EAAE,EAAE,CAAC;YACvB,OAAO,GAAG,CAAC;SACd;gBAAS;YACN,IAAI,CAAC,SAAS,EAAE,CAAC;SACpB;IACL,CAAC;CACJ;AAzBD,oCAyBC"}
|
package/package.json
CHANGED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { TempoCache } from '../tempoCache';
|
|
2
|
+
import { setTimeoutAsync } from '../async/timeout';
|
|
3
|
+
|
|
4
|
+
describe('TempoCache', () => {
|
|
5
|
+
it('just works', async () => {
|
|
6
|
+
let incrementer = 1;
|
|
7
|
+
|
|
8
|
+
const example = new TempoCache(() => Promise.resolve(incrementer++), 199);
|
|
9
|
+
|
|
10
|
+
expect(await example.current).toBe(1);
|
|
11
|
+
await expect(example.current).resolves.toBe(1);
|
|
12
|
+
|
|
13
|
+
await setTimeoutAsync(100);
|
|
14
|
+
|
|
15
|
+
await expect(example.current).resolves.toBe(1);
|
|
16
|
+
|
|
17
|
+
await setTimeoutAsync(200);
|
|
18
|
+
|
|
19
|
+
await expect(example.current).resolves.toBe(2);
|
|
20
|
+
|
|
21
|
+
await setTimeoutAsync(100);
|
|
22
|
+
|
|
23
|
+
await expect(example.current).resolves.toBe(2);
|
|
24
|
+
await expect(example.current).resolves.toBe(2);
|
|
25
|
+
|
|
26
|
+
await setTimeoutAsync(300);
|
|
27
|
+
|
|
28
|
+
await expect(example.current).resolves.toBe(3);
|
|
29
|
+
await expect(example.current).resolves.toBe(3);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
|
|
2
|
+
export class TempoCache<T> {
|
|
3
|
+
|
|
4
|
+
private _expiringAt: number = 0; // already expired
|
|
5
|
+
private _current: T = undefined;
|
|
6
|
+
|
|
7
|
+
constructor(readonly factory: () => T, readonly lifetimeMs: number) { }
|
|
8
|
+
|
|
9
|
+
public get isExpired() { return Date.now() >= this._expiringAt; }
|
|
10
|
+
|
|
11
|
+
public get current() {
|
|
12
|
+
if (this.isExpired) {
|
|
13
|
+
this._current = this.factory();
|
|
14
|
+
this._expiringAt = Date.now() + this.lifetimeMs;
|
|
15
|
+
}
|
|
16
|
+
return this._current;
|
|
17
|
+
}
|
|
18
|
+
}
|
package/src/timeHelper.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
|
+
// eslint-disable-next-line no-console
|
|
3
|
+
console.warn('"timeHelper" module is deprecated. Please use "dates" one.');
|
|
2
4
|
|
|
3
5
|
/** @deprecated */
|
|
4
6
|
export function formatMS(ms: number): string {
|
|
@@ -35,20 +37,3 @@ export function secToFormattedMin(totalSec: number): string {
|
|
|
35
37
|
|
|
36
38
|
return res;
|
|
37
39
|
}
|
|
38
|
-
|
|
39
|
-
class Time {
|
|
40
|
-
@observable
|
|
41
|
-
private _now: number = new Date().getTime();
|
|
42
|
-
|
|
43
|
-
constructor() {
|
|
44
|
-
makeObservable(this);
|
|
45
|
-
// Update _now once an hour
|
|
46
|
-
setInterval(() => { this._now = new Date().getTime(); }, 1000 * 3600);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
get now() {
|
|
50
|
-
return this._now;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export const TimeHelper = new Time();
|
package/src/timer.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { IDisposable } from './disposer';
|
|
2
|
+
import { Event, EventHandler, IEvent } from './event';
|
|
3
|
+
|
|
4
|
+
export class Timer implements IDisposable, IEvent<number> {
|
|
5
|
+
private readonly _event = new Event<number>();
|
|
6
|
+
|
|
7
|
+
private _now: number = Date.now();
|
|
8
|
+
private _handle = null;
|
|
9
|
+
|
|
10
|
+
constructor(readonly interval = 3600 * 1000) {
|
|
11
|
+
this.restart();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
get now() {
|
|
15
|
+
return this._now;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
on(handler: EventHandler<number>): () => void {
|
|
19
|
+
return this._event.on(handler);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
off(handler: EventHandler<number>): void {
|
|
23
|
+
return this._event.off(handler);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public restart() {
|
|
27
|
+
this.dispose();
|
|
28
|
+
|
|
29
|
+
// Update _now once an hour
|
|
30
|
+
this._handle = setInterval(() => {
|
|
31
|
+
this._now = Date.now();
|
|
32
|
+
this._event.trigger(this._now);
|
|
33
|
+
}, this.interval);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public dispose(): void {
|
|
37
|
+
clearInterval(this._handle);
|
|
38
|
+
this._handle = null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -4,9 +4,21 @@ export class LoadingModel extends NumberModel {
|
|
|
4
4
|
|
|
5
5
|
public get isLoading() { return this.value > 0; }
|
|
6
6
|
|
|
7
|
-
public async useLoading<T>(cb: () => (T | Promise<T>)): Promise<T
|
|
7
|
+
public async useLoading<T>(cb: () => (T | Promise<T>)): Promise<T>;
|
|
8
|
+
public async useLoading<T>(cb: () => (T | Promise<T>), exclusive: false): Promise<T>;
|
|
9
|
+
public async useLoading<T>(cb: () => (T | Promise<T>), exclusive: true | 'throw'): Promise<T | false>;
|
|
10
|
+
|
|
11
|
+
public async useLoading<T>(cb: () => (T | Promise<T>), exclusive: boolean | 'throw' = false): Promise<T | false> {
|
|
12
|
+
if (exclusive && this.isLoading) {
|
|
13
|
+
if (exclusive === 'throw') {
|
|
14
|
+
throw new Error('Operation cannot be started because another one is in progress already.');
|
|
15
|
+
}
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
this.increment();
|
|
20
|
+
|
|
8
21
|
try {
|
|
9
|
-
this.increment();
|
|
10
22
|
const res = await cb();
|
|
11
23
|
return res;
|
|
12
24
|
} finally {
|
package/yarn.lock
CHANGED
|
@@ -2752,9 +2752,9 @@ minimatch@^3.0.4:
|
|
|
2752
2752
|
brace-expansion "^1.1.7"
|
|
2753
2753
|
|
|
2754
2754
|
minimist@^1.2.5:
|
|
2755
|
-
version "1.2.
|
|
2756
|
-
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.
|
|
2757
|
-
integrity sha512-
|
|
2755
|
+
version "1.2.6"
|
|
2756
|
+
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.6.tgz#8637a5b759ea0d6e98702cfb3a9283323c93af44"
|
|
2757
|
+
integrity sha512-Jsjnk4bw3YJqYzbdyBiNsPWHPfO++UGG749Cxs6peCu5Xg4nrena6OVxOYxrQTqww0Jmwt+Ref8rggumkTLz9Q==
|
|
2758
2758
|
|
|
2759
2759
|
mkdirp@^0.5.3:
|
|
2760
2760
|
version "0.5.5"
|