@xylabs/base 4.8.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.
package/README.md ADDED
@@ -0,0 +1,69 @@
1
+ [![logo][]](https://xylabs.com)
2
+
3
+ # @xylabs/base
4
+
5
+ [![npm-badge][]][npm-link]
6
+ [![npm-downloads-badge][]][npm-link]
7
+ [![jsdelivr-badge][]][jsdelivr-link]
8
+ [![npm-license-badge][]](LICENSE)
9
+ [![socket-badge][]][socket-link]
10
+
11
+ > XY Labs generalized Javascript library
12
+
13
+ ## Table of Contents
14
+
15
+ - [Description](#description)
16
+ - [Install](#install)
17
+ - [Maintainers](#maintainers)
18
+ - [License](#license)
19
+ - [Credits](#credits)
20
+
21
+ ## Description
22
+
23
+ Common Javascript code that is used throughout XYO projects that use React.
24
+
25
+ ## Install
26
+
27
+ Using npm:
28
+
29
+ ```sh
30
+ npm i --save @xylabs/base
31
+ ```
32
+
33
+ Using yarn:
34
+
35
+ ```sh
36
+ yarn add @xylabs/base
37
+ ```
38
+
39
+ ## Documentation
40
+ [Developer Reference](https://xylabs.github.io/sdk-js)
41
+
42
+ ## Maintainers
43
+
44
+ - [Arie Trouw](https://github.com/arietrouw) ([arietrouw.com](https://arietrouw.com))
45
+ - [Joel Carter](https://github.com/JoelBCarter)
46
+ - [Matt Jones](https://github.com/jonesmac)
47
+ - [Jordan Trouw](https://github.com/jordantrouw)
48
+
49
+ ## License
50
+
51
+ See the [LICENSE](LICENSE) file for license details
52
+
53
+ ## Credits
54
+
55
+ [Made with 🔥and ❄️ by XY Labs](https://xylabs.com)
56
+
57
+ [logo]: https://cdn.xy.company/img/brand/XYPersistentCompany_Logo_Icon_Colored.svg
58
+
59
+ [npm-badge]: https://img.shields.io/npm/v/@xylabs/base.svg
60
+ [npm-link]: https://www.npmjs.com/package/@xylabs/base
61
+
62
+ [npm-downloads-badge]: https://img.shields.io/npm/dw/@xylabs/base
63
+ [npm-license-badge]: https://img.shields.io/npm/l/@xylabs/base
64
+
65
+ [jsdelivr-badge]: https://data.jsdelivr.com/v1/package/npm/@xylabs/base/badge
66
+ [jsdelivr-link]: https://www.jsdelivr.com/package/npm/@xylabs/base
67
+
68
+ [socket-badge]: https://socket.dev/api/badge/npm/package/@xylabs/base
69
+ [socket-link]: https://socket.dev/npm/package/@xylabs/base
@@ -0,0 +1,143 @@
1
+ // src/Base.ts
2
+ import { assertEx } from "@xylabs/assert";
3
+
4
+ // src/globallyUnique.ts
5
+ var xyoGlobal = () => {
6
+ globalThis.xylabs = globalThis.xylabs ?? {};
7
+ return globalThis.xylabs;
8
+ };
9
+ var disableGloballyUnique = () => {
10
+ xyoGlobal().uniqueDisabled = true;
11
+ };
12
+ var globallyUnique = (name, value, domain = "global") => {
13
+ const uniqueName = [domain, name].join(":");
14
+ if (!xyoGlobal().uniqueDisabled) {
15
+ const xylabs = globalThis.xylabs = globalThis.xylabs ?? {};
16
+ const unique = xylabs.unique = xylabs.unique ?? {};
17
+ if (unique[uniqueName] === void 0) {
18
+ unique[uniqueName] = value;
19
+ } else {
20
+ if (unique[uniqueName] !== value) {
21
+ throw new Error(
22
+ `Global unique item ${uniqueName} already defined. Make sure you are not importing two versions of the package that contains this item`
23
+ );
24
+ }
25
+ }
26
+ }
27
+ return uniqueName;
28
+ };
29
+
30
+ // src/Base.ts
31
+ var DEFAULT_HISTORY_INTERVAL = 1e3 * 5;
32
+ var DEFAULT_HISTORY_TIME = 60 * 60 * 1e3;
33
+ var MAX_GC_FREQUENCY = 1e3 * 60;
34
+ var MIN_GC_FREQUENCY = 1e3;
35
+ var MIN_HISTORY_INTERVAL = 1e3;
36
+ var Base = class _Base {
37
+ static defaultLogger;
38
+ static globalInstances = {};
39
+ static globalInstancesCountHistory = {};
40
+ static uniqueName = globallyUnique(this.name, this, "xyo");
41
+ static _historyInterval = DEFAULT_HISTORY_INTERVAL;
42
+ static _historyTime = DEFAULT_HISTORY_TIME;
43
+ static _historyTimeout;
44
+ static _lastGC = 0;
45
+ static _maxGcFrequency = MAX_GC_FREQUENCY;
46
+ _params;
47
+ constructor(params) {
48
+ this._params = params;
49
+ params?.logger?.debug(`Base constructed [${Object(this).name}]`);
50
+ this.recordInstance();
51
+ }
52
+ static get historyInterval() {
53
+ return this._historyInterval;
54
+ }
55
+ static set historyInterval(value) {
56
+ assertEx(value <= this.historyTime, () => `historyInterval [${value}] must be less than or equal to historyTime [${this.historyTime}]`);
57
+ this._historyInterval = Math.max(value, MIN_HISTORY_INTERVAL);
58
+ }
59
+ static get historyTime() {
60
+ return this._historyTime;
61
+ }
62
+ static set historyTime(value) {
63
+ assertEx(value >= this.historyInterval, () => `historyTime [${value}] must be greater than or equal to historyInterval [${this.historyInterval}]`);
64
+ this._historyInterval = value;
65
+ }
66
+ static get maxGcFrequency() {
67
+ return this._maxGcFrequency;
68
+ }
69
+ static set maxGcFrequency(value) {
70
+ this._maxGcFrequency = Math.max(value, MIN_GC_FREQUENCY);
71
+ }
72
+ static get maxHistoryDepth() {
73
+ return Math.floor(this.historyTime / this.historyInterval);
74
+ }
75
+ get logger() {
76
+ return this.params?.logger ?? _Base.defaultLogger;
77
+ }
78
+ get params() {
79
+ return this._params;
80
+ }
81
+ static gc(classNameOrForce = false) {
82
+ if (typeof classNameOrForce === "string") {
83
+ this.gcClass(classNameOrForce);
84
+ } else {
85
+ if (classNameOrForce || Date.now() - this._lastGC > this._maxGcFrequency) {
86
+ this.gcAll();
87
+ }
88
+ }
89
+ }
90
+ static instanceCount(className) {
91
+ return this.globalInstances[className]?.length ?? 0;
92
+ }
93
+ static instanceCounts() {
94
+ this.gc();
95
+ const result = {};
96
+ for (const [className, instances] of Object.entries(this.globalInstances)) result[className] = instances.length;
97
+ return result;
98
+ }
99
+ static startHistory() {
100
+ if (this._historyTimeout) {
101
+ this.stopHistory();
102
+ }
103
+ const timeoutHandler = () => {
104
+ if (this._historyTimeout) {
105
+ this.addToHistory();
106
+ this._historyTimeout = setTimeout(timeoutHandler, this.historyInterval);
107
+ }
108
+ };
109
+ this._historyTimeout = setTimeout(timeoutHandler, this.historyInterval);
110
+ }
111
+ static stopHistory() {
112
+ if (this._historyTimeout) {
113
+ clearTimeout(this._historyTimeout);
114
+ this._historyTimeout = void 0;
115
+ }
116
+ }
117
+ static addToHistory() {
118
+ const counts = this.instanceCounts();
119
+ for (const className of Object.keys(this.globalInstances)) {
120
+ this.globalInstancesCountHistory[className] = this.globalInstancesCountHistory[className]?.slice(-this.maxHistoryDepth) ?? [];
121
+ this.globalInstancesCountHistory[className].push(counts[className]);
122
+ }
123
+ }
124
+ static gcAll() {
125
+ for (const className of Object.keys(this.globalInstances)) {
126
+ this.gcClass(className);
127
+ }
128
+ }
129
+ static gcClass(className) {
130
+ this.globalInstances[className] = this.globalInstances[className]?.filter((ref) => ref.deref() !== null) ?? [];
131
+ }
132
+ recordInstance() {
133
+ const instanceArray = _Base.globalInstances[this.constructor.name] ?? [];
134
+ instanceArray.push(new WeakRef(this));
135
+ _Base.globalInstances[this.constructor.name] = instanceArray;
136
+ }
137
+ };
138
+ export {
139
+ Base,
140
+ disableGloballyUnique,
141
+ globallyUnique
142
+ };
143
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/Base.ts","../../src/globallyUnique.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport type { Logger } from '@xylabs/logger'\nimport type { EmptyObject } from '@xylabs/object'\n\nimport { globallyUnique } from './globallyUnique.ts'\n\nconst DEFAULT_HISTORY_INTERVAL = 1000 * 5\nconst DEFAULT_HISTORY_TIME = 60 * 60 * 1000\nconst MAX_GC_FREQUENCY = 1000 * 60\nconst MIN_GC_FREQUENCY = 1000\nconst MIN_HISTORY_INTERVAL = 1000\n\nexport type BaseClassName = Exclude<string, 'base-class-name-reserved-32546239486'>\n\nexport type BaseParamsFields = {\n logger?: Logger\n}\n\nexport type BaseParams<TAdditionalParams extends EmptyObject | void = void> =\n TAdditionalParams extends EmptyObject ? BaseParamsFields & TAdditionalParams : BaseParamsFields\n\nexport abstract class Base<TParams extends BaseParams | undefined = BaseParams> {\n static defaultLogger?: Logger\n static readonly globalInstances: Record<BaseClassName, WeakRef<Base<BaseParams | undefined>>[]> = {}\n static readonly globalInstancesCountHistory: Record<BaseClassName, number[]> = {}\n static readonly uniqueName = globallyUnique(this.name, this, 'xyo')\n private static _historyInterval = DEFAULT_HISTORY_INTERVAL\n private static _historyTime = DEFAULT_HISTORY_TIME\n private static _historyTimeout?: ReturnType<typeof setTimeout>\n private static _lastGC = 0\n private static _maxGcFrequency = MAX_GC_FREQUENCY\n private _params: TParams\n\n constructor(params: TParams) {\n this._params = params\n params?.logger?.debug(`Base constructed [${Object(this).name}]`)\n this.recordInstance()\n }\n\n static get historyInterval() {\n return this._historyInterval\n }\n\n static set historyInterval(value: number) {\n assertEx(value <= this.historyTime, () => `historyInterval [${value}] must be less than or equal to historyTime [${this.historyTime}]`)\n this._historyInterval = Math.max(value, MIN_HISTORY_INTERVAL)\n }\n\n static get historyTime() {\n return this._historyTime\n }\n\n static set historyTime(value: number) {\n assertEx(value >= this.historyInterval, () => `historyTime [${value}] must be greater than or equal to historyInterval [${this.historyInterval}]`)\n this._historyInterval = value\n }\n\n static get maxGcFrequency() {\n return this._maxGcFrequency\n }\n\n static set maxGcFrequency(value: number) {\n this._maxGcFrequency = Math.max(value, MIN_GC_FREQUENCY)\n }\n\n static get maxHistoryDepth() {\n return Math.floor(this.historyTime / this.historyInterval)\n }\n\n get logger() {\n return this.params?.logger ?? Base.defaultLogger\n }\n\n get params() {\n return this._params\n }\n\n static gc(force?: boolean): void\n static gc(className: string): void\n static gc(classNameOrForce: string | boolean = false): void {\n if (typeof classNameOrForce === 'string') {\n this.gcClass(classNameOrForce)\n } else {\n if (classNameOrForce || Date.now() - this._lastGC > this._maxGcFrequency) {\n this.gcAll()\n }\n }\n }\n\n static instanceCount(className: string): number {\n return this.globalInstances[className]?.length ?? 0\n }\n\n static instanceCounts(): Record<BaseClassName, number> {\n this.gc()\n const result: Record<BaseClassName, number> = {}\n for (const [className, instances] of Object.entries(this.globalInstances)) result[className] = instances.length\n return result\n }\n\n static startHistory(): void {\n if (this._historyTimeout) {\n this.stopHistory()\n }\n\n const timeoutHandler = () => {\n if (this._historyTimeout) {\n this.addToHistory()\n this._historyTimeout = setTimeout(timeoutHandler, this.historyInterval)\n }\n }\n\n this._historyTimeout = setTimeout(timeoutHandler, this.historyInterval)\n }\n\n static stopHistory(): void {\n if (this._historyTimeout) {\n clearTimeout(this._historyTimeout)\n this._historyTimeout = undefined\n }\n }\n\n private static addToHistory() {\n const counts = this.instanceCounts()\n for (const className of Object.keys(this.globalInstances)) {\n this.globalInstancesCountHistory[className] = this.globalInstancesCountHistory[className]?.slice(-this.maxHistoryDepth) ?? []\n this.globalInstancesCountHistory[className].push(counts[className])\n }\n }\n\n private static gcAll() {\n for (const className of Object.keys(this.globalInstances)) {\n this.gcClass(className)\n }\n }\n\n private static gcClass(className: BaseClassName) {\n // remove all the weak refs that are now empty\n this.globalInstances[className] = this.globalInstances[className]?.filter(ref => ref.deref() !== null) ?? []\n }\n\n private recordInstance() {\n const instanceArray = Base.globalInstances[this.constructor.name] ?? []\n instanceArray.push(new WeakRef(this))\n Base.globalInstances[this.constructor.name] = instanceArray\n }\n}\n","declare global {\n var xylabs: {\n unique: Record<string, unknown>\n uniqueDisabled?: boolean\n }\n}\n\nconst xyoGlobal = () => {\n globalThis.xylabs = globalThis.xylabs ?? {}\n return globalThis.xylabs\n}\n\nexport const disableGloballyUnique = () => {\n xyoGlobal().uniqueDisabled = true\n}\n\nexport const globallyUnique = (name: string, value: unknown, domain = 'global') => {\n const uniqueName = [domain, name].join(':')\n if (!xyoGlobal().uniqueDisabled) {\n const xylabs = globalThis.xylabs = globalThis.xylabs ?? {}\n const unique = (xylabs.unique = xylabs.unique ?? {})\n if (unique[uniqueName] === undefined) {\n unique[uniqueName] = value\n } else {\n if (unique[uniqueName] !== value) {\n throw new Error(\n `Global unique item ${uniqueName} already defined. Make sure you are not importing two versions of the package that contains this item`,\n )\n }\n }\n }\n return uniqueName\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;;;ACOzB,IAAM,YAAY,MAAM;AACtB,aAAW,SAAS,WAAW,UAAU,CAAC;AAC1C,SAAO,WAAW;AACpB;AAEO,IAAM,wBAAwB,MAAM;AACzC,YAAU,EAAE,iBAAiB;AAC/B;AAEO,IAAM,iBAAiB,CAAC,MAAc,OAAgB,SAAS,aAAa;AACjF,QAAM,aAAa,CAAC,QAAQ,IAAI,EAAE,KAAK,GAAG;AAC1C,MAAI,CAAC,UAAU,EAAE,gBAAgB;AAC/B,UAAM,SAAS,WAAW,SAAS,WAAW,UAAU,CAAC;AACzD,UAAM,SAAU,OAAO,SAAS,OAAO,UAAU,CAAC;AAClD,QAAI,OAAO,UAAU,MAAM,QAAW;AACpC,aAAO,UAAU,IAAI;AAAA,IACvB,OAAO;AACL,UAAI,OAAO,UAAU,MAAM,OAAO;AAChC,cAAM,IAAI;AAAA,UACR,sBAAsB,UAAU;AAAA,QAClC;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACA,SAAO;AACT;;;AD1BA,IAAM,2BAA2B,MAAO;AACxC,IAAM,uBAAuB,KAAK,KAAK;AACvC,IAAM,mBAAmB,MAAO;AAChC,IAAM,mBAAmB;AACzB,IAAM,uBAAuB;AAWtB,IAAe,OAAf,MAAe,MAA0D;AAAA,EAC9E,OAAO;AAAA,EACP,OAAgB,kBAAkF,CAAC;AAAA,EACnG,OAAgB,8BAA+D,CAAC;AAAA,EAChF,OAAgB,aAAa,eAAe,KAAK,MAAM,MAAM,KAAK;AAAA,EAClE,OAAe,mBAAmB;AAAA,EAClC,OAAe,eAAe;AAAA,EAC9B,OAAe;AAAA,EACf,OAAe,UAAU;AAAA,EACzB,OAAe,kBAAkB;AAAA,EACzB;AAAA,EAER,YAAY,QAAiB;AAC3B,SAAK,UAAU;AACf,YAAQ,QAAQ,MAAM,qBAAqB,OAAO,IAAI,EAA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AAI;AAAA,EAChD;AACF;","names":[]}
@@ -0,0 +1,40 @@
1
+ import type { Logger } from '@xylabs/logger';
2
+ import type { EmptyObject } from '@xylabs/object';
3
+ export type BaseClassName = Exclude<string, 'base-class-name-reserved-32546239486'>;
4
+ export type BaseParamsFields = {
5
+ logger?: Logger;
6
+ };
7
+ export type BaseParams<TAdditionalParams extends EmptyObject | void = void> = TAdditionalParams extends EmptyObject ? BaseParamsFields & TAdditionalParams : BaseParamsFields;
8
+ export declare abstract class Base<TParams extends BaseParams | undefined = BaseParams> {
9
+ static defaultLogger?: Logger;
10
+ static readonly globalInstances: Record<BaseClassName, WeakRef<Base<BaseParams | undefined>>[]>;
11
+ static readonly globalInstancesCountHistory: Record<BaseClassName, number[]>;
12
+ static readonly uniqueName: string;
13
+ private static _historyInterval;
14
+ private static _historyTime;
15
+ private static _historyTimeout?;
16
+ private static _lastGC;
17
+ private static _maxGcFrequency;
18
+ private _params;
19
+ constructor(params: TParams);
20
+ static get historyInterval(): number;
21
+ static set historyInterval(value: number);
22
+ static get historyTime(): number;
23
+ static set historyTime(value: number);
24
+ static get maxGcFrequency(): number;
25
+ static set maxGcFrequency(value: number);
26
+ static get maxHistoryDepth(): number;
27
+ get logger(): Logger | undefined;
28
+ get params(): TParams;
29
+ static gc(force?: boolean): void;
30
+ static gc(className: string): void;
31
+ static instanceCount(className: string): number;
32
+ static instanceCounts(): Record<BaseClassName, number>;
33
+ static startHistory(): void;
34
+ static stopHistory(): void;
35
+ private static addToHistory;
36
+ private static gcAll;
37
+ private static gcClass;
38
+ private recordInstance;
39
+ }
40
+ //# sourceMappingURL=Base.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Base.d.ts","sourceRoot":"","sources":["../../src/Base.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAUjD,MAAM,MAAM,aAA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AAC,KAAK,EAAE,MAAM,EAEtC;IAED,MAAM,KAAK,eAAe,WAEzB;IAED,IAAI,MAAM,uBAET;IAED,IAAI,MAAM,YAET;IAED,MAAM,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,OAAO,GAAG,IAAI;IAChC,MAAM,CAAC,EAAE,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI;IAWlC,MAAM,CAAC,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM;IAI/C,MAAM,CAAC,cAAc,IAAI,MAAM,CAAC,aAAa,EAAE,MAAM,CAAC;IAOtD,MAAM,CAAC,YAAY,IAAI,IAAI;IAe3B,MAAM,CAAC,WAAW,IAAI,IAAI;IAO1B,OAAO,CAAC,MAAM,CAAC,YAAY;IAQ3B,OAAO,CAAC,MAAM,CAAC,KAAK;IAMpB,OAAO,CAAC,MAAM,CAAC,OAAO;IAKtB,OAAO,CAAC,cAAc;CAKvB"}
@@ -0,0 +1,9 @@
1
+ declare global {
2
+ var xylabs: {
3
+ unique: Record<string, unknown>;
4
+ uniqueDisabled?: boolean;
5
+ };
6
+ }
7
+ export declare const disableGloballyUnique: () => void;
8
+ export declare const globallyUnique: (name: string, value: unknown, domain?: string) => string;
9
+ //# sourceMappingURL=globallyUnique.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"globallyUnique.d.ts","sourceRoot":"","sources":["../../src/globallyUnique.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,CAAC;IACb,IAAI,MAAM,EAAE;QACV,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;QAC/B,cAAc,CAAC,EAAE,OAAO,CAAA;KACzB,CAAA;CACF;AAOD,eAAO,MAAM,qBAAqB,YAEjC,CAAA;AAED,eAAO,MAAM,cAAc,GAAI,MAAM,MAAM,EAAE,OAAO,OAAO,EAAE,eAAiB,WAgB7E,CAAA"}
@@ -0,0 +1,3 @@
1
+ export * from './Base.ts';
2
+ export * from './globallyUnique.ts';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAA;AACzB,cAAc,qBAAqB,CAAA"}
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@xylabs/base",
3
+ "version": "4.8.0",
4
+ "description": "Primary SDK for using XYO Protocol 2.0",
5
+ "homepage": "https://xyo.network",
6
+ "bugs": {
7
+ "url": "git+https://github.com/XYOracleNetwork/sdk-xyo-client-js/issues",
8
+ "email": "support@xyo.network"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/XYOracleNetwork/sdk-xyo-client-js.git"
13
+ },
14
+ "license": "LGPL-3.0-or-later",
15
+ "author": {
16
+ "name": "XYO Development Team",
17
+ "email": "support@xyo.network",
18
+ "url": "https://xyo.network"
19
+ },
20
+ "sideEffects": false,
21
+ "type": "module",
22
+ "exports": {
23
+ ".": {
24
+ "types": "./dist/types/index.d.ts",
25
+ "default": "./dist/neutral/index.mjs"
26
+ },
27
+ "./package.json": "./package.json"
28
+ },
29
+ "module": "dist/neutral/index.mjs",
30
+ "types": "dist/types/index.d.ts",
31
+ "dependencies": {
32
+ "@xylabs/assert": "^4.8.0",
33
+ "@xylabs/logger": "^4.8.0",
34
+ "@xylabs/object": "^4.8.0"
35
+ },
36
+ "devDependencies": {
37
+ "@xylabs/ts-scripts-yarn3": "^6.2.1",
38
+ "@xylabs/tsconfig": "^6.2.1",
39
+ "typescript": "^5.8.3"
40
+ },
41
+ "publishConfig": {
42
+ "access": "public"
43
+ }
44
+ }
package/src/Base.ts ADDED
@@ -0,0 +1,147 @@
1
+ import { assertEx } from '@xylabs/assert'
2
+ import type { Logger } from '@xylabs/logger'
3
+ import type { EmptyObject } from '@xylabs/object'
4
+
5
+ import { globallyUnique } from './globallyUnique.ts'
6
+
7
+ const DEFAULT_HISTORY_INTERVAL = 1000 * 5
8
+ const DEFAULT_HISTORY_TIME = 60 * 60 * 1000
9
+ const MAX_GC_FREQUENCY = 1000 * 60
10
+ const MIN_GC_FREQUENCY = 1000
11
+ const MIN_HISTORY_INTERVAL = 1000
12
+
13
+ export type BaseClassName = Exclude<string, 'base-class-name-reserved-32546239486'>
14
+
15
+ export type BaseParamsFields = {
16
+ logger?: Logger
17
+ }
18
+
19
+ export type BaseParams<TAdditionalParams extends EmptyObject | void = void> =
20
+ TAdditionalParams extends EmptyObject ? BaseParamsFields & TAdditionalParams : BaseParamsFields
21
+
22
+ export abstract class Base<TParams extends BaseParams | undefined = BaseParams> {
23
+ static defaultLogger?: Logger
24
+ static readonly globalInstances: Record<BaseClassName, WeakRef<Base<BaseParams | undefined>>[]> = {}
25
+ static readonly globalInstancesCountHistory: Record<BaseClassName, number[]> = {}
26
+ static readonly uniqueName = globallyUnique(this.name, this, 'xyo')
27
+ private static _historyInterval = DEFAULT_HISTORY_INTERVAL
28
+ private static _historyTime = DEFAULT_HISTORY_TIME
29
+ private static _historyTimeout?: ReturnType<typeof setTimeout>
30
+ private static _lastGC = 0
31
+ private static _maxGcFrequency = MAX_GC_FREQUENCY
32
+ private _params: TParams
33
+
34
+ constructor(params: TParams) {
35
+ this._params = params
36
+ params?.logger?.debug(`Base constructed [${Object(this).name}]`)
37
+ this.recordInstance()
38
+ }
39
+
40
+ static get historyInterval() {
41
+ return this._historyInterval
42
+ }
43
+
44
+ static set historyInterval(value: number) {
45
+ assertEx(value <= this.historyTime, () => `historyInterval [${value}] must be less than or equal to historyTime [${this.historyTime}]`)
46
+ this._historyInterval = Math.max(value, MIN_HISTORY_INTERVAL)
47
+ }
48
+
49
+ static get historyTime() {
50
+ return this._historyTime
51
+ }
52
+
53
+ static set historyTime(value: number) {
54
+ assertEx(value >= this.historyInterval, () => `historyTime [${value}] must be greater than or equal to historyInterval [${this.historyInterval}]`)
55
+ this._historyInterval = value
56
+ }
57
+
58
+ static get maxGcFrequency() {
59
+ return this._maxGcFrequency
60
+ }
61
+
62
+ static set maxGcFrequency(value: number) {
63
+ this._maxGcFrequency = Math.max(value, MIN_GC_FREQUENCY)
64
+ }
65
+
66
+ static get maxHistoryDepth() {
67
+ return Math.floor(this.historyTime / this.historyInterval)
68
+ }
69
+
70
+ get logger() {
71
+ return this.params?.logger ?? Base.defaultLogger
72
+ }
73
+
74
+ get params() {
75
+ return this._params
76
+ }
77
+
78
+ static gc(force?: boolean): void
79
+ static gc(className: string): void
80
+ static gc(classNameOrForce: string | boolean = false): void {
81
+ if (typeof classNameOrForce === 'string') {
82
+ this.gcClass(classNameOrForce)
83
+ } else {
84
+ if (classNameOrForce || Date.now() - this._lastGC > this._maxGcFrequency) {
85
+ this.gcAll()
86
+ }
87
+ }
88
+ }
89
+
90
+ static instanceCount(className: string): number {
91
+ return this.globalInstances[className]?.length ?? 0
92
+ }
93
+
94
+ static instanceCounts(): Record<BaseClassName, number> {
95
+ this.gc()
96
+ const result: Record<BaseClassName, number> = {}
97
+ for (const [className, instances] of Object.entries(this.globalInstances)) result[className] = instances.length
98
+ return result
99
+ }
100
+
101
+ static startHistory(): void {
102
+ if (this._historyTimeout) {
103
+ this.stopHistory()
104
+ }
105
+
106
+ const timeoutHandler = () => {
107
+ if (this._historyTimeout) {
108
+ this.addToHistory()
109
+ this._historyTimeout = setTimeout(timeoutHandler, this.historyInterval)
110
+ }
111
+ }
112
+
113
+ this._historyTimeout = setTimeout(timeoutHandler, this.historyInterval)
114
+ }
115
+
116
+ static stopHistory(): void {
117
+ if (this._historyTimeout) {
118
+ clearTimeout(this._historyTimeout)
119
+ this._historyTimeout = undefined
120
+ }
121
+ }
122
+
123
+ private static addToHistory() {
124
+ const counts = this.instanceCounts()
125
+ for (const className of Object.keys(this.globalInstances)) {
126
+ this.globalInstancesCountHistory[className] = this.globalInstancesCountHistory[className]?.slice(-this.maxHistoryDepth) ?? []
127
+ this.globalInstancesCountHistory[className].push(counts[className])
128
+ }
129
+ }
130
+
131
+ private static gcAll() {
132
+ for (const className of Object.keys(this.globalInstances)) {
133
+ this.gcClass(className)
134
+ }
135
+ }
136
+
137
+ private static gcClass(className: BaseClassName) {
138
+ // remove all the weak refs that are now empty
139
+ this.globalInstances[className] = this.globalInstances[className]?.filter(ref => ref.deref() !== null) ?? []
140
+ }
141
+
142
+ private recordInstance() {
143
+ const instanceArray = Base.globalInstances[this.constructor.name] ?? []
144
+ instanceArray.push(new WeakRef(this))
145
+ Base.globalInstances[this.constructor.name] = instanceArray
146
+ }
147
+ }
@@ -0,0 +1,33 @@
1
+ declare global {
2
+ var xylabs: {
3
+ unique: Record<string, unknown>
4
+ uniqueDisabled?: boolean
5
+ }
6
+ }
7
+
8
+ const xyoGlobal = () => {
9
+ globalThis.xylabs = globalThis.xylabs ?? {}
10
+ return globalThis.xylabs
11
+ }
12
+
13
+ export const disableGloballyUnique = () => {
14
+ xyoGlobal().uniqueDisabled = true
15
+ }
16
+
17
+ export const globallyUnique = (name: string, value: unknown, domain = 'global') => {
18
+ const uniqueName = [domain, name].join(':')
19
+ if (!xyoGlobal().uniqueDisabled) {
20
+ const xylabs = globalThis.xylabs = globalThis.xylabs ?? {}
21
+ const unique = (xylabs.unique = xylabs.unique ?? {})
22
+ if (unique[uniqueName] === undefined) {
23
+ unique[uniqueName] = value
24
+ } else {
25
+ if (unique[uniqueName] !== value) {
26
+ throw new Error(
27
+ `Global unique item ${uniqueName} already defined. Make sure you are not importing two versions of the package that contains this item`,
28
+ )
29
+ }
30
+ }
31
+ }
32
+ return uniqueName
33
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './Base.ts'
2
+ export * from './globallyUnique.ts'
package/typedoc.json ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "$schema": "https://typedoc.org/schema.json",
3
+ "entryPoints": ["./src/index.ts"],
4
+ "tsconfig": "./tsconfig.typedoc.json"
5
+ }
package/xy.config.ts ADDED
@@ -0,0 +1,10 @@
1
+ import type { XyTsupConfig } from '@xylabs/ts-scripts-yarn3'
2
+ const config: XyTsupConfig = {
3
+ compile: {
4
+ browser: {},
5
+ neutral: { src: true },
6
+ node: {},
7
+ },
8
+ }
9
+
10
+ export default config