@zajno/common 1.5.2 → 1.5.3

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 (82) hide show
  1. package/coverage/clover.xml +45 -37
  2. package/coverage/coverage-final.json +3 -3
  3. package/coverage/lcov-report/async/index.html +1 -1
  4. package/coverage/lcov-report/async/timeout.ts.html +12 -12
  5. package/coverage/lcov-report/index.html +19 -19
  6. package/coverage/lcov-report/src/__tests__/helpers/index.html +1 -1
  7. package/coverage/lcov-report/src/__tests__/helpers/main.ts.html +1 -1
  8. package/coverage/lcov-report/src/async/arrays.ts.html +1 -1
  9. package/coverage/lcov-report/src/async/index.html +1 -1
  10. package/coverage/lcov-report/src/async/timeout.ts.html +10 -10
  11. package/coverage/lcov-report/src/cache.ts.html +1 -1
  12. package/coverage/lcov-report/src/dates/calc.ts.html +1 -1
  13. package/coverage/lcov-report/src/dates/convert.ts.html +1 -1
  14. package/coverage/lcov-report/src/dates/datex.ts.html +1 -1
  15. package/coverage/lcov-report/src/dates/format.ts.html +1 -1
  16. package/coverage/lcov-report/src/dates/index.html +1 -1
  17. package/coverage/lcov-report/src/dates/index.ts.html +1 -1
  18. package/coverage/lcov-report/src/dates/parse.ts.html +1 -1
  19. package/coverage/lcov-report/src/dates/period.ts.html +1 -1
  20. package/coverage/lcov-report/src/dates/shift.ts.html +1 -1
  21. package/coverage/lcov-report/src/dates/types.ts.html +1 -1
  22. package/coverage/lcov-report/src/dates/yearDate.ts.html +1 -1
  23. package/coverage/lcov-report/src/disposer.ts.html +1 -1
  24. package/coverage/lcov-report/src/enumHelper.ts.html +1 -1
  25. package/coverage/lcov-report/src/event.ts.html +1 -1
  26. package/coverage/lcov-report/src/fields/index.html +1 -1
  27. package/coverage/lcov-report/src/fields/update.ts.html +1 -1
  28. package/coverage/lcov-report/src/functions.ts.html +1 -1
  29. package/coverage/lcov-report/src/index.html +1 -1
  30. package/coverage/lcov-report/src/lazy.light.ts.html +1 -1
  31. package/coverage/lcov-report/src/logger/batch.ts.html +1 -1
  32. package/coverage/lcov-report/src/logger/console.ts.html +1 -1
  33. package/coverage/lcov-report/src/logger/index.html +1 -1
  34. package/coverage/lcov-report/src/logger/index.ts.html +1 -1
  35. package/coverage/lcov-report/src/logger/named.ts.html +1 -1
  36. package/coverage/lcov-report/src/logger/proxy.ts.html +1 -1
  37. package/coverage/lcov-report/src/math/arrays.ts.html +1 -1
  38. package/coverage/lcov-report/src/math/calc.ts.html +1 -1
  39. package/coverage/lcov-report/src/math/distribution.ts.html +1 -1
  40. package/coverage/lcov-report/src/math/index.html +1 -1
  41. package/coverage/lcov-report/src/math/index.ts.html +1 -1
  42. package/coverage/lcov-report/src/observersMap.ts.html +1 -1
  43. package/coverage/lcov-report/src/observingCache.ts.html +1 -1
  44. package/coverage/lcov-report/src/tempoCache.ts.html +1 -1
  45. package/coverage/lcov-report/src/transitionObserver.ts.html +1 -1
  46. package/coverage/lcov-report/src/types.ts.html +1 -1
  47. package/coverage/lcov-report/src/validation/ValidationErrors.ts.html +1 -1
  48. package/coverage/lcov-report/src/validation/creditCard.ts.html +1 -1
  49. package/coverage/lcov-report/src/validation/helpers.ts.html +1 -1
  50. package/coverage/lcov-report/src/validation/index.html +1 -1
  51. package/coverage/lcov-report/src/validation/index.ts.html +1 -1
  52. package/coverage/lcov-report/src/validation/types.ts.html +1 -1
  53. package/coverage/lcov-report/src/validation/validators.ts.html +1 -1
  54. package/coverage/lcov-report/src/validation/wrappers.ts.html +1 -1
  55. package/coverage/lcov-report/src/viewModels/CommonModel.ts.html +1 -1
  56. package/coverage/lcov-report/src/viewModels/FlagModel.ts.html +1 -1
  57. package/coverage/lcov-report/src/viewModels/LoadingModel.ts.html +106 -34
  58. package/coverage/lcov-report/src/viewModels/MultiSelectModel.ts.html +1 -1
  59. package/coverage/lcov-report/src/viewModels/NumberModel.ts.html +13 -13
  60. package/coverage/lcov-report/src/viewModels/SelectModel.ts.html +1 -1
  61. package/coverage/lcov-report/src/viewModels/TextModel.ts.html +1 -1
  62. package/coverage/lcov-report/src/viewModels/Validatable.ts.html +1 -1
  63. package/coverage/lcov-report/src/viewModels/index.html +19 -19
  64. package/coverage/lcov-report/src/viewModels/wrappers.ts.html +1 -1
  65. package/coverage/lcov-report/validation/index.html +1 -1
  66. package/coverage/lcov-report/validation/index.ts.html +6 -6
  67. package/coverage/lcov-report/viewModels/CommonModel.ts.html +1 -1
  68. package/coverage/lcov-report/viewModels/LoadingModel.ts.html +129 -21
  69. package/coverage/lcov-report/viewModels/SelectModel.ts.html +54 -21
  70. package/coverage/lcov-report/viewModels/TextModel.ts.html +1 -1
  71. package/coverage/lcov-report/viewModels/index.html +23 -83
  72. package/coverage/lcov.info +85 -64
  73. package/lib/viewModels/LoadingModel.d.ts +12 -3
  74. package/lib/viewModels/LoadingModel.d.ts.map +1 -1
  75. package/lib/viewModels/LoadingModel.js +33 -16
  76. package/lib/viewModels/LoadingModel.js.map +1 -1
  77. package/lib/viewModels/types.d.ts +4 -1
  78. package/lib/viewModels/types.d.ts.map +1 -1
  79. package/package.json +1 -1
  80. package/src/viewModels/LoadingModel.ts +41 -17
  81. package/src/viewModels/__tests__/common.test.ts +27 -8
  82. package/src/viewModels/types.ts +5 -1
