gingersnap 0.23.13 → 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.
Files changed (59) hide show
  1. package/data/bus.cjs +1 -1
  2. package/data/bus.d.ts +4 -3
  3. package/data/bus.mjs +1 -1
  4. package/data/model/extra.d.ts +1 -1
  5. package/data/signal.d.ts +2 -3
  6. package/data/store/manager.cjs +3 -2
  7. package/data/store/manager.d.ts +5 -4
  8. package/data/store/manager.mjs +4 -3
  9. package/data-structures/object/BufferQueue.d.ts +3 -2
  10. package/data-structures/object/Queue.d.ts +3 -2
  11. package/data-structures/object/Time.cjs +195 -0
  12. package/data-structures/object/Time.d.ts +56 -0
  13. package/data-structures/object/Time.mjs +185 -0
  14. package/data-structures/object/TimeableObject.cjs +3 -2
  15. package/data-structures/object/TimeableObject.d.ts +4 -4
  16. package/data-structures/object/TimeableObject.mjs +3 -2
  17. package/data-structures/object/WaitableObject.d.ts +4 -3
  18. package/data-structures/object/WatchableObject.cjs +5 -4
  19. package/data-structures/object/WatchableObject.d.ts +4 -3
  20. package/data-structures/object/WatchableObject.mjs +5 -4
  21. package/data-structures/object/index.d.ts +1 -0
  22. package/data-structures/object.cjs +3 -0
  23. package/data-structures/object.mjs +1 -0
  24. package/extensions/time.cjs +25 -0
  25. package/extensions/time.d.ts +1 -0
  26. package/extensions/time.mjs +23 -0
  27. package/files.d.ts +3 -2
  28. package/functools/index.d.ts +2 -1
  29. package/functools.cjs +3 -4
  30. package/functools.mjs +3 -4
  31. package/future/future.cjs +3 -12
  32. package/future/future.d.ts +5 -14
  33. package/future/future.mjs +4 -12
  34. package/future/index.d.ts +1 -1
  35. package/mocks.d.ts +3 -3
  36. package/networking/NetworkService.cjs +1 -1
  37. package/networking/NetworkService.mjs +2 -2
  38. package/networking/decorators/options.cjs +4 -3
  39. package/networking/decorators/options.d.ts +1 -1
  40. package/networking/decorators/options.mjs +4 -3
  41. package/networking/decorators/requests.d.ts +2 -2
  42. package/networking/decorators/socket.d.ts +2 -2
  43. package/networking/types.d.ts +4 -4
  44. package/package.json +1 -1
  45. package/socket.cjs +5 -6
  46. package/socket.d.ts +7 -6
  47. package/socket.mjs +5 -6
  48. package/stream/call.cjs +2 -2
  49. package/stream/call.mjs +2 -2
  50. package/stream/index.d.ts +9 -8
  51. package/stream/observable.cjs +5 -4
  52. package/stream/observable.d.ts +7 -6
  53. package/stream/observable.mjs +5 -4
  54. package/stream.cjs +3 -3
  55. package/stream.mjs +4 -4
  56. package/synchronize.d.ts +4 -3
  57. package/types/globals.d.ts +15 -0
  58. /package/{typing.cjs → typing/types.cjs} +0 -0
  59. /package/{typing.mjs → typing/types.mjs} +0 -0
