mutts 1.0.6 → 1.0.8
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 +61 -23
- package/dist/async/browser.d.ts +2 -0
- package/dist/async/browser.d.ts.map +1 -0
- package/dist/async/index.d.ts +18 -0
- package/dist/async/index.d.ts.map +1 -0
- package/dist/async/node.d.ts +2 -0
- package/dist/async/node.d.ts.map +1 -0
- package/dist/{chunks/index-CDCOjzTy.js → browser.cjs} +5913 -4382
- package/dist/browser.cjs.map +1 -0
- package/dist/browser.d.ts +1655 -0
- package/dist/browser.esm.js +305 -0
- package/dist/browser.esm.js.map +1 -0
- package/dist/chunks/async-browser-CA0jPWIi.cjs +304 -0
- package/dist/chunks/async-browser-CA0jPWIi.cjs.map +1 -0
- package/dist/chunks/async-core-UqHzvJ-S.cjs +25 -0
- package/dist/chunks/async-core-UqHzvJ-S.cjs.map +1 -0
- package/dist/chunks/async-node-BYHuGTni.cjs +103 -0
- package/dist/chunks/async-node-BYHuGTni.cjs.map +1 -0
- package/dist/chunks/{index-DiP0RXoZ.esm.js → index-DhaOVusv.esm.js} +5851 -4345
- package/dist/chunks/index-DhaOVusv.esm.js.map +1 -0
- package/dist/decorator.d.ts +17 -18
- package/dist/decorator.d.ts.map +1 -0
- package/dist/destroyable.d.ts +12 -15
- package/dist/destroyable.d.ts.map +1 -0
- package/dist/devtools/devtool/devtools.d.ts +1 -0
- package/dist/devtools/devtool/devtools.d.ts.map +1 -0
- package/dist/devtools/devtool/panel.d.ts +2 -0
- package/dist/devtools/devtool/panel.d.ts.map +1 -0
- package/dist/devtools/panel.js.map +1 -1
- package/dist/entry-browser.d.ts +3 -0
- package/dist/entry-browser.d.ts.map +1 -0
- package/dist/entry-node.d.ts +3 -0
- package/dist/entry-node.d.ts.map +1 -0
- package/dist/eventful.d.ts +3 -5
- package/dist/eventful.d.ts.map +1 -0
- package/dist/index.d.ts +13 -19
- package/dist/index.d.ts.map +1 -0
- package/dist/indexable.d.ts +10 -10
- package/dist/indexable.d.ts.map +1 -0
- package/dist/introspection.d.ts +27 -0
- package/dist/introspection.d.ts.map +1 -0
- package/dist/iterableWeak.d.ts +53 -0
- package/dist/iterableWeak.d.ts.map +1 -0
- package/dist/mixins.d.ts +25 -0
- package/dist/mixins.d.ts.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.cjs +105 -0
- package/dist/node.cjs.map +1 -0
- package/dist/node.d.ts +1 -0
- package/dist/node.esm.js +104 -0
- package/dist/node.esm.js.map +1 -0
- package/dist/promiseChain.d.ts +4 -5
- package/dist/promiseChain.d.ts.map +1 -0
- package/dist/reactive/array.d.ts +49 -0
- package/dist/reactive/array.d.ts.map +1 -0
- package/dist/reactive/buffer.d.ts +44 -0
- package/dist/reactive/buffer.d.ts.map +1 -0
- package/dist/reactive/change.d.ts +29 -0
- package/dist/reactive/change.d.ts.map +1 -0
- package/dist/reactive/debug.d.ts +111 -0
- package/dist/reactive/debug.d.ts.map +1 -0
- package/dist/reactive/deep-touch.d.ts +28 -0
- package/dist/reactive/deep-touch.d.ts.map +1 -0
- package/dist/reactive/deep-watch-state.d.ts +25 -0
- package/dist/reactive/deep-watch-state.d.ts.map +1 -0
- package/dist/reactive/deep-watch.d.ts +19 -0
- package/dist/reactive/deep-watch.d.ts.map +1 -0
- package/dist/reactive/effect-context.d.ts +7 -0
- package/dist/reactive/effect-context.d.ts.map +1 -0
- package/dist/reactive/effects.d.ts +151 -0
- package/dist/reactive/effects.d.ts.map +1 -0
- package/dist/reactive/index.d.ts +20 -0
- package/dist/reactive/index.d.ts.map +1 -0
- package/dist/reactive/interface.d.ts +64 -0
- package/dist/reactive/interface.d.ts.map +1 -0
- package/dist/reactive/map.d.ts +30 -0
- package/dist/reactive/map.d.ts.map +1 -0
- package/dist/reactive/memoize.d.ts +5 -0
- package/dist/reactive/memoize.d.ts.map +1 -0
- package/dist/reactive/non-reactive-state.d.ts +9 -0
- package/dist/reactive/non-reactive-state.d.ts.map +1 -0
- package/dist/reactive/non-reactive.d.ts +11 -0
- package/dist/reactive/non-reactive.d.ts.map +1 -0
- package/dist/reactive/project.d.ts +41 -0
- package/dist/reactive/project.d.ts.map +1 -0
- package/dist/reactive/proxy-state.d.ts +8 -0
- package/dist/reactive/proxy-state.d.ts.map +1 -0
- package/dist/reactive/proxy.d.ts +23 -0
- package/dist/reactive/proxy.d.ts.map +1 -0
- package/dist/reactive/record.d.ts +116 -0
- package/dist/reactive/record.d.ts.map +1 -0
- package/dist/reactive/register.d.ts +64 -0
- package/dist/reactive/register.d.ts.map +1 -0
- package/dist/reactive/registry.d.ts +20 -0
- package/dist/reactive/registry.d.ts.map +1 -0
- package/dist/reactive/set.d.ts +28 -0
- package/dist/reactive/set.d.ts.map +1 -0
- package/dist/reactive/tracking.d.ts +7 -0
- package/dist/reactive/tracking.d.ts.map +1 -0
- package/dist/reactive/types.d.ts +376 -0
- package/dist/reactive/types.d.ts.map +1 -0
- package/dist/std-decorators.d.ts +9 -11
- package/dist/std-decorators.d.ts.map +1 -0
- package/dist/utils.d.ts +49 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/zone.d.ts +40 -0
- package/dist/zone.d.ts.map +1 -0
- package/docs/ai/api-reference.md +0 -2
- package/docs/reactive/advanced.md +2 -5
- package/docs/reactive/collections.md +0 -125
- package/docs/reactive/core.md +27 -24
- package/docs/reactive/debugging.md +12 -2
- package/docs/reactive/project.md +1 -1
- package/docs/reactive/scan.md +78 -0
- package/docs/reactive.md +2 -1
- package/docs/std-decorators.md +69 -0
- package/docs/zone.md +95 -0
- package/package.json +67 -23
- package/src/async/browser.ts +319 -0
- package/src/async/index.ts +23 -0
- package/src/async/node.ts +104 -0
- package/src/decorator.ts +5 -1
- package/src/destroyable.ts +1 -1
- package/src/entry-browser.ts +5 -0
- package/src/entry-node.ts +5 -0
- package/src/index.d.ts +12 -9
- package/src/index.ts +23 -14
- package/src/indexable.ts +42 -0
- package/src/mixins.ts +2 -2
- package/src/reactive/array.ts +274 -179
- package/src/reactive/buffer.ts +168 -0
- package/src/reactive/change.ts +2 -2
- package/src/reactive/effect-context.ts +15 -91
- package/src/reactive/effects.ts +119 -179
- package/src/reactive/index.ts +11 -13
- package/src/reactive/interface.ts +19 -33
- package/src/reactive/map.ts +49 -62
- package/src/reactive/memoize.ts +19 -9
- package/src/reactive/project.ts +43 -22
- package/src/reactive/proxy.ts +16 -41
- package/src/reactive/record.ts +3 -3
- package/src/reactive/register.ts +5 -7
- package/src/reactive/registry.ts +9 -17
- package/src/reactive/set.ts +43 -57
- package/src/reactive/tracking.ts +1 -29
- package/src/reactive/types.ts +46 -23
- package/src/utils.ts +80 -37
- package/src/zone.ts +138 -0
- package/dist/chunks/_tslib-BgjropY9.js +0 -81
- package/dist/chunks/_tslib-BgjropY9.js.map +0 -1
- package/dist/chunks/_tslib-MCKDzsSq.esm.js +0 -75
- package/dist/chunks/_tslib-MCKDzsSq.esm.js.map +0 -1
- package/dist/chunks/decorator-BGILvPtN.esm.js +0 -627
- package/dist/chunks/decorator-BGILvPtN.esm.js.map +0 -1
- package/dist/chunks/decorator-BQ2eBTCj.js +0 -651
- package/dist/chunks/decorator-BQ2eBTCj.js.map +0 -1
- package/dist/chunks/index-CDCOjzTy.js.map +0 -1
- package/dist/chunks/index-DiP0RXoZ.esm.js.map +0 -1
- 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.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.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.esm.js +0 -53
- package/dist/index.esm.js.map +0 -1
- package/dist/index.js +0 -139
- package/dist/index.js.map +0 -1
- 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.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 -910
- 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.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/reactive.esm.js
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export { R as ReactiveBase, b as ReadOnlyError, c as Register, d as addBatchCleanup, e as atomic, f as batch, g as biDi, h as buildReactivityGraph, i as cleanedBy, j as cleanup, k as deepWatch, l as defer, m as derived, n as effect, o as enableDevTools, p as getActivationLog, q as getActiveEffect, r as getActiveProjection, s as getState, t as immutables, u as isDevtoolsEnabled, v as isNonReactive, w as isReactive, x as isZoneEnabled, y as mapped, z as memoize, B as organize, C as organized, D as profileInfo, E as project, F as reactive, G as reduced, H as register, J as registerEffectForDebug, K as registerNativeReactivity, L as registerObjectForDebug, M as root, N as setEffectName, O as setObjectName, P as setZoneEnabled, Q as touched, S as touched1, T as trackEffect, U as unreactive, V as untracked, W as unwrap, X as watch } from './chunks/index-DiP0RXoZ.esm.js';
|
|
2
|
-
export { R as ReactiveError, o as reactiveOptions } from './chunks/decorator-BGILvPtN.esm.js';
|
|
3
|
-
import './indexable.esm.js';
|
|
4
|
-
import './chunks/_tslib-MCKDzsSq.esm.js';
|
|
5
|
-
//# sourceMappingURL=reactive.esm.js.map
|
package/dist/reactive.esm.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"reactive.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;"}
|
package/dist/reactive.js
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var reactive = require('./chunks/index-CDCOjzTy.js');
|
|
4
|
-
var decorator = require('./chunks/decorator-BQ2eBTCj.js');
|
|
5
|
-
require('./indexable.js');
|
|
6
|
-
require('./chunks/_tslib-BgjropY9.js');
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
exports.ReactiveBase = reactive.ReactiveBase;
|
|
11
|
-
exports.ReadOnlyError = reactive.ReadOnlyError;
|
|
12
|
-
exports.Register = reactive.Register;
|
|
13
|
-
exports.addBatchCleanup = reactive.addBatchCleanup;
|
|
14
|
-
exports.atomic = reactive.atomic;
|
|
15
|
-
exports.batch = reactive.batch;
|
|
16
|
-
exports.biDi = reactive.biDi;
|
|
17
|
-
exports.buildReactivityGraph = reactive.buildReactivityGraph;
|
|
18
|
-
exports.cleanedBy = reactive.cleanedBy;
|
|
19
|
-
exports.cleanup = reactive.cleanup;
|
|
20
|
-
exports.deepWatch = reactive.deepWatch;
|
|
21
|
-
exports.defer = reactive.defer;
|
|
22
|
-
exports.derived = reactive.derived;
|
|
23
|
-
exports.effect = reactive.effect;
|
|
24
|
-
exports.enableDevTools = reactive.enableDevTools;
|
|
25
|
-
exports.getActivationLog = reactive.getActivationLog;
|
|
26
|
-
exports.getActiveEffect = reactive.getActiveEffect;
|
|
27
|
-
exports.getActiveProjection = reactive.getActiveProjection;
|
|
28
|
-
exports.getState = reactive.getState;
|
|
29
|
-
exports.immutables = reactive.immutables;
|
|
30
|
-
exports.isDevtoolsEnabled = reactive.isDevtoolsEnabled;
|
|
31
|
-
exports.isNonReactive = reactive.isNonReactive;
|
|
32
|
-
exports.isReactive = reactive.isReactive;
|
|
33
|
-
exports.isZoneEnabled = reactive.isZoneEnabled;
|
|
34
|
-
exports.mapped = reactive.mapped;
|
|
35
|
-
exports.memoize = reactive.memoize;
|
|
36
|
-
exports.organize = reactive.organize;
|
|
37
|
-
exports.organized = reactive.organized;
|
|
38
|
-
exports.profileInfo = reactive.profileInfo;
|
|
39
|
-
exports.project = reactive.project;
|
|
40
|
-
exports.reactive = reactive.reactive;
|
|
41
|
-
exports.reduced = reactive.reduced;
|
|
42
|
-
exports.register = reactive.register;
|
|
43
|
-
exports.registerEffectForDebug = reactive.registerEffectForDebug;
|
|
44
|
-
exports.registerNativeReactivity = reactive.registerNativeReactivity;
|
|
45
|
-
exports.registerObjectForDebug = reactive.registerObjectForDebug;
|
|
46
|
-
exports.root = reactive.root;
|
|
47
|
-
exports.setEffectName = reactive.setEffectName;
|
|
48
|
-
exports.setObjectName = reactive.setObjectName;
|
|
49
|
-
exports.setZoneEnabled = reactive.setZoneEnabled;
|
|
50
|
-
exports.touched = reactive.touched;
|
|
51
|
-
exports.touched1 = reactive.touched1;
|
|
52
|
-
exports.trackEffect = reactive.trackEffect;
|
|
53
|
-
exports.unreactive = reactive.unreactive;
|
|
54
|
-
exports.untracked = reactive.untracked;
|
|
55
|
-
exports.unwrap = reactive.unwrap;
|
|
56
|
-
exports.watch = reactive.watch;
|
|
57
|
-
exports.ReactiveError = decorator.ReactiveError;
|
|
58
|
-
exports.reactiveOptions = decorator.options;
|
|
59
|
-
//# sourceMappingURL=reactive.js.map
|
package/dist/reactive.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"reactive.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
import { d as decorator } from './chunks/decorator-BGILvPtN.esm.js';
|
|
2
|
-
|
|
3
|
-
// In order to avoid async re-entrance, we could use zone.js or something like that.
|
|
4
|
-
const syncCalculating = [];
|
|
5
|
-
/**
|
|
6
|
-
* Decorator that caches the result of a getter method and only recomputes when dependencies change
|
|
7
|
-
* Prevents circular dependencies and provides automatic cache invalidation
|
|
8
|
-
*/
|
|
9
|
-
const cached = decorator({
|
|
10
|
-
getter(original, _target, propertyKey) {
|
|
11
|
-
return function () {
|
|
12
|
-
const alreadyCalculating = syncCalculating.findIndex((c) => c.object === this && c.prop === propertyKey);
|
|
13
|
-
if (alreadyCalculating > -1)
|
|
14
|
-
throw new Error(`Circular dependency detected: ${syncCalculating
|
|
15
|
-
.slice(alreadyCalculating)
|
|
16
|
-
.map((c) => `${c.object.constructor.name}.${String(c.prop)}`)
|
|
17
|
-
.join(' -> ')} -> again`);
|
|
18
|
-
syncCalculating.push({ object: this, prop: propertyKey });
|
|
19
|
-
try {
|
|
20
|
-
const rv = original.call(this);
|
|
21
|
-
cache(this, propertyKey, rv);
|
|
22
|
-
return rv;
|
|
23
|
-
}
|
|
24
|
-
finally {
|
|
25
|
-
syncCalculating.pop();
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
},
|
|
29
|
-
});
|
|
30
|
-
/**
|
|
31
|
-
* Checks if a property is cached (has a cached value)
|
|
32
|
-
* @param object - The object to check
|
|
33
|
-
* @param propertyKey - The property key to check
|
|
34
|
-
* @returns True if the property has a cached value
|
|
35
|
-
*/
|
|
36
|
-
function isCached(object, propertyKey) {
|
|
37
|
-
return !!Object.getOwnPropertyDescriptor(object, propertyKey);
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Caches a value for a property on an object
|
|
41
|
-
* @param object - The object to cache the value on
|
|
42
|
-
* @param propertyKey - The property key to cache
|
|
43
|
-
* @param value - The value to cache
|
|
44
|
-
*/
|
|
45
|
-
function cache(object, propertyKey, value) {
|
|
46
|
-
Object.defineProperty(object, propertyKey, { value });
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Creates a decorator that modifies property descriptors for specified properties
|
|
50
|
-
* @param descriptor - The descriptor properties to apply
|
|
51
|
-
* @returns A class decorator that applies the descriptor to specified properties
|
|
52
|
-
*/
|
|
53
|
-
function describe(descriptor) {
|
|
54
|
-
return (...properties) => (Base) => {
|
|
55
|
-
return class extends Base {
|
|
56
|
-
constructor(...args) {
|
|
57
|
-
super(...args);
|
|
58
|
-
for (const key of properties) {
|
|
59
|
-
Object.defineProperty(this, key, {
|
|
60
|
-
...Object.getOwnPropertyDescriptor(this, key),
|
|
61
|
-
...descriptor,
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Decorator that marks methods, properties, or classes as deprecated
|
|
70
|
-
* Provides warning messages when deprecated items are used
|
|
71
|
-
*/
|
|
72
|
-
const deprecated = Object.assign(decorator({
|
|
73
|
-
method(original, _target, propertyKey) {
|
|
74
|
-
return function (...args) {
|
|
75
|
-
deprecated.warn(this, propertyKey);
|
|
76
|
-
return original.apply(this, args);
|
|
77
|
-
};
|
|
78
|
-
},
|
|
79
|
-
getter(original, _target, propertyKey) {
|
|
80
|
-
return function () {
|
|
81
|
-
deprecated.warn(this, propertyKey);
|
|
82
|
-
return original.call(this);
|
|
83
|
-
};
|
|
84
|
-
},
|
|
85
|
-
setter(original, _target, propertyKey) {
|
|
86
|
-
return function (value) {
|
|
87
|
-
deprecated.warn(this, propertyKey);
|
|
88
|
-
return original.call(this, value);
|
|
89
|
-
};
|
|
90
|
-
},
|
|
91
|
-
class(original) {
|
|
92
|
-
return class extends original {
|
|
93
|
-
constructor(...args) {
|
|
94
|
-
super(...args);
|
|
95
|
-
deprecated.warn(this, 'constructor');
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
|
-
},
|
|
99
|
-
default(message) {
|
|
100
|
-
return decorator({
|
|
101
|
-
method(original, _target, propertyKey) {
|
|
102
|
-
return function (...args) {
|
|
103
|
-
deprecated.warn(this, propertyKey, message);
|
|
104
|
-
return original.apply(this, args);
|
|
105
|
-
};
|
|
106
|
-
},
|
|
107
|
-
getter(original, _target, propertyKey) {
|
|
108
|
-
return function () {
|
|
109
|
-
deprecated.warn(this, propertyKey, message);
|
|
110
|
-
return original.call(this);
|
|
111
|
-
};
|
|
112
|
-
},
|
|
113
|
-
setter(original, _target, propertyKey) {
|
|
114
|
-
return function (value) {
|
|
115
|
-
deprecated.warn(this, propertyKey, message);
|
|
116
|
-
return original.call(this, value);
|
|
117
|
-
};
|
|
118
|
-
},
|
|
119
|
-
class(original) {
|
|
120
|
-
return class extends original {
|
|
121
|
-
constructor(...args) {
|
|
122
|
-
super(...args);
|
|
123
|
-
deprecated.warn(this, 'constructor', message);
|
|
124
|
-
}
|
|
125
|
-
};
|
|
126
|
-
},
|
|
127
|
-
});
|
|
128
|
-
},
|
|
129
|
-
}), {
|
|
130
|
-
warn: (target, propertyKey, message) => {
|
|
131
|
-
// biome-ignore lint/suspicious/noConsole: To be overridden
|
|
132
|
-
console.warn(`${target.constructor.name}.${String(propertyKey)} is deprecated${message ? `: ${message}` : ''}`);
|
|
133
|
-
},
|
|
134
|
-
});
|
|
135
|
-
/**
|
|
136
|
-
* Creates a debounced method decorator that delays execution until after the delay period has passed
|
|
137
|
-
* @param delay - The delay in milliseconds
|
|
138
|
-
* @returns A method decorator that debounces method calls
|
|
139
|
-
*/
|
|
140
|
-
function debounce(delay) {
|
|
141
|
-
return decorator({
|
|
142
|
-
method(original, _target, _propertyKey) {
|
|
143
|
-
let timeoutId = null;
|
|
144
|
-
return function (...args) {
|
|
145
|
-
// Clear existing timeout
|
|
146
|
-
if (timeoutId) {
|
|
147
|
-
clearTimeout(timeoutId);
|
|
148
|
-
}
|
|
149
|
-
// Set new timeout
|
|
150
|
-
timeoutId = setTimeout(() => {
|
|
151
|
-
original.apply(this, args);
|
|
152
|
-
timeoutId = null;
|
|
153
|
-
}, delay);
|
|
154
|
-
};
|
|
155
|
-
},
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Creates a throttled method decorator that limits execution to once per delay period
|
|
160
|
-
* @param delay - The delay in milliseconds
|
|
161
|
-
* @returns A method decorator that throttles method calls
|
|
162
|
-
*/
|
|
163
|
-
function throttle(delay) {
|
|
164
|
-
return decorator({
|
|
165
|
-
method(original, _target, _propertyKey) {
|
|
166
|
-
let lastCallTime = 0;
|
|
167
|
-
let timeoutId = null;
|
|
168
|
-
return function (...args) {
|
|
169
|
-
const now = Date.now();
|
|
170
|
-
// If enough time has passed since last call, execute immediately
|
|
171
|
-
if (now - lastCallTime >= delay) {
|
|
172
|
-
// Clear any pending timeout since we're executing now
|
|
173
|
-
if (timeoutId) {
|
|
174
|
-
clearTimeout(timeoutId);
|
|
175
|
-
timeoutId = null;
|
|
176
|
-
}
|
|
177
|
-
lastCallTime = now;
|
|
178
|
-
return original.apply(this, args);
|
|
179
|
-
}
|
|
180
|
-
// Otherwise, schedule execution for when the delay period ends
|
|
181
|
-
if (!timeoutId) {
|
|
182
|
-
const remainingTime = delay - (now - lastCallTime);
|
|
183
|
-
const scheduledArgs = [...args]; // Capture args at scheduling time
|
|
184
|
-
timeoutId = setTimeout(() => {
|
|
185
|
-
lastCallTime = Date.now();
|
|
186
|
-
original.apply(this, scheduledArgs);
|
|
187
|
-
timeoutId = null;
|
|
188
|
-
}, remainingTime);
|
|
189
|
-
}
|
|
190
|
-
};
|
|
191
|
-
},
|
|
192
|
-
});
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
export { cache, cached, debounce, deprecated, describe, isCached, throttle };
|
|
196
|
-
//# sourceMappingURL=std-decorators.esm.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"std-decorators.esm.js","sources":["../src/std-decorators.ts"],"sourcesContent":["import { decorator, type GenericClassDecorator } from './decorator'\n\n// In order to avoid async re-entrance, we could use zone.js or something like that.\nconst syncCalculating: { object: object; prop: PropertyKey }[] = []\n/**\n * Decorator that caches the result of a getter method and only recomputes when dependencies change\n * Prevents circular dependencies and provides automatic cache invalidation\n */\nexport const cached = decorator({\n\tgetter(original, _target, propertyKey) {\n\t\treturn function (this: any) {\n\t\t\tconst alreadyCalculating = syncCalculating.findIndex(\n\t\t\t\t(c) => c.object === this && c.prop === propertyKey\n\t\t\t)\n\t\t\tif (alreadyCalculating > -1)\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Circular dependency detected: ${syncCalculating\n\t\t\t\t\t\t.slice(alreadyCalculating)\n\t\t\t\t\t\t.map((c) => `${c.object.constructor.name}.${String(c.prop)}`)\n\t\t\t\t\t\t.join(' -> ')} -> again`\n\t\t\t\t)\n\t\t\tsyncCalculating.push({ object: this, prop: propertyKey })\n\t\t\ttry {\n\t\t\t\tconst rv = original.call(this)\n\t\t\t\tcache(this, propertyKey, rv)\n\t\t\t\treturn rv\n\t\t\t} finally {\n\t\t\t\tsyncCalculating.pop()\n\t\t\t}\n\t\t}\n\t},\n})\n\n/**\n * Checks if a property is cached (has a cached value)\n * @param object - The object to check\n * @param propertyKey - The property key to check\n * @returns True if the property has a cached value\n */\nexport function isCached(object: Object, propertyKey: PropertyKey) {\n\treturn !!Object.getOwnPropertyDescriptor(object, propertyKey)\n}\n\n/**\n * Caches a value for a property on an object\n * @param object - The object to cache the value on\n * @param propertyKey - The property key to cache\n * @param value - The value to cache\n */\nexport function cache(object: Object, propertyKey: PropertyKey, value: any) {\n\tObject.defineProperty(object, propertyKey, { value })\n}\n\n/**\n * Creates a decorator that modifies property descriptors for specified properties\n * @param descriptor - The descriptor properties to apply\n * @returns A class decorator that applies the descriptor to specified properties\n */\nexport function describe(descriptor: {\n\tenumerable?: boolean\n\tconfigurable?: boolean // Not modifiable once the property has been defined ?\n\twritable?: boolean\n}) {\n\treturn <T>(...properties: (keyof T)[]): GenericClassDecorator<T> =>\n\t\t(Base) => {\n\t\t\treturn class extends Base {\n\t\t\t\tconstructor(...args: any[]) {\n\t\t\t\t\tsuper(...args)\n\t\t\t\t\tfor (const key of properties) {\n\t\t\t\t\t\tObject.defineProperty(this, key, {\n\t\t\t\t\t\t\t...Object.getOwnPropertyDescriptor(this, key),\n\t\t\t\t\t\t\t...descriptor,\n\t\t\t\t\t\t})\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n}\n\n/**\n * Decorator that marks methods, properties, or classes as deprecated\n * Provides warning messages when deprecated items are used\n */\nexport const deprecated = Object.assign(\n\tdecorator({\n\t\tmethod(original, _target, propertyKey) {\n\t\t\treturn function (this: any, ...args: any[]) {\n\t\t\t\tdeprecated.warn(this, propertyKey)\n\t\t\t\treturn original.apply(this, args)\n\t\t\t}\n\t\t},\n\t\tgetter(original, _target, propertyKey) {\n\t\t\treturn function (this: any) {\n\t\t\t\tdeprecated.warn(this, propertyKey)\n\t\t\t\treturn original.call(this)\n\t\t\t}\n\t\t},\n\t\tsetter(original, _target, propertyKey) {\n\t\t\treturn function (this: any, value: any) {\n\t\t\t\tdeprecated.warn(this, propertyKey)\n\t\t\t\treturn original.call(this, value)\n\t\t\t}\n\t\t},\n\t\tclass(original) {\n\t\t\treturn class extends original {\n\t\t\t\tconstructor(...args: any[]) {\n\t\t\t\t\tsuper(...args)\n\t\t\t\t\tdeprecated.warn(this, 'constructor')\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t\tdefault(message: string) {\n\t\t\treturn decorator({\n\t\t\t\tmethod(original, _target, propertyKey) {\n\t\t\t\t\treturn function (this: any, ...args: any[]) {\n\t\t\t\t\t\tdeprecated.warn(this, propertyKey, message)\n\t\t\t\t\t\treturn original.apply(this, args)\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t\tgetter(original, _target, propertyKey) {\n\t\t\t\t\treturn function (this: any) {\n\t\t\t\t\t\tdeprecated.warn(this, propertyKey, message)\n\t\t\t\t\t\treturn original.call(this)\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t\tsetter(original, _target, propertyKey) {\n\t\t\t\t\treturn function (this: any, value: any) {\n\t\t\t\t\t\tdeprecated.warn(this, propertyKey, message)\n\t\t\t\t\t\treturn original.call(this, value)\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t\tclass(original) {\n\t\t\t\t\treturn class extends original {\n\t\t\t\t\t\tconstructor(...args: any[]) {\n\t\t\t\t\t\t\tsuper(...args)\n\t\t\t\t\t\t\tdeprecated.warn(this, 'constructor', message)\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t})\n\t\t},\n\t}),\n\t{\n\t\twarn: (target: any, propertyKey: PropertyKey, message?: string) => {\n\t\t\t// biome-ignore lint/suspicious/noConsole: To be overridden\n\t\t\tconsole.warn(\n\t\t\t\t`${target.constructor.name}.${String(propertyKey)} is deprecated${message ? `: ${message}` : ''}`\n\t\t\t)\n\t\t},\n\t}\n)\n\n/**\n * Creates a debounced method decorator that delays execution until after the delay period has passed\n * @param delay - The delay in milliseconds\n * @returns A method decorator that debounces method calls\n */\nexport function debounce(delay: number) {\n\treturn decorator({\n\t\tmethod(original, _target, _propertyKey) {\n\t\t\tlet timeoutId: ReturnType<typeof setTimeout> | null = null\n\n\t\t\treturn function (this: any, ...args: any[]) {\n\t\t\t\t// Clear existing timeout\n\t\t\t\tif (timeoutId) {\n\t\t\t\t\tclearTimeout(timeoutId)\n\t\t\t\t}\n\n\t\t\t\t// Set new timeout\n\t\t\t\ttimeoutId = setTimeout(() => {\n\t\t\t\t\toriginal.apply(this, args)\n\t\t\t\t\ttimeoutId = null\n\t\t\t\t}, delay)\n\t\t\t}\n\t\t},\n\t})\n}\n\n/**\n * Creates a throttled method decorator that limits execution to once per delay period\n * @param delay - The delay in milliseconds\n * @returns A method decorator that throttles method calls\n */\nexport function throttle(delay: number) {\n\treturn decorator({\n\t\tmethod(original, _target, _propertyKey) {\n\t\t\tlet lastCallTime = 0\n\t\t\tlet timeoutId: ReturnType<typeof setTimeout> | null = null\n\n\t\t\treturn function (this: any, ...args: any[]) {\n\t\t\t\tconst now = Date.now()\n\n\t\t\t\t// If enough time has passed since last call, execute immediately\n\t\t\t\tif (now - lastCallTime >= delay) {\n\t\t\t\t\t// Clear any pending timeout since we're executing now\n\t\t\t\t\tif (timeoutId) {\n\t\t\t\t\t\tclearTimeout(timeoutId)\n\t\t\t\t\t\ttimeoutId = null\n\t\t\t\t\t}\n\t\t\t\t\tlastCallTime = now\n\t\t\t\t\treturn original.apply(this, args)\n\t\t\t\t}\n\n\t\t\t\t// Otherwise, schedule execution for when the delay period ends\n\t\t\t\tif (!timeoutId) {\n\t\t\t\t\tconst remainingTime = delay - (now - lastCallTime)\n\t\t\t\t\tconst scheduledArgs = [...args] // Capture args at scheduling time\n\t\t\t\t\ttimeoutId = setTimeout(() => {\n\t\t\t\t\t\tlastCallTime = Date.now()\n\t\t\t\t\t\toriginal.apply(this, scheduledArgs)\n\t\t\t\t\t\ttimeoutId = null\n\t\t\t\t\t}, remainingTime)\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t})\n}\n"],"names":[],"mappings":";;AAEA;AACA,MAAM,eAAe,GAA4C,EAAE;AACnE;;;AAGG;AACI,MAAM,MAAM,GAAG,SAAS,CAAC;AAC/B,IAAA,MAAM,CAAC,QAAQ,EAAE,OAAO,EAAE,WAAW,EAAA;QACpC,OAAO,YAAA;YACN,MAAM,kBAAkB,GAAG,eAAe,CAAC,SAAS,CACnD,CAAC,CAAC,KAAK,CAAC,CAAC,MAAM,KAAK,IAAI,IAAI,CAAC,CAAC,IAAI,KAAK,WAAW,CAClD;YACD,IAAI,kBAAkB,GAAG,EAAE;AAC1B,gBAAA,MAAM,IAAI,KAAK,CACd,CAAA,8BAAA,EAAiC;qBAC/B,KAAK,CAAC,kBAAkB;qBACxB,GAAG,CAAC,CAAC,CAAC,KAAK,CAAA,EAAG,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,IAAI,IAAI,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAA,CAAE;AAC3D,qBAAA,IAAI,CAAC,MAAM,CAAC,CAAA,SAAA,CAAW,CACzB;AACF,YAAA,eAAe,C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CAAC,EAAE,aAAa,CAAC;gBAClB;AACD,YAAA,CAAC;QACF,CAAC;AACD,KAAA,CAAC;AACH;;;;"}
|
package/dist/std-decorators.js
DELETED
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var decorator = require('./chunks/decorator-BQ2eBTCj.js');
|
|
4
|
-
|
|
5
|
-
// In order to avoid async re-entrance, we could use zone.js or something like that.
|
|
6
|
-
const syncCalculating = [];
|
|
7
|
-
/**
|
|
8
|
-
* Decorator that caches the result of a getter method and only recomputes when dependencies change
|
|
9
|
-
* Prevents circular dependencies and provides automatic cache invalidation
|
|
10
|
-
*/
|
|
11
|
-
const cached = decorator.decorator({
|
|
12
|
-
getter(original, _target, propertyKey) {
|
|
13
|
-
return function () {
|
|
14
|
-
const alreadyCalculating = syncCalculating.findIndex((c) => c.object === this && c.prop === propertyKey);
|
|
15
|
-
if (alreadyCalculating > -1)
|
|
16
|
-
throw new Error(`Circular dependency detected: ${syncCalculating
|
|
17
|
-
.slice(alreadyCalculating)
|
|
18
|
-
.map((c) => `${c.object.constructor.name}.${String(c.prop)}`)
|
|
19
|
-
.join(' -> ')} -> again`);
|
|
20
|
-
syncCalculating.push({ object: this, prop: propertyKey });
|
|
21
|
-
try {
|
|
22
|
-
const rv = original.call(this);
|
|
23
|
-
cache(this, propertyKey, rv);
|
|
24
|
-
return rv;
|
|
25
|
-
}
|
|
26
|
-
finally {
|
|
27
|
-
syncCalculating.pop();
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
},
|
|
31
|
-
});
|
|
32
|
-
/**
|
|
33
|
-
* Checks if a property is cached (has a cached value)
|
|
34
|
-
* @param object - The object to check
|
|
35
|
-
* @param propertyKey - The property key to check
|
|
36
|
-
* @returns True if the property has a cached value
|
|
37
|
-
*/
|
|
38
|
-
function isCached(object, propertyKey) {
|
|
39
|
-
return !!Object.getOwnPropertyDescriptor(object, propertyKey);
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Caches a value for a property on an object
|
|
43
|
-
* @param object - The object to cache the value on
|
|
44
|
-
* @param propertyKey - The property key to cache
|
|
45
|
-
* @param value - The value to cache
|
|
46
|
-
*/
|
|
47
|
-
function cache(object, propertyKey, value) {
|
|
48
|
-
Object.defineProperty(object, propertyKey, { value });
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Creates a decorator that modifies property descriptors for specified properties
|
|
52
|
-
* @param descriptor - The descriptor properties to apply
|
|
53
|
-
* @returns A class decorator that applies the descriptor to specified properties
|
|
54
|
-
*/
|
|
55
|
-
function describe(descriptor) {
|
|
56
|
-
return (...properties) => (Base) => {
|
|
57
|
-
return class extends Base {
|
|
58
|
-
constructor(...args) {
|
|
59
|
-
super(...args);
|
|
60
|
-
for (const key of properties) {
|
|
61
|
-
Object.defineProperty(this, key, {
|
|
62
|
-
...Object.getOwnPropertyDescriptor(this, key),
|
|
63
|
-
...descriptor,
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Decorator that marks methods, properties, or classes as deprecated
|
|
72
|
-
* Provides warning messages when deprecated items are used
|
|
73
|
-
*/
|
|
74
|
-
const deprecated = Object.assign(decorator.decorator({
|
|
75
|
-
method(original, _target, propertyKey) {
|
|
76
|
-
return function (...args) {
|
|
77
|
-
deprecated.warn(this, propertyKey);
|
|
78
|
-
return original.apply(this, args);
|
|
79
|
-
};
|
|
80
|
-
},
|
|
81
|
-
getter(original, _target, propertyKey) {
|
|
82
|
-
return function () {
|
|
83
|
-
deprecated.warn(this, propertyKey);
|
|
84
|
-
return original.call(this);
|
|
85
|
-
};
|
|
86
|
-
},
|
|
87
|
-
setter(original, _target, propertyKey) {
|
|
88
|
-
return function (value) {
|
|
89
|
-
deprecated.warn(this, propertyKey);
|
|
90
|
-
return original.call(this, value);
|
|
91
|
-
};
|
|
92
|
-
},
|
|
93
|
-
class(original) {
|
|
94
|
-
return class extends original {
|
|
95
|
-
constructor(...args) {
|
|
96
|
-
super(...args);
|
|
97
|
-
deprecated.warn(this, 'constructor');
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
},
|
|
101
|
-
default(message) {
|
|
102
|
-
return decorator.decorator({
|
|
103
|
-
method(original, _target, propertyKey) {
|
|
104
|
-
return function (...args) {
|
|
105
|
-
deprecated.warn(this, propertyKey, message);
|
|
106
|
-
return original.apply(this, args);
|
|
107
|
-
};
|
|
108
|
-
},
|
|
109
|
-
getter(original, _target, propertyKey) {
|
|
110
|
-
return function () {
|
|
111
|
-
deprecated.warn(this, propertyKey, message);
|
|
112
|
-
return original.call(this);
|
|
113
|
-
};
|
|
114
|
-
},
|
|
115
|
-
setter(original, _target, propertyKey) {
|
|
116
|
-
return function (value) {
|
|
117
|
-
deprecated.warn(this, propertyKey, message);
|
|
118
|
-
return original.call(this, value);
|
|
119
|
-
};
|
|
120
|
-
},
|
|
121
|
-
class(original) {
|
|
122
|
-
return class extends original {
|
|
123
|
-
constructor(...args) {
|
|
124
|
-
super(...args);
|
|
125
|
-
deprecated.warn(this, 'constructor', message);
|
|
126
|
-
}
|
|
127
|
-
};
|
|
128
|
-
},
|
|
129
|
-
});
|
|
130
|
-
},
|
|
131
|
-
}), {
|
|
132
|
-
warn: (target, propertyKey, message) => {
|
|
133
|
-
// biome-ignore lint/suspicious/noConsole: To be overridden
|
|
134
|
-
console.warn(`${target.constructor.name}.${String(propertyKey)} is deprecated${message ? `: ${message}` : ''}`);
|
|
135
|
-
},
|
|
136
|
-
});
|
|
137
|
-
/**
|
|
138
|
-
* Creates a debounced method decorator that delays execution until after the delay period has passed
|
|
139
|
-
* @param delay - The delay in milliseconds
|
|
140
|
-
* @returns A method decorator that debounces method calls
|
|
141
|
-
*/
|
|
142
|
-
function debounce(delay) {
|
|
143
|
-
return decorator.decorator({
|
|
144
|
-
method(original, _target, _propertyKey) {
|
|
145
|
-
let timeoutId = null;
|
|
146
|
-
return function (...args) {
|
|
147
|
-
// Clear existing timeout
|
|
148
|
-
if (timeoutId) {
|
|
149
|
-
clearTimeout(timeoutId);
|
|
150
|
-
}
|
|
151
|
-
// Set new timeout
|
|
152
|
-
timeoutId = setTimeout(() => {
|
|
153
|
-
original.apply(this, args);
|
|
154
|
-
timeoutId = null;
|
|
155
|
-
}, delay);
|
|
156
|
-
};
|
|
157
|
-
},
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* Creates a throttled method decorator that limits execution to once per delay period
|
|
162
|
-
* @param delay - The delay in milliseconds
|
|
163
|
-
* @returns A method decorator that throttles method calls
|
|
164
|
-
*/
|
|
165
|
-
function throttle(delay) {
|
|
166
|
-
return decorator.decorator({
|
|
167
|
-
method(original, _target, _propertyKey) {
|
|
168
|
-
let lastCallTime = 0;
|
|
169
|
-
let timeoutId = null;
|
|
170
|
-
return function (...args) {
|
|
171
|
-
const now = Date.now();
|
|
172
|
-
// If enough time has passed since last call, execute immediately
|
|
173
|
-
if (now - lastCallTime >= delay) {
|
|
174
|
-
// Clear any pending timeout since we're executing now
|
|
175
|
-
if (timeoutId) {
|
|
176
|
-
clearTimeout(timeoutId);
|
|
177
|
-
timeoutId = null;
|
|
178
|
-
}
|
|
179
|
-
lastCallTime = now;
|
|
180
|
-
return original.apply(this, args);
|
|
181
|
-
}
|
|
182
|
-
// Otherwise, schedule execution for when the delay period ends
|
|
183
|
-
if (!timeoutId) {
|
|
184
|
-
const remainingTime = delay - (now - lastCallTime);
|
|
185
|
-
const scheduledArgs = [...args]; // Capture args at scheduling time
|
|
186
|
-
timeoutId = setTimeout(() => {
|
|
187
|
-
lastCallTime = Date.now();
|
|
188
|
-
original.apply(this, scheduledArgs);
|
|
189
|
-
timeoutId = null;
|
|
190
|
-
}, remainingTime);
|
|
191
|
-
}
|
|
192
|
-
};
|
|
193
|
-
},
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
exports.cache = cache;
|
|
198
|
-
exports.cached = cached;
|
|
199
|
-
exports.debounce = debounce;
|
|
200
|
-
exports.deprecated = deprecated;
|
|
201
|
-
exports.describe = describe;
|
|
202
|
-
exports.isCached = isCached;
|
|
203
|
-
exports.throttle = throttle;
|
|
204
|
-
//# sourceMappingURL=std-decorators.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"std-decorators.js","sources":["../src/std-decorators.ts"],"sourcesContent":["import { decorator, type GenericClassDecorator } from './decorator'\n\n// In order to avoid async re-entrance, we could use zone.js or something like that.\nconst syncCalculating: { object: object; prop: PropertyKey }[] = []\n/**\n * Decorator that caches the result of a getter method and only recomputes when dependencies change\n * Prevents circular dependencies and provides automatic cache invalidation\n */\nexport const cached = decorator({\n\tgetter(original, _target, propertyKey) {\n\t\treturn function (this: any) {\n\t\t\tconst alreadyCalculating = syncCalculating.findIndex(\n\t\t\t\t(c) => c.object === this && c.prop === propertyKey\n\t\t\t)\n\t\t\tif (alreadyCalculating > -1)\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Circular dependency detected: ${syncCalculating\n\t\t\t\t\t\t.slice(alreadyCalculating)\n\t\t\t\t\t\t.map((c) => `${c.object.constructor.name}.${String(c.prop)}`)\n\t\t\t\t\t\t.join(' -> ')} -> again`\n\t\t\t\t)\n\t\t\tsyncCalculating.push({ object: this, prop: propertyKey })\n\t\t\ttry {\n\t\t\t\tconst rv = original.call(this)\n\t\t\t\tcache(this, propertyKey, rv)\n\t\t\t\treturn rv\n\t\t\t} finally {\n\t\t\t\tsyncCalculating.pop()\n\t\t\t}\n\t\t}\n\t},\n})\n\n/**\n * Checks if a property is cached (has a cached value)\n * @param object - The object to check\n * @param propertyKey - The property key to check\n * @returns True if the property has a cached value\n */\nexport function isCached(object: Object, propertyKey: PropertyKey) {\n\treturn !!Object.getOwnPropertyDescriptor(object, propertyKey)\n}\n\n/**\n * Caches a value for a property on an object\n * @param object - The object to cache the value on\n * @param propertyKey - The property key to cache\n * @param value - The value to cache\n */\nexport function cache(object: Object, propertyKey: PropertyKey, value: any) {\n\tObject.defineProperty(object, propertyKey, { value })\n}\n\n/**\n * Creates a decorator that modifies property descriptors for specified properties\n * @param descriptor - The descriptor properties to apply\n * @returns A class decorator that applies the descriptor to specified properties\n */\nexport function describe(descriptor: {\n\tenumerable?: boolean\n\tconfigurable?: boolean // Not modifiable once the property has been defined ?\n\twritable?: boolean\n}) {\n\treturn <T>(...properties: (keyof T)[]): GenericClassDecorator<T> =>\n\t\t(Base) => {\n\t\t\treturn class extends Base {\n\t\t\t\tconstructor(...args: any[]) {\n\t\t\t\t\tsuper(...args)\n\t\t\t\t\tfor (const key of properties) {\n\t\t\t\t\t\tObject.defineProperty(this, key, {\n\t\t\t\t\t\t\t...Object.getOwnPropertyDescriptor(this, key),\n\t\t\t\t\t\t\t...descriptor,\n\t\t\t\t\t\t})\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n}\n\n/**\n * Decorator that marks methods, properties, or classes as deprecated\n * Provides warning messages when deprecated items are used\n */\nexport const deprecated = Object.assign(\n\tdecorator({\n\t\tmethod(original, _target, propertyKey) {\n\t\t\treturn function (this: any, ...args: any[]) {\n\t\t\t\tdeprecated.warn(this, propertyKey)\n\t\t\t\treturn original.apply(this, args)\n\t\t\t}\n\t\t},\n\t\tgetter(original, _target, propertyKey) {\n\t\t\treturn function (this: any) {\n\t\t\t\tdeprecated.warn(this, propertyKey)\n\t\t\t\treturn original.call(this)\n\t\t\t}\n\t\t},\n\t\tsetter(original, _target, propertyKey) {\n\t\t\treturn function (this: any, value: any) {\n\t\t\t\tdeprecated.warn(this, propertyKey)\n\t\t\t\treturn original.call(this, value)\n\t\t\t}\n\t\t},\n\t\tclass(original) {\n\t\t\treturn class extends original {\n\t\t\t\tconstructor(...args: any[]) {\n\t\t\t\t\tsuper(...args)\n\t\t\t\t\tdeprecated.warn(this, 'constructor')\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t\tdefault(message: string) {\n\t\t\treturn decorator({\n\t\t\t\tmethod(original, _target, propertyKey) {\n\t\t\t\t\treturn function (this: any, ...args: any[]) {\n\t\t\t\t\t\tdeprecated.warn(this, propertyKey, message)\n\t\t\t\t\t\treturn original.apply(this, args)\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t\tgetter(original, _target, propertyKey) {\n\t\t\t\t\treturn function (this: any) {\n\t\t\t\t\t\tdeprecated.warn(this, propertyKey, message)\n\t\t\t\t\t\treturn original.call(this)\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t\tsetter(original, _target, propertyKey) {\n\t\t\t\t\treturn function (this: any, value: any) {\n\t\t\t\t\t\tdeprecated.warn(this, propertyKey, message)\n\t\t\t\t\t\treturn original.call(this, value)\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t\tclass(original) {\n\t\t\t\t\treturn class extends original {\n\t\t\t\t\t\tconstructor(...args: any[]) {\n\t\t\t\t\t\t\tsuper(...args)\n\t\t\t\t\t\t\tdeprecated.warn(this, 'constructor', message)\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t})\n\t\t},\n\t}),\n\t{\n\t\twarn: (target: any, propertyKey: PropertyKey, message?: string) => {\n\t\t\t// biome-ignore lint/suspicious/noConsole: To be overridden\n\t\t\tconsole.warn(\n\t\t\t\t`${target.constructor.name}.${String(propertyKey)} is deprecated${message ? `: ${message}` : ''}`\n\t\t\t)\n\t\t},\n\t}\n)\n\n/**\n * Creates a debounced method decorator that delays execution until after the delay period has passed\n * @param delay - The delay in milliseconds\n * @returns A method decorator that debounces method calls\n */\nexport function debounce(delay: number) {\n\treturn decorator({\n\t\tmethod(original, _target, _propertyKey) {\n\t\t\tlet timeoutId: ReturnType<typeof setTimeout> | null = null\n\n\t\t\treturn function (this: any, ...args: any[]) {\n\t\t\t\t// Clear existing timeout\n\t\t\t\tif (timeoutId) {\n\t\t\t\t\tclearTimeout(timeoutId)\n\t\t\t\t}\n\n\t\t\t\t// Set new timeout\n\t\t\t\ttimeoutId = setTimeout(() => {\n\t\t\t\t\toriginal.apply(this, args)\n\t\t\t\t\ttimeoutId = null\n\t\t\t\t}, delay)\n\t\t\t}\n\t\t},\n\t})\n}\n\n/**\n * Creates a throttled method decorator that limits execution to once per delay period\n * @param delay - The delay in milliseconds\n * @returns A method decorator that throttles method calls\n */\nexport function throttle(delay: number) {\n\treturn decorator({\n\t\tmethod(original, _target, _propertyKey) {\n\t\t\tlet lastCallTime = 0\n\t\t\tlet timeoutId: ReturnType<typeof setTimeout> | null = null\n\n\t\t\treturn function (this: any, ...args: any[]) {\n\t\t\t\tconst now = Date.now()\n\n\t\t\t\t// If enough time has passed since last call, execute immediately\n\t\t\t\tif (now - lastCallTime >= delay) {\n\t\t\t\t\t// Clear any pending timeout since we're executing now\n\t\t\t\t\tif (timeoutId) {\n\t\t\t\t\t\tclearTimeout(timeoutId)\n\t\t\t\t\t\ttimeoutId = null\n\t\t\t\t\t}\n\t\t\t\t\tlastCallTime = now\n\t\t\t\t\treturn original.apply(this, args)\n\t\t\t\t}\n\n\t\t\t\t// Otherwise, schedule execution for when the delay period ends\n\t\t\t\tif (!timeoutId) {\n\t\t\t\t\tconst remainingTime = delay - (now - lastCallTime)\n\t\t\t\t\tconst scheduledArgs = [...args] // Capture args at scheduling time\n\t\t\t\t\ttimeoutId = setTimeout(() => {\n\t\t\t\t\t\tlastCallTime = Date.now()\n\t\t\t\t\t\toriginal.apply(this, scheduledArgs)\n\t\t\t\t\t\ttimeoutId = null\n\t\t\t\t\t}, remainingTime)\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t})\n}\n"],"names":["decorator"],"mappings":";;;;AAEA;AACA,MAAM,eAAe,GAA4C,EAAE;AACnE;;;AAGG;AACI,MAAM,MAAM,GAAGA,mBAAS,CAAC;AAC/B,IAAA,MAAM,CAAC,QAAQ,EAAE,OAAO,EAAE,WAAW,EAAA;QACpC,OAAO,YAAA;YACN,MAAM,kBAAkB,G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S,CAAC;wBACvB,SAAS,GAAG,IAAI;oBACjB;oBACA,YAAY,GAAG,GAAG;oBAClB,OAAO,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC;gBAClC;;gBAGA,IAAI,CAAC,SAAS,EAAE;oBACf,MAAM,aAAa,GAAG,KAAK,IAAI,GAAG,GAAG,YAAY,CAAC;oBAClD,MAAM,aAAa,GAAG,CAAC,GAAG,IAAI,CAAC,CAAA;AAC/B,oBAAA,SAAS,GAAG,UAAU,CAAC,MAAK;AAC3B,wBAAA,YAAY,GAAG,IAAI,CAAC,GAAG,EAAE;AACzB,wBAAA,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,aAAa,CAAC;wBACnC,SAAS,GAAG,IAAI;oBACjB,CAAC,EAAE,aAAa,CAAC;gBAClB;AACD,YAAA,CAAC;QACF,CAAC;AACD,KAAA,CAAC;AACH;;;;;;;;;;"}
|
package/src/reactive/mapped.ts
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
import { Indexable } from '../indexable'
|
|
2
|
-
import { native, ReactiveBaseArray } from './array'
|
|
3
|
-
import { touched, touched1 } from './change'
|
|
4
|
-
import { effect, untracked } from './effects'
|
|
5
|
-
import { cleanedBy } from './interface'
|
|
6
|
-
import { reactive } from './proxy'
|
|
7
|
-
import { dependant } from './tracking'
|
|
8
|
-
import { prototypeForwarding, type ScopedCallback } from './types'
|
|
9
|
-
|
|
10
|
-
// TODO: Lazy reactivity ?
|
|
11
|
-
export class ReadOnlyError extends Error { }
|
|
12
|
-
/**
|
|
13
|
-
* Reactive wrapper around JavaScript's Array class with full array method support
|
|
14
|
-
* Tracks length changes, individual index operations, and collection-wide operations
|
|
15
|
-
*/
|
|
16
|
-
class ReactiveReadOnlyArrayClass extends Indexable(ReactiveBaseArray, {
|
|
17
|
-
get(i: number): any {
|
|
18
|
-
dependant(this, i)
|
|
19
|
-
return reactive(this[native][i])
|
|
20
|
-
},
|
|
21
|
-
set(i: number, _value: any) {
|
|
22
|
-
throw new ReadOnlyError(`Setting index ${i} on a read-only array`)
|
|
23
|
-
},
|
|
24
|
-
getLength() {
|
|
25
|
-
dependant(this, 'length')
|
|
26
|
-
return this[native].length
|
|
27
|
-
},
|
|
28
|
-
setLength(value: number) {
|
|
29
|
-
throw new ReadOnlyError(`Setting length to ${value} on a read-only array`)
|
|
30
|
-
},
|
|
31
|
-
}) {
|
|
32
|
-
constructor(original: any[]) {
|
|
33
|
-
super()
|
|
34
|
-
Object.defineProperties(this, {
|
|
35
|
-
// We have to make it double, as [native] must be `unique symbol` - impossible through import
|
|
36
|
-
[native]: { value: original },
|
|
37
|
-
[prototypeForwarding]: { value: original },
|
|
38
|
-
})
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
push(..._items: any[]) {
|
|
42
|
-
throw new ReadOnlyError(`Pushing items to a read-only array`)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
pop() {
|
|
46
|
-
throw new ReadOnlyError(`Popping from a read-only array`)
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
shift() {
|
|
50
|
-
throw new ReadOnlyError(`Shifting from a read-only array`)
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
unshift(..._items: any[]) {
|
|
54
|
-
throw new ReadOnlyError(`Unshifting items to a read-only array`)
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
splice(_start: number, _deleteCount?: number, ..._items: any[]) {
|
|
58
|
-
throw new ReadOnlyError(`Splice from a read-only array`)
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
reverse() {
|
|
62
|
-
throw new ReadOnlyError(`Reversing a read-only array`)
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
sort(_compareFn?: (a: any, b: any) => number) {
|
|
66
|
-
throw new ReadOnlyError(`Sorting a read-only array`)
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
fill(_value: any, _start?: number, _end?: number) {
|
|
70
|
-
throw new ReadOnlyError(`Filling a read-only array`)
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
copyWithin(_target: number, _start: number, _end?: number) {
|
|
74
|
-
throw new ReadOnlyError(`Copying within a read-only array`)
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export const ReactiveReadOnlyArray = reactive(ReactiveReadOnlyArrayClass)
|
|
79
|
-
export type ReactiveReadOnlyArray<T> = readonly T[]
|
|
80
|
-
export function mapped<T, U>(
|
|
81
|
-
inputs: readonly T[],
|
|
82
|
-
compute: (input: T, index: number, output: U[]) => U,
|
|
83
|
-
resize?: (newLength: number, oldLength: number) => void
|
|
84
|
-
): readonly U[] {
|
|
85
|
-
const result: U[] = []
|
|
86
|
-
const resultReactive = new ReactiveReadOnlyArray(result)
|
|
87
|
-
const cleanups: ScopedCallback[] = []
|
|
88
|
-
function input(index: number) {
|
|
89
|
-
return effect(function computedIndexedMapInputEffect() {
|
|
90
|
-
result[index] = compute(inputs[index], index, resultReactive)
|
|
91
|
-
touched1(resultReactive, { type: 'set', prop: index }, index)
|
|
92
|
-
})
|
|
93
|
-
}
|
|
94
|
-
const cleanupLength = effect(function computedMapLengthEffect({ ascend }) {
|
|
95
|
-
const length = inputs.length
|
|
96
|
-
const resultLength = untracked(() => result.length)
|
|
97
|
-
resize?.(length, resultLength)
|
|
98
|
-
touched1(resultReactive, { type: 'set', prop: 'length' }, 'length')
|
|
99
|
-
if (length < resultLength) {
|
|
100
|
-
const toCleanup = cleanups.splice(length)
|
|
101
|
-
for (const cleanup of toCleanup) cleanup()
|
|
102
|
-
result.length = length
|
|
103
|
-
} else if (length > resultLength)
|
|
104
|
-
// the input effects will be registered as the call's children, so they will remain not cleaned with this effect on length
|
|
105
|
-
ascend(function computedMapNewElements() {
|
|
106
|
-
for (let i = resultLength; i < length; i++) cleanups.push(input(i))
|
|
107
|
-
})
|
|
108
|
-
})
|
|
109
|
-
return cleanedBy(resultReactive, () => {
|
|
110
|
-
for (const cleanup of cleanups) cleanup()
|
|
111
|
-
cleanups.length = 0
|
|
112
|
-
cleanupLength()
|
|
113
|
-
})
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export function reduced<T, U, R extends object = any>(
|
|
117
|
-
inputs: readonly T[],
|
|
118
|
-
compute: (input: T, factor: R) => readonly U[]
|
|
119
|
-
): readonly U[] {
|
|
120
|
-
const result: U[] = []
|
|
121
|
-
const resultReactive = new ReactiveReadOnlyArray(result)
|
|
122
|
-
const cleanupFactor = effect(function computedReducedFactorEffect() {
|
|
123
|
-
const factor: R = {} as R
|
|
124
|
-
result.length = 0
|
|
125
|
-
for (const input of inputs) result.push(...compute(input, factor))
|
|
126
|
-
touched(resultReactive, { type: 'invalidate', prop: 'reduced' })
|
|
127
|
-
})
|
|
128
|
-
return cleanedBy(resultReactive, cleanupFactor)
|
|
129
|
-
}
|