@@ -1125,29 +1125,29 @@ FN:37,(anonymous_5)
1125
1125
  FN:40,(anonymous_6)
1126
1126
  FNF:7
1127
1127
  FNH:3
1128
- FNDA:9,setTimeoutAsync
1129
- FNDA:9,(anonymous_1)
1130
- FNDA:9,(anonymous_2)
1128
+ FNDA:10,setTimeoutAsync
1129
+ FNDA:10,(anonymous_1)
1130
+ FNDA:10,(anonymous_2)
1131
1131
  FNDA:0,(anonymous_3)
1132
1132
  FNDA:0,setTimeoutFramesAsync
1133
1133
  FNDA:0,(anonymous_5)
1134
1134
  FNDA:0,(anonymous_6)
1135
1135
  DA:2,3
1136
- DA:3,9
1137
- DA:6,9
1138
- DA:7,9
1139
- DA:8,9
1140
- DA:9,9
1141
- DA:10,9
1142
- DA:11,9
1143
- DA:16,9
1136
+ DA:3,10
1137
+ DA:6,10
1138
+ DA:7,10
1139
+ DA:8,10
1140
+ DA:9,10
1141
+ DA:10,10
1142
+ DA:11,10
1143
+ DA:16,10
1144
1144
  DA:17,0
1145
1145
  DA:18,0
1146
1146
  DA:19,0
1147
1147
  DA:20,0
1148
1148
  DA:21,0
1149
1149
  DA:24,0
1150
- DA:27,9
1150
+ DA:27,10
1151
1151
  DA:32,3
1152
1152
  DA:33,0
