mutts 1.0.8 → 1.0.10

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 (195) hide show
  1. package/README.md +77 -35
  2. package/dist/browser.cjs +2144 -5355
  3. package/dist/browser.cjs.map +1 -1
  4. package/dist/browser.d.ts +2 -1655
  5. package/dist/browser.dev.cjs +110 -0
  6. package/dist/browser.dev.cjs.map +1 -0
  7. package/dist/browser.dev.d.ts +2 -0
  8. package/dist/browser.dev.esm.js +5 -0
  9. package/dist/browser.dev.esm.js.map +1 -0
  10. package/dist/browser.esm.js +20 -107
  11. package/dist/browser.esm.js.map +1 -1
  12. package/dist/chunks/{async-browser-CA0jPWIi.cjs → async-browser-BU_IfxYD.cjs} +19 -107
  13. package/dist/chunks/async-browser-BU_IfxYD.cjs.map +1 -0
  14. package/dist/chunks/{async-core-UqHzvJ-S.cjs → async-core-CRLKP3l-.cjs} +8 -4
  15. package/dist/chunks/async-core-CRLKP3l-.cjs.map +1 -0
  16. package/dist/chunks/{async-node-BYHuGTni.cjs → async-node-3PrbVAbB.cjs} +7 -12
  17. package/dist/chunks/async-node-3PrbVAbB.cjs.map +1 -0
  18. package/dist/chunks/index-CaaQQlPJ.esm.js +3174 -0
  19. package/dist/chunks/index-CaaQQlPJ.esm.js.map +1 -0
  20. package/dist/chunks/node-nKJBk8iJ.esm.js +96 -0
  21. package/dist/chunks/node-nKJBk8iJ.esm.js.map +1 -0
  22. package/dist/chunks/proxy-Dtg-bJ3T.cjs +3763 -0
  23. package/dist/chunks/proxy-Dtg-bJ3T.cjs.map +1 -0
  24. package/dist/chunks/proxy-r7lARftl.esm.js +3701 -0
  25. package/dist/chunks/proxy-r7lARftl.esm.js.map +1 -0
  26. package/dist/debug.cjs +980 -0
  27. package/dist/debug.cjs.map +1 -0
  28. package/dist/debug.d.ts +208 -0
  29. package/dist/debug.esm.js +950 -0
  30. package/dist/debug.esm.js.map +1 -0
  31. package/dist/devtools/manifest.json +1 -1
  32. package/dist/devtools/panel.html +1 -1
  33. package/dist/devtools/panel.js +107 -94
  34. package/dist/devtools/panel.js.map +1 -1
  35. package/dist/index.d.ts +1447 -13
  36. package/dist/mutts.umd.js +7167 -1
  37. package/dist/mutts.umd.js.map +1 -1
  38. package/dist/mutts.umd.min.js +1 -1
  39. package/dist/mutts.umd.min.js.map +1 -1
  40. package/dist/node.cjs +70 -66
  41. package/dist/node.cjs.map +1 -1
  42. package/dist/node.d.ts +2 -1
  43. package/dist/node.dev.cjs +110 -0
  44. package/dist/node.dev.cjs.map +1 -0
  45. package/dist/node.dev.d.ts +2 -0
  46. package/dist/node.dev.esm.js +6 -0
  47. package/dist/node.dev.esm.js.map +1 -0
  48. package/dist/node.esm.js +4 -103
  49. package/dist/node.esm.js.map +1 -1
  50. package/dist/{reactive/types.d.ts → types-W5vD6m2n.d.ts} +186 -93
  51. package/docs/ai/api-reference.md +12 -19
  52. package/docs/ai/manual.md +521 -17
  53. package/docs/events.md +57 -2
  54. package/docs/flavored.md +210 -0
  55. package/docs/reactive/advanced.md +314 -5
  56. package/docs/reactive/attend.md +142 -0
  57. package/docs/reactive/collections.md +76 -39
  58. package/docs/reactive/core.md +100 -14
  59. package/docs/reactive/debugging.md +123 -18
  60. package/docs/reactive/error-handling.md +121 -0
  61. package/docs/reactive/resource.md +125 -0
  62. package/docs/reactive/scan.md +247 -1
  63. package/docs/reactive.md +4 -1
  64. package/docs/std-decorators.md +102 -51
  65. package/docs/utils.md +70 -0
  66. package/docs/zone.md +3 -7
  67. package/package.json +64 -30
  68. package/dist/async/browser.d.ts +0 -2
  69. package/dist/async/browser.d.ts.map +0 -1
  70. package/dist/async/index.d.ts +0 -18
  71. package/dist/async/index.d.ts.map +0 -1
  72. package/dist/async/node.d.ts +0 -2
  73. package/dist/async/node.d.ts.map +0 -1
  74. package/dist/chunks/async-browser-CA0jPWIi.cjs.map +0 -1
  75. package/dist/chunks/async-core-UqHzvJ-S.cjs.map +0 -1
  76. package/dist/chunks/async-node-BYHuGTni.cjs.map +0 -1
  77. package/dist/chunks/index-DhaOVusv.esm.js +0 -6411
  78. package/dist/chunks/index-DhaOVusv.esm.js.map +0 -1
  79. package/dist/decorator.d.ts +0 -106
  80. package/dist/decorator.d.ts.map +0 -1
  81. package/dist/destroyable.d.ts +0 -87
  82. package/dist/destroyable.d.ts.map +0 -1
  83. package/dist/entry-browser.d.ts +0 -3
  84. package/dist/entry-browser.d.ts.map +0 -1
  85. package/dist/entry-node.d.ts +0 -3
  86. package/dist/entry-node.d.ts.map +0 -1
  87. package/dist/eventful.d.ts +0 -18
  88. package/dist/eventful.d.ts.map +0 -1
  89. package/dist/index.d.ts.map +0 -1
  90. package/dist/indexable.d.ts +0 -243
  91. package/dist/indexable.d.ts.map +0 -1
  92. package/dist/introspection.d.ts +0 -27
  93. package/dist/introspection.d.ts.map +0 -1
  94. package/dist/iterableWeak.d.ts +0 -53
  95. package/dist/iterableWeak.d.ts.map +0 -1
  96. package/dist/mixins.d.ts +0 -25
  97. package/dist/mixins.d.ts.map +0 -1
  98. package/dist/promiseChain.d.ts +0 -20
  99. package/dist/promiseChain.d.ts.map +0 -1
  100. package/dist/reactive/array.d.ts +0 -49
  101. package/dist/reactive/array.d.ts.map +0 -1
  102. package/dist/reactive/buffer.d.ts +0 -44
  103. package/dist/reactive/buffer.d.ts.map +0 -1
  104. package/dist/reactive/change.d.ts +0 -29
  105. package/dist/reactive/change.d.ts.map +0 -1
  106. package/dist/reactive/debug.d.ts +0 -111
  107. package/dist/reactive/debug.d.ts.map +0 -1
  108. package/dist/reactive/deep-touch.d.ts +0 -28
  109. package/dist/reactive/deep-touch.d.ts.map +0 -1
  110. package/dist/reactive/deep-watch-state.d.ts +0 -25
  111. package/dist/reactive/deep-watch-state.d.ts.map +0 -1
  112. package/dist/reactive/deep-watch.d.ts +0 -19
  113. package/dist/reactive/deep-watch.d.ts.map +0 -1
  114. package/dist/reactive/effect-context.d.ts +0 -7
  115. package/dist/reactive/effect-context.d.ts.map +0 -1
  116. package/dist/reactive/effects.d.ts +0 -151
  117. package/dist/reactive/effects.d.ts.map +0 -1
  118. package/dist/reactive/index.d.ts +0 -20
  119. package/dist/reactive/index.d.ts.map +0 -1
  120. package/dist/reactive/interface.d.ts +0 -64
  121. package/dist/reactive/interface.d.ts.map +0 -1
  122. package/dist/reactive/map.d.ts +0 -30
  123. package/dist/reactive/map.d.ts.map +0 -1
  124. package/dist/reactive/memoize.d.ts +0 -5
  125. package/dist/reactive/memoize.d.ts.map +0 -1
  126. package/dist/reactive/non-reactive-state.d.ts +0 -9
  127. package/dist/reactive/non-reactive-state.d.ts.map +0 -1
  128. package/dist/reactive/non-reactive.d.ts +0 -11
  129. package/dist/reactive/non-reactive.d.ts.map +0 -1
  130. package/dist/reactive/project.d.ts +0 -41
  131. package/dist/reactive/project.d.ts.map +0 -1
  132. package/dist/reactive/proxy-state.d.ts +0 -8
  133. package/dist/reactive/proxy-state.d.ts.map +0 -1
  134. package/dist/reactive/proxy.d.ts +0 -23
  135. package/dist/reactive/proxy.d.ts.map +0 -1
  136. package/dist/reactive/record.d.ts +0 -116
  137. package/dist/reactive/record.d.ts.map +0 -1
  138. package/dist/reactive/register.d.ts +0 -64
  139. package/dist/reactive/register.d.ts.map +0 -1
  140. package/dist/reactive/registry.d.ts +0 -20
  141. package/dist/reactive/registry.d.ts.map +0 -1
  142. package/dist/reactive/set.d.ts +0 -28
  143. package/dist/reactive/set.d.ts.map +0 -1
  144. package/dist/reactive/tracking.d.ts +0 -7
  145. package/dist/reactive/tracking.d.ts.map +0 -1
  146. package/dist/reactive/types.d.ts.map +0 -1
  147. package/dist/std-decorators.d.ts +0 -50
  148. package/dist/std-decorators.d.ts.map +0 -1
  149. package/dist/utils.d.ts +0 -49
  150. package/dist/utils.d.ts.map +0 -1
  151. package/dist/zone.d.ts +0 -40
  152. package/dist/zone.d.ts.map +0 -1
  153. package/docs/reactive/project.md +0 -93
  154. package/src/async/browser.ts +0 -319
  155. package/src/async/index.ts +0 -23
  156. package/src/async/node.ts +0 -104
  157. package/src/decorator.ts +0 -272
  158. package/src/destroyable.ts +0 -199
  159. package/src/entry-browser.ts +0 -5
  160. package/src/entry-node.ts +0 -5
  161. package/src/eventful.ts +0 -77
  162. package/src/index.d.ts +0 -12
  163. package/src/index.ts +0 -64
  164. package/src/indexable.ts +0 -526
  165. package/src/introspection.ts +0 -59
  166. package/src/iterableWeak.ts +0 -233
  167. package/src/mixins.ts +0 -123
  168. package/src/promiseChain.ts +0 -110
  169. package/src/reactive/array.ts +0 -507
  170. package/src/reactive/buffer.ts +0 -168
  171. package/src/reactive/change.ts +0 -138
  172. package/src/reactive/debug.ts +0 -517
  173. package/src/reactive/deep-touch.ts +0 -268
  174. package/src/reactive/deep-watch-state.ts +0 -82
  175. package/src/reactive/deep-watch.ts +0 -168
  176. package/src/reactive/effect-context.ts +0 -18
  177. package/src/reactive/effects.ts +0 -1382
  178. package/src/reactive/index.ts +0 -75
  179. package/src/reactive/interface.ts +0 -210
  180. package/src/reactive/map.ts +0 -158
  181. package/src/reactive/memoize.ts +0 -163
  182. package/src/reactive/non-reactive-state.ts +0 -49
  183. package/src/reactive/non-reactive.ts +0 -43
  184. package/src/reactive/project.ts +0 -453
  185. package/src/reactive/proxy-state.ts +0 -27
  186. package/src/reactive/proxy.ts +0 -264
  187. package/src/reactive/record.ts +0 -196
  188. package/src/reactive/register.ts +0 -419
  189. package/src/reactive/registry.ts +0 -59
  190. package/src/reactive/set.ts +0 -130
  191. package/src/reactive/tracking.ts +0 -44
  192. package/src/reactive/types.ts +0 -440
  193. package/src/std-decorators.ts +0 -217
  194. package/src/utils.ts +0 -301
  195. package/src/zone.ts +0 -138
