@develia/commons 0.4.22 → 0.4.23

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 (37) hide show
  1. package/dist/cjs/collections/cache-dictionary.js +2 -6
  2. package/dist/cjs/collections/cache-dictionary.js.map +1 -1
  3. package/dist/cjs/index.js +4 -1
  4. package/dist/cjs/index.js.map +1 -1
  5. package/dist/cjs/result.js +3 -0
  6. package/dist/cjs/result.js.map +1 -0
  7. package/dist/cjs/triggers.js +103 -0
  8. package/dist/cjs/triggers.js.map +1 -0
  9. package/dist/esm/collections/cache-dictionary.js +2 -6
  10. package/dist/esm/collections/cache-dictionary.js.map +1 -1
  11. package/dist/esm/index.js +1 -0
  12. package/dist/esm/index.js.map +1 -1
  13. package/dist/esm/result.js +2 -0
  14. package/dist/esm/result.js.map +1 -0
  15. package/dist/esm/triggers.js +98 -0
  16. package/dist/esm/triggers.js.map +1 -0
  17. package/dist/types/collections/cache-dictionary.d.ts.map +1 -1
  18. package/dist/types/index.d.ts +2 -0
  19. package/dist/types/index.d.ts.map +1 -1
  20. package/dist/types/result.d.ts +11 -0
  21. package/dist/types/result.d.ts.map +1 -0
  22. package/dist/types/triggers.d.ts +64 -0
  23. package/dist/types/triggers.d.ts.map +1 -0
  24. package/dist/web/collections/cache-dictionary.js +2 -6
  25. package/dist/web/collections/cache-dictionary.js.map +1 -1
  26. package/dist/web/index.js +1 -0
  27. package/dist/web/index.js.map +1 -1
  28. package/dist/web/result.js +2 -0
  29. package/dist/web/result.js.map +1 -0
  30. package/dist/web/triggers.js +99 -0
  31. package/dist/web/triggers.js.map +1 -0
  32. package/package.json +3 -4
  33. package/src/collections/cache-dictionary.ts +2 -6
  34. package/src/index.ts +2 -0
  35. package/src/result.ts +6 -0
  36. package/src/triggers.ts +126 -0
  37. package/dist/cjs/package.json +0 -3
@@ -41,9 +41,7 @@ class CacheDictionary {
41
41
  delete this.expiration[key];
42
42
  }
43
43
  else {
44
- const expiration = new Date();
45
- expiration.setSeconds(expiration.getSeconds() + duration);
46
- this.expiration[key] = expiration;
44
+ this.expiration[key] = this.calculateExpiration(duration);
47
45
  }
48
46
  }
49
47
  /**
@@ -97,9 +95,7 @@ class CacheDictionary {
97
95
  if (duration === null) {
98
96
  return undefined;
99
97
  }
100
- const expiration = new Date();
101
- expiration.setSeconds(expiration.getSeconds() + duration);
102
- return expiration;
98
+ return new Date(Date.now() + duration * 1000);
103
99
  }
104
100
  }
105
101
  exports.default = CacheDictionary;
@@ -1 +1 @@
1
- {"version":3,"file":"cache-dictionary.js","sourceRoot":"","sources":["../../../src/collections/cache-dictionary.ts"],"names":[],"mappings":";;AAAA;;;GAGG;AACH,MAAqB,eAAe;IAMhC;;;OAGG;IACH,YAAY,WAAwC,IAAI,EAAE,kBAAiC,IAAI;QAC3F,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACjC,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC;QACvC,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC1C,CAAC;IAED;;;;OAIG;IACI,aAAa,CAAC,GAAW;QAC5B,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;IAChC,CAAC;IAED;;;;OAIG;IACI,aAAa,CAAC,GAAW,EAAE,UAA4B;QAC1D,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,UAAU,CAAC;IACtC,CAAC;IAED;;;;OAIG;IACI,WAAW,CAAC,GAAW,EAAE,QAAuB;QACnD,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;YACpB,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;QAChC,CAAC;aAAM,CAAC;YACJ,MAAM,UAAU,GAAG,IAAI,IAAI,EAAE,CAAC;YAC9B,UAAU,CAAC,UAAU,CAAC,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QACzB,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;QACxC,IAAI,UAAU,YAAY,IAAI,EAAE,CAAC;YAC7B,OAAO,UAAU,GAAG,IAAI,IAAI,EAAE,CAAC;QACnC,CAAC;QACD,OAAO,KAAK,CAAC;IACjB,CAAC;IAEO,mBAAmB,CAAC,QAAuB;QAC/C,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;YACpB,OAAO,SAAS,CAAC;QACrB,CAAC;QAED,MAAM,UAAU,GAAG,IAAI,IAAI,EAAE,CAAC;QAC9B,UAAU,CAAC,UAAU,CAAC,UAAU,CAAC,UAAU,EAAE,GAAG,QAAQ,CAAC,CAAC;QAC1D,OAAO,UAAU,CAAC;IACtB,CAAC;CACJ;AA/GD,kCA+GC","sourcesContent":["/**\n * A dictionary-like structure with caching and expiration capabilities.\n * @template T - The type of the values stored in the cache.\n */\nexport default class CacheDictionary<T> {\n private readonly fallback: ((key: string) => T) | null;\n private readonly cache: { [key: string]: T };\n private readonly defaultDuration: number | null;\n private readonly expiration: { [key: string]: Date | undefined };\n\n /**\n * @param {((key: string) => T) | null} fallback - Optional function to call when a key is not found.\n * @param {number | null} defaultDuration - Default duration in seconds for cache items.\n */\n constructor(fallback: ((key: string) => T) | null = null, defaultDuration: number | null = null) {\n this.fallback = fallback;\n this.cache = Object.create(null);\n this.defaultDuration = defaultDuration;\n this.expiration = Object.create(null);\n }\n\n /**\n * Gets the expiration date for a specific key.\n * @param {string} key - The key.\n * @returns {Date | undefined} The expiration date or undefined.\n */\n public getExpiration(key: string): Date | undefined {\n return this.expiration[key];\n }\n\n /**\n * Sets a specific expiration date for a key.\n * @param {string} key - The key.\n * @param {Date | undefined} expiration - The expiration date.\n */\n public setExpiration(key: string, expiration: Date | undefined): void {\n this.expiration[key] = expiration;\n }\n\n /**\n * Sets a duration in seconds for a key's expiration.\n * @param {string} key - The key.\n * @param {number | null} duration - Duration in seconds. If null, expiration is removed.\n */\n public setDuration(key: string, duration: number | null): void {\n if (duration === null) {\n delete this.expiration[key];\n } else {\n const expiration = new Date();\n expiration.setSeconds(expiration.getSeconds() + duration);\n this.expiration[key] = expiration;\n }\n }\n\n /**\n * Gets a value from the cache. If not found and a fallback exists, it uses the fallback.\n * If the item is expired, it's removed and null is returned.\n * @param {string} key - The key to retrieve.\n * @returns {T | null} The value or null if not found/expired.\n */\n public get(key: string): T | null {\n if (!Object.prototype.hasOwnProperty.call(this.cache, key)) {\n if (this.fallback) {\n this.cache[key] = this.fallback(key);\n this.expiration[key] = this.calculateExpiration(this.defaultDuration);\n } else {\n return null;\n }\n }\n\n if (this.isExpired(key)) {\n this.remove(key);\n return null;\n }\n\n return this.cache[key];\n }\n\n /**\n * Sets a value in the cache with an optional duration.\n * @param {string} key - The key.\n * @param {T} value - The value to store.\n * @param {number | null} duration - Duration in seconds.\n */\n public set(key: string, value: T, duration: number | null = null): void {\n this.cache[key] = value;\n this.expiration[key] = this.calculateExpiration(duration);\n }\n\n /**\n * Removes an item from the cache.\n * @param {string} key - The key to remove.\n */\n public remove(key: string): void {\n delete this.cache[key];\n delete this.expiration[key];\n }\n\n private isExpired(key: string): boolean {\n const expiration = this.expiration[key];\n if (expiration instanceof Date) {\n return expiration < new Date();\n }\n return false;\n }\n\n private calculateExpiration(duration: number | null): Date | undefined {\n if (duration === null) {\n return undefined;\n }\n\n const expiration = new Date();\n expiration.setSeconds(expiration.getSeconds() + duration);\n return expiration;\n }\n}"]}
1
+ {"version":3,"file":"cache-dictionary.js","sourceRoot":"","sources":["../../../src/collections/cache-dictionary.ts"],"names":[],"mappings":";;AAAA;;;GAGG;AACH,MAAqB,eAAe;IAMhC;;;OAGG;IACH,YAAY,WAAwC,IAAI,EAAE,kBAAiC,IAAI;QAC3F,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACjC,IAAI,CAAC,eAAe,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C;QAED,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC3B,CAAC;IAED;;;;;OAKG;IACI,GAAG,CAAC,GAAW,EAAE,KAAQ,EAAE,WAA0B,IAAI;QAC5D,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;QACxB,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,mBAAmB,CAAC,QAAQ,CAAC,CAAC;IAC9D,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,GAAW;QACrB,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACvB,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;IAChC,CAAC;IAEO,SAAS,CAAC,GAAW;QACzB,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;QACxC,IAAI,UAAU,YAAY,IAAI,EAAE,CAAC;YAC7B,OAAO,UAAU,GAAG,IAAI,IAAI,EAAE,CAAC;QACnC,CAAC;QACD,OAAO,KAAK,CAAC;IACjB,CAAC;IAEO,mBAAmB,CAAC,QAAuB;QAC/C,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;YACpB,OAAO,SAAS,CAAC;QACrB,CAAC;QAED,OAAO,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,QAAQ,GAAG,IAAI,CAAC,CAAC;IAClD,CAAC;CACJ;AA3GD,kCA2GC","sourcesContent":["/**\n * A dictionary-like structure with caching and expiration capabilities.\n * @template T - The type of the values stored in the cache.\n */\nexport default class CacheDictionary<T> {\n private readonly fallback: ((key: string) => T) | null;\n private readonly cache: { [key: string]: T };\n private readonly defaultDuration: number | null;\n private readonly expiration: { [key: string]: Date | undefined };\n\n /**\n * @param {((key: string) => T) | null} fallback - Optional function to call when a key is not found.\n * @param {number | null} defaultDuration - Default duration in seconds for cache items.\n */\n constructor(fallback: ((key: string) => T) | null = null, defaultDuration: number | null = null) {\n this.fallback = fallback;\n this.cache = Object.create(null);\n this.defaultDuration = defaultDuration;\n this.expiration = Object.create(null);\n }\n\n /**\n * Gets the expiration date for a specific key.\n * @param {string} key - The key.\n * @returns {Date | undefined} The expiration date or undefined.\n */\n public getExpiration(key: string): Date | undefined {\n return this.expiration[key];\n }\n\n /**\n * Sets a specific expiration date for a key.\n * @param {string} key - The key.\n * @param {Date | undefined} expiration - The expiration date.\n */\n public setExpiration(key: string, expiration: Date | undefined): void {\n this.expiration[key] = expiration;\n }\n\n /**\n * Sets a duration in seconds for a key's expiration.\n * @param {string} key - The key.\n * @param {number | null} duration - Duration in seconds. If null, expiration is removed.\n */\n public setDuration(key: string, duration: number | null): void {\n if (duration === null) {\n delete this.expiration[key];\n } else {\n this.expiration[key] = this.calculateExpiration(duration);\n }\n }\n\n /**\n * Gets a value from the cache. If not found and a fallback exists, it uses the fallback.\n * If the item is expired, it's removed and null is returned.\n * @param {string} key - The key to retrieve.\n * @returns {T | null} The value or null if not found/expired.\n */\n public get(key: string): T | null {\n if (!Object.prototype.hasOwnProperty.call(this.cache, key)) {\n if (this.fallback) {\n this.cache[key] = this.fallback(key);\n this.expiration[key] = this.calculateExpiration(this.defaultDuration);\n } else {\n return null;\n }\n }\n\n if (this.isExpired(key)) {\n this.remove(key);\n return null;\n }\n\n return this.cache[key];\n }\n\n /**\n * Sets a value in the cache with an optional duration.\n * @param {string} key - The key.\n * @param {T} value - The value to store.\n * @param {number | null} duration - Duration in seconds.\n */\n public set(key: string, value: T, duration: number | null = null): void {\n this.cache[key] = value;\n this.expiration[key] = this.calculateExpiration(duration);\n }\n\n /**\n * Removes an item from the cache.\n * @param {string} key - The key to remove.\n */\n public remove(key: string): void {\n delete this.cache[key];\n delete this.expiration[key];\n }\n\n private isExpired(key: string): boolean {\n const expiration = this.expiration[key];\n if (expiration instanceof Date) {\n return expiration < new Date();\n }\n return false;\n }\n\n private calculateExpiration(duration: number | null): Date | undefined {\n if (duration === null) {\n return undefined;\n }\n\n return new Date(Date.now() + duration * 1000);\n }\n}"]}
package/dist/cjs/index.js CHANGED
@@ -18,7 +18,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
18
18
  return (mod && mod.__esModule) ? mod : { "default": mod };