package/data/bus.cjs CHANGED
@@ -37,7 +37,7 @@ class DataBus extends stream_observable.Observable {
37
37
  * @param bufferSize
38
38
  * @param expiryPeriod
39
39
  */
40
- republish(topic, consumer, topicTransformer = (v) => v, bufferSize = undefined, expiryPeriod = undefined) {
40
+ republish(topic, consumer, topicTransformer = (v) => v, bufferSize = undefined, expiryPeriod) {
41
41
  return this.subscribe(topic, bufferSize, expiryPeriod)
42
42
  .map(({ topic: consumerTopic, data }) => this.publish(`${consumer}.${topicTransformer(consumerTopic)}`, data))
43
43
  .consume()
package/data/bus.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { Observable } from "../stream/observable";
2
- import { Future, WaitPeriod } from "../future";
2
+ import { Future } from "../future";
3
3
  import { Stream } from "../stream";
4
+ import { Duration } from "../data-structures/object";
4
5
  /**
5
6
  * A controller than manages exchanging data between various consumers
6
7
  */
@@ -21,7 +22,7 @@ export declare class DataBus<T = any> extends Observable<T> {
21
22
  * @param bufferSize
22
23
  * @param expiryPeriod
23
24
  */
24
- republish(topic: string, consumer: string, topicTransformer?: (v: string) => string, bufferSize?: number | undefined, expiryPeriod?: WaitPeriod | undefined): Future<void>;
25
+ republish(topic: string, consumer: string, topicTransformer?: (v: string) => string, bufferSize?: number | undefined, expiryPeriod?: Duration): Future<void>;
25
26
  /**
26
27
  * Dispatches message to specific consumers listen on the given topic
27
28
  * Format is [consumer/s].[topic]
@@ -45,7 +46,7 @@ export declare class DataBus<T = any> extends Observable<T> {
45
46
  * @param bufferSize
46
47
  * @param expiryPeriod
47
48
  */
48
- subscribe(topic: string | RegExp, bufferSize?: number, expiryPeriod?: WaitPeriod): Stream<{
49
+ subscribe(topic: string | RegExp, bufferSize?: number, expiryPeriod?: Duration): Stream<{
49
50
  topic: string;
50
51
  data: T;
51
52
  }>;
package/data/bus.mjs CHANGED
@@ -35,7 +35,7 @@ class DataBus extends Observable {
35
35
  * @param bufferSize
36
36
  * @param expiryPeriod
37
37
  */
38
- republish(topic, consumer, topicTransformer = (v) => v, bufferSize = undefined, expiryPeriod = undefined) {
38
+ republish(topic, consumer, topicTransformer = (v) => v, bufferSize = undefined, expiryPeriod) {
39
39
  return this.subscribe(topic, bufferSize, expiryPeriod)
40
40
  .map(({ topic: consumerTopic, data }) => this.publish(`${consumer}.${topicTransformer(consumerTopic)}`, data))
41
41
  .consume()
@@ -1,5 +1,5 @@
1
1
  import { Model } from "./model";
2
2
  type UnionParserOptions = "json" | "string" | "blob" | "buffer";
3
3
  export declare const View: (modelClass: typeof Model, path: Array<string | number>, option: UnionParserOptions) => (data: any, ...otherArgs: any[]) => any;
4
- export declare const Union: (modelClasses: Array<typeof Model>, option: UnionParserOptions) => (data: any, ...otherArgs: any[]) => Model | Model[] | Promise<Model | Model[]>;
4
+ export declare const Union: (modelClasses: Array<typeof Model>, option: UnionParserOptions) => (data: any, ...otherArgs: any[]) => Model | Promise<Model>;
5
5
  export {};
package/data/signal.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { Stream } from "../stream";
2
- import { WaitPeriod } from "../future";
3
- import { Queue } from "../data-structures/object";
2
+ import { Duration, Queue } from "../data-structures/object";
4
3
  export interface Token {
5
4
  cancel: () => void;
6
5
  }
@@ -13,5 +12,5 @@ export declare class Signal<T> {
13
12
  start(): void;
14
13
  stop(): void;
15
14
  subscribe(callback: (value: T) => void): Token;
16
- stream(objectMaxSize?: number, expiryPeriod?: WaitPeriod): Stream<T>;
15
+ stream(objectMaxSize?: number, expiryPeriod?: Duration): Stream<T>;
17
16
  }
@@ -6,6 +6,7 @@ require('object-hash');
6
6
  require('ramda');
7
7
  var stream = require('../../stream.cjs');
8
8
  var future = require('../../future/future.cjs');
9
+ var Time = require('../../data-structures/object/Time.cjs');
9
10
  require('uuid');
10
11
  var Pair = require('../../data-structures/array/Pair.cjs');
11
12
  var model = require('../model/model.cjs');
@@ -73,12 +74,12 @@ class Cache {
73
74
  .thenApply(({ value, signal }) => _tslib.__awaiter(this, void 0, void 0, function* () {
74
75
  if (value) {
75
76
  if (this.expiryPeriod) {
76
- const remainingTime = Date.now() - value.lastModifiedOn - future.calculatePeriodValue(this.expiryPeriod);
77
+ const remainingTime = Date.now() - value.lastModifiedOn - this.expiryPeriod.inWholeMilliseconds();
77
78
  if (remainingTime <= 0) {
78
79
  yield this.persistStore.remove(transformedKey).registerSignal(signal);
79
80
  return;
80
81
  }
81
- this.memStore.set(key, value.value, { milliseconds: remainingTime });
82
+ this.memStore.set(key, value.value, Time.milliseconds(remainingTime));
82
83
  }
83
84
  else {
84
85
  this.memStore.set(key, value.value);
@@ -1,4 +1,5 @@
1
- import { Future, WaitPeriod } from "../../future";
1
+ import { Duration } from "../../data-structures/object";
2
+ import { Future } from "../../future";
2
3
  import { Pair } from "../../data-structures/array";
3
4
  import { Stream } from "../../stream";
4
5
  export interface PersistenceStore {
@@ -17,7 +18,7 @@ export declare class Cache<T, K> implements AsyncIterable<Pair<T, K>> {
17
18
  private readonly encode;
18
19
  private readonly persistStore?;
19
20
  private readonly expiryPeriod?;
20
- constructor(decode: (v: string) => K, encode: (v: K) => string | Future<string> | Promise<string>, persistStore?: PersistenceStore, maxSize?: number, expiryPeriod?: WaitPeriod);
21
+ constructor(decode: (v: string) => K, encode: (v: K) => string | Future<string> | Promise<string>, persistStore?: PersistenceStore, maxSize?: number, expiryPeriod?: Duration);
21
22
  private readonly memStore;
22
23
  get(key: T): Future<K | undefined>;
23
24
  getSync(key: T): K;
@@ -37,8 +38,8 @@ export declare class CacheManager {
37
38
  private readonly maxSize?;
38
39
  private readonly expiryPeriod?;
39
40
  private readonly memStores;
40
- constructor(persistStore?: PersistenceStore, maxSize?: number, expiryPeriod?: WaitPeriod);
41
- createCache<T, K>(name: string, persist: boolean, encoder?: (v: K) => string | Future<string> | Promise<string>, decoder?: (v: string) => K, maxSize?: number, expiryPeriod?: WaitPeriod): Cache<T, K>;
41
+ constructor(persistStore?: PersistenceStore, maxSize?: number, expiryPeriod?: Duration);
42
+ createCache<T, K>(name: string, persist: boolean, encoder?: (v: K) => string | Future<string> | Promise<string>, decoder?: (v: string) => K, maxSize?: number, expiryPeriod?: Duration): Cache<T, K>;
42
43
  getCache<T, K>(name: string): Cache<T, K>;
43
44
  removeCache(name: string): void;
44
45
  }
@@ -3,7 +3,8 @@ import { WatchableObject } from '../../data-structures/object/WatchableObject.mj
3
3
  import 'object-hash';
4
4
  import 'ramda';
5
5
  import { Stream } from '../../stream.mjs';
6
- import { Future, calculatePeriodValue } from '../../future/future.mjs';
6
+ import { Future } from '../../future/future.mjs';
7
+ import { milliseconds } from '../../data-structures/object/Time.mjs';
7
8
  import 'uuid';
8
9
  import { pair } from '../../data-structures/array/Pair.mjs';
9
10
  import { Model } from '../model/model.mjs';
@@ -71,12 +72,12 @@ class Cache {
71
72
  .thenApply(({ value, signal }) => __awaiter(this, void 0, void 0, function* () {
72
73
  if (value) {
73
74
  if (this.expiryPeriod) {
74
- const remainingTime = Date.now() - value.lastModifiedOn - calculatePeriodValue(this.expiryPeriod);
75
+ const remainingTime = Date.now() - value.lastModifiedOn - this.expiryPeriod.inWholeMilliseconds();
75
76
  if (remainingTime <= 0) {
76
77
  yield this.persistStore.remove(transformedKey).registerSignal(signal);
77
78
  return;
78
79
  }
79
- this.memStore.set(key, value.value, { milliseconds: remainingTime });
80
+ this.memStore.set(key, value.value, milliseconds(remainingTime));
80
81
  }
81
82
  else {
82
83
  this.memStore.set(key, value.value);
@@ -1,6 +1,7 @@
1
1
  import { WatchableObject } from "./WatchableObject";
2
- import { Future, WaitPeriod } from "../../future";
2
+ import { Future } from "../../future";
3
3
  import { Stream } from "../../stream";
4
+ import { Duration } from "./Time";
4
5
  /**
5
6
  * Queue data structure that is iterable, but never can dequeue
6
7
  */
@@ -8,7 +9,7 @@ export declare class BufferQueue<T> extends WatchableObject<number, T> {
8
9
  private tail;
9
10
  private head;
10
11
  private readonly tracker;
11
- constructor(objectMaxSize?: number, expiryPeriod?: WaitPeriod);
12
+ constructor(objectMaxSize?: number, expiryPeriod?: Duration);
12
13
  ingest(stream: Stream<T>): Future<void>;
13
14
  streamEntries(ignoreCache?: boolean): Stream<T>;
14
15
  flushableStream(ignoreCache?: boolean): Stream<{
@@ -1,7 +1,8 @@
1
1
  import { WatchableObject } from "./WatchableObject";
2
- import { Future, WaitPeriod } from "../../future";
2
+ import { Future } from "../../future";
3
3
  import { Stream } from "../../stream";
4
4
  import { Publisher } from "./Publisher";
5
+ import { Duration } from "./Time";
5
6
  /**
6
7
  * Queue data structure for First In First Out operation (FIFO)
7
8
  */
@@ -11,7 +12,7 @@ export declare class Queue<T> extends WatchableObject<number, T> implements Iter
11
12
  private closedSignal;
12
13
  private closed;
13
14
  private readonly dequeueEvt;
14
- constructor(objectMaxSize?: number, expiryPeriod?: WaitPeriod);
15
+ constructor(objectMaxSize?: number, expiryPeriod?: Duration);
15
16
  get streamEntries(): Stream<T>;
16
17
  /**
17
18
  * A stream where data is only removed from the queue once the terminal action of the stream is finished with the data.
@@ -0,0 +1,195 @@
1
+ 'use strict';
2
+
3
+ const milliseconds = function (value) {
4
+ return new Duration(value);
5
+ };
6
+ const seconds = function (value) {
7
+ return new Duration(value * 1000);
8
+ };
9
+ const minutes = function (value) {
10
+ return new Duration(value * 60000);
11
+ };
12
+ const hours = function (value) {
13
+ return new Duration(value * 3.6e6);
14
+ };
15
+ const days = function (value) {
16
+ return new Duration(value * 8.64e7);
17
+ };
18
+ const months = function (value) {
19
+ return new Duration(value * 2.6298e9);
20
+ };
21
+ const years = function (value) {
22
+ return new Duration(value * 3.15576e10);
23
+ };
24
+ class Duration {
25
+ constructor(valueInMilli) {
26
+ this.valueInMilli = valueInMilli;
27
+ }
28
+ get isPositive() {
29
+ return this.valueInMilli > 0;
30
+ }
31
+ get isNegative() {
32
+ return this.valueInMilli < 0;
33
+ }
34
+ get days() {
35
+ return this.inWholeDays();
36
+ }
37
+ get hours() {
38
+ return this.inWholeHours();
39
+ }
40
+ get minutes() {
41
+ return this.inWholeMinutes();
42
+ }
43
+ get seconds() {
44
+ return this.inWholeSeconds();
45
+ }
46
+ get milliseconds() {
47
+ return this.inWholeMilliseconds();
48
+ }
49
+ inWholeDays() {
50
+ return Math.trunc(this.valueInMilli / 8.64e7);
51
+ }
52
+ inWholeHours() {
53
+ return Math.trunc(this.valueInMilli / 3.6e6);
54
+ }
55
+ inWholeMinutes() {
56
+ return Math.trunc(this.valueInMilli / 60000);
57
+ }
58
+ inWholeSeconds() {
59
+ return Math.trunc(this.valueInMilli / 1000);
60
+ }
61
+ inWholeMilliseconds() {
62
+ return Math.trunc(this.valueInMilli);
63
+ }
64
+ plus(other) {
65
+ return new Duration(this.valueInMilli + other.valueInMilli);
66
+ }
67
+ minus(other) {
68
+ return new Duration(this.valueInMilli - other.valueInMilli);
69
+ }
70
+ times(other) {
71
+ return new Duration(this.valueInMilli * (other instanceof Duration ? other.valueInMilli : other));
72
+ }
73
+ div(other) {
74
+ return new Duration(this.valueInMilli / (other instanceof Duration ? other.valueInMilli : other));
75
+ }
76
+ equals(other) {
77
+ return other.valueInMilli === this.valueInMilli;
78
+ }
79
+ gt(other) {
80
+ return this.valueInMilli > other.valueInMilli;
81
+ }
82
+ gte(other) {
83
+ return this.valueInMilli >= other.valueInMilli;
84
+ }
85
+ lt(other) {
86
+ return this.valueInMilli < other.valueInMilli;
87
+ }
88
+ lte(other) {
89
+ return this.valueInMilli <= other.valueInMilli;
90
+ }
91
+ valueOf() {
92
+ return this.inWholeMilliseconds();
93
+ }
94
+ toString() {
95
+ let value = this.valueInMilli;
96
+ const years = Math.trunc(value / 3.15576e10);
97
+ value %= 3.15576e10;
98
+ const months = Math.trunc(value / 2.6298e9);
99
+ value %= 2.6298e9;
100
+ const days = Math.trunc(value / 8.64e7);
101
+ value %= 8.64e7;
102
+ const hours = Math.trunc(value / 3.6e6);
103
+ value %= 3.6e6;
104
+ const minutes = Math.trunc(value / 60000);
105
+ value %= 60000;
106
+ const seconds = value / 1000;
107
+ let result = "P";
108
+ if (years)
109
+ result += `${years}Y`;
110
+ if (months)
111
+ result += `${months}M`;
112
+ if (days) {
113
+ if (years && !months) {
114
+ result += `0M${days}D`;
115
+ }
116
+ else {
117
+ result += `${days}D`;
118
+ }
119
+ }
120
+ result += `T`;
121
+ if (hours)
122
+ result += `${hours}H`;
123
+ if (minutes)
124
+ result += `${minutes}M`;
125
+ if (seconds) {
126
+ if (hours && !minutes) {
127
+ result += `0M${seconds}S`;
128
+ }
129
+ }
130
+ return result;
131
+ }
132
+ }
133
+ class Instant {
134
+ constructor(epochMilliValue) {
135
+ this.epochMilliValue = epochMilliValue;
136
+ }
137
+ static get DISTANT_FUTURE() {
138
+ return Instant.now().plus(years(100000));
139
+ }
140
+ static get DISTANT_PAST() {
141
+ return Instant.now().minus(years(100000));
142
+ }
143
+ static now() {
144
+ return new Instant(new Date().getTime());
145
+ }
146
+ get isDistantFuture() {
147
+ return this.minus(Instant.now()).gte(years(100000));
148
+ }
149
+ get isDistantPast() {
150
+ return Instant.now().minus(this).gte(years(100000));
151
+ }
152
+ get epochSeconds() {
153
+ return this.toEpochSeconds();
154
+ }
155
+ get epochMilliseconds() {
156
+ return this.toEpochMilliseconds();
157
+ }
158
+ get duration() {
159
+ return milliseconds(this.epochMilliValue);
160
+ }
161
+ plus(other) {
162
+ return new Instant(this.epochMilliValue + other.milliseconds);
163
+ }
164
+ minus(other) {
165
+ if (other instanceof Instant) {
166
+ return this.duration.minus(other.duration);
167
+ }
168
+ return new Instant(this.epochMilliValue - other.milliseconds);
169
+ }
170
+ toEpochMilliseconds() {
171
+ return this.epochMilliValue;
172
+ }
173
+ toEpochSeconds() {
174
+ return Math.trunc(this.epochMilliValue / 1000);
175
+ }
176
+ toString() {
177
+ return this.toDate().toISOString();
178
+ }
179
+ toDate() {
180
+ return new Date(this.epochMilliValue);
181
+ }
182
+ equals(other) {
183
+ return this.epochMilliValue === other.epochMilliValue;
184
+ }
185
+ }
186
+
187
+ exports.Duration = Duration;
188
+ exports.Instant = Instant;
189
+ exports.days = days;
190
+ exports.hours = hours;
191
+ exports.milliseconds = milliseconds;
192
+ exports.minutes = minutes;
193
+ exports.months = months;
194
+ exports.seconds = seconds;
195
+ exports.years = years;
@@ -0,0 +1,56 @@
1
+ export declare const milliseconds: (value: number) => Duration;
2
+ export declare const seconds: (value: number) => Duration;
3
+ export declare const minutes: (value: number) => Duration;
4
+ export declare const hours: (value: number) => Duration;
5
+ export declare const days: (value: number) => Duration;
6
+ export declare const months: (value: number) => Duration;
7
+ export declare const years: (value: number) => Duration;
8
+ export declare class Duration {
9
+ private readonly valueInMilli;
10
+ constructor(valueInMilli: number);
11
+ get isPositive(): boolean;
12
+ get isNegative(): boolean;
13
+ get days(): number;
14
+ get hours(): number;
15
+ get minutes(): number;
16
+ get seconds(): number;
17
+ get milliseconds(): number;
18
+ inWholeDays(): number;
19
+ inWholeHours(): number;
20
+ inWholeMinutes(): number;
21
+ inWholeSeconds(): number;
22
+ inWholeMilliseconds(): number;
23
+ plus(other: Duration): Duration;
24
+ minus(other: Duration): Duration;
25
+ times(other: Duration): Duration;
26
+ times(other: number): Duration;
27
+ div(other: Duration): Duration;
28
+ div(other: number): Duration;
29
+ equals(other: Duration): boolean;
30
+ gt(other: Duration): boolean;
31
+ gte(other: Duration): boolean;
32
+ lt(other: Duration): boolean;
33
+ lte(other: Duration): boolean;
34
+ valueOf(): number;
35
+ toString(): string;
36
+ }
37
+ export declare class Instant {
38
+ private readonly epochMilliValue;
39
+ constructor(epochMilliValue: number);
40
+ static get DISTANT_FUTURE(): Instant;
41
+ static get DISTANT_PAST(): Instant;
42
+ static now(): Instant;
43
+ get isDistantFuture(): boolean;
44
+ get isDistantPast(): boolean;
45
+ get epochSeconds(): number;
46
+ get epochMilliseconds(): number;
47
+ get duration(): Duration;
48
+ plus(other: Duration): Instant;
49
+ minus(other: Instant): Duration;
50
+ minus(other: Duration): Instant;
51
+ toEpochMilliseconds(): number;
52
+ toEpochSeconds(): number;
53
+ toString(): string;
54
+ toDate(): Date;
55
+ equals(other: Instant): boolean;
56
+ }
@@ -0,0 +1,185 @@
1
+ const milliseconds = function (value) {
2
+ return new Duration(value);
3
+ };
4
+ const seconds = function (value) {
5
+ return new Duration(value * 1000);
6
+ };
7
+ const minutes = function (value) {
8
+ return new Duration(value * 60000);
9
+ };
10
+ const hours = function (value) {
11
+ return new Duration(value * 3.6e6);
12
+ };
13
+ const days = function (value) {
14
+ return new Duration(value * 8.64e7);
15
+ };
16
+ const months = function (value) {
17
+ return new Duration(value * 2.6298e9);
18
+ };
19
+ const years = function (value) {
20
+ return new Duration(value * 3.15576e10);
21
+ };
22
+ class Duration {
23
+ constructor(valueInMilli) {
24
+ this.valueInMilli = valueInMilli;
25
+ }
26
+ get isPositive() {
27
+ return this.valueInMilli > 0;
28
+ }
29
+ get isNegative() {
30
+ return this.valueInMilli < 0;
31
+ }
32
+ get days() {
33
+ return this.inWholeDays();
34
+ }
35
+ get hours() {
36
+ return this.inWholeHours();
37
+ }
38
+ get minutes() {
39
+ return this.inWholeMinutes();
40
+ }
41
+ get seconds() {
42
+ return this.inWholeSeconds();
43
+ }
44
+ get milliseconds() {
45
+ return this.inWholeMilliseconds();
46
+ }
47
+ inWholeDays() {
48
+ return Math.trunc(this.valueInMilli / 8.64e7);
49
+ }
50
+ inWholeHours() {
51
+ return Math.trunc(this.valueInMilli / 3.6e6);
52
+ }
53
+ inWholeMinutes() {
54
+ return Math.trunc(this.valueInMilli / 60000);
55
+ }
56
+ inWholeSeconds() {
57
+ return Math.trunc(this.valueInMilli / 1000);
58
+ }
59
+ inWholeMilliseconds() {
60
+ return Math.trunc(this.valueInMilli);
61
+ }
62
+ plus(other) {
63
+ return new Duration(this.valueInMilli + other.valueInMilli);
64
+ }
65
+ minus(other) {
66
+ return new Duration(this.valueInMilli - other.valueInMilli);
67
+ }
68
+ times(other) {
69
+ return new Duration(this.valueInMilli * (other instanceof Duration ? other.valueInMilli : other));
70
+ }
71
+ div(other) {
72
+ return new Duration(this.valueInMilli / (other instanceof Duration ? other.valueInMilli : other));
73
+ }
74
+ equals(other) {
75
+ return other.valueInMilli === this.valueInMilli;
76
+ }
77
+ gt(other) {
78
+ return this.valueInMilli > other.valueInMilli;
79
+ }
80
+ gte(other) {
81
+ return this.valueInMilli >= other.valueInMilli;
82
+ }
83
+ lt(other) {
84
+ return this.valueInMilli < other.valueInMilli;
85
+ }
86
+ lte(other) {
87
+ return this.valueInMilli <= other.valueInMilli;
88
+ }
89
+ valueOf() {
90
+ return this.inWholeMilliseconds();
91
+ }
92
+ toString() {
93
+ let value = this.valueInMilli;
94
+ const years = Math.trunc(value / 3.15576e10);
95
+ value %= 3.15576e10;
96
+ const months = Math.trunc(value / 2.6298e9);
97
+ value %= 2.6298e9;
98
+ const days = Math.trunc(value / 8.64e7);
99
+ value %= 8.64e7;
100
+ const hours = Math.trunc(value / 3.6e6);
101
+ value %= 3.6e6;
102
+ const minutes = Math.trunc(value / 60000);
103
+ value %= 60000;
104
+ const seconds = value / 1000;
105
+ let result = "P";
106
+ if (years)
107
+ result += `${years}Y`;
108
+ if (months)
109
+ result += `${months}M`;
110
+ if (days) {
111
+ if (years && !months) {
112
+ result += `0M${days}D`;
113
+ }
114
+ else {
115
+ result += `${days}D`;
116
+ }
117
+ }
118
+ result += `T`;
119
+ if (hours)
120
+ result += `${hours}H`;
121
+ if (minutes)
122
+ result += `${minutes}M`;
123
+ if (seconds) {
124
+ if (hours && !minutes) {
125
+ result += `0M${seconds}S`;
126
+ }
127
+ }
128
+ return result;
129
+ }
130
+ }
131
+ class Instant {
132
+ constructor(epochMilliValue) {
133
+ this.epochMilliValue = epochMilliValue;
134
+ }
135
+ static get DISTANT_FUTURE() {
136
+ return Instant.now().plus(years(100000));
137
+ }
138
+ static get DISTANT_PAST() {
139
+ return Instant.now().minus(years(100000));
140
+ }
141
+ static now() {
142
+ return new Instant(new Date().getTime());
143
+ }
144
+ get isDistantFuture() {
145
+ return this.minus(Instant.now()).gte(years(100000));
146
+ }
147
+ get isDistantPast() {
148
+ return Instant.now().minus(this).gte(years(100000));
149
+ }
150
+ get epochSeconds() {
151
+ return this.toEpochSeconds();
152
+ }
153
+ get epochMilliseconds() {
154
+ return this.toEpochMilliseconds();
155
+ }
156
+ get duration() {
157
+ return milliseconds(this.epochMilliValue);
158
+ }
159
+ plus(other) {
160
+ return new Instant(this.epochMilliValue + other.milliseconds);
161
+ }
162
+ minus(other) {
163
+ if (other instanceof Instant) {
164
+ return this.duration.minus(other.duration);
165
+ }
166
+ return new Instant(this.epochMilliValue - other.milliseconds);
167
+ }
168
+ toEpochMilliseconds() {
169
+ return this.epochMilliValue;
170
+ }
171
+ toEpochSeconds() {
172
+ return Math.trunc(this.epochMilliValue / 1000);
173
+ }
174
+ toString() {
175
+ return this.toDate().toISOString();
176
+ }
177
+ toDate() {
178
+ return new Date(this.epochMilliValue);
179
+ }
180
+ equals(other) {
181
+ return this.epochMilliValue === other.epochMilliValue;
182
+ }
183
+ }
184
+
185
+ export { Duration, Instant, days, hours, milliseconds, minutes, months, seconds, years };
@@ -5,6 +5,7 @@ var CyclicalObject = require('./CyclicalObject.cjs');
5
5
  var future = require('../../future/future.cjs');
6
6
  require('ramda');
7
7
  require('../../stream.cjs');
8
+ var Time = require('./Time.cjs');
8
9
 
9
10
  /**
10
11
  * Object that has key value pairs that can expire
@@ -28,7 +29,7 @@ class TimeableObject extends CyclicalObject.CyclicalObject {
28
29
  timer === null || timer === void 0 ? void 0 : timer.cancel();
29
30
  super.set(key, value);
30
31
  if (expiryPeriod !== null && expiryPeriod !== void 0 ? expiryPeriod : this.expiryPeriod) {
31
- this.timers.set(hashedKey, future.Future.sleep((_a = expiryPeriod !== null && expiryPeriod !== void 0 ? expiryPeriod : this.expiryPeriod) !== null && _a !== void 0 ? _a : 0)
32
+ this.timers.set(hashedKey, future.Future.sleep((_a = expiryPeriod !== null && expiryPeriod !== void 0 ? expiryPeriod : this.expiryPeriod) !== null && _a !== void 0 ? _a : Time.seconds(0))
32
33
  .thenApply(() => this.delete(key))
33
34
  .schedule());
34
35
  }
@@ -41,7 +42,7 @@ class TimeableObject extends CyclicalObject.CyclicalObject {
41
42
  const hashedKey = this.computeHash(key);
42
43
  const timer = this.timers.get(hashedKey);
43
44
  timer === null || timer === void 0 ? void 0 : timer.cancel();
44
- this.timers.set(hashedKey, future.Future.sleep((_a = this.expiryPeriod) !== null && _a !== void 0 ? _a : 0)
45
+ this.timers.set(hashedKey, future.Future.sleep((_a = this.expiryPeriod) !== null && _a !== void 0 ? _a : Time.seconds(0))
45
46
  .thenApply(() => this.delete(key))
46
47
  .schedule());
47
48
  }
@@ -1,5 +1,5 @@
1
1
  import { CyclicalObject } from "./CyclicalObject";
2
- import { WaitPeriod } from "../../future";
2
+ import { Duration } from "./Time";
3
3
  /**
4
4
  * Object that has key value pairs that can expire
5
5
  */
@@ -8,20 +8,20 @@ export declare class TimeableObject<T, K> extends CyclicalObject<T, K> {
8
8
  * default expiration time for each added key value pair
9
9
  * @private
10
10
  */
11
- protected expiryPeriod?: WaitPeriod;
11
+ protected expiryPeriod?: Duration;
12
12
  /**
13
13
  * mapping of timings for each key value pair
14
14
  * @private
15
15
  */
16
16
  private readonly timers;
17
- constructor(objectMaxSize?: number, expiryPeriod?: WaitPeriod);
17
+ constructor(objectMaxSize?: number, expiryPeriod?: Duration);
18
18
  /**
19
19
  * Sets the value for the given key
20
20
  * @param key
21
21
  * @param value dat to be stored
22
22
  * @param expiryPeriod When should this value expire
23
23
  */
24
- set(key: T, value: K, expiryPeriod?: WaitPeriod): void;
24
+ set(key: T, value: K, expiryPeriod?: Duration): void;
25
25
  get(key: T, defaultValue?: K | undefined): K | undefined;
26
26
  delete(key: T): void;
27
27
  clear(): void;