1153
1153
  DA:34,0
@@ -1160,11 +1160,11 @@ DA:44,0
1160
1160
  DA:48,0
1161
1161
  LF:26
1162
1162
  LH:11
1163
- BRDA:2,0,0,9
1164
- BRDA:10,1,0,9
1163
+ BRDA:2,0,0,10
1164
+ BRDA:10,1,0,10
1165
1165
  BRDA:10,1,1,0
1166
1166
  BRDA:16,2,0,0
1167
- BRDA:16,2,1,9
1167
+ BRDA:16,2,1,10
1168
1168
  BRDA:20,3,0,0
1169
1169
  BRDA:20,3,1,0
1170
1170
  BRDA:33,4,0,0
@@ -3248,35 +3248,56 @@ BRH:1
3248
3248
  end_of_record
3249
3249
  TN:
3250
3250
  SF:src/viewModels/LoadingModel.ts
3251
- FN:5,(anonymous_0)
3252
- FN:11,(anonymous_1)
3253
- FNF:2
3254
- FNH:2
3255
- FNDA:3,(anonymous_0)
3256
- FNDA:1,(anonymous_1)
3251
+ FN:4,(anonymous_0)
3252
+ FN:28,(anonymous_1)
3253
+ FN:8,(anonymous_2)
3254
+ FN:10,(anonymous_3)
3255
+ FN:11,(anonymous_4)
3256
+ FN:24,(anonymous_5)
3257
+ FN:36,withLoading
3258
+ FNF:7
3259
+ FNH:6
3260
+ FNDA:2,(anonymous_0)
3261
+ FNDA:0,(anonymous_1)
3262
+ FNDA:12,(anonymous_2)
3263
+ FNDA:5,(anonymous_3)
3264
+ FNDA:4,(anonymous_4)
3265
+ FNDA:4,(anonymous_5)
3266
+ FNDA:4,withLoading
3257
3267
  DA:1,1
3258
- DA:3,1
3259
- DA:5,3
3260
- DA:12,1
3261
- DA:13,0
3262
- DA:14,0
3263
- DA:16,0
3264
- DA:19,1
3265
- DA:21,1
3266
- DA:22,1
3267
- DA:23,1
3268
- DA:25,1
3269
- LF:12
3270
- LH:9
3271
- BRDA:11,0,0,1
3272
- BRDA:12,1,0,0
3273
- BRDA:12,1,1,1
3274
- BRDA:12,2,0,1
3275
- BRDA:12,2,1,0
3276
- BRDA:13,3,0,0
3277
- BRDA:13,3,1,0
3278
- BRF:7
3279
- BRH:3
3268
+ DA:4,1
3269
+ DA:6,2
3270
+ DA:8,12
3271
+ DA:10,5
3272
+ DA:12,4
3273
+ DA:13,2
3274
+ DA:15,2
3275
+ DA:25,4
3276
+ DA:28,2
3277
+ DA:36,1
3278
+ DA:37,4
3279
+ DA:38,2
3280
+ DA:39,1
3281
+ DA:41,1
3282
+ DA:44,2
3283
+ DA:46,2
3284
+ DA:47,2
3285
+ DA:48,2
3286
+ DA:50,2
3287
+ LF:20
3288
+ LH:20
3289
+ BRDA:12,0,0,2
3290
+ BRDA:12,0,1,2
3291
+ BRDA:24,1,0,1
3292
+ BRDA:36,2,0,0
3293
+ BRDA:37,3,0,2
3294
+ BRDA:37,3,1,2
3295
+ BRDA:37,4,0,4
3296
+ BRDA:37,4,1,3
3297
+ BRDA:38,5,0,1
3298
+ BRDA:38,5,1,1
3299
+ BRF:10
3300
+ BRH:9
3280
3301
  end_of_record
3281
3302
  TN:
3282
3303
  SF:src/viewModels/MultiSelectModel.ts
@@ -3483,36 +3504,36 @@ FN:29,(anonymous_6)
3483
3504
  FN:36,(anonymous_7)
3484
3505
  FNF:8
3485
3506
  FNH:6