19
19
  };
20
20
  Object.defineProperty(exports, "__esModule", { value: true });
21
- exports.formToObject = exports.submitForm = exports.ajaxSubmission = exports.ajaxSubmit = exports.CleanURLSearchParams = exports.draft = exports.Range = exports.Event = exports.EventBus = exports.from = exports.Lazy = exports.AsyncPoolExecutor = exports.Stopwatch = exports.TimeSpan = exports.Timer = exports.Order = exports.KeyValuePair = exports.CancellablePromise = exports.TypeCode = exports.Type = void 0;
21
+ exports.formToObject = exports.submitForm = exports.ajaxSubmission = exports.ajaxSubmit = exports.CleanURLSearchParams = exports.draft = exports.Range = exports.Event = exports.EventBus = exports.from = exports.Lazy = exports.AsyncPoolExecutor = exports.CountTrigger = exports.TimeTrigger = exports.Stopwatch = exports.TimeSpan = exports.Timer = exports.Order = exports.KeyValuePair = exports.CancellablePromise = exports.TypeCode = exports.Type = void 0;
22
22
  // ─── Types ───────────────────────────────────────────
23
23
  __exportStar(require("./types.js"), exports);
24
24
  var type_js_1 = require("./type.js");
@@ -45,6 +45,9 @@ var timespan_js_1 = require("./timespan.js");
45
45
  Object.defineProperty(exports, "TimeSpan", { enumerable: true, get: function () { return __importDefault(timespan_js_1).default; } });
46
46
  var stopwatch_js_1 = require("./stopwatch.js");
47
47
  Object.defineProperty(exports, "Stopwatch", { enumerable: true, get: function () { return __importDefault(stopwatch_js_1).default; } });
48
+ var triggers_js_1 = require("./triggers.js");
49
+ Object.defineProperty(exports, "TimeTrigger", { enumerable: true, get: function () { return triggers_js_1.TimeTrigger; } });
50
+ Object.defineProperty(exports, "CountTrigger", { enumerable: true, get: function () { return triggers_js_1.CountTrigger; } });
48
51
  var async_pool_executor_js_1 = require("./async-pool-executor.js");
49
52
  Object.defineProperty(exports, "AsyncPoolExecutor", { enumerable: true, get: function () { return __importDefault(async_pool_executor_js_1).default; } });
50
53
  // ─── Patterns ────────────────────────────────────────
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA,eAAe;;;;;;;;;;;;;;;;;;;;AAEf,wDAAwD;AACxD,6CAA2B;AAC3B,qCAA0C;AAAlC,gHAAA,OAAO,OAAQ;AACvB,+CAAmD;AAA3C,yHAAA,OAAO,OAAY;AAE3B,wDAAwD;AACxD,iDAA+B;AAC/B,iDAA+B;AAC/B,8CAA4B;AAC5B,4CAA0B;AAC1B,6CAA2B;AAC3B,mEAAuE;AAA/D,6IAAA,OAAO,OAAsB;AAErC,uDAAuD;AACvD,yDAA4D;AAApD,kIAAA,OAAO,OAAgB;AAC/B,uCAA4C;AAApC,kHAAA,OAAO,OAAS;AAExB,uDAAuD;AACvD,uCAA4C;AAApC,kHAAA,OAAO,OAAS;AACxB,6CAAkD;AAA1C,wHAAA,OAAO,OAAY;AAC3B,+CAAoD;AAA5C,0HAAA,OAAO,OAAa;AAC5B,mEAAsE;AAA9D,4IAAA,OAAO,OAAqB;AAEpC,wDAAwD;AACxD,qCAA0C;AAAlC,gHAAA,OAAO,OAAQ;AACvB,iDAAsD;AAA9C,gHAAA,OAAO,OAAQ;AACvB,+CAA+C;AAAvC,wGAAA,QAAQ,OAAA;AAAE,qGAAA,KAAK,OAAA;AACvB,uCAA4C;AAApC,kHAAA,OAAO,OAAS;AAExB,qDAAqD;AACrD,uCAA4C;AAApC,kHAAA,OAAO,OAAS;AAExB,wDAAwD;AACxD,2EAA6E;AAArE,mJAAA,OAAO,OAAwB;AACvC,mBAAmB;AACnB,+CAAwF;AAAhF,sGAAA,UAAU,OAAA;AAAE,0GAAA,cAAc,OAAA;AAAE,sGAAA,UAAU,OAAA;AAAE,wGAAA,YAAY,OAAA;AAI5D,wDAAwD;AACxD,mBAAmB;AACnB,6CAA2B;AAC3B,mBAAmB;AACnB,yDAAuC","sourcesContent":["// src/index.ts\n\n// ─── Types ───────────────────────────────────────────\nexport * from './types.js';\nexport {default as Type} from './type.js';\nexport {default as TypeCode} from './type-code.js';\n\n// ─── Utilities ───────────────────────────────────────\nexport * from './utilities.js';\nexport * from './functions.js';\nexport * from './string.js';\nexport * from './math.js';\nexport * from './parse.js';\nexport {default as CancellablePromise} from \"./cancellable-promise.js\";\n\n// ─── Data Structures ────────────────────────────────\nexport {default as KeyValuePair} from './key-value-pair.js';\nexport {default as Order} from './order.js';\n\n// ─── Async & Timing ─────────────────────────────────\nexport {default as Timer} from './timer.js';\nexport {default as TimeSpan} from './timespan.js';\nexport {default as Stopwatch} from './stopwatch.js';\nexport {default as AsyncPoolExecutor} from './async-pool-executor.js';\n\n// ─── Patterns ────────────────────────────────────────\nexport {default as Lazy} from './lazy.js';\nexport {default as from} from './collections/from.js';\nexport {EventBus, Event} from './event-bus.js';\nexport {default as Range} from './range.js';\n\n// ─── Draft ────────────────────────────────────────\nexport {default as draft} from './draft.js';\n\n// ─── Web ─────────────────────────────────────────────\nexport {default as CleanURLSearchParams} from './clean-url-search-params.js';\n/** @deprecated */\nexport {ajaxSubmit, ajaxSubmission, submitForm, formToObject} from './browser/index.js';\n\n\n\n// ─── Deprecated ──────────────────────────────────────\n/** @deprecated */\nexport * from './array.js';\n/** @deprecated */\nexport * from './collections/index.js';"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA,eAAe;;;;;;;;;;;;;;;;;;;;AAEf,wDAAwD;AACxD,6CAA2B;AAC3B,qCAA0C;AAAlC,gHAAA,OAAO,OAAQ;AACvB,+CAAmD;AAA3C,yHAAA,OAAO,OAAY;AAE3B,wDAAwD;AACxD,iDAA+B;AAC/B,iDAA+B;AAC/B,8CAA4B;AAC5B,4CAA0B;AAC1B,6CAA2B;AAC3B,mEAAuE;AAA/D,6IAAA,OAAO,OAAsB;AAErC,uDAAuD;AACvD,yDAA4D;AAApD,kIAAA,OAAO,OAAgB;AAC/B,uCAA4C;AAApC,kHAAA,OAAO,OAAS;AAExB,uDAAuD;AACvD,uCAA4C;AAApC,kHAAA,OAAO,OAAS;AACxB,6CAAkD;AAA1C,wHAAA,OAAO,OAAY;AAC3B,+CAAoD;AAA5C,0HAAA,OAAO,OAAa;AAE5B,6CAAwD;AAAhD,0GAAA,WAAW,OAAA;AAAE,2GAAA,YAAY,OAAA;AACjC,mEAAsE;AAA9D,4IAAA,OAAO,OAAqB;AAEpC,wDAAwD;AACxD,qCAA0C;AAAlC,gHAAA,OAAO,OAAQ;AACvB,iDAAsD;AAA9C,gHAAA,OAAO,OAAQ;AACvB,+CAA+C;AAAvC,wGAAA,QAAQ,OAAA;AAAE,qGAAA,KAAK,OAAA;AACvB,uCAA4C;AAApC,kHAAA,OAAO,OAAS;AAExB,qDAAqD;AACrD,uCAA4C;AAApC,kHAAA,OAAO,OAAS;AAExB,wDAAwD;AACxD,2EAA6E;AAArE,mJAAA,OAAO,OAAwB;AACvC,mBAAmB;AACnB,+CAAwF;AAAhF,sGAAA,UAAU,OAAA;AAAE,0GAAA,cAAc,OAAA;AAAE,sGAAA,UAAU,OAAA;AAAE,wGAAA,YAAY,OAAA;AAI5D,wDAAwD;AACxD,mBAAmB;AACnB,6CAA2B;AAC3B,mBAAmB;AACnB,yDAAuC","sourcesContent":["// src/index.ts\n\n// ─── Types ───────────────────────────────────────────\nexport * from './types.js';\nexport {default as Type} from './type.js';\nexport {default as TypeCode} from './type-code.js';\n\n// ─── Utilities ───────────────────────────────────────\nexport * from './utilities.js';\nexport * from './functions.js';\nexport * from './string.js';\nexport * from './math.js';\nexport * from './parse.js';\nexport {default as CancellablePromise} from \"./cancellable-promise.js\";\n\n// ─── Data Structures ────────────────────────────────\nexport {default as KeyValuePair} from './key-value-pair.js';\nexport {default as Order} from './order.js';\n\n// ─── Async & Timing ─────────────────────────────────\nexport {default as Timer} from './timer.js';\nexport {default as TimeSpan} from './timespan.js';\nexport {default as Stopwatch} from './stopwatch.js';\nexport type {Result} from './result.js';\nexport {TimeTrigger, CountTrigger} from './triggers.js';\nexport {default as AsyncPoolExecutor} from './async-pool-executor.js';\n\n// ─── Patterns ────────────────────────────────────────\nexport {default as Lazy} from './lazy.js';\nexport {default as from} from './collections/from.js';\nexport {EventBus, Event} from './event-bus.js';\nexport {default as Range} from './range.js';\n\n// ─── Draft ────────────────────────────────────────\nexport {default as draft} from './draft.js';\n\n// ─── Web ─────────────────────────────────────────────\nexport {default as CleanURLSearchParams} from './clean-url-search-params.js';\n/** @deprecated */\nexport {ajaxSubmit, ajaxSubmission, submitForm, formToObject} from './browser/index.js';\n\n\n\n// ─── Deprecated ──────────────────────────────────────\n/** @deprecated */\nexport * from './array.js';\n/** @deprecated */\nexport * from './collections/index.js';"]}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=result.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result.js","sourceRoot":"","sources":["../../src/result.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Represents success and failure outputs narrowed by `success`.\n */\nexport type Result<TSuccessOutput, TFailureOutput = undefined> =\n | {readonly success: true; readonly output: TSuccessOutput}\n | {readonly success: false; readonly output?: TFailureOutput};\n"]}
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CountTrigger = exports.TimeTrigger = void 0;
4
+ /**
5
+ * Runs a callback at most once during a configured time interval.
6
+ */
7
+ class TimeTrigger {
8
+ constructor(callback, interval) {
9
+ this.callback = callback;
10
+ this.interval = interval;
11
+ this.lastRunTime = null;
12
+ }
13
+ /**
14
+ * Executes the callback when the configured interval has elapsed.
15
+ */
16
+ async check() {
17
+ const currentTime = performance.now();
18
+ if (this.lastRunTime === null
19
+ || currentTime - this.lastRunTime >= this.interval.milliseconds) {
20
+ return { success: true, output: await this.trigger() };
21
+ }
22
+ return { success: false };
23
+ }
24
+ /**
25
+ * Executes the callback regardless of the configured interval.
26
+ */
27
+ async trigger() {
28
+ this.lastRunTime = performance.now();
29
+ return this.callback();
30
+ }
31
+ /**
32
+ * Replaces the callback executed by this trigger.
33
+ */
34
+ setCallback(callback) {
35
+ this.callback = callback;
36
+ }
37
+ /**
38
+ * Gets the configured time interval.
39
+ */
40
+ getInterval() {
41
+ return this.interval;
42
+ }
43
+ /**
44
+ * Replaces the configured time interval.
45
+ */
46
+ setInterval(interval) {
47
+ this.interval = interval;
48
+ }
49
+ }
50
+ exports.TimeTrigger = TimeTrigger;
51
+ /**
52
+ * Runs a callback after a configured number of checks.
53
+ */
54
+ class CountTrigger {
55
+ constructor(callback, interval) {
56
+ this.interval = 1;
57
+ this.checkCount = 0;
58
+ this.callback = callback;
59
+ this.setInterval(interval);
60
+ }
61
+ /**
62
+ * Executes the callback when the configured number of checks is reached.
63
+ */
64
+ async check() {
65
+ this.checkCount++;
66
+ if (this.checkCount >= this.interval) {
67
+ return { success: true, output: await this.trigger() };
68
+ }
69
+ return { success: false };
70
+ }
71
+ /**
72
+ * Executes the callback regardless of the configured check count.
73
+ */
74
+ async trigger() {
75
+ this.checkCount = 0;
76
+ return this.callback();
77
+ }
78
+ /**
79
+ * Replaces the callback executed by this trigger.
80
+ */
81
+ setCallback(callback) {
82
+ this.callback = callback;
83
+ }
84
+ /**
85
+ * Gets the configured check interval.
86
+ */
87
+ getInterval() {
88
+ return this.interval;
89
+ }
90
+ /**
91
+ * Replaces the configured check interval.
92
+ *
93
+ * @throws {RangeError} When the interval is less than one.
94
+ */
95
+ setInterval(interval) {
96
+ if (interval < 1) {
97
+ throw new RangeError('Interval must be greater than zero.');
98
+ }
99
+ this.interval = interval;
100
+ }
101
+ }
102
+ exports.CountTrigger = CountTrigger;
103
+ //# sourceMappingURL=triggers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"triggers.js","sourceRoot":"","sources":["../../src/triggers.ts"],"names":[],"mappings":";;;AAIA;;GAEG;AACH,MAAa,WAAW;IAK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;OAEG;IACI,KAAK,CAAC,KAAK;QACd,IAAI,CAAC,UAAU,EAAE,CAAC;QAElB,IAAI,IAAI,CAAC,UAAU,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YACnC,OAAO,EAAC,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC,OAAO,EAAE,EAAC,CAAC;QACzD,CAAC;QAED,OAAO,EAAC,OAAO,EAAE,KAAK,EAAC,CAAC;IAC5B,CAAC;IAED;;OAEG;IACI,KAAK,CAAC,OAAO;QAChB,IAAI,CAAC,UAAU,GAAG,CAAC,CAAC;QACpB,OAAO,IAAI,CAAC,QAAQ,EAAE,CAAC;IAC3B,CAAC;IAED;;OAEG;IACI,WAAW,CAAC,QAAkC;QACjD,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC7B,CAAC;IAED;;OAEG;IACI,WAAW;QACd,OAAO,IAAI,CAAC,QAAQ,CAAC;IACzB,CAAC;IAED;;;;OAIG;IACI,WAAW,CAAC,QAAgB;QAC/B,IAAI,QAAQ,GAAG,CAAC,EAAE,CAAC;YACf,MAAM,IAAI,UAAU,CAAC,qCAAqC,CAAC,CAAC;QAChE,CAAC;QAED,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC7B,CAAC;CACJ;AAzDD,oCAyDC","sourcesContent":["import type {Result} from './result.js';\nimport TimeSpan from './timespan.js';\nimport type {Nullable, Supplier} from './types.js';\n\n/**\n * Runs a callback at most once during a configured time interval.\n */\nexport class TimeTrigger<T> {\n private callback: Supplier<T | Promise<T>>;\n private interval: TimeSpan;\n private lastRunTime: Nullable<number>;\n\n public constructor(callback: Supplier<T | Promise<T>>, interval: TimeSpan) {\n this.callback = callback;\n this.interval = interval;\n this.lastRunTime = null;\n }\n\n /**\n * Executes the callback when the configured interval has elapsed.\n */\n public async check(): Promise<Result<T>> {\n const currentTime = performance.now();\n\n if (this.lastRunTime === null\n || currentTime - this.lastRunTime >= this.interval.milliseconds\n ) {\n return {success: true, output: await this.trigger()};\n }\n\n return {success: false};\n }\n\n /**\n * Executes the callback regardless of the configured interval.\n */\n public async trigger(): Promise<T> {\n this.lastRunTime = performance.now();\n return this.callback();\n }\n\n /**\n * Replaces the callback executed by this trigger.\n */\n public setCallback(callback: Supplier<T | Promise<T>>): void {\n this.callback = callback;\n }\n\n /**\n * Gets the configured time interval.\n */\n public getInterval(): TimeSpan {\n return this.interval;\n }\n\n /**\n * Replaces the configured time interval.\n */\n public setInterval(interval: TimeSpan): void {\n this.interval = interval;\n }\n\n\n}\n\n/**\n * Runs a callback after a configured number of checks.\n */\nexport class CountTrigger<T> {\n private callback: Supplier<T | Promise<T>>;\n private interval: number = 1;\n private checkCount: number = 0;\n\n public constructor(callback: Supplier<T | Promise<T>>, interval: number) {\n this.callback = callback;\n this.setInterval(interval);\n }\n\n /**\n * Executes the callback when the configured number of checks is reached.\n */\n public async check(): Promise<Result<T>> {\n this.checkCount++;\n\n if (this.checkCount >= this.interval) {\n return {success: true, output: await this.trigger()};\n }\n\n return {success: false};\n }\n\n /**\n * Executes the callback regardless of the configured check count.\n */\n public async trigger(): Promise<T> {\n this.checkCount = 0;\n return this.callback();\n }\n\n /**\n * Replaces the callback executed by this trigger.\n */\n public setCallback(callback: Supplier<T | Promise<T>>): void {\n this.callback = callback;\n }\n\n /**\n * Gets the configured check interval.\n */\n public getInterval(): number {\n return this.interval;\n }\n\n /**\n * Replaces the configured check interval.\n *\n * @throws {RangeError} When the interval is less than one.\n */\n public setInterval(interval: number): void {\n if (interval < 1) {\n throw new RangeError('Interval must be greater than zero.');\n }\n\n this.interval = interval;\n }\n}\n"]}
@@ -39,9 +39,7 @@ export default class CacheDictionary {
39
39
  delete this.expiration[key];
40
40
  }
