mutts 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/README.md +14 -6
  2. package/dist/chunks/{_tslib-C-cuVLvZ.js → _tslib-BgjropY9.js} +9 -1
  3. package/dist/chunks/_tslib-BgjropY9.js.map +1 -0
  4. package/dist/chunks/{_tslib-CMEnd0VE.esm.js → _tslib-Mzh1rNsX.esm.js} +9 -2
  5. package/dist/chunks/_tslib-Mzh1rNsX.esm.js.map +1 -0
  6. package/dist/chunks/{decorator-D4DU97Zg.js → decorator-DLvrD0UF.js} +42 -19
  7. package/dist/chunks/decorator-DLvrD0UF.js.map +1 -0
  8. package/dist/chunks/{decorator-GnHw1Az7.esm.js → decorator-DqiszP7i.esm.js} +42 -19
  9. package/dist/chunks/decorator-DqiszP7i.esm.js.map +1 -0
  10. package/dist/chunks/index-DzUDtFc7.esm.js +4841 -0
  11. package/dist/chunks/index-DzUDtFc7.esm.js.map +1 -0
  12. package/dist/chunks/index-HNVqPzjz.js +4891 -0
  13. package/dist/chunks/index-HNVqPzjz.js.map +1 -0
  14. package/dist/decorator.esm.js +1 -1
  15. package/dist/decorator.js +1 -1
  16. package/dist/destroyable.d.ts +1 -1
  17. package/dist/destroyable.esm.js +1 -1
  18. package/dist/destroyable.esm.js.map +1 -1
  19. package/dist/destroyable.js +1 -1
  20. package/dist/destroyable.js.map +1 -1
  21. package/dist/devtools/devtools.html +9 -0
  22. package/dist/devtools/devtools.js +5 -0
  23. package/dist/devtools/devtools.js.map +1 -0
  24. package/dist/devtools/manifest.json +8 -0
  25. package/dist/devtools/panel.css +72 -0
  26. package/dist/devtools/panel.html +31 -0
  27. package/dist/devtools/panel.js +13048 -0
  28. package/dist/devtools/panel.js.map +1 -0
  29. package/dist/eventful.esm.js +1 -1
  30. package/dist/eventful.js +1 -1
  31. package/dist/index.d.ts +18 -63
  32. package/dist/index.esm.js +4 -4
  33. package/dist/index.js +36 -11
  34. package/dist/index.js.map +1 -1
  35. package/dist/indexable.d.ts +187 -1
  36. package/dist/indexable.esm.js +197 -3
  37. package/dist/indexable.esm.js.map +1 -1
  38. package/dist/indexable.js +198 -2
  39. package/dist/indexable.js.map +1 -1
  40. package/dist/mutts.umd.js +1 -1
  41. package/dist/mutts.umd.js.map +1 -1
  42. package/dist/mutts.umd.min.js +1 -1
  43. package/dist/mutts.umd.min.js.map +1 -1
  44. package/dist/promiseChain.esm.js.map +1 -1
  45. package/dist/promiseChain.js.map +1 -1
  46. package/dist/reactive.d.ts +601 -97
  47. package/dist/reactive.esm.js +3 -3
  48. package/dist/reactive.js +31 -10
  49. package/dist/reactive.js.map +1 -1
  50. package/dist/std-decorators.esm.js +1 -1
  51. package/dist/std-decorators.js +1 -1
  52. package/docs/ai/api-reference.md +133 -0
  53. package/docs/ai/manual.md +105 -0
  54. package/docs/iterableWeak.md +646 -0
  55. package/docs/reactive/advanced.md +1280 -0
  56. package/docs/reactive/collections.md +767 -0
  57. package/docs/reactive/core.md +973 -0
  58. package/docs/reactive.md +21 -9545
  59. package/package.json +18 -5
  60. package/src/decorator.ts +266 -0
  61. package/src/destroyable.ts +199 -0
  62. package/src/eventful.ts +77 -0
  63. package/src/index.d.ts +9 -0
  64. package/src/index.ts +9 -0
  65. package/src/indexable.ts +484 -0
  66. package/src/introspection.ts +59 -0
  67. package/src/iterableWeak.ts +233 -0
  68. package/src/mixins.ts +123 -0
  69. package/src/promiseChain.ts +110 -0
  70. package/src/reactive/array.ts +414 -0
  71. package/src/reactive/change.ts +134 -0
  72. package/src/reactive/debug.ts +517 -0
  73. package/src/reactive/deep-touch.ts +268 -0
  74. package/src/reactive/deep-watch-state.ts +82 -0
  75. package/src/reactive/deep-watch.ts +168 -0
  76. package/src/reactive/effect-context.ts +94 -0
  77. package/src/reactive/effects.ts +1333 -0
  78. package/src/reactive/index.ts +75 -0
  79. package/src/reactive/interface.ts +223 -0
  80. package/src/reactive/map.ts +171 -0
  81. package/src/reactive/mapped.ts +130 -0
  82. package/src/reactive/memoize.ts +107 -0
  83. package/src/reactive/non-reactive-state.ts +49 -0
  84. package/src/reactive/non-reactive.ts +43 -0
  85. package/src/reactive/project.project.md +93 -0
  86. package/src/reactive/project.ts +335 -0
  87. package/src/reactive/proxy-state.ts +27 -0
  88. package/src/reactive/proxy.ts +285 -0
  89. package/src/reactive/record.ts +196 -0
  90. package/src/reactive/register.ts +421 -0
  91. package/src/reactive/set.ts +144 -0
  92. package/src/reactive/tracking.ts +101 -0
  93. package/src/reactive/types.ts +358 -0
  94. package/src/reactive/zone.ts +208 -0
  95. package/src/std-decorators.ts +217 -0
  96. package/src/utils.ts +117 -0
  97. package/dist/chunks/_tslib-C-cuVLvZ.js.map +0 -1
  98. package/dist/chunks/_tslib-CMEnd0VE.esm.js.map +0 -1
  99. package/dist/chunks/decorator-D4DU97Zg.js.map +0 -1
  100. package/dist/chunks/decorator-GnHw1Az7.esm.js.map +0 -1
  101. package/dist/chunks/index-DBScoeCX.esm.js +0 -1960
  102. package/dist/chunks/index-DBScoeCX.esm.js.map +0 -1
  103. package/dist/chunks/index-DOTmXL89.js +0 -1983
  104. package/dist/chunks/index-DOTmXL89.js.map +0 -1
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mutts",
3
3
  "description": "Modern UTility TS: A collection of TypeScript utilities",