3486
- FNDA:8,(anonymous_0)
3487
- FNDA:10,(anonymous_1)
3507
+ FNDA:9,(anonymous_0)
3508
+ FNDA:12,(anonymous_1)
3488
3509
  FNDA:0,(anonymous_2)
3489
- FNDA:4,(anonymous_3)
3490
- FNDA:3,(anonymous_4)
3491
- FNDA:13,(anonymous_5)
3492
- FNDA:8,(anonymous_6)
3510
+ FNDA:5,(anonymous_3)
3511
+ FNDA:4,(anonymous_4)
3512
+ FNDA:23,(anonymous_5)
3513
+ FNDA:10,(anonymous_6)
3493
3514
  FNDA:0,(anonymous_7)
3494
3515
  DA:1,3
3495
3516
  DA:9,3
3496
- DA:12,8
3497
- DA:14,8
3498
- DA:17,8
3499
- DA:24,8
3500
- DA:25,8
3501
- DA:28,13
3502
- DA:29,8
3503
- DA:32,8
3504
- DA:33,10
3517
+ DA:12,9
3518
+ DA:14,9
3519
+ DA:17,9
3520
+ DA:24,9
3521
+ DA:25,9
3522
+ DA:28,23
3523
+ DA:29,10
3524
+ DA:32,9
3525
+ DA:33,12
3505
3526
  DA:36,0
3506
- DA:39,8
3527
+ DA:39,9
3507
3528
  DA:40,0
3508
- DA:44,8
3509
- DA:47,8
3529
+ DA:44,9
3530
+ DA:47,9
3510
3531
  DA:50,3
3511
3532
  LF:17
3512
3533
  LH:15
3513
- BRDA:16,0,0,5
3514
- BRDA:44,1,0,4
3515
- BRDA:47,2,0,3
3534
+ BRDA:16,0,0,4
3535
+ BRDA:44,1,0,5
3536
+ BRDA:47,2,0,4
3516
3537
  BRF:3
3517
3538
  BRH:3
3518
3539
  end_of_record