41
41
  else {
42
- const expiration = new Date();
43
- expiration.setSeconds(expiration.getSeconds() + duration);
44
- this.expiration[key] = expiration;
42
+ this.expiration[key] = this.calculateExpiration(duration);
45
43
  }
46
44
  }
47
45
  /**
@@ -95,9 +93,7 @@ export default class CacheDictionary {
95
93
  if (duration === null) {
96
94
  return undefined;
97
95
  }
98
- const expiration = new Date();
99
- expiration.setSeconds(expiration.getSeconds() + duration);
100
- return expiration;
96
+ return new Date(Date.now() + duration * 1000);
101
97
  }
102
98
  }
103
99
  //# sourceMappingURL=cache-dictionary.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"cache-dictionary.js","sourceRoot":"","sources":["../../../src/collections/cache-dictionary.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAC,OAAO,OAAO,eAAe;IAMhC;;;OAGG;IACH,YAAY,WAAwC,IAAI,EAAE,kBAAiC,IAAI;QAC3F,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACjC,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC;QACvC,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC1C,CAAC;IAED;;;;OAIG;IACI,aAAa,CAAC,GAAW;QAC5B,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;IAChC,CAAC;IAED;;;;OAIG;IACI,aAAa,CAAC,GAAW,EAAE,UAA4B;QAC1D,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,UAAU,CAAC;IACtC,CAAC;IAED;;;;OAIG;IACI,WAAW,CAAC,GAAW,EAAE,QAAuB;QACnD,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;YACpB,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;QAChC,CAAC;aAAM,CAAC;YACJ,MAAM,UAAU,GAAG,IAAI,IAAI,EAAE,CAAC;YAC9B,UAAU,CAAC,UAAU,CAAC,UAAU,CAAC,UAAU,EAAE,GAAG,QAAQ,CAAC,CAAC;YAC1D,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,UAAU,CAAC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AC7B,OAAO,UAAU,GAAG,IAAI,IAAI,EAAE,CAAC;QACnC,CAAC;QACD,OAAO,KAAK,CAAC;IACjB,CAAC;IAEO,mBAAmB,CAAC,QAAuB;QAC/C,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;YACpB,OAAO,SAAS,CAAC;QACrB,CAAC;QAED,MAAM,UAAU,GAAG,IAAI,IAAI,EAAE,CAAC;QAC9B,UAAU,CAAC,UAAU,CAAC,UAAU,CAAC,UAAU,EAAE,GAAG,QAAQ,CAAC,CAAC;QAC1D,OAAO,UAAU,CAAC;IACtB,CAAC;CACJ","sourcesContent":["/**\n * A dictionary-like structure with caching and expiration capabilities.\n * @template T - The type of the values stored in the cache.\n */\nexport default class CacheDictionary<T> {\n private readonly fallback: ((key: string) => T) | null;\n private readonly cache: { [key: string]: T };\n private readonly defaultDuration: number | null;\n private readonly expiration: { [key: string]: Date | undefined };\n\n /**\n * @param {((key: string) => T) | null} fallback - Optional function to call when a key is not found.\n * @param {number | null} defaultDuration - Default duration in seconds for cache items.\n */\n constructor(fallback: ((key: string) => T) | null = null, defaultDuration: number | null = null) {\n this.fallback = fallback;\n this.cache = Object.create(null);\n this.defaultDuration = defaultDuration;\n this.expiration = Object.create(null);\n }\n\n /**\n * Gets the expiration date for a specific key.\n * @param {string} key - The key.\n * @returns {Date | undefined} The expiration date or undefined.\n */\n public getExpiration(key: string): Date | undefined {\n return this.expiration[key];\n }\n\n /**\n * Sets a specific expiration date for a key.\n * @param {string} key - The key.\n * @param {Date | undefined} expiration - The expiration date.\n */\n public setExpiration(key: string, expiration: Date | undefined): void {\n this.expiration[key] = expiration;\n }\n\n /**\n * Sets a duration in seconds for a key's expiration.\n * @param {string} key - The key.\n * @param {number | null} duration - Duration in seconds. If null, expiration is removed.\n */\n public setDuration(key: string, duration: number | null): void {\n if (duration === null) {\n delete this.expiration[key];\n } else {\n const expiration = new Date();\n expiration.setSeconds(expiration.getSeconds() + duration);\n this.expiration[key] = expiration;\n }\n }\n\n /**\n * Gets a value from the cache. If not found and a fallback exists, it uses the fallback.\n * If the item is expired, it's removed and null is returned.\n * @param {string} key - The key to retrieve.\n * @returns {T | null} The value or null if not found/expired.\n */\n public get(key: string): T | null {\n if (!Object.prototype.hasOwnProperty.call(this.cache, key)) {\n if (this.fallback) {\n this.cache[key] = this.fallback(key);\n this.expiration[key] = this.calculateExpiration(this.defaultDuration);\n } else {\n return null;\n }\n }\n\n if (this.isExpired(key)) {\n this.remove(key);\n return null;\n }\n\n return this.cache[key];\n }\n\n /**\n * Sets a value in the cache with an optional duration.\n * @param {string} key - The key.\n * @param {T} value - The value to store.\n * @param {number | null} duration - Duration in seconds.\n */\n public set(key: string, value: T, duration: number | null = null): void {\n this.cache[key] = value;\n this.expiration[key] = this.calculateExpiration(duration);\n }\n\n /**\n * Removes an item from the cache.\n * @param {string} key - The key to remove.\n */\n public remove(key: string): void {\n delete this.cache[key];\n delete this.expiration[key];\n }\n\n private isExpired(key: string): boolean {\n const expiration = this.expiration[key];\n if (expiration instanceof Date) {\n return expiration < new Date();\n }\n return false;\n }\n\n private calculateExpiration(duration: number | null): Date | undefined {\n if (duration === null) {\n return undefined;\n }\n\n const expiration = new Date();\n expiration.setSeconds(expiration.getSeconds() + duration);\n return expiration;\n }\n}"]}
