mutts 1.0.5 → 1.0.7
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 +2 -1
- package/dist/browser.d.ts +2 -0
- package/dist/browser.esm.js +70 -0
- package/dist/browser.esm.js.map +1 -0
- package/dist/browser.js +161 -0
- package/dist/browser.js.map +1 -0
- package/dist/chunks/{index-Cvxdw6Ax.js → index-BFYK02LG.js} +5377 -4059
- package/dist/chunks/index-BFYK02LG.js.map +1 -0
- package/dist/chunks/{index-qiWwozOc.esm.js → index-CNR6QRUl.esm.js} +5247 -3963
- package/dist/chunks/index-CNR6QRUl.esm.js.map +1 -0
- 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/node.d.ts +2 -0
- package/dist/node.esm.js +45 -0
- package/dist/node.esm.js.map +1 -0
- package/dist/node.js +136 -0
- package/dist/node.js.map +1 -0
- package/docs/ai/api-reference.md +0 -2
- package/docs/ai/manual.md +14 -95
- package/docs/reactive/advanced.md +7 -111
- package/docs/reactive/collections.md +0 -125
- package/docs/reactive/core.md +27 -24
- package/docs/reactive/debugging.md +168 -0
- package/docs/reactive/project.md +1 -1
- package/docs/reactive/scan.md +78 -0
- package/docs/reactive.md +8 -6
- package/docs/std-decorators.md +1 -0
- package/docs/zone.md +88 -0
- package/package.json +47 -65
- package/src/async/browser.ts +87 -0
- package/src/async/index.ts +8 -0
- package/src/async/node.ts +46 -0
- package/src/decorator.ts +15 -9
- package/src/destroyable.ts +4 -4
- package/src/index.ts +54 -0
- package/src/indexable.ts +42 -0
- package/src/mixins.ts +2 -2
- package/src/reactive/array.ts +149 -141
- package/src/reactive/buffer.ts +168 -0
- package/src/reactive/change.ts +3 -3
- 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 +15 -91
- package/src/reactive/effects.ts +138 -170
- package/src/reactive/index.ts +10 -13
- package/src/reactive/interface.ts +20 -33
- package/src/reactive/map.ts +48 -61
- package/src/reactive/memoize.ts +87 -31
- package/src/reactive/project.ts +43 -22
- package/src/reactive/proxy.ts +18 -43
- package/src/reactive/record.ts +3 -3
- package/src/reactive/register.ts +5 -7
- package/src/reactive/registry.ts +59 -0
- package/src/reactive/set.ts +42 -56
- package/src/reactive/tracking.ts +5 -62
- package/src/reactive/types.ts +79 -19
- package/src/std-decorators.ts +9 -9
- package/src/utils.ts +203 -19
- package/src/zone.ts +127 -0
- package/dist/chunks/_tslib-BgjropY9.js +0 -81
- package/dist/chunks/_tslib-BgjropY9.js.map +0 -1
- package/dist/chunks/_tslib-Mzh1rNsX.esm.js +0 -75
- 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 +0 -107
- package/dist/decorator.esm.js +0 -2
- package/dist/decorator.esm.js.map +0 -1
- package/dist/decorator.js +0 -11
- package/dist/decorator.js.map +0 -1
- package/dist/destroyable.d.ts +0 -90
- package/dist/destroyable.esm.js +0 -109
- package/dist/destroyable.esm.js.map +0 -1
- package/dist/destroyable.js +0 -116
- package/dist/destroyable.js.map +0 -1
- package/dist/eventful.d.ts +0 -20
- package/dist/eventful.esm.js +0 -66
- package/dist/eventful.esm.js.map +0 -1
- package/dist/eventful.js +0 -68
- package/dist/eventful.js.map +0 -1
- package/dist/index.d.ts +0 -19
- package/dist/index.esm.js +0 -8
- package/dist/index.esm.js.map +0 -1
- package/dist/index.js +0 -95
- package/dist/index.js.map +0 -1
- package/dist/indexable.d.ts +0 -243
- package/dist/indexable.esm.js +0 -285
- package/dist/indexable.esm.js.map +0 -1
- package/dist/indexable.js +0 -291
- package/dist/indexable.js.map +0 -1
- package/dist/promiseChain.d.ts +0 -21
- package/dist/promiseChain.esm.js +0 -78
- package/dist/promiseChain.esm.js.map +0 -1
- package/dist/promiseChain.js +0 -80
- package/dist/promiseChain.js.map +0 -1
- package/dist/reactive.d.ts +0 -885
- package/dist/reactive.esm.js +0 -5
- package/dist/reactive.esm.js.map +0 -1
- package/dist/reactive.js +0 -59
- package/dist/reactive.js.map +0 -1
- package/dist/std-decorators.d.ts +0 -52
- package/dist/std-decorators.esm.js +0 -196
- package/dist/std-decorators.esm.js.map +0 -1
- package/dist/std-decorators.js +0 -204
- package/dist/std-decorators.js.map +0 -1
- package/src/reactive/mapped.ts +0 -129
- package/src/reactive/zone.ts +0 -208
package/dist/decorator.d.ts
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Error thrown when decorator operations fail
|
|
3
|
-
*/
|
|
4
|
-
declare class DecoratorError extends Error {
|
|
5
|
-
constructor(message: string);
|
|
6
|
-
}
|
|
7
|
-
/**
|
|
8
|
-
* Legacy property decorator type for methods, getters, and setters
|
|
9
|
-
*/
|
|
10
|
-
type LegacyPropertyDecorator<T> = (target: T, name: string | symbol, descriptor: PropertyDescriptor) => any;
|
|
11
|
-
/**
|
|
12
|
-
* Legacy class decorator type
|
|
13
|
-
*/
|
|
14
|
-
type LegacyClassDecorator<T> = (target: T) => any;
|
|
15
|
-
/**
|
|
16
|
-
* Modern method decorator type
|
|
17
|
-
*/
|
|
18
|
-
type ModernMethodDecorator<T> = (target: T, context: ClassMethodDecoratorContext) => any;
|
|
19
|
-
/**
|
|
20
|
-
* Modern getter decorator type
|
|
21
|
-
*/
|
|
22
|
-
type ModernGetterDecorator<T> = (target: T, context: ClassGetterDecoratorContext) => any;
|
|
23
|
-
/**
|
|
24
|
-
* Modern setter decorator type
|
|
25
|
-
*/
|
|
26
|
-
type ModernSetterDecorator<T> = (target: T, context: ClassSetterDecoratorContext) => any;
|
|
27
|
-
/**
|
|
28
|
-
* Modern accessor decorator type
|
|
29
|
-
*/
|
|
30
|
-
type ModernAccessorDecorator<T> = (target: T, context: ClassAccessorDecoratorContext) => any;
|
|
31
|
-
/**
|
|
32
|
-
* Modern class decorator type
|
|
33
|
-
*/
|
|
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;
|
|
38
|
-
type DDClass<T> = <Ctor extends new (...args: any[]) => T = new (...args: any[]) => T>(target: Ctor) => Ctor | void;
|
|
39
|
-
/**
|
|
40
|
-
* Description object for creating decorators that work with both Legacy and Modern decorator proposals
|
|
41
|
-
*/
|
|
42
|
-
interface DecoratorDescription<T> {
|
|
43
|
-
/** Handler for method decorators */
|
|
44
|
-
method?: DDMethod<T>;
|
|
45
|
-
/** Handler for class decorators */
|
|
46
|
-
class?: DDClass<T>;
|
|
47
|
-
/** Handler for getter decorators */
|
|
48
|
-
getter?: DDGetter<T>;
|
|
49
|
-
/** Handler for setter decorators */
|
|
50
|
-
setter?: DDSetter<T>;
|
|
51
|
-
/** Default handler for any decorator type not explicitly defined */
|
|
52
|
-
default?: (...args: any[]) => any;
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Type for decorators that work with both Legacy and Modern decorator proposals
|
|
56
|
-
* Automatically infers the correct decorator type based on the description
|
|
57
|
-
*/
|
|
58
|
-
type Decorator<T, Description extends DecoratorDescription<T>> = (Description extends {
|
|
59
|
-
method: DDMethod<T>;
|
|
60
|
-
} ? LegacyPropertyDecorator<T> & ModernMethodDecorator<T> : unknown) & (Description extends {
|
|
61
|
-
class: DDClass<new (...args: any[]) => T>;
|
|
62
|
-
} ? LegacyClassDecorator<new (...args: any[]) => T> & ModernClassDecorator<new (...args: any[]) => T> : unknown) & (Description extends {
|
|
63
|
-
getter: DDGetter<T>;
|
|
64
|
-
} ? LegacyPropertyDecorator<T> & ModernGetterDecorator<T> & ModernAccessorDecorator<T> : unknown) & (Description extends {
|
|
65
|
-
setter: DDSetter<T>;
|
|
66
|
-
} ? LegacyPropertyDecorator<T> & ModernSetterDecorator<T> & ModernAccessorDecorator<T> : unknown) & (Description extends {
|
|
67
|
-
default: infer Signature;
|
|
68
|
-
} ? Signature : unknown);
|
|
69
|
-
/**
|
|
70
|
-
* Factory type for creating decorators that work with both Legacy and Modern decorator proposals
|
|
71
|
-
*/
|
|
72
|
-
type DecoratorFactory<T> = <Description extends DecoratorDescription<T>>(description: Description) => (Description extends {
|
|
73
|
-
method: DDMethod<T>;
|
|
74
|
-
} ? LegacyPropertyDecorator<T> & ModernMethodDecorator<T> : unknown) & (Description extends {
|
|
75
|
-
class: DDClass<new (...args: any[]) => T>;
|
|
76
|
-
} ? LegacyClassDecorator<new (...args: any[]) => T> & ModernClassDecorator<new (...args: any[]) => T> : unknown) & (Description extends {
|
|
77
|
-
getter: DDGetter<T>;
|
|
78
|
-
} ? LegacyPropertyDecorator<T> & ModernGetterDecorator<T> & ModernAccessorDecorator<T> : unknown) & (Description extends {
|
|
79
|
-
setter: DDSetter<T>;
|
|
80
|
-
} ? LegacyPropertyDecorator<T> & ModernSetterDecorator<T> & ModernAccessorDecorator<T> : unknown) & (Description extends {
|
|
81
|
-
default: infer Signature;
|
|
82
|
-
} ? Signature : unknown);
|
|
83
|
-
/**
|
|
84
|
-
* Creates a decorator that works with Legacy decorator proposals
|
|
85
|
-
* @param description - The decorator description object
|
|
86
|
-
* @returns A decorator function compatible with Legacy decorators
|
|
87
|
-
*/
|
|
88
|
-
declare function legacyDecorator<T = any>(description: DecoratorDescription<T>): any;
|
|
89
|
-
/**
|
|
90
|
-
* Creates a decorator that works with Modern decorator proposals
|
|
91
|
-
* @param description - The decorator description object
|
|
92
|
-
* @returns A decorator function compatible with Modern decorators
|
|
93
|
-
*/
|
|
94
|
-
declare function modernDecorator<T = any>(description: DecoratorDescription<T>): any;
|
|
95
|
-
/**
|
|
96
|
-
* Main decorator factory that automatically detects and works with both Legacy and Modern decorator proposals
|
|
97
|
-
* @param description - The decorator description object
|
|
98
|
-
* @returns A decorator that works in both Legacy and Modern environments
|
|
99
|
-
*/
|
|
100
|
-
declare const decorator: DecoratorFactory<any>;
|
|
101
|
-
/**
|
|
102
|
-
* Generic class decorator type that works with both Legacy and Modern decorator proposals
|
|
103
|
-
*/
|
|
104
|
-
type GenericClassDecorator<T> = LegacyClassDecorator<abstract new (...args: any[]) => T> & ModernClassDecorator<abstract new (...args: any[]) => T>;
|
|
105
|
-
|
|
106
|
-
export { DecoratorError, decorator, legacyDecorator, modernDecorator };
|
|
107
|
-
export type { Decorator, DecoratorDescription, DecoratorFactory, GenericClassDecorator, LegacyClassDecorator, LegacyPropertyDecorator, ModernAccessorDecorator, ModernClassDecorator, ModernGetterDecorator, ModernMethodDecorator, ModernSetterDecorator };
|
package/dist/decorator.esm.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"decorator.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
|
package/dist/decorator.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var decorator = require('./chunks/decorator-DLvrD0UF.js');
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
exports.DecoratorError = decorator.DecoratorError;
|
|
8
|
-
exports.decorator = decorator.decorator;
|
|
9
|
-
exports.legacyDecorator = decorator.legacyDecorator;
|
|
10
|
-
exports.modernDecorator = decorator.modernDecorator;
|
|
11
|
-
//# sourceMappingURL=decorator.js.map
|
package/dist/decorator.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"decorator.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;"}
|
package/dist/destroyable.d.ts
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import { LegacyPropertyDecorator, ModernSetterDecorator, ModernAccessorDecorator } from './decorator.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Symbol for marking destructor methods
|
|
5
|
-
*/
|
|
6
|
-
declare const destructor: unique symbol;
|
|
7
|
-
/**
|
|
8
|
-
* Symbol for accessing allocated values in destroyable objects
|
|
9
|
-
*/
|
|
10
|
-
declare const allocatedValues: unique symbol;
|
|
11
|
-
/**
|
|
12
|
-
* Error thrown when attempting to access a destroyed object
|
|
13
|
-
*/
|
|
14
|
-
declare class DestructionError extends Error {
|
|
15
|
-
static throw<_T = void>(msg: string): () => never;
|
|
16
|
-
constructor(msg: string);
|
|
17
|
-
}
|
|
18
|
-
declare abstract class AbstractDestroyable<Allocated> {
|
|
19
|
-
abstract [destructor](allocated: Allocated): void;
|
|
20
|
-
[Symbol.dispose](): void;
|
|
21
|
-
}
|
|
22
|
-
interface Destructor<Allocated> {
|
|
23
|
-
destructor(allocated: Allocated): void;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Creates a destroyable class with a base class and destructor object
|
|
27
|
-
* @param base - The base class to extend
|
|
28
|
-
* @param destructorObj - Object containing the destructor method
|
|
29
|
-
* @returns A destroyable class with static destroy and isDestroyable methods
|
|
30
|
-
*/
|
|
31
|
-
declare function Destroyable<T extends new (...args: any[]) => any, Allocated extends Partial<InstanceType<T>>>(base: T, destructorObj: Destructor<Allocated>): (new (...args: ConstructorParameters<T>) => InstanceType<T> & {
|
|
32
|
-
[allocatedValues]: Allocated;
|
|
33
|
-
}) & {
|
|
34
|
-
destroy(obj: InstanceType<T>): boolean;
|
|
35
|
-
isDestroyable(obj: InstanceType<T>): boolean;
|
|
36
|
-
};
|
|
37
|
-
/**
|
|
38
|
-
* Creates a destroyable class with only a destructor object (no base class)
|
|
39
|
-
* @param destructorObj - Object containing the destructor method
|
|
40
|
-
* @returns A destroyable class with static destroy and isDestroyable methods
|
|
41
|
-
*/
|
|
42
|
-
declare function Destroyable<Allocated extends Record<PropertyKey, any> = Record<PropertyKey, any>>(destructorObj: Destructor<Allocated>): (new () => {
|
|
43
|
-
[allocatedValues]: Allocated;
|
|
44
|
-
}) & {
|
|
45
|
-
destroy(obj: any): boolean;
|
|
46
|
-
isDestroyable(obj: any): boolean;
|
|
47
|
-
};
|
|
48
|
-
/**
|
|
49
|
-
* Creates a destroyable class with a base class (requires [destructor] method)
|
|
50
|
-
* @param base - The base class to extend
|
|
51
|
-
* @returns A destroyable class with static destroy and isDestroyable methods
|
|
52
|
-
*/
|
|
53
|
-
declare function Destroyable<T extends new (...args: any[]) => any, Allocated extends Record<PropertyKey, any> = Record<PropertyKey, any>>(base: T): (new (...args: ConstructorParameters<T>) => AbstractDestroyable<Allocated> & InstanceType<T> & {
|
|
54
|
-
[allocatedValues]: Allocated;
|
|
55
|
-
}) & {
|
|
56
|
-
destroy(obj: InstanceType<T>): boolean;
|
|
57
|
-
isDestroyable(obj: InstanceType<T>): boolean;
|
|
58
|
-
};
|
|
59
|
-
/**
|
|
60
|
-
* Creates an abstract destroyable base class
|
|
61
|
-
* @returns An abstract destroyable class with static destroy and isDestroyable methods
|
|
62
|
-
*/
|
|
63
|
-
declare function Destroyable<Allocated extends Record<PropertyKey, any> = Record<PropertyKey, any>>(): abstract new () => (AbstractDestroyable<Allocated> & {
|
|
64
|
-
[allocatedValues]: Allocated;
|
|
65
|
-
}) & {
|
|
66
|
-
destroy(obj: any): boolean;
|
|
67
|
-
isDestroyable(obj: any): boolean;
|
|
68
|
-
};
|
|
69
|
-
/**
|
|
70
|
-
* Decorator that marks properties to be stored in the allocated object and passed to the destructor
|
|
71
|
-
* Use with accessor properties or explicit get/set pairs
|
|
72
|
-
*/
|
|
73
|
-
declare const allocated: LegacyPropertyDecorator<any> & ModernSetterDecorator<any> & ModernAccessorDecorator<any>;
|
|
74
|
-
/**
|
|
75
|
-
* Registers a callback to be called when an object is garbage collected
|
|
76
|
-
* @param cb - The callback function to execute on garbage collection
|
|
77
|
-
* @returns The object whose reference can be collected
|
|
78
|
-
*/
|
|
79
|
-
declare function callOnGC(cb: () => void): () => void;
|
|
80
|
-
/**
|
|
81
|
-
* Context Manager Protocol for `using` statement integration
|
|
82
|
-
* Provides automatic resource cleanup when used with the `using` statement
|
|
83
|
-
*/
|
|
84
|
-
interface ContextManager<T = any> {
|
|
85
|
-
[Symbol.dispose](): void;
|
|
86
|
-
value?: T;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export { Destroyable, DestructionError, allocated, allocatedValues, callOnGC, destructor };
|
|
90
|
-
export type { ContextManager };
|
package/dist/destroyable.esm.js
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import { d as decorator } from './chunks/decorator-DqiszP7i.esm.js';
|
|
2
|
-
|
|
3
|
-
// Integrated with `using` statement via Symbol.dispose
|
|
4
|
-
const fr = new FinalizationRegistry((f) => f());
|
|
5
|
-
/**
|
|
6
|
-
* Symbol for marking destructor methods
|
|
7
|
-
*/
|
|
8
|
-
const destructor = Symbol('destructor');
|
|
9
|
-
/**
|
|
10
|
-
* Symbol for accessing allocated values in destroyable objects
|
|
11
|
-
*/
|
|
12
|
-
const allocatedValues = Symbol('allocated');
|
|
13
|
-
/**
|
|
14
|
-
* Error thrown when attempting to access a destroyed object
|
|
15
|
-
*/
|
|
16
|
-
class DestructionError extends Error {
|
|
17
|
-
static throw(msg) {
|
|
18
|
-
return () => {
|
|
19
|
-
throw new DestructionError(msg);
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
constructor(msg) {
|
|
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
|
-
};
|
|
32
|
-
function Destroyable(base, destructorObj) {
|
|
33
|
-
var _a;
|
|
34
|
-
if (base && typeof base !== 'function') {
|
|
35
|
-
destructorObj = base;
|
|
36
|
-
base = undefined;
|
|
37
|
-
}
|
|
38
|
-
if (!base) {
|
|
39
|
-
base = class {
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
return _a = class Destroyable extends base {
|
|
43
|
-
static destroy(obj) {
|
|
44
|
-
const destructor = _a.destructors.get(obj);
|
|
45
|
-
if (!destructor)
|
|
46
|
-
return false;
|
|
47
|
-
fr.unregister(obj);
|
|
48
|
-
_a.destructors.delete(obj);
|
|
49
|
-
Object.setPrototypeOf(obj, new Proxy({}, destroyedHandler));
|
|
50
|
-
// Clear all own properties
|
|
51
|
-
for (const key of Object.getOwnPropertyNames(obj)) {
|
|
52
|
-
delete obj[key];
|
|
53
|
-
}
|
|
54
|
-
destructor();
|
|
55
|
-
return true;
|
|
56
|
-
}
|
|
57
|
-
static isDestroyable(obj) {
|
|
58
|
-
return _a.destructors.has(obj);
|
|
59
|
-
}
|
|
60
|
-
constructor(...args) {
|
|
61
|
-
super(...args);
|
|
62
|
-
const allocated = {};
|
|
63
|
-
this[allocatedValues] = allocated;
|
|
64
|
-
// @ts-expect-error `this` is an AbstractDestroyable
|
|
65
|
-
const myDestructor = destructorObj?.destructor ?? this[destructor];
|
|
66
|
-
if (!myDestructor) {
|
|
67
|
-
throw new DestructionError('Destructor is not defined');
|
|
68
|
-
}
|
|
69
|
-
function destruction() {
|
|
70
|
-
myDestructor(allocated);
|
|
71
|
-
}
|
|
72
|
-
_a.destructors.set(this, destruction);
|
|
73
|
-
fr.register(this, destruction, this);
|
|
74
|
-
}
|
|
75
|
-
},
|
|
76
|
-
_a.destructors = new WeakMap(),
|
|
77
|
-
_a;
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Decorator that marks properties to be stored in the allocated object and passed to the destructor
|
|
81
|
-
* Use with accessor properties or explicit get/set pairs
|
|
82
|
-
*/
|
|
83
|
-
const allocated = decorator({
|
|
84
|
-
setter(original, propertyKey) {
|
|
85
|
-
return function (value) {
|
|
86
|
-
this[allocatedValues][propertyKey] = value;
|
|
87
|
-
return original.call(this, value);
|
|
88
|
-
};
|
|
89
|
-
},
|
|
90
|
-
});
|
|
91
|
-
/**
|
|
92
|
-
* Registers a callback to be called when an object is garbage collected
|
|
93
|
-
* @param cb - The callback function to execute on garbage collection
|
|
94
|
-
* @returns The object whose reference can be collected
|
|
95
|
-
*/
|
|
96
|
-
function callOnGC(cb) {
|
|
97
|
-
let called = false;
|
|
98
|
-
const forward = () => {
|
|
99
|
-
if (called)
|
|
100
|
-
return;
|
|
101
|
-
called = true;
|
|
102
|
-
cb();
|
|
103
|
-
};
|
|
104
|
-
fr.register(forward, cb, cb);
|
|
105
|
-
return forward;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export { Destroyable, DestructionError, allocated, allocatedValues, callOnGC, destructor };
|
|
109
|
-
//# sourceMappingURL=destroyable.esm.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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, this)\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, 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,CAAC;AAChC,QAAA,CAAC;IACF;AACA,IAAA,WAAA,CAAY,GAAW,EAAA;AACtB,QAAA,KAAK,CAAC,CAAA,qBAAA,EAAwB,GAAG,CAAA,CAAE,CAAC;AACpC,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/dist/destroyable.js
DELETED
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var decorator = require('./chunks/decorator-DLvrD0UF.js');
|
|
4
|
-
|
|
5
|
-
// Integrated with `using` statement via Symbol.dispose
|
|
6
|
-
const fr = new FinalizationRegistry((f) => f());
|
|
7
|
-
/**
|
|
8
|
-
* Symbol for marking destructor methods
|
|
9
|
-
*/
|
|
10
|
-
const destructor = Symbol('destructor');
|
|
11
|
-
/**
|
|
12
|
-
* Symbol for accessing allocated values in destroyable objects
|
|
13
|
-
*/
|
|
14
|
-
const allocatedValues = Symbol('allocated');
|
|
15
|
-
/**
|
|
16
|
-
* Error thrown when attempting to access a destroyed object
|
|
17
|
-
*/
|
|
18
|
-
class DestructionError extends Error {
|
|
19
|
-
static throw(msg) {
|
|
20
|
-
return () => {
|
|
21
|
-
throw new DestructionError(msg);
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
constructor(msg) {
|
|
25
|
-
super(`Object is destroyed. ${msg}`);
|
|
26
|
-
this.name = 'DestroyedAccessError';
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
const destroyedHandler = {
|
|
30
|
-
[Symbol.toStringTag]: 'MutTs Destroyable',
|
|
31
|
-
get: DestructionError.throw('Cannot access destroyed object'),
|
|
32
|
-
set: DestructionError.throw('Cannot access destroyed object'),
|
|
33
|
-
};
|
|
34
|
-
function Destroyable(base, destructorObj) {
|
|
35
|
-
var _a;
|
|
36
|
-
if (base && typeof base !== 'function') {
|
|
37
|
-
destructorObj = base;
|
|
38
|
-
base = undefined;
|
|
39
|
-
}
|
|
40
|
-
if (!base) {
|
|
41
|
-
base = class {
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
return _a = class Destroyable extends base {
|
|
45
|
-
static destroy(obj) {
|
|
46
|
-
const destructor = _a.destructors.get(obj);
|
|
47
|
-
if (!destructor)
|
|
48
|
-
return false;
|
|
49
|
-
fr.unregister(obj);
|
|
50
|
-
_a.destructors.delete(obj);
|
|
51
|
-
Object.setPrototypeOf(obj, new Proxy({}, destroyedHandler));
|
|
52
|
-
// Clear all own properties
|
|
53
|
-
for (const key of Object.getOwnPropertyNames(obj)) {
|
|
54
|
-
delete obj[key];
|
|
55
|
-
}
|
|
56
|
-
destructor();
|
|
57
|
-
return true;
|
|
58
|
-
}
|
|
59
|
-
static isDestroyable(obj) {
|
|
60
|
-
return _a.destructors.has(obj);
|
|
61
|
-
}
|
|
62
|
-
constructor(...args) {
|
|
63
|
-
super(...args);
|
|
64
|
-
const allocated = {};
|
|
65
|
-
this[allocatedValues] = allocated;
|
|
66
|
-
// @ts-expect-error `this` is an AbstractDestroyable
|
|
67
|
-
const myDestructor = destructorObj?.destructor ?? this[destructor];
|
|
68
|
-
if (!myDestructor) {
|
|
69
|
-
throw new DestructionError('Destructor is not defined');
|
|
70
|
-
}
|
|
71
|
-
function destruction() {
|
|
72
|
-
myDestructor(allocated);
|
|
73
|
-
}
|
|
74
|
-
_a.destructors.set(this, destruction);
|
|
75
|
-
fr.register(this, destruction, this);
|
|
76
|
-
}
|
|
77
|
-
},
|
|
78
|
-
_a.destructors = new WeakMap(),
|
|
79
|
-
_a;
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Decorator that marks properties to be stored in the allocated object and passed to the destructor
|
|
83
|
-
* Use with accessor properties or explicit get/set pairs
|
|
84
|
-
*/
|
|
85
|
-
const allocated = decorator.decorator({
|
|
86
|
-
setter(original, propertyKey) {
|
|
87
|
-
return function (value) {
|
|
88
|
-
this[allocatedValues][propertyKey] = value;
|
|
89
|
-
return original.call(this, value);
|
|
90
|
-
};
|
|
91
|
-
},
|
|
92
|
-
});
|
|
93
|
-
/**
|
|
94
|
-
* Registers a callback to be called when an object is garbage collected
|
|
95
|
-
* @param cb - The callback function to execute on garbage collection
|
|
96
|
-
* @returns The object whose reference can be collected
|
|
97
|
-
*/
|
|
98
|
-
function callOnGC(cb) {
|
|
99
|
-
let called = false;
|
|
100
|
-
const forward = () => {
|
|
101
|
-
if (called)
|
|
102
|
-
return;
|
|
103
|
-
called = true;
|
|
104
|
-
cb();
|
|
105
|
-
};
|
|
106
|
-
fr.register(forward, cb, cb);
|
|
107
|
-
return forward;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
exports.Destroyable = Destroyable;
|
|
111
|
-
exports.DestructionError = DestructionError;
|
|
112
|
-
exports.allocated = allocated;
|
|
113
|
-
exports.allocatedValues = allocatedValues;
|
|
114
|
-
exports.callOnGC = callOnGC;
|
|
115
|
-
exports.destructor = destructor;
|
|
116
|
-
//# sourceMappingURL=destroyable.js.map
|
package/dist/destroyable.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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, this)\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, 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,CAAC,gCAAgC,CAAC;CACpD;AA8EJ,SAAU,WAAW,CAKzB,IAAgC,EAAE,aAAqC,EAAA;;AACxE,IAAA,IAAI,IAAI,IAAI,OAAO,IAAI,KAAK,UAAU,E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CAAC,SAAS,CAAC;gBACxB;gBACA,EAAW,CAAC,WAAW,CAAC,GAAG,CAAC,IAAI,EAAE,WAAW,CAAC;gBAC9C,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,WAAW,EAAE,IAAI,CAAC;YACrC;AACA,SAAA;QAnCgB,EAAA,CAAA,WAAW,GAAG,IAAI,OAAO,EAAmB;AAmC5D,QAAA,EAAA;AACF;AAGA;;;AAGG;AACI,MAAM,SAAS,GAAGA,mBAAS,CAAC;IAClC,MAAM,CAAC,QAAQ,EAAE,WAAW,EAAA;AAC3B,QAAA,OAAO,UAAU,KAAK,EAAA;YACrB,IAAI,CAAC,eAAe,CAAC,CAAC,WAAW,CAAC,GAAG,KAAK;YAC1C,OAAO,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC;AAClC,QAAA,CAAC;IACF,CAAC;AACD,CAAA;AAED;;;;AAIG;AACG,SAAU,QAAQ,CAAC,EAAc,EAAA;IACtC,IAAI,MAAM,GAAG,KAAK;IAClB,MAAM,OAAO,GAAG,MAAK;AACpB,QAAA,IAAI,MAAM;YAAE;QACZ,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/eventful.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Base type for event maps - all event handlers must be functions
|
|
3
|
-
*/
|
|
4
|
-
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
|
-
declare class Eventful<Events extends EventsBase> {
|
|
10
|
-
#private;
|
|
11
|
-
hook(cb: <EventType extends keyof Events>(event: EventType, ...args: Parameters<Events[EventType]>) => void): () => void;
|
|
12
|
-
on(events: Partial<Events>): void;
|
|
13
|
-
on<EventType extends keyof Events>(event: EventType, cb: Events[EventType]): () => void;
|
|
14
|
-
off(events: Partial<Events>): void;
|
|
15
|
-
off<EventType extends keyof Events>(event: EventType, cb?: Events[EventType]): void;
|
|
16
|
-
emit<EventType extends keyof Events>(event: EventType, ...args: Parameters<Events[EventType]>): void;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export { Eventful };
|
|
20
|
-
export type { EventsBase };
|
package/dist/eventful.esm.js
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { _ as __classPrivateFieldGet } from './chunks/_tslib-Mzh1rNsX.esm.js';
|
|
2
|
-
|
|
3
|
-
var _Eventful_events, _Eventful_hooks;
|
|
4
|
-
/**
|
|
5
|
-
* A type-safe event system that provides a clean API for event handling
|
|
6
|
-
* @template Events - The event map defining event names and their handler signatures
|
|
7
|
-
*/
|
|
8
|
-
class Eventful {
|
|
9
|
-
constructor() {
|
|
10
|
-
_Eventful_events.set(this, new Map());
|
|
11
|
-
_Eventful_hooks.set(this, []);
|
|
12
|
-
}
|
|
13
|
-
hook(cb) {
|
|
14
|
-
if (!__classPrivateFieldGet(this, _Eventful_hooks, "f").includes(cb))
|
|
15
|
-
__classPrivateFieldGet(this, _Eventful_hooks, "f").push(cb);
|
|
16
|
-
return () => {
|
|
17
|
-
__classPrivateFieldGet(this, _Eventful_hooks, "f").splice(__classPrivateFieldGet(this, _Eventful_hooks, "f").indexOf(cb), 1);
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
on(eventOrEvents, cb) {
|
|
21
|
-
if (typeof eventOrEvents === 'object') {
|
|
22
|
-
for (const e of Object.keys(eventOrEvents)) {
|
|
23
|
-
this.on(e, eventOrEvents[e]);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
else if (cb !== undefined) {
|
|
27
|
-
let callbacks = __classPrivateFieldGet(this, _Eventful_events, "f").get(eventOrEvents);
|
|
28
|
-
if (!callbacks) {
|
|
29
|
-
callbacks = [];
|
|
30
|
-
__classPrivateFieldGet(this, _Eventful_events, "f").set(eventOrEvents, callbacks);
|
|
31
|
-
}
|
|
32
|
-
callbacks.push(cb);
|
|
33
|
-
}
|
|
34
|
-
// @ts-expect-error Generic case leads to generic case
|
|
35
|
-
return () => this.off(eventOrEvents, cb);
|
|
36
|
-
}
|
|
37
|
-
off(eventOrEvents, cb) {
|
|
38
|
-
if (typeof eventOrEvents === 'object') {
|
|
39
|
-
for (const e of Object.keys(eventOrEvents)) {
|
|
40
|
-
this.off(e, eventOrEvents[e]);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
else if (cb !== null && cb !== undefined) {
|
|
44
|
-
const callbacks = __classPrivateFieldGet(this, _Eventful_events, "f").get(eventOrEvents);
|
|
45
|
-
if (callbacks) {
|
|
46
|
-
__classPrivateFieldGet(this, _Eventful_events, "f").set(eventOrEvents, callbacks.filter((c) => c !== cb));
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
// Remove all listeners for this event
|
|
51
|
-
__classPrivateFieldGet(this, _Eventful_events, "f").delete(eventOrEvents);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
emit(event, ...args) {
|
|
55
|
-
const callbacks = __classPrivateFieldGet(this, _Eventful_events, "f").get(event);
|
|
56
|
-
if (callbacks)
|
|
57
|
-
for (const cb of callbacks)
|
|
58
|
-
cb.apply(this, args);
|
|
59
|
-
for (const cb of __classPrivateFieldGet(this, _Eventful_hooks, "f"))
|
|
60
|
-
cb.call(this, event, ...args);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
_Eventful_events = new WeakMap(), _Eventful_hooks = new WeakMap();
|
|
64
|
-
|
|
65
|
-
export { Eventful };
|
|
66
|
-
//# sourceMappingURL=eventful.esm.js.map
|
package/dist/eventful.esm.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"eventful.esm.js","sources":["../src/eventful.ts"],"sourcesContent":["/**\n * Base type for event maps - all event handlers must be functions\n */\nexport type EventsBase = Record<string, (...args: any[]) => void>\n/**\n * A type-safe event system that provides a clean API for event handling\n * @template Events - The event map defining event names and their handler signatures\n */\nexport class Eventful<Events extends EventsBase> {\n\treadonly #events = new Map<keyof Events, ((...args: any[]) => void)[]>()\n\treadonly #hooks = [] as ((...args: any[]) => void)[]\n\n\tpublic hook(\n\t\tcb: <EventType extends keyof Events>(\n\t\t\tevent: EventType,\n\t\t\t...args: Parameters<Events[EventType]>\n\t\t) => void\n\t): () => void {\n\t\tif (!this.#hooks.includes(cb)) this.#hooks.push(cb)\n\t\treturn () => {\n\t\t\tthis.#hooks.splice(this.#hooks.indexOf(cb), 1)\n\t\t}\n\t}\n\n\tpublic on(events: Partial<Events>): void\n\tpublic on<EventType extends keyof Events>(event: EventType, cb: Events[EventType]): () => void\n\tpublic on<EventType extends keyof Events>(\n\t\teventOrEvents: EventType | Partial<Events>,\n\t\tcb?: Events[EventType]\n\t): () => void {\n\t\tif (typeof eventOrEvents === 'object') {\n\t\t\tfor (const e of Object.keys(eventOrEvents) as (keyof Events)[]) {\n\t\t\t\tthis.on(e, eventOrEvents[e]!)\n\t\t\t}\n\t\t} else if (cb !== undefined) {\n\t\t\tlet callbacks = this.#events.get(eventOrEvents)\n\t\t\tif (!callbacks) {\n\t\t\t\tcallbacks = []\n\t\t\t\tthis.#events.set(eventOrEvents, callbacks)\n\t\t\t}\n\t\t\tcallbacks.push(cb)\n\t\t}\n\t\t// @ts-expect-error Generic case leads to generic case\n\t\treturn () => this.off(eventOrEvents, cb)\n\t}\n\tpublic off(events: Partial<Events>): void\n\tpublic off<EventType extends keyof Events>(event: EventType, cb?: Events[EventType]): void\n\tpublic off<EventType extends keyof Events>(\n\t\teventOrEvents: EventType | Partial<Events>,\n\t\tcb?: Events[EventType]\n\t): void {\n\t\tif (typeof eventOrEvents === 'object') {\n\t\t\tfor (const e of Object.keys(eventOrEvents) as (keyof Events)[]) {\n\t\t\t\tthis.off(e, eventOrEvents[e])\n\t\t\t}\n\t\t} else if (cb !== null && cb !== undefined) {\n\t\t\tconst callbacks = this.#events.get(eventOrEvents)\n\t\t\tif (callbacks) {\n\t\t\t\tthis.#events.set(\n\t\t\t\t\teventOrEvents,\n\t\t\t\t\tcallbacks.filter((c) => c !== cb)\n\t\t\t\t)\n\t\t\t}\n\t\t} else {\n\t\t\t// Remove all listeners for this event\n\t\t\tthis.#events.delete(eventOrEvents)\n\t\t}\n\t}\n\tpublic emit<EventType extends keyof Events>(\n\t\tevent: EventType,\n\t\t...args: Parameters<Events[EventType]>\n\t) {\n\t\tconst callbacks = this.#events.get(event)\n\t\tif (callbacks) for (const cb of callbacks) cb.apply(this, args)\n\t\tfor (const cb of this.#hooks) cb.call(this, event, ...args)\n\t}\n}\n"],"names":[],"mappings":";;;AAIA;;;AAGG;MACU,QAAQ,CAAA;AAArB,IAAA,WAAA,GAAA;QACU,gBAAA,CAAA,GAAA,CAAA,IAAA,EAAU,IAAI,GAAG,EAA8C,CAAA;AAC/D,QAAA,eAAA,CAAA,GAAA,CAAA,IAAA,EAAS,EAAkC,CAAA;IAkErD;AAhEQ,IAAA,IAAI,CACV,EAGS,EAAA;QAET,IAAI,CAAC,uBAAA,IAAI,EAAA,eAAA,EAAA,GAAA,CAAO,CAAC,QAAQ,CAAC,EAAE,CAAC;AAAE,YAAA,sBAAA,CAAA,IAAI,EAAA,eAAA,EAAA,GAAA,CAAO,CAAC,IAAI,CAAC,EAAE,CAAC;AACnD,QAAA,OAAO,MAAK;AACX,YAAA,sBAAA,CAAA,IAAI,EAAA,eAAA,EAAA,GAAA,CAAO,CAAC,MAAM,CAAC,uBAAA,IAAI,EAAA,eAAA,EAAA,GAAA,CAAO,CAAC,OAAO,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;AAC/C,QAAA,CAAC;IACF;IAIO,EAAE,CACR,aAA0C,EAC1C,EAAsB,EAAA;AAEt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AA,CAAA,IAAI,EAAA,gBAAA,EAAA,GAAA,CAAQ,CAAC,MAAM,CAAC,aAAa,CAAC;QACnC;IACD;AACO,IAAA,IAAI,CACV,KAAgB,EAChB,GAAG,IAAmC,EAAA;QAEtC,MAAM,SAAS,GAAG,sBAAA,CAAA,IAAI,EAAA,gBAAA,EAAA,GAAA,CAAQ,CAAC,GAAG,CAAC,KAAK,CAAC;AACzC,QAAA,IAAI,SAAS;YAAE,KAAK,MAAM,EAAE,IAAI,SAAS;AAAE,gBAAA,EAAE,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC;AAC/D,QAAA,KAAK,MAAM,EAAE,IAAI,sBAAA,CAAA,IAAI,EAAA,eAAA,EAAA,GAAA,CAAO;YAAE,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC;IAC5D;AACA;;;;;"}
|
package/dist/eventful.js
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var _tslib = require('./chunks/_tslib-BgjropY9.js');
|
|
4
|
-
|
|
5
|
-
var _Eventful_events, _Eventful_hooks;
|
|
6
|
-
/**
|
|
7
|
-
* A type-safe event system that provides a clean API for event handling
|
|
8
|
-
* @template Events - The event map defining event names and their handler signatures
|
|
9
|
-
*/
|
|
10
|
-
class Eventful {
|
|
11
|
-
constructor() {
|
|
12
|
-
_Eventful_events.set(this, new Map());
|
|
13
|
-
_Eventful_hooks.set(this, []);
|
|
14
|
-
}
|
|
15
|
-
hook(cb) {
|
|
16
|
-
if (!_tslib.__classPrivateFieldGet(this, _Eventful_hooks, "f").includes(cb))
|
|
17
|
-
_tslib.__classPrivateFieldGet(this, _Eventful_hooks, "f").push(cb);
|
|
18
|
-
return () => {
|
|
19
|
-
_tslib.__classPrivateFieldGet(this, _Eventful_hooks, "f").splice(_tslib.__classPrivateFieldGet(this, _Eventful_hooks, "f").indexOf(cb), 1);
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
on(eventOrEvents, cb) {
|
|
23
|
-
if (typeof eventOrEvents === 'object') {
|
|
24
|
-
for (const e of Object.keys(eventOrEvents)) {
|
|
25
|
-
this.on(e, eventOrEvents[e]);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
else if (cb !== undefined) {
|
|
29
|
-
let callbacks = _tslib.__classPrivateFieldGet(this, _Eventful_events, "f").get(eventOrEvents);
|
|
30
|
-
if (!callbacks) {
|
|
31
|
-
callbacks = [];
|
|
32
|
-
_tslib.__classPrivateFieldGet(this, _Eventful_events, "f").set(eventOrEvents, callbacks);
|
|
33
|
-
}
|
|
34
|
-
callbacks.push(cb);
|
|
35
|
-
}
|
|
36
|
-
// @ts-expect-error Generic case leads to generic case
|
|
37
|
-
return () => this.off(eventOrEvents, cb);
|
|
38
|
-
}
|
|
39
|
-
off(eventOrEvents, cb) {
|
|
40
|
-
if (typeof eventOrEvents === 'object') {
|
|
41
|
-
for (const e of Object.keys(eventOrEvents)) {
|
|
42
|
-
this.off(e, eventOrEvents[e]);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
else if (cb !== null && cb !== undefined) {
|
|
46
|
-
const callbacks = _tslib.__classPrivateFieldGet(this, _Eventful_events, "f").get(eventOrEvents);
|
|
47
|
-
if (callbacks) {
|
|
48
|
-
_tslib.__classPrivateFieldGet(this, _Eventful_events, "f").set(eventOrEvents, callbacks.filter((c) => c !== cb));
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
// Remove all listeners for this event
|
|
53
|
-
_tslib.__classPrivateFieldGet(this, _Eventful_events, "f").delete(eventOrEvents);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
emit(event, ...args) {
|
|
57
|
-
const callbacks = _tslib.__classPrivateFieldGet(this, _Eventful_events, "f").get(event);
|
|
58
|
-
if (callbacks)
|
|
59
|
-
for (const cb of callbacks)
|
|
60
|
-
cb.apply(this, args);
|
|
61
|
-
for (const cb of _tslib.__classPrivateFieldGet(this, _Eventful_hooks, "f"))
|
|
62
|
-
cb.call(this, event, ...args);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
_Eventful_events = new WeakMap(), _Eventful_hooks = new WeakMap();
|
|
66
|
-
|
|
67
|
-
exports.Eventful = Eventful;
|
|
68
|
-
//# sourceMappingURL=eventful.js.map
|