4
- "version": "1.0.2",
4
+ "version": "1.0.3",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",
7
7
  "types": "dist/index.d.ts",
@@ -70,17 +70,27 @@
70
70
  },
71
71
  "files": [
72
72
  "dist",
73
+ "src",
73
74
  "README.md",
74
75
  "docs"
75
76
  ],
76
77
  "scripts": {
77
78
  "build:js": "rollup -c",
78
- "build": "npm run build:js",
79
+ "build:devtools": "rollup -c rollup.devtools.config.mjs",
80
+ "build": "npm run build:js && npm run build:devtools",
79
81
  "build:watch": "rollup -c --watch",
80
82
  "prepublishOnly": "npm run build",
81
83
  "test": "node --expose-gc node_modules/.bin/jest",
84
+ "test:coverage": "node --expose-gc node_modules/.bin/jest --coverage",
85
+ "test:coverage:watch": "node --expose-gc node_modules/.bin/jest --coverage --watch",
82
86
  "test:legacy": "TSCONFIG=tsconfig.legacy.json node node_modules/.bin/jest --detectOpenHandles --testPathPatterns=decorator",
83
87
  "test:modern": "TSCONFIG=tsconfig.modern.json node node_modules/.bin/jest --detectOpenHandles --testPathPatterns=decorator",
88
+ "test:profile": "RUN_PROFILING=1 node --expose-gc node_modules/.bin/jest --testPathPatterns=profiling",
89
+ "test:profile:benchmark": "RUN_PROFILING=1 node --expose-gc node_modules/.bin/jest --testPathPatterns=profiling --testNamePattern=benchmark",
90
+ "test:profile:detailed": "RUN_PROFILING=1 node --prof node_modules/.bin/jest --testPathPatterns=profiling --no-coverage",
91
+ "benchmark:save": "tsx tests/profiling/benchmark.ts save",
92
+ "benchmark:compare": "tsx tests/profiling/benchmark.ts compare",
93
+ "benchmark:list": "tsx tests/profiling/benchmark.ts list",
84
94
  "biome": "biome check --write src"
85
95
  },