1
+ {"version":3,"file":"cache-dictionary.js","sourceRoot":"","sources":["../../../src/collections/cache-dictionary.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAC,OAAO,OAAO,eAAe;IAMhC;;;OAGG;IACH,YAAY,WAAwC,IAAI,EAAE,kBAAiC,IAAI;QAC3F,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACjC,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC;QACvC,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC1C,CAAC;IAED;;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AE,KAAQ,EAAE,WAA0B,IAAI;QAC5D,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;QACxB,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,mBAAmB,CAAC,QAAQ,CAAC,CAAC;IAC9D,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,GAAW;QACrB,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACvB,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;IAChC,CAAC;IAEO,SAAS,CAAC,GAAW;QACzB,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;QACxC,IAAI,UAAU,YAAY,IAAI,EAAE,CAAC;YAC7B,OAAO,UAAU,GAAG,IAAI,IAAI,EAAE,CAAC;QACnC,CAAC;QACD,OAAO,KAAK,CAAC;IACjB,CAAC;IAEO,mBAAmB,CAAC,QAAuB;QAC/C,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;YACpB,OAAO,SAAS,CAAC;QACrB,CAAC;QAED,OAAO,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,QAAQ,GAAG,IAAI,CAAC,CAAC;IAClD,CAAC;CACJ","sourcesContent":["/**\n * A dictionary-like structure with caching and expiration capabilities.\n * @template T - The type of the values stored in the cache.\n */\nexport default class CacheDictionary<T> {\n private readonly fallback: ((key: string) => T) | null;\n private readonly cache: { [key: string]: T };\n private readonly defaultDuration: number | null;\n private readonly expiration: { [key: string]: Date | undefined };\n\n /**\n * @param {((key: string) => T) | null} fallback - Optional function to call when a key is not found.\n * @param {number | null} defaultDuration - Default duration in seconds for cache items.\n */\n constructor(fallback: ((key: string) => T) | null = null, defaultDuration: number | null = null) {\n this.fallback = fallback;\n this.cache = Object.create(null);\n this.defaultDuration = defaultDuration;\n this.expiration = Object.create(null);\n }\n\n /**\n * Gets the expiration date for a specific key.\n * @param {string} key - The key.\n * @returns {Date | undefined} The expiration date or undefined.\n */\n public getExpiration(key: string): Date | undefined {\n return this.expiration[key];\n }\n\n /**\n * Sets a specific expiration date for a key.\n * @param {string} key - The key.\n * @param {Date | undefined} expiration - The expiration date.\n */\n public setExpiration(key: string, expiration: Date | undefined): void {\n this.expiration[key] = expiration;\n }\n\n /**\n * Sets a duration in seconds for a key's expiration.\n * @param {string} key - The key.\n * @param {number | null} duration - Duration in seconds. If null, expiration is removed.\n */\n public setDuration(key: string, duration: number | null): void {\n if (duration === null) {\n delete this.expiration[key];\n } else {\n this.expiration[key] = this.calculateExpiration(duration);\n }\n }\n\n /**\n * Gets a value from the cache. If not found and a fallback exists, it uses the fallback.\n * If the item is expired, it's removed and null is returned.\n * @param {string} key - The key to retrieve.\n * @returns {T | null} The value or null if not found/expired.\n */\n public get(key: string): T | null {\n if (!Object.prototype.hasOwnProperty.call(this.cache, key)) {\n if (this.fallback) {\n this.cache[key] = this.fallback(key);\n this.expiration[key] = this.calculateExpiration(this.defaultDuration);\n } else {\n return null;\n }\n }\n\n if (this.isExpired(key)) {\n this.remove(key);\n return null;\n }\n\n return this.cache[key];\n }\n\n /**\n * Sets a value in the cache with an optional duration.\n * @param {string} key - The key.\n * @param {T} value - The value to store.\n * @param {number | null} duration - Duration in seconds.\n */\n public set(key: string, value: T, duration: number | null = null): void {\n this.cache[key] = value;\n this.expiration[key] = this.calculateExpiration(duration);\n }\n\n /**\n * Removes an item from the cache.\n * @param {string} key - The key to remove.\n */\n public remove(key: string): void {\n delete this.cache[key];\n delete this.expiration[key];\n }\n\n private isExpired(key: string): boolean {\n const expiration = this.expiration[key];\n if (expiration instanceof Date) {\n return expiration < new Date();\n }\n return false;\n }\n\n private calculateExpiration(duration: number | null): Date | undefined {\n if (duration === null) {\n return undefined;\n }\n\n return new Date(Date.now() + duration * 1000);\n }\n}"]}
package/dist/esm/index.js CHANGED
@@ -17,6 +17,7 @@ export { default as Order } from './order.js';
17
17
  export { default as Timer } from './timer.js';
18
18
  export { default as TimeSpan } from './timespan.js';
19
19
  export { default as Stopwatch } from './stopwatch.js';
20
+ export { TimeTrigger, CountTrigger } from './triggers.js';
20
21
  export { default as AsyncPoolExecutor } from './async-pool-executor.js';
21
22
  // ─── Patterns ────────────────────────────────────────
22
23
  export { default as Lazy } from './lazy.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,eAAe;AAEf,wDAAwD;AACxD,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAC,OAAO,IAAI,IAAI,EAAC,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAC,OAAO,IAAI,QAAQ,EAAC,MAAM,gBAAgB,CAAC;AAEnD,wDAAwD;AACxD,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAC,OAAO,IAAI,kBAAkB,EAAC,MAAM,0BAA0B,CAAC;AAEvE,uDAAuD;AACvD,OAAO,EAAC,OAAO,IAAI,YAAY,EAAC,MAAM,qBAAqB,CAAC;AAC5D,OAAO,EAAC,OAAO,IAAI,KAAK,EAAC,MAAM,YAAY,CAAC;AAE5C,uDAAuD;AACvD,OAAO,EAAC,OAAO,IAAI,KAAK,EAAC,MAAM,YAAY,CAAC;AAC5C,OAAO,EAAC,OAAO,IAAI,QAAQ,EAAC,MAAM,eAAe,CAAC;AAClD,OAAO,EAAC,OAAO,IAAI,SAAS,EAAC,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAC,OAAO,IAAI,iBAAiB,EAAC,MAAM,0BAA0B,CAAC;AAEtE,wDAAwD;AACxD,OAAO,EAAC,OAAO,IAAI,IAAI,EAAC,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAC,OAAO,IAAI,IAAI,EAAC,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAC,QAAQ,EAAE,KAAK,EAAC,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAAC,OAAO,IAAI,KAAK,EAAC,MAAM,YAAY,CAAC;AAE5C,qDAAqD;AACrD,OAAO,EAAC,OAAO,IAAI,KAAK,EAAC,MAAM,YAAY,CAAC;AAE5C,wDAAwD;AACxD,OAAO,EAAC,OAAO,IAAI,oBAAoB,EAAC,MAAM,8BAA8B,CAAC;AAC7E,mBAAmB;AACnB,OAAO,EAAC,UAAU,EAAE,cAAc,EAAE,UAAU,EAAE,YAAY,EAAC,MAAM,oBAAoB,CAAC;AAIxF,wDAAwD;AACxD,mBAAmB;AACnB,cAAc,YAAY,CAAC;AAC3B,mBAAmB;AACnB,cAAc,wBAAwB,CAAC","sourcesContent":["// src/index.ts\n\n// ─── Types ───────────────────────────────────────────\nexport * from './types.js';\nexport {default as Type} from './type.js';\nexport {default as TypeCode} from './type-code.js';\n\n// ─── Utilities ───────────────────────────────────────\nexport * from './utilities.js';\nexport * from './functions.js';\nexport * from './string.js';\nexport * from './math.js';\nexport * from './parse.js';\nexport {default as CancellablePromise} from \"./cancellable-promise.js\";\n\n// ─── Data Structures ────────────────────────────────\nexport {default as KeyValuePair} from './key-value-pair.js';\nexport {default as Order} from './order.js';\n\n// ─── Async & Timing ─────────────────────────────────\nexport {default as Timer} from './timer.js';\nexport {default as TimeSpan} from './timespan.js';\nexport {default as Stopwatch} from './stopwatch.js';\nexport {default as AsyncPoolExecutor} from './async-pool-executor.js';\n\n// ─── Patterns ────────────────────────────────────────\nexport {default as Lazy} from './lazy.js';\nexport {default as from} from './collections/from.js';\nexport {EventBus, Event} from './event-bus.js';\nexport {default as Range} from './range.js';\n\n// ─── Draft ────────────────────────────────────────\nexport {default as draft} from './draft.js';\n\n// ─── Web ─────────────────────────────────────────────\nexport {default as CleanURLSearchParams} from './clean-url-search-params.js';\n/** @deprecated */\nexport {ajaxSubmit, ajaxSubmission, submitForm, formToObject} from './browser/index.js';\n\n\n\n// ─── Deprecated ──────────────────────────────────────\n/** @deprecated */\nexport * from './array.js';\n/** @deprecated */\nexport * from './collections/index.js';"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,eAAe;AAEf,wDAAwD;AACxD,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAC,OAAO,IAAI,IAAI,EAAC,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAC,OAAO,IAAI,QAAQ,EAAC,MAAM,gBAAgB,CAAC;AAEnD,wDAAwD;AACxD,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAC,OAAO,IAAI,kBAAkB,EAAC,MAAM,0BAA0B,CAAC;AAEvE,uDAAuD;AACvD,OAAO,EAAC,OAAO,IAAI,YAAY,EAAC,MAAM,qBAAqB,CAAC;AAC5D,OAAO,EAAC,OAAO,IAAI,KAAK,EAAC,MAAM,YAAY,CAAC;AAE5C,uDAAuD;AACvD,OAAO,EAAC,OAAO,IAAI,KAAK,EAAC,MAAM,YAAY,CAAC;AAC5C,OAAO,EAAC,OAAO,IAAI,QAAQ,EAAC,MAAM,eAAe,CAAC;AAClD,OAAO,EAAC,OAAO,IAAI,SAAS,EAAC,MAAM,gBAAgB,CAAC;AAEpD,OAAO,EAAC,WAAW,EAAE,YAAY,EAAC,MAAM,eAAe,CAAC;AACxD,OAAO,EAAC,OAAO,IAAI,iBAAiB,EAAC,MAAM,0BAA0B,CAAC;AAEtE,wDAAwD;AACxD,OAAO,EAAC,OAAO,IAAI,IAAI,EAAC,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAC,OAAO,IAAI,IAAI,EAAC,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAC,QAAQ,EAAE,KAAK,EAAC,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAAC,OAAO,IAAI,KAAK,EAAC,MAAM,YAAY,CAAC;AAE5C,qDAAqD;AACrD,OAAO,EAAC,OAAO,IAAI,KAAK,EAAC,MAAM,YAAY,CAAC;AAE5C,wDAAwD;AACxD,OAAO,EAAC,OAAO,IAAI,oBAAoB,EAAC,MAAM,8BAA8B,CAAC;AAC7E,mBAAmB;AACnB,OAAO,EAAC,UAAU,EAAE,cAAc,EAAE,UAAU,EAAE,YAAY,EAAC,MAAM,oBAAoB,CAAC;AAIxF,wDAAwD;AACxD,mBAAmB;AACnB,cAAc,YAAY,CAAC;AAC3B,mBAAmB;AACnB,cAAc,wBAAwB,CAAC","sourcesContent":["// src/index.ts\n\n// ─── Types ───────────────────────────────────────────\nexport * from './types.js';\nexport {default as Type} from './type.js';\nexport {default as TypeCode} from './type-code.js';\n\n// ─── Utilities ───────────────────────────────────────\nexport * from './utilities.js';\nexport * from './functions.js';\nexport * from './string.js';\nexport * from './math.js';\nexport * from './parse.js';\nexport {default as CancellablePromise} from \"./cancellable-promise.js\";\n\n// ─── Data Structures ────────────────────────────────\nexport {default as KeyValuePair} from './key-value-pair.js';\nexport {default as Order} from './order.js';\n\n// ─── Async & Timing ─────────────────────────────────\nexport {default as Timer} from './timer.js';\nexport {default as TimeSpan} from './timespan.js';\nexport {default as Stopwatch} from './stopwatch.js';\nexport type {Result} from './result.js';\nexport {TimeTrigger, CountTrigger} from './triggers.js';\nexport {default as AsyncPoolExecutor} from './async-pool-executor.js';\n\n// ─── Patterns ────────────────────────────────────────\nexport {default as Lazy} from './lazy.js';\nexport {default as from} from './collections/from.js';\nexport {EventBus, Event} from './event-bus.js';\nexport {default as Range} from './range.js';\n\n// ─── Draft ────────────────────────────────────────\nexport {default as draft} from './draft.js';\n\n// ─── Web ─────────────────────────────────────────────\nexport {default as CleanURLSearchParams} from './clean-url-search-params.js';\n/** @deprecated */\nexport {ajaxSubmit, ajaxSubmission, submitForm, formToObject} from './browser/index.js';\n\n\n\n// ─── Deprecated ──────────────────────────────────────\n/** @deprecated */\nexport * from './array.js';\n/** @deprecated */\nexport * from './collections/index.js';"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=result.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result.js","sourceRoot":"","sources":["../../src/result.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Represents success and failure outputs narrowed by `success`.\n */\nexport type Result<TSuccessOutput, TFailureOutput = undefined> =\n | {readonly success: true; readonly output: TSuccessOutput}\n | {readonly success: false; readonly output?: TFailureOutput};\n"]}
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Runs a callback at most once during a configured time interval.
3
+ */
4
+ export class TimeTrigger {
5
+ constructor(callback, interval) {
6
+ this.callback = callback;
7
+ this.interval = interval;
8
+ this.lastRunTime = null;
9
+ }
10
+ /**
11
+ * Executes the callback when the configured interval has elapsed.
12
+ */
13
+ async check() {
14
+ const currentTime = performance.now();
15
+ if (this.lastRunTime === null
16
+ || currentTime - this.lastRunTime >= this.interval.milliseconds) {
17
+ return { success: true, output: await this.trigger() };
18
+ }
19
+ return { success: false };
20
+ }
21
+ /**
22
+ * Executes the callback regardless of the configured interval.
23
+ */
24
+ async trigger() {
25
+ this.lastRunTime = performance.now();
26
+ return this.callback();
27
+ }
28
+ /**
29
+ * Replaces the callback executed by this trigger.
30
+ */
31
+ setCallback(callback) {
32
+ this.callback = callback;
33
+ }
34
+ /**
35
+ * Gets the configured time interval.
36
+ */
37
+ getInterval() {
38
+ return this.interval;
39
+ }
40
+ /**
41
+ * Replaces the configured time interval.
42
+ */
43
+ setInterval(interval) {
44
+ this.interval = interval;
45
+ }
46
+ }
47
+ /**
48
+ * Runs a callback after a configured number of checks.
49
+ */
50
+ export class CountTrigger {
51
+ constructor(callback, interval) {
52
+ this.interval = 1;
53
+ this.checkCount = 0;
54
+ this.callback = callback;
55
+ this.setInterval(interval);
56
+ }
57
+ /**
58
+ * Executes the callback when the configured number of checks is reached.
59
+ */
60
+ async check() {
61
+ this.checkCount++;
62
+ if (this.checkCount >= this.interval) {
63
+ return { success: true, output: await this.trigger() };
64
+ }
65
+ return { success: false };
66
+ }
67
+ /**
68
+ * Executes the callback regardless of the configured check count.
69
+ */
70
+ async trigger() {
71
+ this.checkCount = 0;
72
+ return this.callback();
73
+ }
74
+ /**
75
+ * Replaces the callback executed by this trigger.
76
+ */
77
+ setCallback(callback) {
78
+ this.callback = callback;
79
+ }
80
+ /**
81
+ * Gets the configured check interval.
82
+ */
83
+ getInterval() {
84
+ return this.interval;
85
+ }
86
+ /**
87
+ * Replaces the configured check interval.
88
+ *
89
+ * @throws {RangeError} When the interval is less than one.
90
+ */
91
+ setInterval(interval) {
92
+ if (interval < 1) {
93
+ throw new RangeError('Interval must be greater than zero.');
94
+ }
95
+ this.interval = interval;
96
+ }
97
+ }
98
+ //# sourceMappingURL=triggers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"triggers.js","sourceRoot":"","sources":["../../src/triggers.ts"],"names":[],"mappings":"AAIA;;GAEG;AACH,MAAM,OAAO,WAAW;IAKpB,YAAmB,QAAkC,EAAE,QAAkB;QACrE,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC;IAC5B,CAAC;IAED;;OAEG;IACI,KAAK,CAAC,KAAK;QACd,MAAM,WAAW,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC;QAEtC,IAAI,IAAI,CAAC,WAAW,KAAK,IAAI;eACtB,WAAW,GAAG,IAAI,CAAC,WAAW,IAAI,IAAI,CAAC,QAAQ,CAAC,YAAY,EACjE,CAAC;YACC,OAAO,EAAC,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC,OAAO,EAAE,EAAC,CAAC;QACzD,CAAC;QAED,OAAO,EAAC,OAAO,EAAE,KAAK,EAAC,CAAC;IAC5B,CAAC;IAED;;OAEG;IACI,KAAK,CAAC,OAAO;QAChB,I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G;IACI,WAAW,CAAC,QAAgB;QAC/B,IAAI,QAAQ,GAAG,CAAC,EAAE,CAAC;YACf,MAAM,IAAI,UAAU,CAAC,qCAAqC,CAAC,CAAC;QAChE,CAAC;QAED,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC7B,CAAC;CACJ","sourcesContent":["import type {Result} from './result.js';\nimport TimeSpan from './timespan.js';\nimport type {Nullable, Supplier} from './types.js';\n\n/**\n * Runs a callback at most once during a configured time interval.\n */\nexport class TimeTrigger<T> {\n private callback: Supplier<T | Promise<T>>;\n private interval: TimeSpan;\n private lastRunTime: Nullable<number>;\n\n public constructor(callback: Supplier<T | Promise<T>>, interval: TimeSpan) {\n this.callback = callback;\n this.interval = interval;\n this.lastRunTime = null;\n }\n\n /**\n * Executes the callback when the configured interval has elapsed.\n */\n public async check(): Promise<Result<T>> {\n const currentTime = performance.now();\n\n if (this.lastRunTime === null\n || currentTime - this.lastRunTime >= this.interval.milliseconds\n ) {\n return {success: true, output: await this.trigger()};\n }\n\n return {success: false};\n }\n\n /**\n * Executes the callback regardless of the configured interval.\n */\n public async trigger(): Promise<T> {\n this.lastRunTime = performance.now();\n return this.callback();\n }\n\n /**\n * Replaces the callback executed by this trigger.\n */\n public setCallback(callback: Supplier<T | Promise<T>>): void {\n this.callback = callback;\n }\n\n /**\n * Gets the configured time interval.\n */\n public getInterval(): TimeSpan {\n return this.interval;\n }\n\n /**\n * Replaces the configured time interval.\n */\n public setInterval(interval: TimeSpan): void {\n this.interval = interval;\n }\n\n\n}\n\n/**\n * Runs a callback after a configured number of checks.\n */\nexport class CountTrigger<T> {\n private callback: Supplier<T | Promise<T>>;\n private interval: number = 1;\n private checkCount: number = 0;\n\n public constructor(callback: Supplier<T | Promise<T>>, interval: number) {\n this.callback = callback;\n this.setInterval(interval);\n }\n\n /**\n * Executes the callback when the configured number of checks is reached.\n */\n public async check(): Promise<Result<T>> {\n this.checkCount++;\n\n if (this.checkCount >= this.interval) {\n return {success: true, output: await this.trigger()};\n }\n\n return {success: false};\n }\n\n /**\n * Executes the callback regardless of the configured check count.\n */\n public async trigger(): Promise<T> {\n this.checkCount = 0;\n return this.callback();\n }\n\n /**\n * Replaces the callback executed by this trigger.\n */\n public setCallback(callback: Supplier<T | Promise<T>>): void {\n this.callback = callback;\n }\n\n /**\n * Gets the configured check interval.\n */\n public getInterval(): number {\n return this.interval;\n }\n\n /**\n * Replaces the configured check interval.\n *\n * @throws {RangeError} When the interval is less than one.\n */\n public setInterval(interval: number): void {\n if (interval < 1) {\n throw new RangeError('Interval must be greater than zero.');\n }\n\n this.interval = interval;\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"cache-dictionary.d.ts","sourceRoot":"","sources":["../../../src/collections/cache-dictionary.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAC,OAAO,OAAO,eAAe,CAAC,CAAC;IAClC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA8B;IACvD,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAuB;IAC7C,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAgB;IAChD,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAsC;IAEjE;;;OAGG;gBACS,QAAQ,GAAE,CAAC,CAAC,GAAG,EAAE,MAAM,KAAK,CAAC,CAAC,GAAG,IAAW,EAAE,eAAe,GAAE,MAAM,GAAG,IAAW;IAO/F;;;;OAIG;IACI,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS;IAInD;;;;OAIG;IACI,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,UAAU,EAAE,IAAI,GAAG,SAAS,GAAG,IAAI;IAIrE;;;;OAIG;IACI,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI;IAU9D;;;;;OAKG;IACI,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,CAAC,GAAG,IAAI;IAkBjC;;;;;OAKG;I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I;IAKhC,OAAO,CAAC,SAAS;IAQjB,OAAO,CAAC,mBAAmB;CAO9B"}
@@ -12,6 +12,8 @@ export { default as Order } from './order.js';
12
12
  export { default as Timer } from './timer.js';
13
13
  export { default as TimeSpan } from './timespan.js';
14
14
  export { default as Stopwatch } from './stopwatch.js';
15
+ export type { Result } from './result.js';
16
+ export { TimeTrigger, CountTrigger } from './triggers.js';
15
17
  export { default as AsyncPoolExecutor } from './async-pool-executor.js';
16
18
  export { default as Lazy } from './lazy.js';
17
19
  export { default as from } from './collections/from.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAGA,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAC,OAAO,IAAI,IAAI,EAAC,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAC,OAAO,IAAI,QAAQ,EAAC,MAAM,gBAAgB,CAAC;AAGnD,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAC,OAAO,IAAI,kBAAkB,EAAC,MAAM,0BAA0B,CAAC;AAGvE,OAAO,EAAC,OAAO,IAAI,YAAY,EAAC,MAAM,qBAAqB,CAAC;AAC5D,OAAO,EAAC,OAAO,IAAI,KAAK,EAAC,MAAM,YAAY,CAAC;AAG5C,OAAO,EAAC,OAAO,IAAI,KAAK,EAAC,MAAM,YAAY,CAAC;AAC5C,OAAO,EAAC,OAAO,IAAI,QAAQ,EAAC,MAAM,eAAe,CAAC;AAClD,OAAO,EAAC,OAAO,IAAI,SAAS,EAAC,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAC,OAAO,IAAI,iBAAiB,EAAC,MAAM,0BAA0B,CAAC;AAGtE,OAAO,EAAC,OAAO,IAAI,IAAI,EAAC,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAC,OAAO,IA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AAM,aAAa,CAAC;AACxC,OAAO,EAAC,WAAW,EAAE,YAAY,EAAC,MAAM,eAAe,CAAC;AACxD,OAAO,EAAC,OAAO,IAAI,iBAAiB,EAAC,MAAM,0BAA0B,CAAC;AAGtE,OAAO,EAAC,OAAO,IAAI,IAAI,EAAC,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAC,OAAO,IAAI,IAAI,EAAC,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAC,QAAQ,EAAE,KAAK,EAAC,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAAC,OAAO,IAAI,KAAK,EAAC,MAAM,YAAY,CAAC;AAG5C,OAAO,EAAC,OAAO,IAAI,KAAK,EAAC,MAAM,YAAY,CAAC;AAG5C,OAAO,EAAC,OAAO,IAAI,oBAAoB,EAAC,MAAM,8BAA8B,CAAC;AAC7E,mBAAmB;AACnB,OAAO,EAAC,UAAU,EAAE,cAAc,EAAE,UAAU,EAAE,YAAY,EAAC,MAAM,oBAAoB,CAAC;AAKxF,mBAAmB;AACnB,cAAc,YAAY,CAAC;AAC3B,mBAAmB;AACnB,cAAc,wBAAwB,CAAC"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Represents success and failure outputs narrowed by `success`.
3
+ */
4
+ export type Result<TSuccessOutput, TFailureOutput = undefined> = {
5
+ readonly success: true;
6
+ readonly output: TSuccessOutput;
7
+ } | {
8
+ readonly success: false;
9
+ readonly output?: TFailureOutput;
10
+ };
11
+ //# sourceMappingURL=result.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result.d.ts","sourceRoot":"","sources":["../../src/result.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,MAAM,CAAC,cAAc,EAAE,cAAc,GAAG,SAAS,IACvD;IAAC,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAA;CAAC,GACzD;IAAC,QAAQ,CAAC,OAAO,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,cAAc,CAAA;CAAC,CAAC"}
@@ -0,0 +1,64 @@
1
+ import type { Result } from './result.js';
2
+ import TimeSpan from './timespan.js';
3
+ import type { Supplier } from './types.js';
4
+ /**
5
+ * Runs a callback at most once during a configured time interval.
6
+ */
7
+ export declare class TimeTrigger<T> {
8
+ private callback;
9
+ private interval;
10
+ private lastRunTime;
11
+ constructor(callback: Supplier<T | Promise<T>>, interval: TimeSpan);
12
+ /**
13
+ * Executes the callback when the configured interval has elapsed.
14
+ */
15
+ check(): Promise<Result<T>>;
16
+ /**
17
+ * Executes the callback regardless of the configured interval.
18
+ */
19
+ trigger(): Promise<T>;
20
+ /**
21
+ * Replaces the callback executed by this trigger.
22
+ */
23
+ setCallback(callback: Supplier<T | Promise<T>>): void;
24
+ /**
25
+ * Gets the configured time interval.
26
+ */
27
+ getInterval(): TimeSpan;
28
+ /**
29
+ * Replaces the configured time interval.
30
+ */
31
+ setInterval(interval: TimeSpan): void;
32
+ }
33
+ /**
34
+ * Runs a callback after a configured number of checks.
35
+ */
36
+ export declare class CountTrigger<T> {
37
+ private callback;
38
+ private interval;
39
+ private checkCount;
40
+ constructor(callback: Supplier<T | Promise<T>>, interval: number);
41
+ /**
42
+ * Executes the callback when the configured number of checks is reached.
43
+ */
44
+ check(): Promise<Result<T>>;
45
+ /**
46
+ * Executes the callback regardless of the configured check count.
47
+ */
48
+ trigger(): Promise<T>;
49
+ /**
50
+ * Replaces the callback executed by this trigger.
51
+ */
52
+ setCallback(callback: Supplier<T | Promise<T>>): void;
53
+ /**
54
+ * Gets the configured check interval.
55
+ */
56
+ getInterval(): number;
57
+ /**
58
+ * Replaces the configured check interval.
59
+ *
60
+ * @throws {RangeError} When the interval is less than one.
61
+ */
62
+ setInterval(interval: number): void;
63
+ }
64
+ //# sourceMappingURL=triggers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"triggers.d.ts","sourceRoot":"","sources":["../../src/triggers.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G,IAAI;IAI5D;;OAEG;IACI,WAAW,IAAI,MAAM;IAI5B;;;;OAIG;IACI,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;CAO7C"}
@@ -39,9 +39,7 @@ export default class CacheDictionary {
39
39
  delete this.expiration[key];
40
40
  }
41
41
  else {
42
- const expiration = new Date();
43
- expiration.setSeconds(expiration.getSeconds() + duration);
44
- this.expiration[key] = expiration;
42
+ this.expiration[key] = this.calculateExpiration(duration);
45
43
  }
46
44
  }
