mutts 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/README.md +150 -0
  2. package/dist/chunks/decorator-BXsign4Z.js +176 -0
  3. package/dist/chunks/decorator-BXsign4Z.js.map +1 -0
  4. package/dist/chunks/decorator-CPbZNnsX.esm.js +168 -0
  5. package/dist/chunks/decorator-CPbZNnsX.esm.js.map +1 -0
  6. package/dist/decorator.d.ts +50 -0
  7. package/dist/decorator.esm.js +2 -0
  8. package/dist/decorator.esm.js.map +1 -0
  9. package/dist/decorator.js +11 -0
  10. package/dist/decorator.js.map +1 -0
  11. package/dist/destroyable.d.ts +48 -0
  12. package/dist/destroyable.esm.js +91 -0
  13. package/dist/destroyable.esm.js.map +1 -0
  14. package/dist/destroyable.js +98 -0
  15. package/dist/destroyable.js.map +1 -0
  16. package/dist/eventful.d.ts +11 -0
  17. package/dist/eventful.esm.js +88 -0
  18. package/dist/eventful.esm.js.map +1 -0
  19. package/dist/eventful.js +90 -0
  20. package/dist/eventful.js.map +1 -0
  21. package/dist/index.d.ts +15 -0
  22. package/dist/index.esm.js +7 -0
  23. package/dist/index.esm.js.map +1 -0
  24. package/dist/index.js +52 -0
  25. package/dist/index.js.map +1 -0
  26. package/dist/indexable.d.ts +31 -0
  27. package/dist/indexable.esm.js +85 -0
  28. package/dist/indexable.esm.js.map +1 -0
  29. package/dist/indexable.js +89 -0
  30. package/dist/indexable.js.map +1 -0
  31. package/dist/mutts.umd.js +2 -0
  32. package/dist/mutts.umd.js.map +1 -0
  33. package/dist/mutts.umd.min.js +2 -0
  34. package/dist/mutts.umd.min.js.map +1 -0
  35. package/dist/promiseChain.d.ts +11 -0
  36. package/dist/promiseChain.esm.js +72 -0
  37. package/dist/promiseChain.esm.js.map +1 -0
  38. package/dist/promiseChain.js +74 -0
  39. package/dist/promiseChain.js.map +1 -0
  40. package/dist/reactive.d.ts +114 -0
  41. package/dist/reactive.esm.js +1455 -0
  42. package/dist/reactive.esm.js.map +1 -0
  43. package/dist/reactive.js +1472 -0
  44. package/dist/reactive.js.map +1 -0
  45. package/dist/std-decorators.d.ts +17 -0
  46. package/dist/std-decorators.esm.js +161 -0
  47. package/dist/std-decorators.esm.js.map +1 -0
  48. package/dist/std-decorators.js +169 -0
  49. package/dist/std-decorators.js.map +1 -0
  50. package/docs/decorator.md +300 -0
  51. package/docs/destroyable.md +294 -0
  52. package/docs/events.md +225 -0
  53. package/docs/indexable.md +561 -0
  54. package/docs/promiseChain.md +218 -0
  55. package/docs/reactive.md +2072 -0
  56. package/docs/std-decorators.md +558 -0
  57. package/package.json +132 -0
  58. package/src/decorator.test.ts +495 -0
  59. package/src/decorator.ts +205 -0
  60. package/src/destroyable.test.ts +155 -0
  61. package/src/destroyable.ts +158 -0
  62. package/src/eventful.test.ts +380 -0
  63. package/src/eventful.ts +69 -0
  64. package/src/index.ts +7 -0
  65. package/src/indexable.test.ts +388 -0
  66. package/src/indexable.ts +124 -0
  67. package/src/promiseChain.test.ts +201 -0
  68. package/src/promiseChain.ts +99 -0
  69. package/src/reactive/array.test.ts +923 -0
  70. package/src/reactive/array.ts +352 -0
  71. package/src/reactive/core.test.ts +1663 -0
  72. package/src/reactive/core.ts +866 -0
  73. package/src/reactive/index.ts +28 -0
  74. package/src/reactive/interface.test.ts +1477 -0
  75. package/src/reactive/interface.ts +231 -0
  76. package/src/reactive/map.test.ts +866 -0
  77. package/src/reactive/map.ts +162 -0
  78. package/src/reactive/set.test.ts +289 -0
  79. package/src/reactive/set.ts +142 -0
  80. package/src/std-decorators.test.ts +679 -0
  81. package/src/std-decorators.ts +182 -0
  82. package/src/utils.ts +52 -0