86
96
  "keywords": [
@@ -120,12 +130,15 @@
120
130
  "@types/jest": "^30.0.0",
121
131
  "jest": "^30.0.4",
122
132
  "rollup": "^4.52.2",
133
+ "rollup-plugin-copy": "^3.5.0",
123
134
  "rollup-plugin-dts": "^6.2.3",
124
135
  "rollup-plugin-typescript2": "^0.36.0",
125
136
  "ts-jest": "^29.4.0",
126
137
  "ts-node": "^10.9.2",
127
138
  "tslib": "^2.8.1",
128
139
  "tsx": "^4.20.4",
129
- "typescript": "^5.8.3"
130
- }
131
- }
140
+ "typescript": "^5.8.3",
141
+ "vis-network": "^9.1.9"
142
+ },
143
+ "packageManager": "pnpm@10.7.1+sha512.2d92c86b7928dc8284f53494fb4201f983da65f0fb4f0d40baafa5cf628fa31dae3e5968f12466f17df7e97310e30f343a648baea1b9b350685dafafffdf5808"
144
+ }
@@ -0,0 +1,266 @@
1
+ // biome-ignore-all lint/suspicious/noConfusingVoidType: We *love* voids
2
+ // Standardized decorator system that works with both Legacy and Modern decorators
3
+
4
+ import { isConstructor } from './utils'
5
+
6
+ /**
7
+ * Error thrown when decorator operations fail
8
+ */
9
+ export class DecoratorError extends Error {
10
+ constructor(message: string) {
11
+ super(message)
12
+ this.name = 'DecoratorException'
13
+ }
14
+ }
15
+ //#region all decorator types
16
+
17
+ // Used for get/set and method decorators
18
+ /**
19
+ * Legacy property decorator type for methods, getters, and setters
20
+ */
21
+ export type LegacyPropertyDecorator<T> = (
22
+ target: T,
23
+ name: string | symbol,
24
+ descriptor: PropertyDescriptor
25
+ ) => any
26
+
27
+ /**
28
+ * Legacy class decorator type
29
+ */
30
+ export type LegacyClassDecorator<T> = (target: T) => any
31
+
32
+ /**
33
+ * Modern method decorator type
34
+ */
35
+ export type ModernMethodDecorator<T> = (target: T, context: ClassMethodDecoratorContext) => any
36
+
37
+ /**
38
+ * Modern getter decorator type
39
+ */
40
+ export type ModernGetterDecorator<T> = (target: T, context: ClassGetterDecoratorContext) => any
41
+
42
+ /**
43
+ * Modern setter decorator type
44
+ */
45
+ export type ModernSetterDecorator<T> = (target: T, context: ClassSetterDecoratorContext) => any
46
+
47
+ /**
48
+ * Modern accessor decorator type
49
+ */
50
+ export type ModernAccessorDecorator<T> = (target: T, context: ClassAccessorDecoratorContext) => any
51
+
52
+ /**
53
+ * Modern class decorator type
54
+ */
55
+ export type ModernClassDecorator<T> = (target: T, context: ClassDecoratorContext) => any
56
+
57
+ //#endregion
58
+
59
+ type DDMethod<T> = (
60
+ original: (this: T, ...args: any[]) => any,
61
+ name: PropertyKey
62
+ ) => ((this: T, ...args: any[]) => any) | void
63
+
64
+ type DDGetter<T> = (original: (this: T) => any, name: PropertyKey) => ((this: T) => any) | void
65
+
66
+ type DDSetter<T> = (
67
+ original: (this: T, value: any) => void,
68
+ name: PropertyKey
69
+ ) => ((this: T, value: any) => void) | void
70
+
71
+ type DDClass<T> = <Ctor extends new (...args: any[]) => T = new (...args: any[]) => T>(
72
+ target: Ctor
73
+ ) => Ctor | void
74
+ /**
75
+ * Description object for creating decorators that work with both Legacy and Modern decorator proposals
76
+ */
77
+ export interface DecoratorDescription<T> {
78
+ /** Handler for method decorators */
79
+ method?: DDMethod<T>
80
+ /** Handler for class decorators */
81
+ class?: DDClass<T>
82
+ /** Handler for getter decorators */
83
+ getter?: DDGetter<T>
84
+ /** Handler for setter decorators */
85
+ setter?: DDSetter<T>
86
+ /** Default handler for any decorator type not explicitly defined */
87
+ default?: (...args: any[]) => any
88
+ }
89
+
90
+ /**
91
+ * Type for decorators that work with both Legacy and Modern decorator proposals
92
+ * Automatically infers the correct decorator type based on the description
93
+ */
94
+ export type Decorator<T, Description extends DecoratorDescription<T>> = (Description extends {
95
+ method: DDMethod<T>
96
+ }
97
+ ? LegacyPropertyDecorator<T> & ModernMethodDecorator<T>
98
+ : unknown) &
99
+ (Description extends { class: DDClass<new (...args: any[]) => T> }
100
+ ? LegacyClassDecorator<new (...args: any[]) => T> &
101
+ ModernClassDecorator<new (...args: any[]) => T>
102
+ : unknown) &
103
+ (Description extends { getter: DDGetter<T> }
104
+ ? LegacyPropertyDecorator<T> & ModernGetterDecorator<T> & ModernAccessorDecorator<T>
105
+ : unknown) &
106
+ (Description extends { setter: DDSetter<T> }
107
+ ? LegacyPropertyDecorator<T> & ModernSetterDecorator<T> & ModernAccessorDecorator<T>
108
+ : unknown) &
109
+ (Description extends { default: infer Signature } ? Signature : unknown)
110
+
111
+ /**
112
+ * Factory type for creating decorators that work with both Legacy and Modern decorator proposals
113
+ */
114
+ export type DecoratorFactory<T> = <Description extends DecoratorDescription<T>>(
115
+ description: Description
116
+ ) => (Description extends { method: DDMethod<T> }
117
+ ? LegacyPropertyDecorator<T> & ModernMethodDecorator<T>
118
+ : unknown) &
119
+ (Description extends { class: DDClass<new (...args: any[]) => T> }
120
+ ? LegacyClassDecorator<new (...args: any[]) => T> &
121
+ ModernClassDecorator<new (...args: any[]) => T>
122
+ : unknown) &
123
+ (Description extends { getter: DDGetter<T> }
124
+ ? LegacyPropertyDecorator<T> & ModernGetterDecorator<T> & ModernAccessorDecorator<T>
125
+ : unknown) &
126
+ (Description extends { setter: DDSetter<T> }
127
+ ? LegacyPropertyDecorator<T> & ModernSetterDecorator<T> & ModernAccessorDecorator<T>
128
+ : unknown) &
129
+ (Description extends { default: infer Signature } ? Signature : unknown)
130
+
131
+ /**
132
+ * Creates a decorator that works with Legacy decorator proposals
133
+ * @param description - The decorator description object
134
+ * @returns A decorator function compatible with Legacy decorators
135
+ */
136
+ export function legacyDecorator<T = any>(description: DecoratorDescription<T>): any {
137
+ return function (
138
+ this: any,
139
+ target: any,
140
+ propertyKey?: PropertyKey,
141
+ descriptor?: PropertyDescriptor,
142
+ ...args: any[]
143
+ ) {
144
+ if (propertyKey === undefined) {
145
+ if (isConstructor(target)) {
146
+ if (!('class' in description)) throw new Error('Decorator cannot be applied to a class')
147
+ return description.class!(target)
148
+ }
149
+ } else if (typeof target === 'object' && ['string', 'symbol'].includes(typeof propertyKey)) {
150
+ if (!descriptor) throw new Error('Decorator cannot be applied to a field')
151
+ else if (typeof descriptor === 'object' && 'configurable' in descriptor) {
152
+ if ('get' in descriptor || 'set' in descriptor) {
153
+ if (!('getter' in description || 'setter' in description))
154
+ throw new Error('Decorator cannot be applied to a getter or setter')
155
+ if ('getter' in description) {
156
+ const newGetter = description.getter!(descriptor.get as any, propertyKey)
157
+ if (newGetter) descriptor.get = newGetter
158
+ }
159
+ if ('setter' in description) {
160
+ const newSetter = description.setter!(descriptor.set as any, propertyKey)
161
+ if (newSetter) descriptor.set = newSetter
162
+ }
163
+ return descriptor
164
+ } else if (typeof descriptor.value === 'function') {
165
+ if (!('method' in description)) throw new Error('Decorator cannot be applied to a method')
166
+ const newMethod = description.method!(descriptor.value, propertyKey)
167
+ if (newMethod) descriptor.value = newMethod
168
+ return descriptor
169
+ }
170
+ }
171
+ }
172
+ if (!('default' in description))
173
+ throw new Error('Decorator do not have a default implementation')
174
+ return description.default!.call(this, target, propertyKey, descriptor, ...args)
175
+ }
176
+ }
177
+
178
+ /**
179
+ * Creates a decorator that works with Modern decorator proposals
180
+ * @param description - The decorator description object
181
+ * @returns A decorator function compatible with Modern decorators
182
+ */
183
+ export function modernDecorator<T = any>(description: DecoratorDescription<T>): any {
184
+ /*return function (target: any, context?: DecoratorContext, ...args: any[]) {*/
185
+ return function (this: any, target: any, context?: DecoratorContext, ...args: any[]) {
186
+ if (!context?.kind || typeof context.kind !== 'string') {
187
+ if (!('default' in description))
188
+ throw new Error('Decorator do not have a default implementation')
189
+ return description.default!.call(this, target, context, ...args)
190
+ }
191
+ switch (context.kind) {
192
+ case 'class':
193
+ if (!('class' in description)) throw new Error('Decorator cannot be applied to a class')
194
+ return description.class!(target)
195
+ case 'field':
196
+ throw new Error('Decorator cannot be applied to a field')
197
+ case 'getter':
198
+ if (!('getter' in description)) throw new Error('Decorator cannot be applied to a getter')
199
+ return description.getter!(target, context.name)
200
+ case 'setter':
201
+ if (!('setter' in description)) throw new Error('Decorator cannot be applied to a setter')
202
+ return description.setter!(target, context.name)
203
+ case 'method':
204
+ if (!('method' in description)) throw new Error('Decorator cannot be applied to a method')
205
+ return description.method!(target, context.name)
206
+ case 'accessor': {
207
+ if (!('getter' in description || 'setter' in description))
208
+ throw new Error('Decorator cannot be applied to a getter or setter')
209
+ const rv: Partial<ClassAccessorDecoratorResult<any, any>> = {}
210
+ if ('getter' in description) {
211
+ const newGetter = description.getter!(target.get, context.name)
212
+ if (newGetter) rv.get = newGetter
213
+ }
214
+ if ('setter' in description) {
215
+ const newSetter = description.setter!(target.set, context.name)
216
+ if (newSetter) rv.set = newSetter
217
+ }
218
+ return rv
219
+ }
220
+ //return description.accessor?.(target, context.name, target)
221
+ }
222
+ }
223
+ }
224
+
225
+ /**
226
+ * Detects if the decorator is being called in modern (Modern) or legacy (Legacy) mode
227
+ * based on the arguments passed to the decorator function
228
+ */
229
+ function detectDecoratorMode(
230
+ _target: any,
231
+ contextOrKey?: any,
232
+ _descriptor?: any
233
+ ): 'modern' | 'legacy' {
234
+ // Modern decorators have a context object as the second parameter
235
+ // Legacy decorators have a string/symbol key as the second parameter
236
+ if (
237
+ typeof contextOrKey === 'object' &&
238
+ contextOrKey !== null &&
239
+ typeof contextOrKey.kind === 'string'
240
+ ) {
241
+ return 'modern'
242
+ }
243
+ return 'legacy'
244
+ }
245
+
246
+ /**
247
+ * Main decorator factory that automatically detects and works with both Legacy and Modern decorator proposals
248
+ * @param description - The decorator description object
249
+ * @returns A decorator that works in both Legacy and Modern environments
250
+ */
251
+ export const decorator: DecoratorFactory<any> = (description: DecoratorDescription<any>) => {
252
+ const modern = modernDecorator(description)
253
+ const legacy = legacyDecorator(description)
254
+ return ((target: any, contextOrKey?: any, ...args: any[]) => {
255
+ const mode = detectDecoratorMode(target, contextOrKey, args[0])
256
+ return mode === 'modern'
257
+ ? modern(target, contextOrKey, ...args)
258
+ : legacy(target, contextOrKey, ...args)
259
+ }) as any
260
+ }
261
+
262
+ /**
263
+ * Generic class decorator type that works with both Legacy and Modern decorator proposals
264
+ */
265
+ export type GenericClassDecorator<T> = LegacyClassDecorator<abstract new (...args: any[]) => T> &
266
+ ModernClassDecorator<abstract new (...args: any[]) => T>
@@ -0,0 +1,199 @@
1
+ import { decorator } from './decorator'
2
+
3
+ // Integrated with `using` statement via Symbol.dispose
4
+ const fr = new FinalizationRegistry<() => void>((f) => f())
5
+ /**
6
+ * Symbol for marking destructor methods
7
+ */
8
+ export const destructor = Symbol('destructor')
9
+ /**
10
+ * Symbol for accessing allocated values in destroyable objects
11
+ */
12
+ export const allocatedValues = Symbol('allocated')
13
+ /**
14
+ * Error thrown when attempting to access a destroyed object
15
+ */
16
+ export class DestructionError extends Error {
17
+ static throw<_T = void>(msg: string) {
18
+ return () => {
19
+ throw new DestructionError(msg)
20
+ }
21
+ }
22
+ constructor(msg: string) {
23
+ super(`Object is destroyed. ${msg}`)
24
+ this.name = 'DestroyedAccessError'
25
+ }
26
+ }
27
+ const destroyedHandler = {
28
+ [Symbol.toStringTag]: 'MutTs Destroyable',
29
+ get: DestructionError.throw('Cannot access destroyed object'),
30
+ set: DestructionError.throw('Cannot access destroyed object'),
31
+ } as const
32
+
33
+ abstract class AbstractDestroyable<Allocated> {
34
+ abstract [destructor](allocated: Allocated): void
35
+ [Symbol.dispose](): void {
36
+ this[destructor](this as unknown as Allocated)
37
+ }
38
+ }
39
+
40
+ interface Destructor<Allocated> {
41
+ destructor(allocated: Allocated): void
42
+ }
43
+
44
+ /**
45
+ * Creates a destroyable class with a base class and destructor object
46
+ * @param base - The base class to extend
47
+ * @param destructorObj - Object containing the destructor method
48
+ * @returns A destroyable class with static destroy and isDestroyable methods
49
+ */
50
+ export function Destroyable<
51
+ T extends new (
52
+ ...args: any[]
53
+ ) => any,
54
+ Allocated extends Partial<InstanceType<T>>,
55
+ >(
56
+ base: T,
57
+ destructorObj: Destructor<Allocated>
58
+ ): (new (
59
+ ...args: ConstructorParameters<T>
60
+ ) => InstanceType<T> & { [allocatedValues]: Allocated }) & {
61
+ destroy(obj: InstanceType<T>): boolean
62
+ isDestroyable(obj: InstanceType<T>): boolean
63
+ }
64
+
65
+ /**
66
+ * Creates a destroyable class with only a destructor object (no base class)
67
+ * @param destructorObj - Object containing the destructor method
68
+ * @returns A destroyable class with static destroy and isDestroyable methods
69
+ */
70
+ export function Destroyable<Allocated extends Record<PropertyKey, any> = Record<PropertyKey, any>>(
71
+ destructorObj: Destructor<Allocated>
72
+ ): (new () => { [allocatedValues]: Allocated }) & {
73
+ destroy(obj: any): boolean
74
+ isDestroyable(obj: any): boolean
75
+ }
76
+
77
+ /**
78
+ * Creates a destroyable class with a base class (requires [destructor] method)
79
+ * @param base - The base class to extend
80
+ * @returns A destroyable class with static destroy and isDestroyable methods
81
+ */
82
+ export function Destroyable<
83
+ T extends new (
84
+ ...args: any[]
85
+ ) => any,
86
+ Allocated extends Record<PropertyKey, any> = Record<PropertyKey, any>,
87
+ >(
88
+ base: T
89
+ ): (new (
90
+ ...args: ConstructorParameters<T>
91
+ ) => AbstractDestroyable<Allocated> & InstanceType<T> & { [allocatedValues]: Allocated }) & {
92
+ destroy(obj: InstanceType<T>): boolean
93
+ isDestroyable(obj: InstanceType<T>): boolean
94
+ }
95
+
96
+ /**
97
+ * Creates an abstract destroyable base class
98
+ * @returns An abstract destroyable class with static destroy and isDestroyable methods
99
+ */
100
+ export function Destroyable<
101
+ Allocated extends Record<PropertyKey, any> = Record<PropertyKey, any>,
102
+ >(): abstract new () => (AbstractDestroyable<Allocated> & {
103
+ [allocatedValues]: Allocated
104
+ }) & {
105
+ destroy(obj: any): boolean
106
+ isDestroyable(obj: any): boolean
107
+ }
108
+
109
+ export function Destroyable<
110
+ T extends new (
111
+ ...args: any[]
112
+ ) => any,
113
+ Allocated extends Record<PropertyKey, any> = Record<PropertyKey, any>,
114
+ >(base?: T | Destructor<Allocated>, destructorObj?: Destructor<Allocated>) {
115
+ if (base && typeof base !== 'function') {
116
+ destructorObj = base as Destructor<Allocated>
117
+ base = undefined
118
+ }
119
+ if (!base) {
120
+ base = class {} as T
121
+ }
122
+
123
+ return class Destroyable extends (base as T) {
124
+ static readonly destructors = new WeakMap<any, () => void>()
125
+ static destroy(obj: Destroyable) {
126
+ const destructor = Destroyable.destructors.get(obj)
127
+ if (!destructor) return false
128
+ fr.unregister(obj)
129
+ Destroyable.destructors.delete(obj)
130
+ Object.setPrototypeOf(obj, new Proxy({}, destroyedHandler))
131
+ // Clear all own properties
132
+ for (const key of Object.getOwnPropertyNames(obj)) {
133
+ delete (obj as any)[key]
134
+ }
135
+ destructor()
136
+ return true
137
+ }
138
+ static isDestroyable(obj: Destroyable) {
139
+ return Destroyable.destructors.has(obj)
140
+ }
141
+
142
+ declare [forwardProperties]: PropertyKey[]
143
+ readonly [allocatedValues]: Allocated
144
+ constructor(...args: any[]) {
145
+ super(...args)
146
+ const allocated = {} as Allocated
147
+ this[allocatedValues] = allocated
148
+ // @ts-expect-error `this` is an AbstractDestroyable
149
+ const myDestructor = destructorObj?.destructor ?? this[destructor]
150
+ if (!myDestructor) {
151
+ throw new DestructionError('Destructor is not defined')
152
+ }
153
+ function destruction() {
154
+ myDestructor(allocated)
155
+ }
156
+ Destroyable.destructors.set(this, destruction)
157
+ fr.register(this, destruction, this)
158
+ }
159
+ }
160
+ }
161
+
162
+ const forwardProperties = Symbol('forwardProperties')
163
+ /**
164
+ * Decorator that marks properties to be stored in the allocated object and passed to the destructor
165
+ * Use with accessor properties or explicit get/set pairs
166
+ */
167
+ export const allocated = decorator({
168
+ setter(original, propertyKey) {
169
+ return function (value) {
170
+ this[allocatedValues][propertyKey] = value
171
+ return original.call(this, value)
172
+ }
173
+ },
174
+ })
175
+
176
+ /**
177
+ * Registers a callback to be called when an object is garbage collected
178
+ * @param cb - The callback function to execute on garbage collection
179
+ * @returns The object whose reference can be collected
180
+ */
181
+ export function callOnGC(cb: () => void) {
182
+ let called = false
183
+ const forward = () => {
184
+ if (called) return
185
+ called = true
186
+ cb()
187
+ }
188
+ fr.register(forward, cb, cb)
189
+ return forward
190
+ }
191
+
192
+ /**
193
+ * Context Manager Protocol for `using` statement integration
194
+ * Provides automatic resource cleanup when used with the `using` statement
195
+ */
196
+ export interface ContextManager<T = any> {
197
+ [Symbol.dispose](): void
198
+ value?: T
199
+ }
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Base type for event maps - all event handlers must be functions
3
+ */
4
+ export type EventsBase = Record<string, (...args: any[]) => void>
5
+ /**
6
+ * A type-safe event system that provides a clean API for event handling
7
+ * @template Events - The event map defining event names and their handler signatures
8
+ */
9
+ export class Eventful<Events extends EventsBase> {
10
+ readonly #events = new Map<keyof Events, ((...args: any[]) => void)[]>()
11
+ readonly #hooks = [] as ((...args: any[]) => void)[]
12
+
13
+ public hook(
14
+ cb: <EventType extends keyof Events>(
15
+ event: EventType,
16
+ ...args: Parameters<Events[EventType]>
17
+ ) => void
18
+ ): () => void {
19
+ if (!this.#hooks.includes(cb)) this.#hooks.push(cb)
20
+ return () => {
21
+ this.#hooks.splice(this.#hooks.indexOf(cb), 1)
22
+ }
23
+ }
24
+
25
+ public on(events: Partial<Events>): void
26
+ public on<EventType extends keyof Events>(event: EventType, cb: Events[EventType]): () => void
27
+ public on<EventType extends keyof Events>(
28
+ eventOrEvents: EventType | Partial<Events>,
29
+ cb?: Events[EventType]
30
+ ): () => void {
31
+ if (typeof eventOrEvents === 'object') {
32
+ for (const e of Object.keys(eventOrEvents) as (keyof Events)[]) {
33
+ this.on(e, eventOrEvents[e]!)
34
+ }
35
+ } else if (cb !== undefined) {
36
+ let callbacks = this.#events.get(eventOrEvents)
37
+ if (!callbacks) {
38
+ callbacks = []
39
+ this.#events.set(eventOrEvents, callbacks)
40
+ }
41
+ callbacks.push(cb)
42
+ }
43
+ // @ts-expect-error Generic case leads to generic case
44
+ return () => this.off(eventOrEvents, cb)
45
+ }
46
+ public off(events: Partial<Events>): void
47
+ public off<EventType extends keyof Events>(event: EventType, cb?: Events[EventType]): void
48
+ public off<EventType extends keyof Events>(
49
+ eventOrEvents: EventType | Partial<Events>,
50
+ cb?: Events[EventType]
51
+ ): void {
52
+ if (typeof eventOrEvents === 'object') {
53
+ for (const e of Object.keys(eventOrEvents) as (keyof Events)[]) {
54
+ this.off(e, eventOrEvents[e])
55
+ }
56
+ } else if (cb !== null && cb !== undefined) {
57
+ const callbacks = this.#events.get(eventOrEvents)
58
+ if (callbacks) {
59
+ this.#events.set(
60
+ eventOrEvents,
61
+ callbacks.filter((c) => c !== cb)
62
+ )
63
+ }
64
+ } else {
65
+ // Remove all listeners for this event
66
+ this.#events.delete(eventOrEvents)
67
+ }
68
+ }
69
+ public emit<EventType extends keyof Events>(
70
+ event: EventType,
71
+ ...args: Parameters<Events[EventType]>
72
+ ) {
73
+ const callbacks = this.#events.get(event)
74
+ if (callbacks) for (const cb of callbacks) cb.apply(this, args)
75
+ for (const cb of this.#hooks) cb.call(this, event, ...args)
76
+ }
77
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ // Augment Array.isArray to properly handle readonly arrays in type narrowing
2
+ interface ArrayConstructor {
3
+ /**
4
+ * Determines whether an object is an array.
5
+ * @param arg Any value to test.
6
+ * @returns True if the value is an array (mutable or readonly), false otherwise.
7
+ */
8
+ isArray(arg: any): arg is any[] | readonly any[]
9
+ }
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ export * from './decorator'
2
+ export * from './destroyable'
3
+ export * from './eventful'
4
+ export * from './indexable'
5
+ export * from './iterableWeak'
6
+ export * from './mixins'
7
+ export * from './reactive'
8
+ export * from './std-decorators'
9
+ export * from './utils'