47
45
  /**
@@ -95,9 +93,7 @@ export default class CacheDictionary {
95
93
  if (duration === null) {
96
94
  return undefined;
97
95
  }
98
- const expiration = new Date();
99
- expiration.setSeconds(expiration.getSeconds() + duration);
100
- return expiration;
96
+ return new Date(Date.now() + duration * 1000);
101
97
  }
102
98
  }
103
99
  //# sourceMappingURL=cache-dictionary.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"cache-dictionary.js","sourceRoot":"","sources":["../../../src/collections/cache-dictionary.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAC,OAAO,OAAO,eAAe;IAMhC;;;OAGG;IACH,YAAY,WAAwC,IAAI,EAAE,kBAAiC,IAAI;QAC3F,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACjC,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC;QACvC,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC1C,CAAC;IAED;;;;OAIG;IACI,aAAa,CAAC,GAAW;QAC5B,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;IAChC,CAAC;IAED;;;;OAIG;IACI,aAAa,CAAC,GAAW,EAAE,UAA4B;QAC1D,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,UAAU,CAAC;IACtC,CAAC;IAED;;;;OAIG;IACI,WAA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,mBAAmB,CAAC,QAAQ,CAAC,CAAC;IAC9D,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,GAAW;QACrB,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACvB,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;IAChC,CAAC;IAEO,SAAS,CAAC,GAAW;QACzB,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;QACxC,IAAI,UAAU,YAAY,IAAI,EAAE,CAAC;YAC7B,OAAO,UAAU,GAAG,IAAI,IAAI,EAAE,CAAC;QACnC,CAAC;QACD,OAAO,KAAK,CAAC;IACjB,CAAC;IAEO,mBAAmB,CAAC,QAAuB;QAC/C,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;YACpB,OAAO,SAAS,CAAC;QACrB,CAAC;QAED,MAAM,UAAU,GAAG,IAAI,IAAI,EAAE,CAAC;QAC9B,UAAU,CAAC,UAAU,CAAC,UAAU,CAAC,UAAU,EAAE,GAAG,QAAQ,CAAC,CAAC;QAC1D,OAAO,UAAU,CAAC;IACtB,CAAC;CACJ","sourcesContent":["/**\n * A dictionary-like structure with caching and expiration capabilities.\n * @template T - The type of the values stored in the cache.\n */\nexport default class CacheDictionary<T> {\n private readonly fallback: ((key: string) => T) | null;\n private readonly cache: { [key: string]: T };\n private readonly defaultDuration: number | null;\n private readonly expiration: { [key: string]: Date | undefined };\n\n /**\n * @param {((key: string) => T) | null} fallback - Optional function to call when a key is not found.\n * @param {number | null} defaultDuration - Default duration in seconds for cache items.\n */\n constructor(fallback: ((key: string) => T) | null = null, defaultDuration: number | null = null) {\n this.fallback = fallback;\n this.cache = Object.create(null);\n this.defaultDuration = defaultDuration;\n this.expiration = Object.create(null);\n }\n\n /**\n * Gets the expiration date for a specific key.\n * @param {string} key - The key.\n * @returns {Date | undefined} The expiration date or undefined.\n */\n public getExpiration(key: string): Date | undefined {\n return this.expiration[key];\n }\n\n /**\n * Sets a specific expiration date for a key.\n * @param {string} key - The key.\n * @param {Date | undefined} expiration - The expiration date.\n */\n public setExpiration(key: string, expiration: Date | undefined): void {\n this.expiration[key] = expiration;\n }\n\n /**\n * Sets a duration in seconds for a key's expiration.\n * @param {string} key - The key.\n * @param {number | null} duration - Duration in seconds. If null, expiration is removed.\n */\n public setDuration(key: string, duration: number | null): void {\n if (duration === null) {\n delete this.expiration[key];\n } else {\n const expiration = new Date();\n expiration.setSeconds(expiration.getSeconds() + duration);\n this.expiration[key] = expiration;\n }\n }\n\n /**\n * Gets a value from the cache. If not found and a fallback exists, it uses the fallback.\n * If the item is expired, it's removed and null is returned.\n * @param {string} key - The key to retrieve.\n * @returns {T | null} The value or null if not found/expired.\n */\n public get(key: string): T | null {\n if (!Object.prototype.hasOwnProperty.call(this.cache, key)) {\n if (this.fallback) {\n this.cache[key] = this.fallback(key);\n this.expiration[key] = this.calculateExpiration(this.defaultDuration);\n } else {\n return null;\n }\n }\n\n if (this.isExpired(key)) {\n this.remove(key);\n return null;\n }\n\n return this.cache[key];\n }\n\n /**\n * Sets a value in the cache with an optional duration.\n * @param {string} key - The key.\n * @param {T} value - The value to store.\n * @param {number | null} duration - Duration in seconds.\n */\n public set(key: string, value: T, duration: number | null = null): void {\n this.cache[key] = value;\n this.expiration[key] = this.calculateExpiration(duration);\n }\n\n /**\n * Removes an item from the cache.\n * @param {string} key - The key to remove.\n */\n public remove(key: string): void {\n delete this.cache[key];\n delete this.expiration[key];\n }\n\n private isExpired(key: string): boolean {\n const expiration = this.expiration[key];\n if (expiration instanceof Date) {\n return expiration < new Date();\n }\n return false;\n }\n\n private calculateExpiration(duration: number | null): Date | undefined {\n if (duration === null) {\n return undefined;\n }\n\n const expiration = new Date();\n expiration.setSeconds(expiration.getSeconds() + duration);\n return expiration;\n }\n}"]}