package/src/decorator.ts DELETED
@@ -1,272 +0,0 @@
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
- target: any,
62
- name: PropertyKey
63
- ) => ((this: T, ...args: any[]) => any) | void
64
-
65
- type DDGetter<T> = (
66
- original: (this: T) => any,
67
- target: any,
68
- name: PropertyKey
69
- ) => ((this: T) => any) | void
70
-
71
- type DDSetter<T> = (
72
- original: (this: T, value: any) => void,
73
- target: any,
74
- name: PropertyKey
75
- ) => ((this: T, value: any) => void) | void
76
-
77
- type DDClass<T> = <Ctor extends new (...args: any[]) => T = new (...args: any[]) => T>(
78
- target: Ctor
79
- ) => Ctor | void
80
- /**
81
- * Description object for creating decorators that work with both Legacy and Modern decorator proposals
82
- */
83
- export interface DecoratorDescription<T> {
84
- /** Handler for method decorators */
85
- method?: DDMethod<T>
86
- /** Handler for class decorators */
87
- class?: DDClass<T>
88
- /** Handler for getter decorators */
89
- getter?: DDGetter<T>
90
- /** Handler for setter decorators */
91
- setter?: DDSetter<T>
92
- /** Default handler for any decorator type not explicitly defined */
93
- default?: (...args: any[]) => any
94
- }
95
-
96
- /**
97
- * Type for decorators that work with both Legacy and Modern decorator proposals
98
- * Automatically infers the correct decorator type based on the description
99
- */
100
- export type Decorator<T, Description extends DecoratorDescription<T>> = (Description extends {
101
- method: DDMethod<T>
102
- }
103
- ? LegacyPropertyDecorator<T> & ModernMethodDecorator<T>
104
- : unknown) &
105
- (Description extends { class: DDClass<new (...args: any[]) => T> }
106
- ? LegacyClassDecorator<new (...args: any[]) => T> &
107
- ModernClassDecorator<new (...args: any[]) => T>
108
- : unknown) &
109
- (Description extends { getter: DDGetter<T> }
110
- ? LegacyPropertyDecorator<T> & ModernGetterDecorator<T> & ModernAccessorDecorator<T>
111
- : unknown) &
112
- (Description extends { setter: DDSetter<T> }
113
- ? LegacyPropertyDecorator<T> & ModernSetterDecorator<T> & ModernAccessorDecorator<T>
114
- : unknown) &
115
- (Description extends { default: infer Signature } ? Signature : unknown)
116
-
117
- /**
118
- * Factory type for creating decorators that work with both Legacy and Modern decorator proposals
119
- */
120
- export type DecoratorFactory<T> = <Description extends DecoratorDescription<T>>(
121
- description: Description
122
- ) => (Description extends { method: DDMethod<T> }
123
- ? LegacyPropertyDecorator<T> & ModernMethodDecorator<T>
124
- : unknown) &
125
- (Description extends { class: DDClass<new (...args: any[]) => T> }
126
- ? LegacyClassDecorator<new (...args: any[]) => T> &
127
- ModernClassDecorator<new (...args: any[]) => T>
128
- : unknown) &
129
- (Description extends { getter: DDGetter<T> }
130
- ? LegacyPropertyDecorator<T> & ModernGetterDecorator<T> & ModernAccessorDecorator<T>
131
- : unknown) &
132
- (Description extends { setter: DDSetter<T> }
133
- ? LegacyPropertyDecorator<T> & ModernSetterDecorator<T> & ModernAccessorDecorator<T>
134
- : unknown) &
135
- (Description extends { default: infer Signature } ? Signature : unknown)
136
-
137
- /**
138
- * Creates a decorator that works with Legacy decorator proposals
139
- * @param description - The decorator description object
140
- * @returns A decorator function compatible with Legacy decorators
141
- */
142
- export function legacyDecorator<T = any>(description: DecoratorDescription<T>): any {
143
- return function (
144
- this: any,
145
- target: any,
146
- propertyKey?: PropertyKey,
147
- descriptor?: PropertyDescriptor,
148
- ...args: any[]
149
- ) {
150
- if (propertyKey === undefined) {
151
- if (isConstructor(target)) {
152
- if (!('class' in description)) throw new Error('Decorator cannot be applied to a class')
153
- return description.class!(target)
154
- }
155
- } else if (typeof target === 'object' && ['string', 'symbol'].includes(typeof propertyKey)) {
156
- if (!descriptor) throw new Error('Decorator cannot be applied to a field')
157
- else if (typeof descriptor === 'object' && 'configurable' in descriptor) {
158
- if ('get' in descriptor || 'set' in descriptor) {
159
- if (!('getter' in description || 'setter' in description))
160
- throw new Error('Decorator cannot be applied to a getter or setter')
161
- if ('getter' in description) {
162
- const newGetter = description.getter!(descriptor.get as any, target, propertyKey)
163
- if (newGetter) descriptor.get = newGetter
164
- }
165
- if ('setter' in description) {
166
- const newSetter = description.setter!(descriptor.set as any, target, propertyKey)
167
- if (newSetter) descriptor.set = newSetter
168
- }
169
- return descriptor
170
- } else if (typeof descriptor.value === 'function') {
171
- if (!('method' in description)) throw new Error('Decorator cannot be applied to a method')
172
- const newMethod = description.method!(descriptor.value, target, propertyKey)
173
- if (newMethod) descriptor.value = newMethod
174
- return descriptor
175
- }
176
- }
177
- }
178
- if (!('default' in description))
179
- throw new Error('Decorator do not have a default implementation')
180
- return description.default!.call(this, target, propertyKey, descriptor, ...args)
181
- }
182
- }
183
-
184
- /**
185
- * Creates a decorator that works with Modern decorator proposals
186
- * @param description - The decorator description object
187
- * @returns A decorator function compatible with Modern decorators
188
- */
189
- export function modernDecorator<T = any>(description: DecoratorDescription<T>): any {
190
- /*return function (target: any, context?: DecoratorContext, ...args: any[]) {*/
191
- return function (this: any, target: any, context?: DecoratorContext, ...args: any[]) {
192
- if (!context?.kind || typeof context.kind !== 'string') {
193
- if (!('default' in description))
194
- throw new Error('Decorator do not have a default implementation')
195
- return description.default!.call(this, target, context, ...args)
196
- }
197
- switch (context.kind) {
198
- case 'class':
199
- if (!('class' in description)) throw new Error('Decorator cannot be applied to a class')
200
- return description.class!(target)
201
- case 'field':
202
- throw new Error('Decorator cannot be applied to a field')
203
- case 'getter':
204
- if (!('getter' in description)) throw new Error('Decorator cannot be applied to a getter')
205
- return description.getter!(target, target, context.name)
206
- case 'setter':
207
- if (!('setter' in description)) throw new Error('Decorator cannot be applied to a setter')
208
- return description.setter!(target, target, context.name)
209
- case 'method':
210
- if (!('method' in description)) throw new Error('Decorator cannot be applied to a method')
211
- return description.method!(target, target, context.name)
212
- case 'accessor': {
213
- if (!('getter' in description || 'setter' in description))
214
- throw new Error('Decorator cannot be applied to a getter or setter')
215
- const rv: Partial<ClassAccessorDecoratorResult<any, any>> = {}
216
- if ('getter' in description) {
217
- const newGetter = description.getter!(target.get, target, context.name)
218
- if (newGetter) rv.get = newGetter
219
- }
220
- if ('setter' in description) {
221
- const newSetter = description.setter!(target.set, target, context.name)
222
- if (newSetter) rv.set = newSetter
223
- }
224
- return rv
225
- }
226
- //return description.accessor?.(target, context.name, target)
227
- }
228
- }
229
- }
230
-
231
- /**
232
- * Detects if the decorator is being called in modern (Modern) or legacy (Legacy) mode
233
- * based on the arguments passed to the decorator function
234
- */
235
- function detectDecoratorMode(
236
- _target: any,
237
- contextOrKey?: any,
238
- _descriptor?: any
239
- ): 'modern' | 'legacy' {
240
- // Modern decorators have a context object as the second parameter
241
- // Legacy decorators have a string/symbol key as the second parameter
242
- if (
243
- typeof contextOrKey === 'object' &&
244
- contextOrKey !== null &&
245
- typeof contextOrKey.kind === 'string'
246
- ) {
247
- return 'modern'
248
- }
249
- return 'legacy'
250
- }
251
-
252
- /**
253
- * Main decorator factory that automatically detects and works with both Legacy and Modern decorator proposals
254
- * @param description - The decorator description object
255
- * @returns A decorator that works in both Legacy and Modern environments
256
- */
257
- export const decorator: DecoratorFactory<any> = (description: DecoratorDescription<any>) => {
258
- const modern = modernDecorator(description)
259
- const legacy = legacyDecorator(description)
260
- return ((target: any, contextOrKey?: any, ...args: any[]) => {
261
- const mode = detectDecoratorMode(target, contextOrKey, args[0])
262
- return mode === 'modern'
263
- ? modern(target, contextOrKey, ...args)
264
- : legacy(target, contextOrKey, ...args)
265
- }) as any
266
- }
267
-
268
- /**
269
- * Generic class decorator type that works with both Legacy and Modern decorator proposals
270
- */
271
- export type GenericClassDecorator<T> = LegacyClassDecorator<abstract new (...args: any[]) => T> &
272
- ModernClassDecorator<abstract new (...args: any[]) => T>
@@ -1,199 +0,0 @@
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[allocatedValues])
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
- [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, allocated)
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, _target, 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
- }
@@ -1,5 +0,0 @@
1
- // Import environment-specific patches first (SIDE EFFECTS)
2
- import './async/browser'
3
-
4
- // Then export the library
5
- export * from './index'
package/src/entry-node.ts DELETED
@@ -1,5 +0,0 @@
1
- // Import environment-specific patches first (SIDE EFFECTS)
2
- import './async/node'
3
-
4
- // Then export the library
5
- export * from './index'
package/src/eventful.ts DELETED
@@ -1,77 +0,0 @@
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 DELETED
@@ -1,12 +0,0 @@
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 './promiseChain';
8
- export * from './reactive';
9
- export * from './std-decorators';
10
- export * from './utils';
11
- export * from './zone';
12
- export * from './async';
package/src/index.ts DELETED
@@ -1,64 +0,0 @@
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 './promiseChain'
8
- export * from './reactive'
9
- export * from './std-decorators'
10
- export * from './utils'
11
- export * from './zone'
12
- export * from './async'
13
-
14
- import pkg from '../package.json'
15
-
16
- const { version } = pkg
17
-
18
- // Singleton verification
19
- const GLOBAL_MUTTS_KEY = '__MUTTS_INSTANCE__'
20
- const globalScope = (
21
- typeof globalThis !== 'undefined'
22
- ? globalThis
23
- : typeof window !== 'undefined'
24
- ? window
25
- : typeof global !== 'undefined'
26
- ? global
27
- : false
28
- ) as any
29
- if (globalScope) {
30
- // Detect the source of this instance safely across different environments
31
- let source = 'mutts/index'
32
- const viteEval = eval
33
- try {
34
- // @ts-ignore
35
- if (typeof __filename !== 'undefined') source = __filename
36
- else {
37
- // Using eval to avoid SyntaxError in CJS environments where import.meta is not allowed
38
- const meta = viteEval('import.meta')
39
- if (meta && meta.url) source = meta.url
40
- }
41
- } catch (e) {
42
- // Fallback for environments where neither is available or accessible
43
- }
44
-
45
- const currentSourceInfo = {
46
- version,
47
- source,
48
- timestamp: Date.now(),
49
- }
50
-
51
- if (globalScope[GLOBAL_MUTTS_KEY]) {
52
- const existing = globalScope[GLOBAL_MUTTS_KEY]
53
- throw new Error(
54
- `[Mutts] Multiple instances detected!\n` +
55
- `Existing instance: ${JSON.stringify(existing, null, 2)}\n` +
56
- `New instance: ${JSON.stringify(currentSourceInfo, null, 2)}\n` +
57
- `This usually happens when 'mutts' is both installed as a dependency and bundled, ` +
58
- `or when different versions are loaded. ` +
59
- `Please check your build configuration (aliases, externals) to ensure a single source of truth.`
60
- )
61
- }
62
-
63
- globalScope[GLOBAL_MUTTS_KEY] = currentSourceInfo
64
- }