@@ -0,0 +1,48 @@
1
+ import { LegacyPropertyDecorator, ModernSetterDecorator, ModernAccessorDecorator } from './decorator.js';
2
+
3
+ declare const destructor: unique symbol;
4
+ declare const allocatedValues: unique symbol;
5
+ declare class DestructionError extends Error {
6
+ static throw<_T = void>(msg: string): () => never;
7
+ constructor(msg: string);
8
+ }
9
+ declare abstract class AbstractDestroyable<Allocated> {
10
+ abstract [destructor](allocated: Allocated): void;
11
+ [Symbol.dispose](): void;
12
+ }
13
+ interface Destructor<Allocated> {
14
+ destructor(allocated: Allocated): void;
15
+ }
16
+ declare function Destroyable<T extends new (...args: any[]) => any, Allocated extends Partial<typeof this>>(base: T, destructorObj: Destructor<Allocated>): (new (...args: ConstructorParameters<T>) => InstanceType<T> & {
17
+ [allocatedValues]: Allocated;
18
+ }) & {
19
+ destroy(obj: InstanceType<T>): boolean;
20
+ isDestroyable(obj: InstanceType<T>): boolean;
21
+ };
22
+ declare function Destroyable<Allocated extends Record<PropertyKey, any> = Record<PropertyKey, any>>(destructorObj: Destructor<Allocated>): (new () => {
23
+ [allocatedValues]: Allocated;
24
+ }) & {
25
+ destroy(obj: any): boolean;
26
+ isDestroyable(obj: any): boolean;
27
+ };
28
+ 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> & {
29
+ [allocatedValues]: Allocated;
30
+ }) & {
31
+ destroy(obj: InstanceType<T>): boolean;
32
+ isDestroyable(obj: InstanceType<T>): boolean;
33
+ };
34
+ declare function Destroyable<Allocated extends Record<PropertyKey, any> = Record<PropertyKey, any>>(): abstract new () => (AbstractDestroyable<Allocated> & {
35
+ [allocatedValues]: Allocated;
36
+ }) & {
37
+ destroy(obj: any): boolean;
38
+ isDestroyable(obj: any): boolean;
39
+ };
40
+ declare const allocated: LegacyPropertyDecorator<any> & ModernSetterDecorator<any> & ModernAccessorDecorator<any>;
41
+ declare function callOnGC(cb: () => void): () => void;
42
+ interface ContextManager<T = any> {
43
+ [Symbol.dispose](): void;
44
+ value?: T;
45
+ }
46
+
47
+ export { Destroyable, DestructionError, allocated, allocatedValues, callOnGC, destructor };
48
+ export type { ContextManager };
@@ -0,0 +1,91 @@
1
+ import { d as decorator } from './chunks/decorator-CPbZNnsX.esm.js';
2
+
3
+ // Integrated with `using` statement via Symbol.dispose
4
+ const fr = new FinalizationRegistry((f) => f());
5
+ const destructor = Symbol('destructor');
6
+ const allocatedValues = Symbol('allocated');
7
+ class DestructionError extends Error {
8
+ static throw(msg) {
9
+ return () => {
10
+ throw new DestructionError(msg);
11
+ };
12
+ }
13
+ constructor(msg) {
14
+ super(`Object is destroyed. ${msg}`);
15
+ this.name = 'DestroyedAccessError';
16
+ }
17
+ }
18
+ const destroyedHandler = {
19
+ [Symbol.toStringTag]: 'MutTs Destroyable',
20
+ get: DestructionError.throw('Cannot access destroyed object'),
21
+ set: DestructionError.throw('Cannot access destroyed object'),
22
+ };
23
+ function Destroyable(base, destructorObj) {
24
+ var _a;
25
+ if (base && typeof base !== 'function') {
26
+ destructorObj = base;
27
+ base = undefined;
28
+ }
29
+ if (!base) {
30
+ base = class {
31
+ };
32
+ }
33
+ return _a = class Destroyable extends base {
34
+ static destroy(obj) {
35
+ const destructor = _a.destructors.get(obj);
36
+ if (!destructor)
37
+ return false;
38
+ fr.unregister(obj);
39
+ _a.destructors.delete(obj);
40
+ Object.setPrototypeOf(obj, new Proxy({}, destroyedHandler));
41
+ // Clear all own properties
42
+ for (const key of Object.getOwnPropertyNames(obj)) {
43
+ delete obj[key];
44
+ }
45
+ destructor();
46
+ return true;
47
+ }
48
+ static isDestroyable(obj) {
49
+ return _a.destructors.has(obj);
50
+ }
51
+ constructor(...args) {
52
+ super(...args);
53
+ const allocated = {};
54
+ this[allocatedValues] = allocated;
55
+ // @ts-expect-error `this` is an AbstractDestroyable
56
+ const myDestructor = destructorObj?.destructor ?? this[destructor];
57
+ if (!myDestructor) {
58
+ throw new DestructionError('Destructor is not defined');
59
+ }
60
+ function destruction() {
61
+ myDestructor(allocated);
62
+ }
63
+ _a.destructors.set(this, destruction);
64
+ fr.register(this, destruction, this);
65
+ }
66
+ },
67
+ _a.destructors = new WeakMap(),
68
+ _a;
69
+ }
70
+ const allocated = decorator({
71
+ setter(original, propertyKey) {
72
+ return function (value) {
73
+ this[allocatedValues][propertyKey] = value;
74
+ return original.call(this, value);
75
+ };
76
+ },
77
+ });
78
+ function callOnGC(cb) {
79
+ let called = false;
80
+ const forward = () => {
81
+ if (called)
82
+ return;
83
+ called = true;
84
+ cb();
85
+ };
86
+ fr.register(forward, cb, cb);
87
+ return forward;
88
+ }
89
+
90
+ export { Destroyable, DestructionError, allocated, allocatedValues, callOnGC, destructor };
91
+ //# sourceMappingURL=destroyable.esm.js.map
@@ -0,0 +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())\nexport const destructor = Symbol('destructor')\nexport const allocatedValues = Symbol('allocated')\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\nexport function Destroyable<\n\tT extends new (\n\t\t...args: any[]\n\t) => any,\n\tAllocated extends Partial<typeof this>,\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\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\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\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\tdeclare [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')\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\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// Context Manager Protocol for `with` statement integration\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;MAC9C,UAAU,GAAG,MAAM,CAAC,YAAY;MAChC,eAAe,GAAG,MAAM,CAAC,WAAW;AAC3C,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,GA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AAE,EAAE,CAAC;AAC5B,IAAA,OAAO,OAAO;AACf;;;;"}
@@ -0,0 +1,98 @@
1
+ 'use strict';
2
+
3
+ var decorator = require('./chunks/decorator-BXsign4Z.js');
4
+
5
+ // Integrated with `using` statement via Symbol.dispose
6
+ const fr = new FinalizationRegistry((f) => f());
7
+ const destructor = Symbol('destructor');
8
+ const allocatedValues = Symbol('allocated');
9
+ class DestructionError extends Error {
10
+ static throw(msg) {
11
+ return () => {
12
+ throw new DestructionError(msg);
13
+ };
14
+ }
15
+ constructor(msg) {
16
+ super(`Object is destroyed. ${msg}`);
17
+ this.name = 'DestroyedAccessError';
18
+ }
19
+ }
20
+ const destroyedHandler = {
21
+ [Symbol.toStringTag]: 'MutTs Destroyable',
22
+ get: DestructionError.throw('Cannot access destroyed object'),
23
+ set: DestructionError.throw('Cannot access destroyed object'),
24
+ };
25
+ function Destroyable(base, destructorObj) {
26
+ var _a;
27
+ if (base && typeof base !== 'function') {
28
+ destructorObj = base;
29
+ base = undefined;
30
+ }
31
+ if (!base) {
32
+ base = class {
33
+ };
34
+ }
35
+ return _a = class Destroyable extends base {
36
+ static destroy(obj) {
37
+ const destructor = _a.destructors.get(obj);
38
+ if (!destructor)
39
+ return false;
40
+ fr.unregister(obj);
41
+ _a.destructors.delete(obj);
42
+ Object.setPrototypeOf(obj, new Proxy({}, destroyedHandler));
43
+ // Clear all own properties
44
+ for (const key of Object.getOwnPropertyNames(obj)) {
45
+ delete obj[key];
46
+ }
47
+ destructor();
48
+ return true;
49
+ }
50
+ static isDestroyable(obj) {
51
+ return _a.destructors.has(obj);
52
+ }
53
+ constructor(...args) {
54
+ super(...args);
55
+ const allocated = {};
56
+ this[allocatedValues] = allocated;
57
+ // @ts-expect-error `this` is an AbstractDestroyable
58
+ const myDestructor = destructorObj?.destructor ?? this[destructor];
59
+ if (!myDestructor) {
60
+ throw new DestructionError('Destructor is not defined');
61
+ }
62
+ function destruction() {
63
+ myDestructor(allocated);
64
+ }
65
+ _a.destructors.set(this, destruction);
66
+ fr.register(this, destruction, this);
67
+ }
68
+ },
69
+ _a.destructors = new WeakMap(),
70
+ _a;
71
+ }
72
+ const allocated = decorator.decorator({
73
+ setter(original, propertyKey) {
74
+ return function (value) {
75
+ this[allocatedValues][propertyKey] = value;
76
+ return original.call(this, value);
77
+ };
78
+ },
79
+ });
80
+ function callOnGC(cb) {
81
+ let called = false;
82
+ const forward = () => {
83
+ if (called)
84
+ return;
85
+ called = true;
86
+ cb();
87
+ };
88
+ fr.register(forward, cb, cb);
89
+ return forward;
90
+ }
91
+
92
+ exports.Destroyable = Destroyable;
93
+ exports.DestructionError = DestructionError;
94
+ exports.allocated = allocated;
95
+ exports.allocatedValues = allocatedValues;
96
+ exports.callOnGC = callOnGC;
97
+ exports.destructor = destructor;
98
+ //# sourceMappingURL=destroyable.js.map
@@ -0,0 +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())\nexport const destructor = Symbol('destructor')\nexport const allocatedValues = Symbol('allocated')\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\nexport function Destroyable<\n\tT extends new (\n\t\t...args: any[]\n\t) => any,\n\tAllocated extends Partial<typeof this>,\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\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\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\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\tdeclare [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')\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\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// Context Manager Protocol for `with` statement integration\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;MAC9C,UAAU,GAAG,MAAM,CAAC,YAAY;MAChC,eAAe,GAAG,MAAM,CAAC,WAAW;AAC3C,MAAO,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AM,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;;;;;;;;;"}
@@ -0,0 +1,11 @@
1
+ declare class Eventful<Events extends Record<string, (...args: any[]) => void>> {
2
+ #private;
3
+ hook(cb: <EventType extends keyof Events>(event: EventType, ...args: Parameters<Events[EventType]>) => void): () => void;
4
+ on(events: Partial<Events>): void;
5
+ on<EventType extends keyof Events>(event: EventType, cb: Events[EventType]): () => void;
6
+ off(events: Partial<Events>): void;
7
+ off<EventType extends keyof Events>(event: EventType, cb?: Events[EventType]): void;
8
+ emit<EventType extends keyof Events>(event: EventType, ...args: Parameters<Events[EventType]>): void;
9
+ }
10
+
11
+ export { Eventful };
@@ -0,0 +1,88 @@
1
+ /******************************************************************************
2
+ Copyright (c) Microsoft Corporation.
3
+
4
+ Permission to use, copy, modify, and/or distribute this software for any
5
+ purpose with or without fee is hereby granted.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13
+ PERFORMANCE OF THIS SOFTWARE.
14
+ ***************************************************************************** */
15
+ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
16
+
17
+
18
+ function __classPrivateFieldGet(receiver, state, kind, f) {
19
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
20
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
21
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
22
+ }
23
+
24
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
25
+ var e = new Error(message);
26
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
27
+ };
28
+
29
+ var _Eventful_events, _Eventful_hooks;
30
+ class Eventful {
31
+ constructor() {
32
+ _Eventful_events.set(this, new Map());
33
+ _Eventful_hooks.set(this, []);
34
+ }
35
+ hook(cb) {
36
+ if (!__classPrivateFieldGet(this, _Eventful_hooks, "f").includes(cb))
37
+ __classPrivateFieldGet(this, _Eventful_hooks, "f").push(cb);
38
+ return () => {
39
+ __classPrivateFieldGet(this, _Eventful_hooks, "f").splice(__classPrivateFieldGet(this, _Eventful_hooks, "f").indexOf(cb), 1);
40
+ };
41
+ }
42
+ on(eventOrEvents, cb) {
43
+ if (typeof eventOrEvents === 'object') {
44
+ for (const e of Object.keys(eventOrEvents)) {
45
+ this.on(e, eventOrEvents[e]);
46
+ }
47
+ }
48
+ else if (cb !== undefined) {
49
+ let callbacks = __classPrivateFieldGet(this, _Eventful_events, "f").get(eventOrEvents);
50
+ if (!callbacks) {
51
+ callbacks = [];
52
+ __classPrivateFieldGet(this, _Eventful_events, "f").set(eventOrEvents, callbacks);
53
+ }
54
+ callbacks.push(cb);
55
+ }
56
+ // @ts-expect-error Generic case leads to generic case
57
+ return () => this.off(eventOrEvents, cb);
58
+ }
59
+ off(eventOrEvents, cb) {
60
+ if (typeof eventOrEvents === 'object') {
61
+ for (const e of Object.keys(eventOrEvents)) {
62
+ this.off(e, eventOrEvents[e]);
63
+ }
64
+ }
65
+ else if (cb !== null && cb !== undefined) {
66
+ const callbacks = __classPrivateFieldGet(this, _Eventful_events, "f").get(eventOrEvents);
67
+ if (callbacks) {
68
+ __classPrivateFieldGet(this, _Eventful_events, "f").set(eventOrEvents, callbacks.filter((c) => c !== cb));
69
+ }
70
+ }
71
+ else {
72
+ // Remove all listeners for this event
73
+ __classPrivateFieldGet(this, _Eventful_events, "f").delete(eventOrEvents);
74
+ }
75
+ }
76
+ emit(event, ...args) {
77
+ const callbacks = __classPrivateFieldGet(this, _Eventful_events, "f").get(event);
78
+ if (callbacks)
79
+ for (const cb of callbacks)
80
+ cb.apply(this, args);
81
+ for (const cb of __classPrivateFieldGet(this, _Eventful_hooks, "f"))
82
+ cb.call(this, event, ...args);
83
+ }
84
+ }
85
+ _Eventful_events = new WeakMap(), _Eventful_hooks = new WeakMap();
86
+
87
+ export { Eventful };
88
+ //# sourceMappingURL=eventful.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"eventful.esm.js","sources":["../src/eventful.ts"],"sourcesContent":["export class Eventful<Events extends Record<string, (...args: any[]) => void>> {\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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;MAAa,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,IAA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CAAC,aAAa,CAAqB,EAAE;gBAC/D,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,aAAa,CAAC,CAAC,CAAC,CAAC;YAC9B;QACD;aAAO,IAAI,EAAE,KAAK,IAAI,IAAI,EAAE,KAAK,SAAS,EAAE;YAC3C,MAAM,SAAS,GAAG,sBAAA,CAAA,IAAI,EAAA,gBAAA,EAAA,GAAA,CAAQ,CAAC,GAAG,CAAC,aAAa,CAAC;YACjD,IAAI,SAAS,EAAE;gBACd,sBAAA,CAAA,IAAI,wBAAQ,CAAC,GAAG,CACf,aAAa,EACb,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC,CACjC;YACF;QACD;aAAO;;AAEN,YAAA,sBAAA,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;;;;;"}
@@ -0,0 +1,90 @@
1
+ 'use strict';
2
+
3
+ /******************************************************************************
4
+ Copyright (c) Microsoft Corporation.
5
+
6
+ Permission to use, copy, modify, and/or distribute this software for any
7
+ purpose with or without fee is hereby granted.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
+ PERFORMANCE OF THIS SOFTWARE.
16
+ ***************************************************************************** */
17
+ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
18
+
19
+
20
+ function __classPrivateFieldGet(receiver, state, kind, f) {
21
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
22
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
23
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
24
+ }
25
+
26
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
27
+ var e = new Error(message);
28
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
29
+ };
30
+
31
+ var _Eventful_events, _Eventful_hooks;
32
+ class Eventful {
33
+ constructor() {
34
+ _Eventful_events.set(this, new Map());
35
+ _Eventful_hooks.set(this, []);
36
+ }
37
+ hook(cb) {
38
+ if (!__classPrivateFieldGet(this, _Eventful_hooks, "f").includes(cb))
39
+ __classPrivateFieldGet(this, _Eventful_hooks, "f").push(cb);
40
+ return () => {
41
+ __classPrivateFieldGet(this, _Eventful_hooks, "f").splice(__classPrivateFieldGet(this, _Eventful_hooks, "f").indexOf(cb), 1);
42
+ };
43
+ }
44
+ on(eventOrEvents, cb) {
45
+ if (typeof eventOrEvents === 'object') {
46
+ for (const e of Object.keys(eventOrEvents)) {
47
+ this.on(e, eventOrEvents[e]);
48
+ }
49
+ }
50
+ else if (cb !== undefined) {
51
+ let callbacks = __classPrivateFieldGet(this, _Eventful_events, "f").get(eventOrEvents);
52
+ if (!callbacks) {
53
+ callbacks = [];
54
+ __classPrivateFieldGet(this, _Eventful_events, "f").set(eventOrEvents, callbacks);
55
+ }
56
+ callbacks.push(cb);
57
+ }
58
+ // @ts-expect-error Generic case leads to generic case
59
+ return () => this.off(eventOrEvents, cb);
60
+ }
61
+ off(eventOrEvents, cb) {
62
+ if (typeof eventOrEvents === 'object') {
63
+ for (const e of Object.keys(eventOrEvents)) {
64
+ this.off(e, eventOrEvents[e]);
65
+ }
66
+ }
67
+ else if (cb !== null && cb !== undefined) {
68
+ const callbacks = __classPrivateFieldGet(this, _Eventful_events, "f").get(eventOrEvents);
69
+ if (callbacks) {
70
+ __classPrivateFieldGet(this, _Eventful_events, "f").set(eventOrEvents, callbacks.filter((c) => c !== cb));
71
+ }
72
+ }
73
+ else {
74
+ // Remove all listeners for this event
75
+ __classPrivateFieldGet(this, _Eventful_events, "f").delete(eventOrEvents);
76
+ }
77
+ }
78
+ emit(event, ...args) {
79
+ const callbacks = __classPrivateFieldGet(this, _Eventful_events, "f").get(event);
80
+ if (callbacks)
81
+ for (const cb of callbacks)
82
+ cb.apply(this, args);
83
+ for (const cb of __classPrivateFieldGet(this, _Eventful_hooks, "f"))
84
+ cb.call(this, event, ...args);
85
+ }
86
+ }
87
+ _Eventful_events = new WeakMap(), _Eventful_hooks = new WeakMap();
88
+
89
+ exports.Eventful = Eventful;
90
+ //# sourceMappingURL=eventful.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"eventful.js","sources":["../src/eventful.ts"],"sourcesContent":["export class Eventful<Events extends Record<string, (...args: any[]) => void>> {\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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;MAAa,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;AAEtB,QAAA,IAAI,OAAO,aAAa,KAAK,QAAQ,EAAE;YACtC,KAAK,MAAM,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,aAAa,CAAqB,EAAE;gBAC/D,IAAI,CAAC,EAAE,CAAC,CAAC,EAAE,aAAa,CAAC,CAAC,CAAE,CAAC;YAC9B;QACD;AAAO,aAAA,IAAI,EAAE,KAAK,SAAS,EAAE;YAC5B,IAAI,SAAS,GAAG,sBAAA,CAAA,IAAI,EAAA,gBAAA,EAAA,GAAA,CAAQ,CAAC,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AAC,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;;;;;"}
@@ -0,0 +1,15 @@
1
+ export { Decorator, DecoratorDescription, DecoratorError, DecoratorFactory, GenericClassDecorator, LegacyClassDecorator, LegacyPropertyDecorator, ModernAccessorDecorator, ModernClassDecorator, ModernGetterDecorator, ModernMethodDecorator, ModernSetterDecorator, decorator, legacyDecorator, modernDecorator } from './decorator.js';
2
+ export { ContextManager, Destroyable, DestructionError, allocated, allocatedValues, callOnGC, destructor } from './destroyable.js';
3
+ export { Eventful } from './eventful.js';
4
+ export { Indexable, getAt, setAt } from './indexable.js';
5
+ export { ReactiveBase, ReactiveError, ScopedCallback, computed, effect, getState, immutables, invalidateComputed, isNonReactive, isReactive, profileInfo, reactive, reactiveOptions, unreactive, untracked, unwrap, watch } from './reactive.js';
6
+ export { cache, cached, debounce, deprecated, describe, isCached, throttle } from './std-decorators.js';
7
+
8
+ type ElementTypes<T extends readonly unknown[]> = {
9
+ [K in keyof T]: T[K] extends readonly (infer U)[] ? U : T[K];
10
+ };
11
+ declare function zip<T extends (readonly unknown[])[]>(...args: T): ElementTypes<T>[];
12
+ declare function isConstructor(fn: Function): boolean;
13
+ declare function renamed<F extends Function>(fct: F, name: string): F;
14
+
15
+ export { isConstructor, renamed, zip };
@@ -0,0 +1,7 @@
1
+ export { D as DecoratorError, d as decorator, i as isConstructor, l as legacyDecorator, m as modernDecorator, r as renamed, z as zip } from './chunks/decorator-CPbZNnsX.esm.js';
2
+ export { Destroyable, DestructionError, allocated, allocatedValues, callOnGC, destructor } from './destroyable.esm.js';
3
+ export { Eventful } from './eventful.esm.js';
4
+ export { Indexable, getAt, setAt } from './indexable.esm.js';
5
+ export { ReactiveBase, ReactiveError, computed, effect, getState, immutables, invalidateComputed, isNonReactive, isReactive, profileInfo, reactive, reactiveOptions, unreactive, untracked, unwrap, watch } from './reactive.esm.js';
6
+ export { cache, cached, debounce, deprecated, describe, isCached, throttle } from './std-decorators.esm.js';
7
+ //# sourceMappingURL=index.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;"}
package/dist/index.js ADDED
@@ -0,0 +1,52 @@
1
+ 'use strict';
2
+
3
+ var decorator = require('./chunks/decorator-BXsign4Z.js');
4
+ var destroyable = require('./destroyable.js');
5
+ var eventful = require('./eventful.js');
6
+ var indexable = require('./indexable.js');
7
+ var reactive = require('./reactive.js');
8
+ var stdDecorators = require('./std-decorators.js');
9
+
10
+
11
+
12
+ exports.DecoratorError = decorator.DecoratorError;
13
+ exports.decorator = decorator.decorator;
14
+ exports.isConstructor = decorator.isConstructor;
15
+ exports.legacyDecorator = decorator.legacyDecorator;
16
+ exports.modernDecorator = decorator.modernDecorator;
17
+ exports.renamed = decorator.renamed;
18
+ exports.zip = decorator.zip;
19
+ exports.Destroyable = destroyable.Destroyable;
20
+ exports.DestructionError = destroyable.DestructionError;
21
+ exports.allocated = destroyable.allocated;
22
+ exports.allocatedValues = destroyable.allocatedValues;
23
+ exports.callOnGC = destroyable.callOnGC;
24
+ exports.destructor = destroyable.destructor;
25
+ exports.Eventful = eventful.Eventful;
26
+ exports.Indexable = indexable.Indexable;
27
+ exports.getAt = indexable.getAt;
28
+ exports.setAt = indexable.setAt;
29
+ exports.ReactiveBase = reactive.ReactiveBase;
30
+ exports.ReactiveError = reactive.ReactiveError;
31
+ exports.computed = reactive.computed;
32
+ exports.effect = reactive.effect;
33
+ exports.getState = reactive.getState;
34
+ exports.immutables = reactive.immutables;
35
+ exports.invalidateComputed = reactive.invalidateComputed;
36
+ exports.isNonReactive = reactive.isNonReactive;
37
+ exports.isReactive = reactive.isReactive;
38
+ exports.profileInfo = reactive.profileInfo;
39
+ exports.reactive = reactive.reactive;
40
+ exports.reactiveOptions = reactive.reactiveOptions;
41
+ exports.unreactive = reactive.unreactive;
42
+ exports.untracked = reactive.untracked;
43
+ exports.unwrap = reactive.unwrap;
44
+ exports.watch = reactive.watch;
45
+ exports.cache = stdDecorators.cache;
46
+ exports.cached = stdDecorators.cached;
47
+ exports.debounce = stdDecorators.debounce;
48
+ exports.deprecated = stdDecorators.deprecated;
49
+ exports.describe = stdDecorators.describe;
50
+ exports.isCached = stdDecorators.isCached;
51
+ exports.throttle = stdDecorators.throttle;
52
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,31 @@
1
+ declare const getAt: unique symbol;
2
+ declare const setAt: unique symbol;
3
+ interface IndexingAt<Items = any> {
4
+ [getAt](index: number): Items;
5
+ }
6
+ interface Accessor<T, Items> {
7
+ get(this: T, index: number): Items;
8
+ set?(this: T, index: number, value: Items): void;
9
+ getLength?(this: T): number;
10
+ setLength?(this: T, value: number): void;
11
+ }
12
+ declare abstract class AbstractGetAt<Items = any> {
13
+ abstract [getAt](index: number): Items;
14
+ }
15
+ declare function Indexable<Items, Base extends abstract new (...args: any[]) => any>(base: Base, accessor: Accessor<InstanceType<Base>, Items>): new (...args: ConstructorParameters<Base>) => InstanceType<Base> & {
16
+ [x: number]: Items;
17
+ };
18
+ declare function Indexable<Items>(accessor: Accessor<any, Items>): new () => {
19
+ [x: number]: Items;
20
+ };
21
+ declare function Indexable<Base extends new (...args: any[]) => IndexingAt>(base: Base): new (...args: ConstructorParameters<Base>) => InstanceType<Base> & {
22
+ [x: number]: AtReturnType<InstanceType<Base>>;
23
+ };
24
+ declare function Indexable<Items>(): abstract new (...args: any[]) => AbstractGetAt & {
25
+ [x: number]: Items;
26
+ };
27
+ type AtReturnType<T> = T extends {
28
+ [getAt](index: number): infer R;
29
+ } ? R : never;
30
+
31
+ export { Indexable, getAt, setAt };