1
+ {"version":3,"file":"cache-dictionary.js","sourceRoot":"","sources":["../../../src/collections/cache-dictionary.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAA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,CAAC,CAAC;YAC1E,CAAC;iBAAM,CAAC;gBACJ,OAAO,IAAI,CAAC;YAChB,CAAC;QACL,CAAC;QAED,IAAI,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC;YACtB,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;YACjB,OAAO,IAAI,CAAC;QAChB,CAAC;QAED,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC3B,CAAC;IAED;;;;;OAKG;IACI,GAAG,CAAC,GAAW,EAAE,KAAQ,EAAE,WAA0B,IAAI;QAC5D,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;QACxB,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,mBAAmB,CAAC,QAAQ,CAAC,CAAC;IAC9D,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,GAAW;QACrB,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACvB,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;IAChC,CAAC;IAEO,SAAS,CAAC,GAAW;QACzB,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;QACxC,IAAI,UAAU,YAAY,IAAI,EAAE,CAAC;YAC7B,OAAO,UAAU,GAAG,IAAI,IAAI,EAAE,CAAC;QACnC,CAAC;QACD,OAAO,KAAK,CAAC;IACjB,CAAC;IAEO,mBAAmB,CAAC,QAAuB;QAC/C,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;YACpB,OAAO,SAAS,CAAC;QACrB,CAAC;QAED,OAAO,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,QAAQ,GAAG,IAAI,CAAC,CAAC;IAClD,CAAC;CACJ","sourcesContent":["/**\n * A dictionary-like structure with caching and expiration capabilities.\n * @template T - The type of the values stored in the cache.\n */\nexport default class CacheDictionary<T> {\n private readonly fallback: ((key: string) => T) | null;\n private readonly cache: { [key: string]: T };\n private readonly defaultDuration: number | null;\n private readonly expiration: { [key: string]: Date | undefined };\n\n /**\n * @param {((key: string) => T) | null} fallback - Optional function to call when a key is not found.\n * @param {number | null} defaultDuration - Default duration in seconds for cache items.\n */\n constructor(fallback: ((key: string) => T) | null = null, defaultDuration: number | null = null) {\n this.fallback = fallback;\n this.cache = Object.create(null);\n this.defaultDuration = defaultDuration;\n this.expiration = Object.create(null);\n }\n\n /**\n * Gets the expiration date for a specific key.\n * @param {string} key - The key.\n * @returns {Date | undefined} The expiration date or undefined.\n */\n public getExpiration(key: string): Date | undefined {\n return this.expiration[key];\n }\n\n /**\n * Sets a specific expiration date for a key.\n * @param {string} key - The key.\n * @param {Date | undefined} expiration - The expiration date.\n */\n public setExpiration(key: string, expiration: Date | undefined): void {\n this.expiration[key] = expiration;\n }\n\n /**\n * Sets a duration in seconds for a key's expiration.\n * @param {string} key - The key.\n * @param {number | null} duration - Duration in seconds. If null, expiration is removed.\n */\n public setDuration(key: string, duration: number | null): void {\n if (duration === null) {\n delete this.expiration[key];\n } else {\n this.expiration[key] = this.calculateExpiration(duration);\n }\n }\n\n /**\n * Gets a value from the cache. If not found and a fallback exists, it uses the fallback.\n * If the item is expired, it's removed and null is returned.\n * @param {string} key - The key to retrieve.\n * @returns {T | null} The value or null if not found/expired.\n */\n public get(key: string): T | null {\n if (!Object.prototype.hasOwnProperty.call(this.cache, key)) {\n if (this.fallback) {\n this.cache[key] = this.fallback(key);\n this.expiration[key] = this.calculateExpiration(this.defaultDuration);\n } else {\n return null;\n }\n }\n\n if (this.isExpired(key)) {\n this.remove(key);\n return null;\n }\n\n return this.cache[key];\n }\n\n /**\n * Sets a value in the cache with an optional duration.\n * @param {string} key - The key.\n * @param {T} value - The value to store.\n * @param {number | null} duration - Duration in seconds.\n */\n public set(key: string, value: T, duration: number | null = null): void {\n this.cache[key] = value;\n this.expiration[key] = this.calculateExpiration(duration);\n }\n\n /**\n * Removes an item from the cache.\n * @param {string} key - The key to remove.\n */\n public remove(key: string): void {\n delete this.cache[key];\n delete this.expiration[key];\n }\n\n private isExpired(key: string): boolean {\n const expiration = this.expiration[key];\n if (expiration instanceof Date) {\n return expiration < new Date();\n }\n return false;\n }\n\n private calculateExpiration(duration: number | null): Date | undefined {\n if (duration === null) {\n return undefined;\n }\n\n return new Date(Date.now() + duration * 1000);\n }\n}"]}
package/dist/web/index.js CHANGED
@@ -17,6 +17,7 @@ export { default as Order } from './order.js';
17
17
  export { default as Timer } from './timer.js';
18
18
  export { default as TimeSpan } from './timespan.js';
19
19
  export { default as Stopwatch } from './stopwatch.js';
20
+ export { TimeTrigger, CountTrigger } from './triggers.js';
20
21
  export { default as AsyncPoolExecutor } from './async-pool-executor.js';
21
22
  // ─── Patterns ────────────────────────────────────────
22
23
  export { default as Lazy } from './lazy.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,eAAe;AAEf,wDAAwD;AACxD,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAC,OAAO,IAAI,IAAI,EAAC,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAC,OAAO,IAAI,QAAQ,EAAC,MAAM,gBAAgB,CAAC;AAEnD,wDAAwD;AACxD,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAC,OAAO,IAAI,kBAAkB,EAAC,MAAM,0BAA0B,CAAC;AAEvE,uDAAuD;AACvD,OAAO,EAAC,OAAO,IAAI,YAAY,EAAC,MAAM,qBAAqB,CAAC;AAC5D,OAAO,EAAC,OAAO,IAAI,KAAK,EAAC,MAAM,YAAY,CAAC;AAE5C,uDAAuD;AACvD,OAAO,EAAC,OAAO,IAAI,KAAK,EAAC,MAAM,YAAY,CAAC;AAC5C,OAAO,EAAC,OAAO,IAAI,QAAQ,EAAC,MAAM,eAAe,CAAC;AAClD,OAAO,EAAC,OAAO,IAAI,SAAS,EAAC,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAC,OAAO,IAAI,iBAAiB,EAAC,MAAM,0BAA0B,CAAC;AAEtE,wDAAwD;AACxD,OAAO,EAAC,OAAO,IAAI,IAAI,EAAC,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAC,OAAO,IAAI,IAAI,EAAC,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAC,QAAQ,EAAE,KAAK,EAAC,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAAC,OAAO,IAAI,KAAK,EAAC,MAAM,YAAY,CAAC;AAE5C,qDAAqD;AACrD,OAAO,EAAC,OAAO,IAAI,KAAK,EAAC,MAAM,YAAY,CAAC;AAE5C,wDAAwD;AACxD,OAAO,EAAC,OAAO,IAAI,oBAAoB,EAAC,MAAM,8BAA8B,CAAC;AAC7E,mBAAmB;AACnB,OAAO,EAAC,UAAU,EAAE,cAAc,EAAE,UAAU,EAAE,YAAY,EAAC,MAAM,oBAAoB,CAAC;AAIxF,wDAAwD;AACxD,mBAAmB;AACnB,cAAc,YAAY,CAAC;AAC3B,mBAAmB;AACnB,cAAc,wBAAwB,CAAC","sourcesContent":["// src/index.ts\n\n// ─── Types ───────────────────────────────────────────\nexport * from './types.js';\nexport {default as Type} from './type.js';\nexport {default as TypeCode} from './type-code.js';\n\n// ─── Utilities ───────────────────────────────────────\nexport * from './utilities.js';\nexport * from './functions.js';\nexport * from './string.js';\nexport * from './math.js';\nexport * from './parse.js';\nexport {default as CancellablePromise} from \"./cancellable-promise.js\";\n\n// ─── Data Structures ────────────────────────────────\nexport {default as KeyValuePair} from './key-value-pair.js';\nexport {default as Order} from './order.js';\n\n// ─── Async & Timing ─────────────────────────────────\nexport {default as Timer} from './timer.js';\nexport {default as TimeSpan} from './timespan.js';\nexport {default as Stopwatch} from './stopwatch.js';\nexport {default as AsyncPoolExecutor} from './async-pool-executor.js';\n\n// ─── Patterns ────────────────────────────────────────\nexport {default as Lazy} from './lazy.js';\nexport {default as from} from './collections/from.js';\nexport {EventBus, Event} from './event-bus.js';\nexport {default as Range} from './range.js';\n\n// ─── Draft ────────────────────────────────────────\nexport {default as draft} from './draft.js';\n\n// ─── Web ─────────────────────────────────────────────\nexport {default as CleanURLSearchParams} from './clean-url-search-params.js';\n/** @deprecated */\nexport {ajaxSubmit, ajaxSubmission, submitForm, formToObject} from './browser/index.js';\n\n\n\n// ─── Deprecated ──────────────────────────────────────\n/** @deprecated */\nexport * from './array.js';\n/** @deprecated */\nexport * from './collections/index.js';"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,eAAe;AAEf,wDAAwD;AACxD,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAC,OAAO,IAAI,IAAI,EAAC,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAC,OAAO,IAAI,QAAQ,EAAC,MAAM,gBAAgB,CAAC;AAEnD,wDAAwD;AACxD,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAC,OAAO,IAAI,kBAAkB,EAAC,MAAM,0BAA0B,CAAC;AAEvE,uDAAuD;AACvD,OAAO,EAAC,OAAO,IAAI,YAAY,EAAC,MAAM,qBAAqB,CAAC;AAC5D,OAAO,EAAC,OAAO,IAAI,KAAK,EAAC,MAAM,YAAY,CAAC;AAE5C,uDAAuD;AACvD,OAAO,EAAC,OAAO,IAAI,KAAK,EAAC,MAAM,YAAY,CAAC;AAC5C,OAAO,EAAC,OAAO,IAAI,QAAQ,EAAC,MAAM,eAAe,CAAC;AAClD,OAAO,EAAC,OAAO,IAAI,SAAS,EAAC,MAAM,gBAAgB,CAAC;AAEpD,OAAO,EAAC,WAAW,EAAE,YAAY,EAAC,MAAM,eAAe,CAAC;AACxD,OAAO,EAAC,OAAO,IAAI,iBAAiB,EAAC,MAAM,0BAA0B,CAAC;AAEtE,wDAAwD;AACxD,OAAO,EAAC,OAAO,IAAI,IAAI,EAAC,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAC,OAAO,IAAI,IAAI,EAAC,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAC,QAAQ,EAAE,KAAK,EAAC,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAAC,OAAO,IAAI,KAAK,EAAC,MAAM,YAAY,CAAC;AAE5C,qDAAqD;AACrD,OAAO,EAAC,OAAO,IAAI,KAAK,EAAC,MAAM,YAAY,CAAC;AAE5C,wDAAwD;AACxD,OAAO,EAAC,OAAO,IAAI,oBAAoB,EAAC,MAAM,8BAA8B,CAAC;AAC7E,mBAAmB;AACnB,OAAO,EAAC,UAAU,EAAE,cAAc,EAAE,UAAU,EAAE,YAAY,EAAC,MAAM,oBAAoB,CAAC;AAIxF,wDAAwD;AACxD,mBAAmB;AACnB,cAAc,YAAY,CAAC;AAC3B,mBAAmB;AACnB,cAAc,wBAAwB,CAAC","sourcesContent":["// src/index.ts\n\n// ─── Types ───────────────────────────────────────────\nexport * from './types.js';\nexport {default as Type} from './type.js';\nexport {default as TypeCode} from './type-code.js';\n\n// ─── Utilities ───────────────────────────────────────\nexport * from './utilities.js';\nexport * from './functions.js';\nexport * from './string.js';\nexport * from './math.js';\nexport * from './parse.js';\nexport {default as CancellablePromise} from \"./cancellable-promise.js\";\n\n// ─── Data Structures ────────────────────────────────\nexport {default as KeyValuePair} from './key-value-pair.js';\nexport {default as Order} from './order.js';\n\n// ─── Async & Timing ─────────────────────────────────\nexport {default as Timer} from './timer.js';\nexport {default as TimeSpan} from './timespan.js';\nexport {default as Stopwatch} from './stopwatch.js';\nexport type {Result} from './result.js';\nexport {TimeTrigger, CountTrigger} from './triggers.js';\nexport {default as AsyncPoolExecutor} from './async-pool-executor.js';\n\n// ─── Patterns ────────────────────────────────────────\nexport {default as Lazy} from './lazy.js';\nexport {default as from} from './collections/from.js';\nexport {EventBus, Event} from './event-bus.js';\nexport {default as Range} from './range.js';\n\n// ─── Draft ────────────────────────────────────────\nexport {default as draft} from './draft.js';\n\n// ─── Web ─────────────────────────────────────────────\nexport {default as CleanURLSearchParams} from './clean-url-search-params.js';\n/** @deprecated */\nexport {ajaxSubmit, ajaxSubmission, submitForm, formToObject} from './browser/index.js';\n\n\n\n// ─── Deprecated ──────────────────────────────────────\n/** @deprecated */\nexport * from './array.js';\n/** @deprecated */\nexport * from './collections/index.js';"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=result.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result.js","sourceRoot":"","sources":["../../src/result.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Represents success and failure outputs narrowed by `success`.\n */\nexport type Result<TSuccessOutput, TFailureOutput = undefined> =\n | {readonly success: true; readonly output: TSuccessOutput}\n | {readonly success: false; readonly output?: TFailureOutput};\n"]}
@@ -0,0 +1,99 @@
1
+ import TimeSpan from './timespan.js';
2
+ /**
3
+ * Runs a callback at most once during a configured time interval.
4
+ */
5
+ export class TimeTrigger {
6
+ constructor(callback, interval) {
7
+ this.callback = callback;
8
+ this.interval = interval;
9
+ this.lastRunTime = null;
10
+ }
11
+ /**
12
+ * Executes the callback when the configured interval has elapsed.
13
+ */
14
+ async check() {
15
+ const currentTime = performance.now();
16
+ if (this.lastRunTime === null
17
+ || currentTime - this.lastRunTime >= this.interval.milliseconds) {
18
+ return { success: true, output: await this.trigger() };
19
+ }
20
+ return { success: false };
21
+ }
22
+ /**
23
+ * Executes the callback regardless of the configured interval.
24
+ */
25
+ async trigger() {
26
+ this.lastRunTime = performance.now();
27
+ return this.callback();
28
+ }
29
+ /**
30
+ * Replaces the callback executed by this trigger.
31
+ */
32
+ setCallback(callback) {
33
+ this.callback = callback;
34
+ }
35
+ /**
36
+ * Gets the configured time interval.
37
+ */
38
+ getInterval() {
39
+ return this.interval;
40
+ }
41
+ /**
42
+ * Replaces the configured time interval.
43
+ */
44
+ setInterval(interval) {
45
+ this.interval = interval;
46
+ }
47
+ }
48
+ /**
49
+ * Runs a callback after a configured number of checks.
50
+ */
51
+ export class CountTrigger {
52
+ constructor(callback, interval) {
53
+ this.interval = 1;
54
+ this.checkCount = 0;
55
+ this.callback = callback;
56
+ this.setInterval(interval);
57
+ }
58
+ /**
59
+ * Executes the callback when the configured number of checks is reached.
60
+ */
61
+ async check() {
62
+ this.checkCount++;
63
+ if (this.checkCount >= this.interval) {
64
+ return { success: true, output: await this.trigger() };
65
+ }
66
+ return { success: false };
67
+ }
68
+ /**
69
+ * Executes the callback regardless of the configured check count.
70
+ */
71
+ async trigger() {
72
+ this.checkCount = 0;
73
+ return this.callback();
74
+ }
75
+ /**
76
+ * Replaces the callback executed by this trigger.
77
+ */
78
+ setCallback(callback) {
79
+ this.callback = callback;
80
+ }
81
+ /**
82
+ * Gets the configured check interval.
83
+ */
84
+ getInterval() {
85
+ return this.interval;
86
+ }
87
+ /**
88
+ * Replaces the configured check interval.
89
+ *
90
+ * @throws {RangeError} When the interval is less than one.
91
+ */
92
+ setInterval(interval) {
93
+ if (interval < 1) {
94
+ throw new RangeError('Interval must be greater than zero.');
95
+ }
96
+ this.interval = interval;
97
+ }
98
+ }
99
+ //# sourceMappingURL=triggers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"triggers.js","sourceRoot":"","sources":["../../src/triggers.ts"],"names":[],"mappings":"AACA,OAAO,QAAQ,MAAM,eAAe,CAAC;AAGrC;;GAEG;AACH,MAAM,OAAO,WAAW;IAKpB,YAAmB,QAAkC,EAAE,QAAkB;QACrE,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC;IAC5B,CAAC;IAED;;OAEG;IACI,KAAK,CAAC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AE,EAAC,CAAC;QACzD,CAAC;QAED,OAAO,EAAC,OAAO,EAAE,KAAK,EAAC,CAAC;IAC5B,CAAC;IAED;;OAEG;IACI,KAAK,CAAC,OAAO;QAChB,IAAI,CAAC,UAAU,GAAG,CAAC,CAAC;QACpB,OAAO,IAAI,CAAC,QAAQ,EAAE,CAAC;IAC3B,CAAC;IAED;;OAEG;IACI,WAAW,CAAC,QAAkC;QACjD,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC7B,CAAC;IAED;;OAEG;IACI,WAAW;QACd,OAAO,IAAI,CAAC,QAAQ,CAAC;IACzB,CAAC;IAED;;;;OAIG;IACI,WAAW,CAAC,QAAgB;QAC/B,IAAI,QAAQ,GAAG,CAAC,EAAE,CAAC;YACf,MAAM,IAAI,UAAU,CAAC,qCAAqC,CAAC,CAAC;QAChE,CAAC;QAED,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC7B,CAAC;CACJ","sourcesContent":["import type {Result} from './result.js';\nimport TimeSpan from './timespan.js';\nimport type {Nullable, Supplier} from './types.js';\n\n/**\n * Runs a callback at most once during a configured time interval.\n */\nexport class TimeTrigger<T> {\n private callback: Supplier<T | Promise<T>>;\n private interval: TimeSpan;\n private lastRunTime: Nullable<number>;\n\n public constructor(callback: Supplier<T | Promise<T>>, interval: TimeSpan) {\n this.callback = callback;\n this.interval = interval;\n this.lastRunTime = null;\n }\n\n /**\n * Executes the callback when the configured interval has elapsed.\n */\n public async check(): Promise<Result<T>> {\n const currentTime = performance.now();\n\n if (this.lastRunTime === null\n || currentTime - this.lastRunTime >= this.interval.milliseconds\n ) {\n return {success: true, output: await this.trigger()};\n }\n\n return {success: false};\n }\n\n /**\n * Executes the callback regardless of the configured interval.\n */\n public async trigger(): Promise<T> {\n this.lastRunTime = performance.now();\n return this.callback();\n }\n\n /**\n * Replaces the callback executed by this trigger.\n */\n public setCallback(callback: Supplier<T | Promise<T>>): void {\n this.callback = callback;\n }\n\n /**\n * Gets the configured time interval.\n */\n public getInterval(): TimeSpan {\n return this.interval;\n }\n\n /**\n * Replaces the configured time interval.\n */\n public setInterval(interval: TimeSpan): void {\n this.interval = interval;\n }\n\n\n}\n\n/**\n * Runs a callback after a configured number of checks.\n */\nexport class CountTrigger<T> {\n private callback: Supplier<T | Promise<T>>;\n private interval: number = 1;\n private checkCount: number = 0;\n\n public constructor(callback: Supplier<T | Promise<T>>, interval: number) {\n this.callback = callback;\n this.setInterval(interval);\n }\n\n /**\n * Executes the callback when the configured number of checks is reached.\n */\n public async check(): Promise<Result<T>> {\n this.checkCount++;\n\n if (this.checkCount >= this.interval) {\n return {success: true, output: await this.trigger()};\n }\n\n return {success: false};\n }\n\n /**\n * Executes the callback regardless of the configured check count.\n */\n public async trigger(): Promise<T> {\n this.checkCount = 0;\n return this.callback();\n }\n\n /**\n * Replaces the callback executed by this trigger.\n */\n public setCallback(callback: Supplier<T | Promise<T>>): void {\n this.callback = callback;\n }\n\n /**\n * Gets the configured check interval.\n */\n public getInterval(): number {\n return this.interval;\n }\n\n /**\n * Replaces the configured check interval.\n *\n * @throws {RangeError} When the interval is less than one.\n */\n public setInterval(interval: number): void {\n if (interval < 1) {\n throw new RangeError('Interval must be greater than zero.');\n }\n\n this.interval = interval;\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@develia/commons",
3
- "version": "0.4.22",
3
+ "version": "0.4.23",
4
4
  "author": {
5
5
  "name": "Antonio Gil Espinosa",
6
6
  "email": "antonio.gil.espinosa@gmail.com",
@@ -78,12 +78,11 @@
78
78
  "scripts": {
79
79
  "clean": "rimraf dist || rm -rf dist",
80
80
  "build:esm": "tsc -p tsconfig.esm.json",
81
- "build:cjs": "tsc -p tsconfig.cjs.json && node scripts/write-cjs-package.mjs",
81
+ "build:cjs": "tsc -p tsconfig.cjs.json",
82
82
  "build:web": "tsc -p tsconfig.web.json",
83
83
  "build": "npm run clean && npm run build:esm && npm run build:cjs && npm run build:web",
84
84
  "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
85
- "test:package": "node scripts/verify-package.mjs",
86
- "validate": "npm test && npm run build && npm run test:package",
85
+ "validate": "npm test && npm run build",
87
86
  "prepack": "npm run validate"
88
87
  },
89
88
  "files": [
@@ -46,9 +46,7 @@ export default class CacheDictionary<T> {
46
46
  if (duration === null) {
47
47
  delete this.expiration[key];
48
48
  } else {
49
- const expiration = new Date();
50
- expiration.setSeconds(expiration.getSeconds() + duration);
51
- this.expiration[key] = expiration;
49
+ this.expiration[key] = this.calculateExpiration(duration);
52
50
  }
53
51
  }
54
52
 
@@ -109,8 +107,6 @@ export default class CacheDictionary<T> {
109
107
  return undefined;
110
108
  }
111
109
 
112
- const expiration = new Date();
113
- expiration.setSeconds(expiration.getSeconds() + duration);
114
- return expiration;
110
+ return new Date(Date.now() + duration * 1000);
115
111
  }
116
112
  }
package/src/index.ts CHANGED
@@ -21,6 +21,8 @@ export {default as Order} from './order.js';
21
21
  export {default as Timer} from './timer.js';
22
22
  export {default as TimeSpan} from './timespan.js';
23
23
  export {default as Stopwatch} from './stopwatch.js';
24
+ export type {Result} from './result.js';
25
+ export {TimeTrigger, CountTrigger} from './triggers.js';
24
26
  export {default as AsyncPoolExecutor} from './async-pool-executor.js';
25
27
 
26
28
  // ─── Patterns ────────────────────────────────────────
package/src/result.ts ADDED
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Represents success and failure outputs narrowed by `success`.
3
+ */
4
+ export type Result<TSuccessOutput, TFailureOutput = undefined> =
5
+ | {readonly success: true; readonly output: TSuccessOutput}
6
+ | {readonly success: false; readonly output?: TFailureOutput};
@@ -0,0 +1,126 @@
1
+ import type {Result} from './result.js';
2
+ import TimeSpan from './timespan.js';
3
+ import type {Nullable, Supplier} from './types.js';
4
+
5
+ /**
6
+ * Runs a callback at most once during a configured time interval.
7
+ */
8
+ export class TimeTrigger<T> {
9
+ private callback: Supplier<T | Promise<T>>;
10
+ private interval: TimeSpan;
11
+ private lastRunTime: Nullable<number>;
12
+
13
+ public constructor(callback: Supplier<T | Promise<T>>, interval: TimeSpan) {
14
+ this.callback = callback;
15
+ this.interval = interval;
16
+ this.lastRunTime = null;
17
+ }
18
+
19
+ /**
20
+ * Executes the callback when the configured interval has elapsed.
21
+ */
22
+ public async check(): Promise<Result<T>> {
23
+ const currentTime = performance.now();
24
+
25
+ if (this.lastRunTime === null
26
+ || currentTime - this.lastRunTime >= this.interval.milliseconds
27
+ ) {
28
+ return {success: true, output: await this.trigger()};
29
+ }
30
+
31
+ return {success: false};
32
+ }
33
+
34
+ /**
35
+ * Executes the callback regardless of the configured interval.
36
+ */
37
+ public async trigger(): Promise<T> {
38
+ this.lastRunTime = performance.now();
39
+ return this.callback();
40
+ }
41
+
42
+ /**
43
+ * Replaces the callback executed by this trigger.
44
+ */
45
+ public setCallback(callback: Supplier<T | Promise<T>>): void {
46
+ this.callback = callback;
47
+ }
48
+
49
+ /**
50
+ * Gets the configured time interval.
51
+ */
52
+ public getInterval(): TimeSpan {
53
+ return this.interval;
54
+ }
55
+
56
+ /**
57
+ * Replaces the configured time interval.
58
+ */
59
+ public setInterval(interval: TimeSpan): void {
60
+ this.interval = interval;
61
+ }
62
+
63
+
64
+ }
65
+
66
+ /**
67
+ * Runs a callback after a configured number of checks.
68
+ */
69
+ export class CountTrigger<T> {
70
+ private callback: Supplier<T | Promise<T>>;
71
+ private interval: number = 1;
72
+ private checkCount: number = 0;
73
+
74
+ public constructor(callback: Supplier<T | Promise<T>>, interval: number) {
75
+ this.callback = callback;
76
+ this.setInterval(interval);
77
+ }
78
+
79
+ /**
80
+ * Executes the callback when the configured number of checks is reached.
81
+ */
82
+ public async check(): Promise<Result<T>> {
83
+ this.checkCount++;
84
+
85
+ if (this.checkCount >= this.interval) {
86
+ return {success: true, output: await this.trigger()};
87
+ }
88
+
89
+ return {success: false};
90
+ }
91
+
92
+ /**
93
+ * Executes the callback regardless of the configured check count.
94
+ */
95
+ public async trigger(): Promise<T> {
96
+ this.checkCount = 0;
97
+ return this.callback();
98
+ }
99
+
100
+ /**
101
+ * Replaces the callback executed by this trigger.
102
+ */
103
+ public setCallback(callback: Supplier<T | Promise<T>>): void {
104
+ this.callback = callback;
105
+ }
106
+
107
+ /**
108
+ * Gets the configured check interval.
109
+ */
110
+ public getInterval(): number {
111
+ return this.interval;
112
+ }
113
+
114
+ /**
115
+ * Replaces the configured check interval.
116
+ *
117
+ * @throws {RangeError} When the interval is less than one.
118
+ */
119
+ public setInterval(interval: number): void {
120
+ if (interval < 1) {
121
+ throw new RangeError('Interval must be greater than zero.');
122
+ }
123
+
124
+ this.interval = interval;
125
+ }
126
+ }
@@ -1,3 +0,0 @@
1
- {
2
- "type": "commonjs"
3
- }