mutts 1.0.5 → 1.0.6
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 +1 -0
- package/dist/chunks/{_tslib-Mzh1rNsX.esm.js → _tslib-MCKDzsSq.esm.js} +2 -2
- package/dist/chunks/_tslib-MCKDzsSq.esm.js.map +1 -0
- package/dist/chunks/decorator-BGILvPtN.esm.js +627 -0
- package/dist/chunks/decorator-BGILvPtN.esm.js.map +1 -0
- package/dist/chunks/decorator-BQ2eBTCj.js +651 -0
- package/dist/chunks/decorator-BQ2eBTCj.js.map +1 -0
- package/dist/chunks/{index-Cvxdw6Ax.js → index-CDCOjzTy.js} +396 -500
- package/dist/chunks/index-CDCOjzTy.js.map +1 -0
- package/dist/chunks/{index-qiWwozOc.esm.js → index-DiP0RXoZ.esm.js} +301 -403
- package/dist/chunks/index-DiP0RXoZ.esm.js.map +1 -0
- package/dist/decorator.d.ts +3 -3
- package/dist/decorator.esm.js +1 -1
- package/dist/decorator.js +1 -1
- package/dist/destroyable.esm.js +4 -4
- package/dist/destroyable.esm.js.map +1 -1
- package/dist/destroyable.js +4 -4
- package/dist/destroyable.js.map +1 -1
- package/dist/devtools/panel.js.map +1 -1
- package/dist/eventful.esm.js +1 -1
- package/dist/index.esm.js +48 -3
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +48 -4
- package/dist/index.js.map +1 -1
- package/dist/mutts.umd.js +1 -1
- package/dist/mutts.umd.js.map +1 -1
- package/dist/mutts.umd.min.js +1 -1
- package/dist/mutts.umd.min.js.map +1 -1
- package/dist/reactive.d.ts +25 -0
- package/dist/reactive.esm.js +3 -3
- package/dist/reactive.js +4 -4
- package/dist/std-decorators.d.ts +1 -1
- package/dist/std-decorators.esm.js +10 -10
- package/dist/std-decorators.esm.js.map +1 -1
- package/dist/std-decorators.js +10 -10
- package/dist/std-decorators.js.map +1 -1
- package/docs/ai/manual.md +14 -95
- package/docs/reactive/advanced.md +6 -107
- package/docs/reactive/debugging.md +158 -0
- package/docs/reactive.md +6 -5
- package/package.json +16 -66
- package/src/decorator.ts +11 -9
- package/src/destroyable.ts +3 -3
- package/src/index.ts +46 -0
- package/src/reactive/change.ts +1 -1
- package/src/reactive/debug.ts +1 -1
- package/src/reactive/deep-touch.ts +1 -1
- package/src/reactive/deep-watch.ts +1 -1
- package/src/reactive/effect-context.ts +2 -2
- package/src/reactive/effects.ts +44 -16
- package/src/reactive/index.ts +1 -1
- package/src/reactive/interface.ts +9 -8
- package/src/reactive/memoize.ts +77 -31
- package/src/reactive/proxy.ts +4 -4
- package/src/reactive/registry.ts +67 -0
- package/src/reactive/tracking.ts +12 -41
- package/src/reactive/types.ts +37 -0
- package/src/std-decorators.ts +9 -9
- package/src/utils.ts +141 -0
- package/dist/chunks/_tslib-Mzh1rNsX.esm.js.map +0 -1
- package/dist/chunks/decorator-DLvrD0UF.js +0 -265
- package/dist/chunks/decorator-DLvrD0UF.js.map +0 -1
- package/dist/chunks/decorator-DqiszP7i.esm.js +0 -253
- package/dist/chunks/decorator-DqiszP7i.esm.js.map +0 -1
- package/dist/chunks/index-Cvxdw6Ax.js.map +0 -1
- package/dist/chunks/index-qiWwozOc.esm.js.map +0 -1
package/dist/decorator.d.ts
CHANGED
|
@@ -32,9 +32,9 @@ type ModernAccessorDecorator<T> = (target: T, context: ClassAccessorDecoratorCon
|
|
|
32
32
|
* Modern class decorator type
|
|
33
33
|
*/
|
|
34
34
|
type ModernClassDecorator<T> = (target: T, context: ClassDecoratorContext) => any;
|
|
35
|
-
type DDMethod<T> = (original: (this: T, ...args: any[]) => any, name: PropertyKey) => ((this: T, ...args: any[]) => any) | void;
|
|
36
|
-
type DDGetter<T> = (original: (this: T) => any, name: PropertyKey) => ((this: T) => any) | void;
|
|
37
|
-
type DDSetter<T> = (original: (this: T, value: any) => void, name: PropertyKey) => ((this: T, value: any) => void) | void;
|
|
35
|
+
type DDMethod<T> = (original: (this: T, ...args: any[]) => any, target: any, name: PropertyKey) => ((this: T, ...args: any[]) => any) | void;
|
|
36
|
+
type DDGetter<T> = (original: (this: T) => any, target: any, name: PropertyKey) => ((this: T) => any) | void;
|
|
37
|
+
type DDSetter<T> = (original: (this: T, value: any) => void, target: any, name: PropertyKey) => ((this: T, value: any) => void) | void;
|
|
38
38
|
type DDClass<T> = <Ctor extends new (...args: any[]) => T = new (...args: any[]) => T>(target: Ctor) => Ctor | void;
|
|
39
39
|
/**
|
|
40
40
|
* Description object for creating decorators that work with both Legacy and Modern decorator proposals
|
package/dist/decorator.esm.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { D as DecoratorError, d as decorator, l as legacyDecorator, m as modernDecorator } from './chunks/decorator-
|
|
1
|
+
export { D as DecoratorError, d as decorator, l as legacyDecorator, m as modernDecorator } from './chunks/decorator-BGILvPtN.esm.js';
|
|
2
2
|
//# sourceMappingURL=decorator.esm.js.map
|
package/dist/decorator.js
CHANGED
package/dist/destroyable.esm.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { d as decorator } from './chunks/decorator-
|
|
1
|
+
import { d as decorator } from './chunks/decorator-BGILvPtN.esm.js';
|
|
2
2
|
|
|
3
3
|
// Integrated with `using` statement via Symbol.dispose
|
|
4
4
|
const fr = new FinalizationRegistry((f) => f());
|
|
@@ -44,7 +44,7 @@ function Destroyable(base, destructorObj) {
|
|
|
44
44
|
const destructor = _a.destructors.get(obj);
|
|
45
45
|
if (!destructor)
|
|
46
46
|
return false;
|
|
47
|
-
fr.unregister(obj);
|
|
47
|
+
fr.unregister(obj[allocatedValues]);
|
|
48
48
|
_a.destructors.delete(obj);
|
|
49
49
|
Object.setPrototypeOf(obj, new Proxy({}, destroyedHandler));
|
|
50
50
|
// Clear all own properties
|
|
@@ -70,7 +70,7 @@ function Destroyable(base, destructorObj) {
|
|
|
70
70
|
myDestructor(allocated);
|
|
71
71
|
}
|
|
72
72
|
_a.destructors.set(this, destruction);
|
|
73
|
-
fr.register(this, destruction,
|
|
73
|
+
fr.register(this, destruction, allocated);
|
|
74
74
|
}
|
|
75
75
|
},
|
|
76
76
|
_a.destructors = new WeakMap(),
|
|
@@ -81,7 +81,7 @@ function Destroyable(base, destructorObj) {
|
|
|
81
81
|
* Use with accessor properties or explicit get/set pairs
|
|
82
82
|
*/
|
|
83
83
|
const allocated = decorator({
|
|
84
|
-
setter(original, propertyKey) {
|
|
84
|
+
setter(original, _target, propertyKey) {
|
|
85
85
|
return function (value) {
|
|
86
86
|
this[allocatedValues][propertyKey] = value;
|
|
87
87
|
return original.call(this, value);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"destroyable.esm.js","sources":["../src/destroyable.ts"],"sourcesContent":["import { decorator } from './decorator'\n\n// Integrated with `using` statement via Symbol.dispose\nconst fr = new FinalizationRegistry<() => void>((f) => f())\n/**\n * Symbol for marking destructor methods\n */\nexport const destructor = Symbol('destructor')\n/**\n * Symbol for accessing allocated values in destroyable objects\n */\nexport const allocatedValues = Symbol('allocated')\n/**\n * Error thrown when attempting to access a destroyed object\n */\nexport class DestructionError extends Error {\n\tstatic throw<_T = void>(msg: string) {\n\t\treturn () => {\n\t\t\tthrow new DestructionError(msg)\n\t\t}\n\t}\n\tconstructor(msg: string) {\n\t\tsuper(`Object is destroyed. ${msg}`)\n\t\tthis.name = 'DestroyedAccessError'\n\t}\n}\nconst destroyedHandler = {\n\t[Symbol.toStringTag]: 'MutTs Destroyable',\n\tget: DestructionError.throw('Cannot access destroyed object'),\n\tset: DestructionError.throw('Cannot access destroyed object'),\n} as const\n\nabstract class AbstractDestroyable<Allocated> {\n\tabstract [destructor](allocated: Allocated): void\n\t[Symbol.dispose](): void {\n\t\tthis[destructor](this as unknown as Allocated)\n\t}\n}\n\ninterface Destructor<Allocated> {\n\tdestructor(allocated: Allocated): void\n}\n\n/**\n * Creates a destroyable class with a base class and destructor object\n * @param base - The base class to extend\n * @param destructorObj - Object containing the destructor method\n * @returns A destroyable class with static destroy and isDestroyable methods\n */\nexport function Destroyable<\n\tT extends new (\n\t\t...args: any[]\n\t) => any,\n\tAllocated extends Partial<InstanceType<T>>,\n>(\n\tbase: T,\n\tdestructorObj: Destructor<Allocated>\n): (new (\n\t...args: ConstructorParameters<T>\n) => InstanceType<T> & { [allocatedValues]: Allocated }) & {\n\tdestroy(obj: InstanceType<T>): boolean\n\tisDestroyable(obj: InstanceType<T>): boolean\n}\n\n/**\n * Creates a destroyable class with only a destructor object (no base class)\n * @param destructorObj - Object containing the destructor method\n * @returns A destroyable class with static destroy and isDestroyable methods\n */\nexport function Destroyable<Allocated extends Record<PropertyKey, any> = Record<PropertyKey, any>>(\n\tdestructorObj: Destructor<Allocated>\n): (new () => { [allocatedValues]: Allocated }) & {\n\tdestroy(obj: any): boolean\n\tisDestroyable(obj: any): boolean\n}\n\n/**\n * Creates a destroyable class with a base class (requires [destructor] method)\n * @param base - The base class to extend\n * @returns A destroyable class with static destroy and isDestroyable methods\n */\nexport function Destroyable<\n\tT extends new (\n\t\t...args: any[]\n\t) => any,\n\tAllocated extends Record<PropertyKey, any> = Record<PropertyKey, any>,\n>(\n\tbase: T\n): (new (\n\t...args: ConstructorParameters<T>\n) => AbstractDestroyable<Allocated> & InstanceType<T> & { [allocatedValues]: Allocated }) & {\n\tdestroy(obj: InstanceType<T>): boolean\n\tisDestroyable(obj: InstanceType<T>): boolean\n}\n\n/**\n * Creates an abstract destroyable base class\n * @returns An abstract destroyable class with static destroy and isDestroyable methods\n */\nexport function Destroyable<\n\tAllocated extends Record<PropertyKey, any> = Record<PropertyKey, any>,\n>(): abstract new () => (AbstractDestroyable<Allocated> & {\n\t[allocatedValues]: Allocated\n}) & {\n\tdestroy(obj: any): boolean\n\tisDestroyable(obj: any): boolean\n}\n\nexport function Destroyable<\n\tT extends new (\n\t\t...args: any[]\n\t) => any,\n\tAllocated extends Record<PropertyKey, any> = Record<PropertyKey, any>,\n>(base?: T | Destructor<Allocated>, destructorObj?: Destructor<Allocated>) {\n\tif (base && typeof base !== 'function') {\n\t\tdestructorObj = base as Destructor<Allocated>\n\t\tbase = undefined\n\t}\n\tif (!base) {\n\t\tbase = class { } as T\n\t}\n\n\treturn class Destroyable extends (base as T) {\n\t\tstatic readonly destructors = new WeakMap<any, () => void>()\n\t\tstatic destroy(obj: Destroyable) {\n\t\t\tconst destructor = Destroyable.destructors.get(obj)\n\t\t\tif (!destructor) return false\n\t\t\tfr.unregister(obj)\n\t\t\tDestroyable.destructors.delete(obj)\n\t\t\tObject.setPrototypeOf(obj, new Proxy({}, destroyedHandler))\n\t\t\t// Clear all own properties\n\t\t\tfor (const key of Object.getOwnPropertyNames(obj)) {\n\t\t\t\tdelete (obj as any)[key]\n\t\t\t}\n\t\t\tdestructor()\n\t\t\treturn true\n\t\t}\n\t\tstatic isDestroyable(obj: Destroyable) {\n\t\t\treturn Destroyable.destructors.has(obj)\n\t\t}\n\n\t\t[forwardProperties]!: PropertyKey[]\n\t\treadonly [allocatedValues]: Allocated\n\t\tconstructor(...args: any[]) {\n\t\t\tsuper(...args)\n\t\t\tconst allocated = {} as Allocated\n\t\t\tthis[allocatedValues] = allocated\n\t\t\t// @ts-expect-error `this` is an AbstractDestroyable\n\t\t\tconst myDestructor = destructorObj?.destructor ?? this[destructor]\n\t\t\tif (!myDestructor) {\n\t\t\t\tthrow new DestructionError('Destructor is not defined')\n\t\t\t}\n\t\t\tfunction destruction() {\n\t\t\t\tmyDestructor(allocated)\n\t\t\t}\n\t\t\tDestroyable.destructors.set(this, destruction)\n\t\t\tfr.register(this, destruction,
|
|
1
|
+
{"version":3,"file":"destroyable.esm.js","sources":["../src/destroyable.ts"],"sourcesContent":["import { decorator } from './decorator'\n\n// Integrated with `using` statement via Symbol.dispose\nconst fr = new FinalizationRegistry<() => void>((f) => f())\n/**\n * Symbol for marking destructor methods\n */\nexport const destructor = Symbol('destructor')\n/**\n * Symbol for accessing allocated values in destroyable objects\n */\nexport const allocatedValues = Symbol('allocated')\n/**\n * Error thrown when attempting to access a destroyed object\n */\nexport class DestructionError extends Error {\n\tstatic throw<_T = void>(msg: string) {\n\t\treturn () => {\n\t\t\tthrow new DestructionError(msg)\n\t\t}\n\t}\n\tconstructor(msg: string) {\n\t\tsuper(`Object is destroyed. ${msg}`)\n\t\tthis.name = 'DestroyedAccessError'\n\t}\n}\nconst destroyedHandler = {\n\t[Symbol.toStringTag]: 'MutTs Destroyable',\n\tget: DestructionError.throw('Cannot access destroyed object'),\n\tset: DestructionError.throw('Cannot access destroyed object'),\n} as const\n\nabstract class AbstractDestroyable<Allocated> {\n\tabstract [destructor](allocated: Allocated): void\n\t[Symbol.dispose](): void {\n\t\tthis[destructor](this as unknown as Allocated)\n\t}\n}\n\ninterface Destructor<Allocated> {\n\tdestructor(allocated: Allocated): void\n}\n\n/**\n * Creates a destroyable class with a base class and destructor object\n * @param base - The base class to extend\n * @param destructorObj - Object containing the destructor method\n * @returns A destroyable class with static destroy and isDestroyable methods\n */\nexport function Destroyable<\n\tT extends new (\n\t\t...args: any[]\n\t) => any,\n\tAllocated extends Partial<InstanceType<T>>,\n>(\n\tbase: T,\n\tdestructorObj: Destructor<Allocated>\n): (new (\n\t...args: ConstructorParameters<T>\n) => InstanceType<T> & { [allocatedValues]: Allocated }) & {\n\tdestroy(obj: InstanceType<T>): boolean\n\tisDestroyable(obj: InstanceType<T>): boolean\n}\n\n/**\n * Creates a destroyable class with only a destructor object (no base class)\n * @param destructorObj - Object containing the destructor method\n * @returns A destroyable class with static destroy and isDestroyable methods\n */\nexport function Destroyable<Allocated extends Record<PropertyKey, any> = Record<PropertyKey, any>>(\n\tdestructorObj: Destructor<Allocated>\n): (new () => { [allocatedValues]: Allocated }) & {\n\tdestroy(obj: any): boolean\n\tisDestroyable(obj: any): boolean\n}\n\n/**\n * Creates a destroyable class with a base class (requires [destructor] method)\n * @param base - The base class to extend\n * @returns A destroyable class with static destroy and isDestroyable methods\n */\nexport function Destroyable<\n\tT extends new (\n\t\t...args: any[]\n\t) => any,\n\tAllocated extends Record<PropertyKey, any> = Record<PropertyKey, any>,\n>(\n\tbase: T\n): (new (\n\t...args: ConstructorParameters<T>\n) => AbstractDestroyable<Allocated> & InstanceType<T> & { [allocatedValues]: Allocated }) & {\n\tdestroy(obj: InstanceType<T>): boolean\n\tisDestroyable(obj: InstanceType<T>): boolean\n}\n\n/**\n * Creates an abstract destroyable base class\n * @returns An abstract destroyable class with static destroy and isDestroyable methods\n */\nexport function Destroyable<\n\tAllocated extends Record<PropertyKey, any> = Record<PropertyKey, any>,\n>(): abstract new () => (AbstractDestroyable<Allocated> & {\n\t[allocatedValues]: Allocated\n}) & {\n\tdestroy(obj: any): boolean\n\tisDestroyable(obj: any): boolean\n}\n\nexport function Destroyable<\n\tT extends new (\n\t\t...args: any[]\n\t) => any,\n\tAllocated extends Record<PropertyKey, any> = Record<PropertyKey, any>,\n>(base?: T | Destructor<Allocated>, destructorObj?: Destructor<Allocated>) {\n\tif (base && typeof base !== 'function') {\n\t\tdestructorObj = base as Destructor<Allocated>\n\t\tbase = undefined\n\t}\n\tif (!base) {\n\t\tbase = class { } as T\n\t}\n\n\treturn class Destroyable extends (base as T) {\n\t\tstatic readonly destructors = new WeakMap<any, () => void>()\n\t\tstatic destroy(obj: Destroyable) {\n\t\t\tconst destructor = Destroyable.destructors.get(obj)\n\t\t\tif (!destructor) return false\n\t\t\tfr.unregister(obj[allocatedValues])\n\t\t\tDestroyable.destructors.delete(obj)\n\t\t\tObject.setPrototypeOf(obj, new Proxy({}, destroyedHandler))\n\t\t\t// Clear all own properties\n\t\t\tfor (const key of Object.getOwnPropertyNames(obj)) {\n\t\t\t\tdelete (obj as any)[key]\n\t\t\t}\n\t\t\tdestructor()\n\t\t\treturn true\n\t\t}\n\t\tstatic isDestroyable(obj: Destroyable) {\n\t\t\treturn Destroyable.destructors.has(obj)\n\t\t}\n\n\t\t[forwardProperties]!: PropertyKey[]\n\t\treadonly [allocatedValues]: Allocated\n\t\tconstructor(...args: any[]) {\n\t\t\tsuper(...args)\n\t\t\tconst allocated = {} as Allocated\n\t\t\tthis[allocatedValues] = allocated\n\t\t\t// @ts-expect-error `this` is an AbstractDestroyable\n\t\t\tconst myDestructor = destructorObj?.destructor ?? this[destructor]\n\t\t\tif (!myDestructor) {\n\t\t\t\tthrow new DestructionError('Destructor is not defined')\n\t\t\t}\n\t\t\tfunction destruction() {\n\t\t\t\tmyDestructor(allocated)\n\t\t\t}\n\t\t\tDestroyable.destructors.set(this, destruction)\n\t\t\tfr.register(this, destruction, allocated)\n\t\t}\n\t}\n}\n\nconst forwardProperties = Symbol('forwardProperties')\n/**\n * Decorator that marks properties to be stored in the allocated object and passed to the destructor\n * Use with accessor properties or explicit get/set pairs\n */\nexport const allocated = decorator({\n\tsetter(original, _target, propertyKey) {\n\t\treturn function (value) {\n\t\t\tthis[allocatedValues][propertyKey] = value\n\t\t\treturn original.call(this, value)\n\t\t}\n\t},\n})\n\n/**\n * Registers a callback to be called when an object is garbage collected\n * @param cb - The callback function to execute on garbage collection\n * @returns The object whose reference can be collected\n */\nexport function callOnGC(cb: () => void) {\n\tlet called = false\n\tconst forward = () => {\n\t\tif (called) return\n\t\tcalled = true\n\t\tcb()\n\t}\n\tfr.register(forward, cb, cb)\n\treturn forward\n}\n\n/**\n * Context Manager Protocol for `using` statement integration\n * Provides automatic resource cleanup when used with the `using` statement\n */\nexport interface ContextManager<T = any> {\n\t[Symbol.dispose](): void\n\tvalue?: T\n}\n"],"names":[],"mappings":";;AAEA;AACA,MAAM,EAAE,GAAG,IAAI,oBAAoB,CAAa,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC;AAC3D;;AAEG;MACU,UAAU,GAAG,MAAM,CAAC,YAAY;AAC7C;;AAEG;MACU,eAAe,GAAG,MAAM,CAAC,WAAW;AACjD;;AAEG;AACG,MAAO,gBAAiB,SAAQ,KAAK,CAAA;IAC1C,OAAO,KAAK,CAAY,GAAW,EAAA;AAClC,QAAA,OAAO,MAAK;AACX,YAAA,MAAM,IAAI,gBAAgB,CAAC,GAAG,CAA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MAAM,GAAG,IAAI;AACb,QAAA,EAAE,EAAE;AACL,IAAA,CAAC;IACD,EAAE,CAAC,QAAQ,CAAC,OAAO,EAAE,EAAE,EAAE,EAAE,CAAC;AAC5B,IAAA,OAAO,OAAO;AACf;;;;"}
|
package/dist/destroyable.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var decorator = require('./chunks/decorator-
|
|
3
|
+
var decorator = require('./chunks/decorator-BQ2eBTCj.js');
|
|
4
4
|
|
|
5
5
|
// Integrated with `using` statement via Symbol.dispose
|
|
6
6
|
const fr = new FinalizationRegistry((f) => f());
|
|
@@ -46,7 +46,7 @@ function Destroyable(base, destructorObj) {
|
|
|
46
46
|
const destructor = _a.destructors.get(obj);
|
|
47
47
|
if (!destructor)
|
|
48
48
|
return false;
|
|
49
|
-
fr.unregister(obj);
|
|
49
|
+
fr.unregister(obj[allocatedValues]);
|
|
50
50
|
_a.destructors.delete(obj);
|
|
51
51
|
Object.setPrototypeOf(obj, new Proxy({}, destroyedHandler));
|
|
52
52
|
// Clear all own properties
|
|
@@ -72,7 +72,7 @@ function Destroyable(base, destructorObj) {
|
|
|
72
72
|
myDestructor(allocated);
|
|
73
73
|
}
|
|
74
74
|
_a.destructors.set(this, destruction);
|
|
75
|
-
fr.register(this, destruction,
|
|
75
|
+
fr.register(this, destruction, allocated);
|
|
76
76
|
}
|
|
77
77
|
},
|
|
78
78
|
_a.destructors = new WeakMap(),
|
|
@@ -83,7 +83,7 @@ function Destroyable(base, destructorObj) {
|
|
|
83
83
|
* Use with accessor properties or explicit get/set pairs
|
|
84
84
|
*/
|
|
85
85
|
const allocated = decorator.decorator({
|
|
86
|
-
setter(original, propertyKey) {
|
|
86
|
+
setter(original, _target, propertyKey) {
|
|
87
87
|
return function (value) {
|
|
88
88
|
this[allocatedValues][propertyKey] = value;
|
|
89
89
|
return original.call(this, value);
|
package/dist/destroyable.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"destroyable.js","sources":["../src/destroyable.ts"],"sourcesContent":["import { decorator } from './decorator'\n\n// Integrated with `using` statement via Symbol.dispose\nconst fr = new FinalizationRegistry<() => void>((f) => f())\n/**\n * Symbol for marking destructor methods\n */\nexport const destructor = Symbol('destructor')\n/**\n * Symbol for accessing allocated values in destroyable objects\n */\nexport const allocatedValues = Symbol('allocated')\n/**\n * Error thrown when attempting to access a destroyed object\n */\nexport class DestructionError extends Error {\n\tstatic throw<_T = void>(msg: string) {\n\t\treturn () => {\n\t\t\tthrow new DestructionError(msg)\n\t\t}\n\t}\n\tconstructor(msg: string) {\n\t\tsuper(`Object is destroyed. ${msg}`)\n\t\tthis.name = 'DestroyedAccessError'\n\t}\n}\nconst destroyedHandler = {\n\t[Symbol.toStringTag]: 'MutTs Destroyable',\n\tget: DestructionError.throw('Cannot access destroyed object'),\n\tset: DestructionError.throw('Cannot access destroyed object'),\n} as const\n\nabstract class AbstractDestroyable<Allocated> {\n\tabstract [destructor](allocated: Allocated): void\n\t[Symbol.dispose](): void {\n\t\tthis[destructor](this as unknown as Allocated)\n\t}\n}\n\ninterface Destructor<Allocated> {\n\tdestructor(allocated: Allocated): void\n}\n\n/**\n * Creates a destroyable class with a base class and destructor object\n * @param base - The base class to extend\n * @param destructorObj - Object containing the destructor method\n * @returns A destroyable class with static destroy and isDestroyable methods\n */\nexport function Destroyable<\n\tT extends new (\n\t\t...args: any[]\n\t) => any,\n\tAllocated extends Partial<InstanceType<T>>,\n>(\n\tbase: T,\n\tdestructorObj: Destructor<Allocated>\n): (new (\n\t...args: ConstructorParameters<T>\n) => InstanceType<T> & { [allocatedValues]: Allocated }) & {\n\tdestroy(obj: InstanceType<T>): boolean\n\tisDestroyable(obj: InstanceType<T>): boolean\n}\n\n/**\n * Creates a destroyable class with only a destructor object (no base class)\n * @param destructorObj - Object containing the destructor method\n * @returns A destroyable class with static destroy and isDestroyable methods\n */\nexport function Destroyable<Allocated extends Record<PropertyKey, any> = Record<PropertyKey, any>>(\n\tdestructorObj: Destructor<Allocated>\n): (new () => { [allocatedValues]: Allocated }) & {\n\tdestroy(obj: any): boolean\n\tisDestroyable(obj: any): boolean\n}\n\n/**\n * Creates a destroyable class with a base class (requires [destructor] method)\n * @param base - The base class to extend\n * @returns A destroyable class with static destroy and isDestroyable methods\n */\nexport function Destroyable<\n\tT extends new (\n\t\t...args: any[]\n\t) => any,\n\tAllocated extends Record<PropertyKey, any> = Record<PropertyKey, any>,\n>(\n\tbase: T\n): (new (\n\t...args: ConstructorParameters<T>\n) => AbstractDestroyable<Allocated> & InstanceType<T> & { [allocatedValues]: Allocated }) & {\n\tdestroy(obj: InstanceType<T>): boolean\n\tisDestroyable(obj: InstanceType<T>): boolean\n}\n\n/**\n * Creates an abstract destroyable base class\n * @returns An abstract destroyable class with static destroy and isDestroyable methods\n */\nexport function Destroyable<\n\tAllocated extends Record<PropertyKey, any> = Record<PropertyKey, any>,\n>(): abstract new () => (AbstractDestroyable<Allocated> & {\n\t[allocatedValues]: Allocated\n}) & {\n\tdestroy(obj: any): boolean\n\tisDestroyable(obj: any): boolean\n}\n\nexport function Destroyable<\n\tT extends new (\n\t\t...args: any[]\n\t) => any,\n\tAllocated extends Record<PropertyKey, any> = Record<PropertyKey, any>,\n>(base?: T | Destructor<Allocated>, destructorObj?: Destructor<Allocated>) {\n\tif (base && typeof base !== 'function') {\n\t\tdestructorObj = base as Destructor<Allocated>\n\t\tbase = undefined\n\t}\n\tif (!base) {\n\t\tbase = class { } as T\n\t}\n\n\treturn class Destroyable extends (base as T) {\n\t\tstatic readonly destructors = new WeakMap<any, () => void>()\n\t\tstatic destroy(obj: Destroyable) {\n\t\t\tconst destructor = Destroyable.destructors.get(obj)\n\t\t\tif (!destructor) return false\n\t\t\tfr.unregister(obj)\n\t\t\tDestroyable.destructors.delete(obj)\n\t\t\tObject.setPrototypeOf(obj, new Proxy({}, destroyedHandler))\n\t\t\t// Clear all own properties\n\t\t\tfor (const key of Object.getOwnPropertyNames(obj)) {\n\t\t\t\tdelete (obj as any)[key]\n\t\t\t}\n\t\t\tdestructor()\n\t\t\treturn true\n\t\t}\n\t\tstatic isDestroyable(obj: Destroyable) {\n\t\t\treturn Destroyable.destructors.has(obj)\n\t\t}\n\n\t\t[forwardProperties]!: PropertyKey[]\n\t\treadonly [allocatedValues]: Allocated\n\t\tconstructor(...args: any[]) {\n\t\t\tsuper(...args)\n\t\t\tconst allocated = {} as Allocated\n\t\t\tthis[allocatedValues] = allocated\n\t\t\t// @ts-expect-error `this` is an AbstractDestroyable\n\t\t\tconst myDestructor = destructorObj?.destructor ?? this[destructor]\n\t\t\tif (!myDestructor) {\n\t\t\t\tthrow new DestructionError('Destructor is not defined')\n\t\t\t}\n\t\t\tfunction destruction() {\n\t\t\t\tmyDestructor(allocated)\n\t\t\t}\n\t\t\tDestroyable.destructors.set(this, destruction)\n\t\t\tfr.register(this, destruction,
|
|
1
|
+
{"version":3,"file":"destroyable.js","sources":["../src/destroyable.ts"],"sourcesContent":["import { decorator } from './decorator'\n\n// Integrated with `using` statement via Symbol.dispose\nconst fr = new FinalizationRegistry<() => void>((f) => f())\n/**\n * Symbol for marking destructor methods\n */\nexport const destructor = Symbol('destructor')\n/**\n * Symbol for accessing allocated values in destroyable objects\n */\nexport const allocatedValues = Symbol('allocated')\n/**\n * Error thrown when attempting to access a destroyed object\n */\nexport class DestructionError extends Error {\n\tstatic throw<_T = void>(msg: string) {\n\t\treturn () => {\n\t\t\tthrow new DestructionError(msg)\n\t\t}\n\t}\n\tconstructor(msg: string) {\n\t\tsuper(`Object is destroyed. ${msg}`)\n\t\tthis.name = 'DestroyedAccessError'\n\t}\n}\nconst destroyedHandler = {\n\t[Symbol.toStringTag]: 'MutTs Destroyable',\n\tget: DestructionError.throw('Cannot access destroyed object'),\n\tset: DestructionError.throw('Cannot access destroyed object'),\n} as const\n\nabstract class AbstractDestroyable<Allocated> {\n\tabstract [destructor](allocated: Allocated): void\n\t[Symbol.dispose](): void {\n\t\tthis[destructor](this as unknown as Allocated)\n\t}\n}\n\ninterface Destructor<Allocated> {\n\tdestructor(allocated: Allocated): void\n}\n\n/**\n * Creates a destroyable class with a base class and destructor object\n * @param base - The base class to extend\n * @param destructorObj - Object containing the destructor method\n * @returns A destroyable class with static destroy and isDestroyable methods\n */\nexport function Destroyable<\n\tT extends new (\n\t\t...args: any[]\n\t) => any,\n\tAllocated extends Partial<InstanceType<T>>,\n>(\n\tbase: T,\n\tdestructorObj: Destructor<Allocated>\n): (new (\n\t...args: ConstructorParameters<T>\n) => InstanceType<T> & { [allocatedValues]: Allocated }) & {\n\tdestroy(obj: InstanceType<T>): boolean\n\tisDestroyable(obj: InstanceType<T>): boolean\n}\n\n/**\n * Creates a destroyable class with only a destructor object (no base class)\n * @param destructorObj - Object containing the destructor method\n * @returns A destroyable class with static destroy and isDestroyable methods\n */\nexport function Destroyable<Allocated extends Record<PropertyKey, any> = Record<PropertyKey, any>>(\n\tdestructorObj: Destructor<Allocated>\n): (new () => { [allocatedValues]: Allocated }) & {\n\tdestroy(obj: any): boolean\n\tisDestroyable(obj: any): boolean\n}\n\n/**\n * Creates a destroyable class with a base class (requires [destructor] method)\n * @param base - The base class to extend\n * @returns A destroyable class with static destroy and isDestroyable methods\n */\nexport function Destroyable<\n\tT extends new (\n\t\t...args: any[]\n\t) => any,\n\tAllocated extends Record<PropertyKey, any> = Record<PropertyKey, any>,\n>(\n\tbase: T\n): (new (\n\t...args: ConstructorParameters<T>\n) => AbstractDestroyable<Allocated> & InstanceType<T> & { [allocatedValues]: Allocated }) & {\n\tdestroy(obj: InstanceType<T>): boolean\n\tisDestroyable(obj: InstanceType<T>): boolean\n}\n\n/**\n * Creates an abstract destroyable base class\n * @returns An abstract destroyable class with static destroy and isDestroyable methods\n */\nexport function Destroyable<\n\tAllocated extends Record<PropertyKey, any> = Record<PropertyKey, any>,\n>(): abstract new () => (AbstractDestroyable<Allocated> & {\n\t[allocatedValues]: Allocated\n}) & {\n\tdestroy(obj: any): boolean\n\tisDestroyable(obj: any): boolean\n}\n\nexport function Destroyable<\n\tT extends new (\n\t\t...args: any[]\n\t) => any,\n\tAllocated extends Record<PropertyKey, any> = Record<PropertyKey, any>,\n>(base?: T | Destructor<Allocated>, destructorObj?: Destructor<Allocated>) {\n\tif (base && typeof base !== 'function') {\n\t\tdestructorObj = base as Destructor<Allocated>\n\t\tbase = undefined\n\t}\n\tif (!base) {\n\t\tbase = class { } as T\n\t}\n\n\treturn class Destroyable extends (base as T) {\n\t\tstatic readonly destructors = new WeakMap<any, () => void>()\n\t\tstatic destroy(obj: Destroyable) {\n\t\t\tconst destructor = Destroyable.destructors.get(obj)\n\t\t\tif (!destructor) return false\n\t\t\tfr.unregister(obj[allocatedValues])\n\t\t\tDestroyable.destructors.delete(obj)\n\t\t\tObject.setPrototypeOf(obj, new Proxy({}, destroyedHandler))\n\t\t\t// Clear all own properties\n\t\t\tfor (const key of Object.getOwnPropertyNames(obj)) {\n\t\t\t\tdelete (obj as any)[key]\n\t\t\t}\n\t\t\tdestructor()\n\t\t\treturn true\n\t\t}\n\t\tstatic isDestroyable(obj: Destroyable) {\n\t\t\treturn Destroyable.destructors.has(obj)\n\t\t}\n\n\t\t[forwardProperties]!: PropertyKey[]\n\t\treadonly [allocatedValues]: Allocated\n\t\tconstructor(...args: any[]) {\n\t\t\tsuper(...args)\n\t\t\tconst allocated = {} as Allocated\n\t\t\tthis[allocatedValues] = allocated\n\t\t\t// @ts-expect-error `this` is an AbstractDestroyable\n\t\t\tconst myDestructor = destructorObj?.destructor ?? this[destructor]\n\t\t\tif (!myDestructor) {\n\t\t\t\tthrow new DestructionError('Destructor is not defined')\n\t\t\t}\n\t\t\tfunction destruction() {\n\t\t\t\tmyDestructor(allocated)\n\t\t\t}\n\t\t\tDestroyable.destructors.set(this, destruction)\n\t\t\tfr.register(this, destruction, allocated)\n\t\t}\n\t}\n}\n\nconst forwardProperties = Symbol('forwardProperties')\n/**\n * Decorator that marks properties to be stored in the allocated object and passed to the destructor\n * Use with accessor properties or explicit get/set pairs\n */\nexport const allocated = decorator({\n\tsetter(original, _target, propertyKey) {\n\t\treturn function (value) {\n\t\t\tthis[allocatedValues][propertyKey] = value\n\t\t\treturn original.call(this, value)\n\t\t}\n\t},\n})\n\n/**\n * Registers a callback to be called when an object is garbage collected\n * @param cb - The callback function to execute on garbage collection\n * @returns The object whose reference can be collected\n */\nexport function callOnGC(cb: () => void) {\n\tlet called = false\n\tconst forward = () => {\n\t\tif (called) return\n\t\tcalled = true\n\t\tcb()\n\t}\n\tfr.register(forward, cb, cb)\n\treturn forward\n}\n\n/**\n * Context Manager Protocol for `using` statement integration\n * Provides automatic resource cleanup when used with the `using` statement\n */\nexport interface ContextManager<T = any> {\n\t[Symbol.dispose](): void\n\tvalue?: T\n}\n"],"names":["decorator"],"mappings":";;;;AAEA;AACA,MAAM,EAAE,GAAG,IAAI,oBAAoB,CAAa,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC;AAC3D;;AAEG;MACU,UAAU,GAAG,MAAM,CAAC,YAAY;AAC7C;;AAEG;MACU,eAAe,GAAG,MAAM,CAAC,WAAW;AACjD;;AAEG;AACG,MAAO,gBAAiB,SAAQ,KAAK,CAAA;IAC1C,OAAO,KAAK,CAAY,GAAW,EAAA;AAClC,QAAA,OAAO,MAAK;AACX,YAAA,MAAM,IAAI,gBAAgB,CAAC,GAAG,CAAC;AAChC,QAAA,CAAC;IACF;AACA,IAAA,WAAA,CAAY,GAAW,EAAA;AACtB,QAAA,KAAK,CAAC,CAAA,qBAAA,EAAwB,GAAG,CAAA,CAAE,CAAC;AACpC,QAAA,IAAI,CAAC,IAAI,GAAG,sBAAsB;IACnC;AACA;AACD,MAAM,gBAAgB,GAAG;AACxB,IAAA,CAAC,MAAM,CAAC,WAAW,GAAG,mBAAmB;AACzC,IAAA,GAAG,EAAE,gBAAgB,CAAC,KAAK,CAAC,gCAAgC,CAAC;AAC7D,IAAA,GAAG,EAAE,gBAAgB,CAAC,KAAK,C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