@@ -1,8 +1,17 @@
1
- import { NumberModel } from './NumberModel';
2
- export declare class LoadingModel extends NumberModel {
1
+ import { IResetableModel, IValueModel } from './types';
2
+ export declare class LoadingModel implements IValueModel<boolean>, IResetableModel {
3
+ private readonly _number;
3
4
  get isLoading(): boolean;
5
+ get value(): boolean;
6
+ set value(v: boolean);
4
7
  useLoading<T>(cb: () => (T | Promise<T>)): Promise<T>;
5
8
  useLoading<T>(cb: () => (T | Promise<T>), exclusive: false): Promise<T>;
6
- useLoading<T>(cb: () => (T | Promise<T>), exclusive: true | 'throw'): Promise<T | false>;
9
+ useLoading<T>(cb: () => (T | Promise<T>), exclusive: true): Promise<T | false>;
10
+ useLoading<T>(cb: () => (T | Promise<T>), exclusive: 'throw'): Promise<T>;
11
+ reset: () => void;
7
12
  }
13
+ export declare function withLoading<T>(model: IValueModel<boolean>, cb: () => (T | Promise<T>)): Promise<T>;
14
+ export declare function withLoading<T>(model: IValueModel<boolean>, cb: () => (T | Promise<T>), exclusive: false): Promise<T>;
15
+ export declare function withLoading<T>(model: IValueModel<boolean>, cb: () => (T | Promise<T>), exclusive: true): Promise<T | false>;
16
+ export declare function withLoading<T>(model: IValueModel<boolean>, cb: () => (T | Promise<T>), exclusive: 'throw'): Promise<T>;
8
17
  //# sourceMappingURL=LoadingModel.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"LoadingModel.d.ts","sourceRoot":"","sources":["../../src/viewModels/LoadingModel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,qBAAa,YAAa,SAAQ,WAAW;IAEzC,IAAW,SAAS,YAA6B;IAEpC,UAAU,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IACrD,UAAU,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,KAAK,GAAG,OAAO,CAAC,CAAC,CAAC;IACvE,UAAU,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,IAAI,GAAG,OAAO,GAAG,OAAO,CAAC,CAAC,GAAG,KAAK,CAAC;CAmBxG"}
1
+ {"version":3,"file":"LoadingModel.d.ts","sourceRoot":"","sources":["../../src/viewModels/LoadingModel.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAEvD,qBAAa,YAAa,YAAW,WAAW,CAAC,OAAO,CAAC,EAAE,eAAe;IAEtE,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAsB;IAE9C,IAAW,SAAS,YAAqC;IAEzD,IAAW,KAAK,IAAI,OAAO,CAA2B;IACtD,IAAW,KAAK,CAAC,CAAC,EAAE,OAAO,EAM1B;IAEY,UAAU,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IACrD,UAAU,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,KAAK,GAAG,OAAO,CAAC,CAAC,CAAC;IACvE,UAAU,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,IAAI,GAAG,OAAO,CAAC,CAAC,GAAG,KAAK,CAAC;IAC9E,UAAU,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC;IAM/E,KAAK,aAA8B;CAC7C;AAED,wBAAgB,WAAW,CAAC,CAAC,EAAE,KAAK,EAAE,WAAW,CAAC,OAAO,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AACpG,wBAAgB,WAAW,CAAC,CAAC,EAAE,KAAK,EAAE,WAAW,CAAC,OAAO,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,KAAK,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AACtH,wBAAgB,WAAW,CAAC,CAAC,EAAE,KAAK,EAAE,WAAW,CAAC,OAAO,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,IAAI,GAAG,OAAO,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC;AAC7H,wBAAgB,WAAW,CAAC,CAAC,EAAE,KAAK,EAAE,WAAW,CAAC,OAAO,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC"}
@@ -1,25 +1,42 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.LoadingModel = void 0;
3
+ exports.withLoading = exports.LoadingModel = void 0;
4
4
  const NumberModel_1 = require("./NumberModel");
5
- class LoadingModel extends NumberModel_1.NumberModel {
6
- get isLoading() { return this.value > 0; }
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();
15
- try {
16
- const res = await cb();
17
- return res;
5
+ class LoadingModel {
6
+ constructor() {
7
+ this._number = new NumberModel_1.NumberModel(0);
8
+ this.reset = () => this._number.reset();
9
+ }
10
+ get isLoading() { return this._number.value > 0; }
11
+ get value() { return this.isLoading; }
12
+ set value(v) {
13
+ if (v) {
14
+ this._number.increment();
18
15
  }
19
- finally {
20
- this.decrement();
16
+ else {
17
+ this._number.decrement();
21
18
  }
22
19
  }
20
+ useLoading(cb, exclusive = false) {
21
+ return withLoading(this, cb, exclusive);
22
+ }
23
23
  }
24
24
  exports.LoadingModel = LoadingModel;
25
+ async function withLoading(model, cb, exclusive = false) {
26
+ if (exclusive && model.value) {
27
+ if (exclusive === 'throw') {
28
+ throw new Error('Operation cannot be started because another one is in progress already.');
29
+ }
30
+ return false;
31
+ }
32
+ model.value = true;
33
+ try {
34
+ const res = await cb();
35
+ return res;
36
+ }
37
+ finally {
38
+ model.value = false;
39
+ }
40
+ }
41
+ exports.withLoading = withLoading;
25
42
  //# sourceMappingURL=LoadingModel.js.map
@@ -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;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"}
1
+ {"version":3,"file":"LoadingModel.js","sourceRoot":"","sources":["../../src/viewModels/LoadingModel.ts"],"names":[],"mappings":";;;AAAA,+CAA4C;AAG5C,MAAa,YAAY;IAAzB;QAEqB,YAAO,GAAG,IAAI,yBAAW,CAAC,CAAC,CAAC,CAAC;QAsBvC,UAAK,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;IAC9C,CAAC;IArBG,IAAW,SAAS,KAAK,OAAO,IAAI,CAAC,OAAO,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC;IAEzD,IAAW,KAAK,KAAc,OAAO,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC;IACtD,IAAW,KAAK,CAAC,CAAU;QACvB,IAAI,CAAC,EAAE;YACH,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC;SAC5B;aAAM;YACH,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC;SAC5B;IACL,CAAC;IAOM,UAAU,CAAI,EAA0B,EAAE,YAA+B,KAAK;QACjF,OAAO,WAAW,CAAC,IAAI,EAAE,EAAE,EAAE,SAAgB,CAAC,CAAC;IACnD,CAAC;CAGJ;AAzBD,oCAyBC;AAOM,KAAK,UAAU,WAAW,CAAgB,KAA2B,EAAE,EAA0B,EAAE,YAA+B,KAAK;IAC1I,IAAI,SAAS,IAAI,KAAK,CAAC,KAAK,EAAE;QAC1B,IAAI,SAAS,KAAK,OAAO,EAAE;YACvB,MAAM,IAAI,KAAK,CAAC,yEAAyE,CAAC,CAAC;SAC9F;QACD,OAAO,KAAK,CAAC;KAChB;IAED,KAAK,CAAC,KAAK,GAAG,IAAI,CAAC;IAEnB,IAAI;QACA,MAAM,GAAG,GAAG,MAAM,EAAE,EAAE,CAAC;QACvB,OAAO,GAAG,CAAC;KACd;YAAS;QACN,KAAK,CAAC,KAAK,GAAG,KAAK,CAAC;KACvB;AACL,CAAC;AAhBD,kCAgBC"}
@@ -1,4 +1,7 @@
1
- export interface IValueModel<TValue> {
1
+ export interface IValueModelReadonly<TValue> {
2
+ readonly value: TValue;
3
+ }
4
+ export interface IValueModel<TValue> extends IValueModelReadonly<TValue> {
2
5
  value: TValue;
3
6
  }
4
7
  export interface ILabel<T> {
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/viewModels/types.ts"],"names":[],"mappings":"AACA,MAAM,WAAW,WAAW,CAAC,MAAM;IAC/B,KAAK,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,MAAM,CAAC,CAAC;IACrB,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC;CACrB;AAED,MAAM,WAAW,eAAe;IAC5B,QAAQ,CAAC,KAAK,EAAE,MAAM,IAAI,CAAC;IAC3B,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,CAAC;CAChC;AAED,MAAM,WAAW,eAAe;IAC5B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;IAChC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;CAC7B"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/viewModels/types.ts"],"names":[],"mappings":"AACA,MAAM,WAAW,mBAAmB,CAAC,MAAM;IACvC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CAC1B;AAED,MAAM,WAAW,WAAW,CAAC,MAAM,CAAE,SAAQ,mBAAmB,CAAC,MAAM,CAAC;IACpE,KAAK,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,MAAM,CAAC,CAAC;IACrB,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC;CACrB;AAED,MAAM,WAAW,eAAe;IAC5B,QAAQ,CAAC,KAAK,EAAE,MAAM,IAAI,CAAC;IAC3B,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,CAAC;CAChC;AAED,MAAM,WAAW,eAAe;IAC5B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;IAChC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;CAC7B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zajno/common",
3
- "version": "1.5.2",
3
+ "version": "1.5.3",
4
4
  "description": "Zajno's re-usable utilities for JS/TS projects",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,28 +1,52 @@
1
1
  import { NumberModel } from './NumberModel';
2
+ import { IResetableModel, IValueModel } from './types';
2
3
 
3
- export class LoadingModel extends NumberModel {
4
+ export class LoadingModel implements IValueModel<boolean>, IResetableModel {
4
5
 
5
- public get isLoading() { return this.value > 0; }
6
+ private readonly _number = new NumberModel(0);
7
+
8
+ public get isLoading() { return this._number.value > 0; }
9
+
10
+ public get value(): boolean { return this.isLoading; }
11
+ public set value(v: boolean) {
12
+ if (v) {
13
+ this._number.increment();
14
+ } else {
15
+ this._number.decrement();
16
+ }
17
+ }
6
18
 
7
19
  public async useLoading<T>(cb: () => (T | Promise<T>)): Promise<T>;
8
20
  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
- }
21
+ public async useLoading<T>(cb: () => (T | Promise<T>), exclusive: true): Promise<T | false>;
22
+ public async useLoading<T>(cb: () => (T | Promise<T>), exclusive: 'throw'): Promise<T>;
23
+
24
+ public useLoading<T>(cb: () => (T | Promise<T>), exclusive: boolean | 'throw' = false): Promise<T | false> {
25
+ return withLoading(this, cb, exclusive as any);
26
+ }
27
+
28
+ public reset = () => this._number.reset();
29
+ }
18
30
 
19
- this.increment();
31
+ export function withLoading<T>(model: IValueModel<boolean>, cb: () => (T | Promise<T>)): Promise<T>;
32
+ export function withLoading<T>(model: IValueModel<boolean>, cb: () => (T | Promise<T>), exclusive: false): Promise<T>;
33
+ export function withLoading<T>(model: IValueModel<boolean>, cb: () => (T | Promise<T>), exclusive: true): Promise<T | false>;
34
+ export function withLoading<T>(model: IValueModel<boolean>, cb: () => (T | Promise<T>), exclusive: 'throw'): Promise<T>;
20
35
 
21
- try {
22
- const res = await cb();
23
- return res;
24
- } finally {
25
- this.decrement();
36
+ export async function withLoading<T>(this: void, model: IValueModel<boolean>, cb: () => (T | Promise<T>), exclusive: boolean | 'throw' = false): Promise<T | false> {
37
+ if (exclusive && model.value) {
38
+ if (exclusive === 'throw') {
39
+ throw new Error('Operation cannot be started because another one is in progress already.');
26
40
  }
41
+ return false;
42
+ }
43
+
44
+ model.value = true;
45
+
46
+ try {
47
+ const res = await cb();
48
+ return res;
49
+ } finally {
50
+ model.value = false;
27
51
  }
28
52
  }
@@ -51,19 +51,18 @@ describe('CommonModel', () => {
51
51
  });
52
52
  });
53
53
 
54
- describe('LoadingModel', () => {
54
+ describe('LoadingModel works', () => {
55
+ const worker = async () => {
56
+ await setTimeoutAsync(100);
57
+ return 100;
58
+ };
55
59
 
56
- it('works', async () => {
60
+ it('basic', async () => {
57
61
 
58
62
  const m = new LoadingModel();
59
- expect(m.value).toBe(0);
63
+ expect(m.value).toBe(false);
60
64
  expect(m.isLoading).toBeFalsy();
61
65
 
62
- const worker = async () => {
63
- await setTimeoutAsync(100);
64
- return 100;
65
- };
66
-
67
66
  const promise = m.useLoading(worker);
68
67
  expect(m.isLoading).toBeTruthy();
69
68
 
@@ -71,6 +70,26 @@ describe('LoadingModel', () => {
71
70
 
72
71
  expect(m.isLoading).toBeFalsy();
73
72
  });
73
+
74
+ it('with exclusive', async () => {
75
+ const m = new LoadingModel();
76
+
77
+ const first = m.useLoading(worker, true);
78
+ expect(m.isLoading).toBeTruthy();
79
+
80
+ const second = m.useLoading(worker, true);
81
+ expect(m.isLoading).toBeTruthy();
82
+
83
+ await expect(() => m.useLoading(worker, 'throw')).rejects.toThrow();
84
+
85
+ expect(m.isLoading).toBeTruthy();
86
+
87
+ await expect(second).resolves.toBe(false);
88
+ await expect(first).resolves.toBe(100);
89
+
90
+ expect(m.value).toBe(false);
91
+ expect(m.isLoading).toBeFalsy();
92
+ });
74
93
  });
75
94
 
76
95
  describe('Others', () => {
@@ -1,5 +1,9 @@
1
1
 
2
- export interface IValueModel<TValue> {
2
+ export interface IValueModelReadonly<TValue> {
3
+ readonly value: TValue;
4
+ }
5
+
6
+ export interface IValueModel<TValue> extends IValueModelReadonly<TValue> {
3
7
  value: TValue;
4
8
  }
5
9