@oscarpalmer/mora 0.20.0 → 0.22.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 (44) hide show
  1. package/dist/batch.js +11 -24
  2. package/dist/effect.js +17 -26
  3. package/dist/helpers/is.js +14 -21
  4. package/dist/helpers/proxy.js +34 -47
  5. package/dist/helpers/value.js +20 -46
  6. package/dist/index.js +4 -17
  7. package/dist/mora.full.js +8 -2
  8. package/dist/subscription.js +20 -28
  9. package/dist/value/array.js +94 -173
  10. package/dist/value/computed.js +34 -56
  11. package/dist/value/reactive.js +29 -53
  12. package/dist/value/signal.js +20 -32
  13. package/dist/value/store.js +30 -59
  14. package/package.json +12 -18
  15. package/src/helpers/value.ts +1 -1
  16. package/src/index.ts +0 -1
  17. package/src/value/array.ts +13 -3
  18. package/types/value/array.d.ts +1 -0
  19. package/dist/batch.cjs +0 -32
  20. package/dist/effect.cjs +0 -30
  21. package/dist/helpers/is.cjs +0 -40
  22. package/dist/helpers/proxy.cjs +0 -56
  23. package/dist/helpers/value.cjs +0 -54
  24. package/dist/index.cjs +0 -21
  25. package/dist/node_modules/@oscarpalmer/atoms/dist/internal/is.cjs +0 -17
  26. package/dist/node_modules/@oscarpalmer/atoms/dist/internal/is.js +0 -17
  27. package/dist/subscription.cjs +0 -32
  28. package/dist/value/array.cjs +0 -181
  29. package/dist/value/computed.cjs +0 -60
  30. package/dist/value/reactive.cjs +0 -55
  31. package/dist/value/signal.cjs +0 -36
  32. package/dist/value/store.cjs +0 -63
  33. package/types/batch.d.cts +0 -79
  34. package/types/effect.d.cts +0 -16
  35. package/types/helpers/is.d.cts +0 -258
  36. package/types/helpers/proxy.d.cts +0 -243
  37. package/types/helpers/value.d.cts +0 -71
  38. package/types/index.d.cts +0 -280
  39. package/types/subscription.d.cts +0 -71
  40. package/types/value/array.d.cts +0 -160
  41. package/types/value/computed.d.cts +0 -81
  42. package/types/value/reactive.d.cts +0 -68
  43. package/types/value/signal.d.cts +0 -87
  44. package/types/value/store.d.cts +0 -136
package/package.json CHANGED
@@ -8,35 +8,30 @@
8
8
  },
9
9
  "description": "Signals and stuff…",
10
10
  "devDependencies": {
11
- "@biomejs/biome": "^1.9",
11
+ "@biomejs/biome": "^2.2",
12
12
  "@rollup/plugin-node-resolve": "^16",
13
13
  "@rollup/plugin-typescript": "^12.1",
14
- "@types/node": "^24",
14
+ "@types/node": "^24.3",
15
15
  "@vitest/coverage-istanbul": "^3.2",
16
16
  "dts-bundle-generator": "^9.5",
17
17
  "glob": "^11",
18
18
  "jsdom": "^26.1",
19
+ "rollup": "^4.50",
19
20
  "tslib": "^2.8",
20
- "typescript": "^5.8",
21
- "vite": "^6.3",
21
+ "typescript": "^5.9",
22
+ "vite": "npm:rolldown-vite@latest",
22
23
  "vitest": "^3.2"
23
24
  },
24
25
  "exports": {
26
+ "./package.json": "./package.json",
25
27
  ".": {
26
- "import": {
27
- "types": "./types/index.d.ts",
28
- "default": "./dist/index.js"
29
- },
30
- "require": {
31
- "types": "./types/index.d.cts",
32
- "default": "./dist/index.cjs"
33
- }
28
+ "types": "./types/index.d.ts",
29
+ "default": "./dist/index.js"
34
30
  }
35
31
  },
36
32
  "files": ["dist", "src", "types"],
37
33
  "keywords": ["signal", "signals", "reactive"],
38
34
  "license": "MIT",
39
- "main": "./dist/index.cjs",
40
35
  "module": "./dist/index.js",
41
36
  "name": "@oscarpalmer/mora",
42
37
  "repository": {
@@ -44,15 +39,14 @@
44
39
  "url": "git+https://github.com/oscarpalmer/mora.git"
45
40
  },
46
41
  "scripts": {
47
- "build": "npm run clean && npm run build:js && npm run rollup && npm run types",
48
- "build:js": "npx vite build",
42
+ "build": "npm run clean && npx vite build && npm run rollup:build && npx tsc",
49
43
  "clean": "rm -rf ./dist && rm -rf ./types && rm -f ./tsconfig.tsbuildinfo",
50
- "rollup": "npx rollup -c",
44
+ "rollup:build": "npx rollup -c",
45
+ "rollup:watch": "npx rollup -c --watch",
51
46
  "test": "npx vitest --coverage",
52
- "types": "npx tsc && npx dts-bundle-generator --config ./dts.config.cts --silent",
53
47
  "watch": "npx vite build --watch"
54
48
  },
55
49
  "type": "module",
56
50
  "types": "./types/index.d.ts",
57
- "version": "0.20.0"
51
+ "version": "0.22.0"
58
52
  }
@@ -1,6 +1,6 @@
1
1
  import {batchDepth, batchedHandlers, flushHandlers} from '../batch';
2
2
  import {activeEffect} from '../effect';
3
- import {type InternalComputed, activeComputed} from '../value/computed';
3
+ import {activeComputed, type InternalComputed} from '../value/computed';
4
4
  import type {ReactiveState} from '../value/reactive';
5
5
 
6
6
  export function emitValue<Value>(state: ReactiveState<Value, never>): void {
package/src/index.ts CHANGED
@@ -12,4 +12,3 @@ export {computed, type Computed} from './value/computed';
12
12
  export type {Reactive} from './value/reactive';
13
13
  export {signal, type Signal} from './value/signal';
14
14
  export {store, type Store} from './value/store';
15
-
@@ -1,4 +1,4 @@
1
- import type {GenericCallback} from '@oscarpalmer/atoms';
1
+ import type {GenericCallback} from '@oscarpalmer/atoms/models';
2
2
  import {arrayName} from '../helpers/is';
3
3
  import {getReactiveValueInProxy, setValueInProxy} from '../helpers/proxy';
4
4
  import {emitValue, equalArrays, getValue} from '../helpers/value';
@@ -112,13 +112,23 @@ export class ReactiveArray<Item> extends Reactive<Item[], Item> {
112
112
  */
113
113
  peek(): Item[];
114
114
 
115
+ peek(index: number): Item | undefined;
116
+
115
117
  /**
116
118
  * Get the length of the array _(without reactivity)_
117
119
  */
118
120
  peek(length: true): number;
119
121
 
120
- peek(length?: unknown): Item[] | number {
121
- return length === true ? this.#size.peek() : [...this.state.value];
122
+ peek(value?: unknown): unknown {
123
+ if (value === true) {
124
+ return this.#size.peek();
125
+ }
126
+
127
+ if (typeof value === 'number') {
128
+ return this.state.value.at(value);
129
+ }
130
+
131
+ return [...this.state.value];
122
132
  }
123
133
 
124
134
  /**
@@ -40,6 +40,7 @@ export declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
40
40
  * @inheritdoc
41
41
  */
42
42
  peek(): Item[];
43
+ peek(index: number): Item | undefined;
43
44
  /**
44
45
  * Get the length of the array _(without reactivity)_
45
46
  */
package/dist/batch.cjs DELETED
@@ -1,32 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const effect = require("./effect.cjs");
4
- const helpers_is = require("./helpers/is.cjs");
5
- function flushHandlers() {
6
- while (exports.batchDepth === 0 && batchedHandlers.size > 0) {
7
- const handlers = [...batchedHandlers];
8
- batchedHandlers.clear();
9
- for (const handler of handlers) {
10
- if (helpers_is.isEffect(handler)) {
11
- effect.runEffect(handler);
12
- } else {
13
- handler.callback(handler.state.value);
14
- }
15
- }
16
- }
17
- }
18
- function startBatch() {
19
- exports.batchDepth += 1;
20
- }
21
- function stopBatch() {
22
- if (exports.batchDepth > 0) {
23
- exports.batchDepth -= 1;
24
- }
25
- flushHandlers();
26
- }
27
- const batchedHandlers = /* @__PURE__ */ new Set();
28
- exports.batchDepth = 0;
29
- exports.batchedHandlers = batchedHandlers;
30
- exports.flushHandlers = flushHandlers;
31
- exports.startBatch = startBatch;
32
- exports.stopBatch = stopBatch;
package/dist/effect.cjs DELETED
@@ -1,30 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const helpers_is = require("./helpers/is.cjs");
4
- class Effect {
5
- constructor(callback) {
6
- Object.defineProperty(this, "$mora", {
7
- value: helpers_is.effectName
8
- });
9
- this.state = {
10
- callback
11
- };
12
- runEffect(this);
13
- }
14
- }
15
- function runEffect(effect2) {
16
- const previousEffect = exports.activeEffect;
17
- exports.activeEffect = effect2;
18
- try {
19
- effect2.state.callback();
20
- } finally {
21
- exports.activeEffect = previousEffect;
22
- }
23
- }
24
- function effect(callback) {
25
- return new Effect(callback);
26
- }
27
- exports.activeEffect = void 0;
28
- exports.Effect = Effect;
29
- exports.effect = effect;
30
- exports.runEffect = runEffect;
@@ -1,40 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- function isArray(value) {
4
- return isMora(value, arrayName);
5
- }
6
- function isComputed(value) {
7
- return isMora(value, computedName);
8
- }
9
- function isEffect(value) {
10
- return isMora(value, effectName);
11
- }
12
- function isMora(value, name) {
13
- return typeof value === "object" && value != null && "$mora" in value && (typeof name === "string" ? value.$mora === name : name.has(value.$mora));
14
- }
15
- function isReactive(value) {
16
- return isMora(value, reactiveNames);
17
- }
18
- function isSignal(value) {
19
- return isMora(value, signalName);
20
- }
21
- function isStore(value) {
22
- return isMora(value, storeName);
23
- }
24
- const arrayName = "array";
25
- const computedName = "computed";
26
- const effectName = "effect";
27
- const signalName = "signal";
28
- const storeName = "store";
29
- const reactiveNames = /* @__PURE__ */ new Set([arrayName, computedName, signalName, storeName]);
30
- exports.arrayName = arrayName;
31
- exports.computedName = computedName;
32
- exports.effectName = effectName;
33
- exports.isArray = isArray;
34
- exports.isComputed = isComputed;
35
- exports.isEffect = isEffect;
36
- exports.isReactive = isReactive;
37
- exports.isSignal = isSignal;
38
- exports.isStore = isStore;
39
- exports.signalName = signalName;
40
- exports.storeName = storeName;
@@ -1,56 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const batch = require("../batch.cjs");
4
- const value_computed = require("../value/computed.cjs");
5
- const helpers_value = require("./value.cjs");
6
- function getReactiveValueInProxy(reactive, mapped, key, isArray) {
7
- let item = mapped.get(key);
8
- if (item == null) {
9
- item = value_computed.computed(() => {
10
- const value = reactive.get();
11
- return isArray ? value.at(key) : value[key];
12
- });
13
- mapped.set(key, item);
14
- }
15
- return item;
16
- }
17
- function setProxyValue(proxy, value) {
18
- batch.startBatch();
19
- const proxyKeys = Object.keys(proxy);
20
- const valueKeys = Object.keys(value);
21
- let { length } = proxyKeys;
22
- for (let index = 0; index < length; index += 1) {
23
- const key = proxyKeys[index];
24
- proxy[key] = valueKeys.includes(key) ? value[key] : void 0;
25
- }
26
- length = valueKeys.length;
27
- for (let index = 0; index < length; index += 1) {
28
- const key = valueKeys[index];
29
- if (!proxyKeys.includes(key)) {
30
- const keyedValue = value[key];
31
- proxy[key] = keyedValue;
32
- }
33
- }
34
- batch.stopBatch();
35
- }
36
- function setValueInProxy(target, property, value, state, isArray, length) {
37
- if (isArray) {
38
- const isIndex = !Number.isNaN(Number(property));
39
- const isLength = property === "length";
40
- if (!isIndex && !isLength) {
41
- return Reflect.set(target, property, value);
42
- }
43
- }
44
- const previous = Reflect.get(target, property);
45
- if (!state.equal(previous, value)) {
46
- Reflect.set(target, property, value);
47
- helpers_value.emitValue(state);
48
- if (isArray) {
49
- length == null ? void 0 : length.set(target.length);
50
- }
51
- }
52
- return true;
53
- }
54
- exports.getReactiveValueInProxy = getReactiveValueInProxy;
55
- exports.setProxyValue = setProxyValue;
56
- exports.setValueInProxy = setValueInProxy;
@@ -1,54 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const batch = require("../batch.cjs");
4
- const effect = require("../effect.cjs");
5
- const value_computed = require("../value/computed.cjs");
6
- function emitValue(state) {
7
- for (const computed of state.computeds) {
8
- computed.effect.dirty = true;
9
- }
10
- for (const effect2 of state.effects) {
11
- batch.batchedHandlers.add(effect2);
12
- }
13
- for (const [, subscription] of state.subscriptions) {
14
- batch.batchedHandlers.add(subscription);
15
- }
16
- if (batch.batchDepth === 0) {
17
- batch.flushHandlers();
18
- }
19
- }
20
- function equalArrays(state, first, second) {
21
- let { length } = first;
22
- if (length !== second.length) {
23
- return false;
24
- }
25
- let offset = 0;
26
- if (length >= 100) {
27
- offset = Math.round(length / 10);
28
- offset = offset > 25 ? 25 : offset;
29
- for (let index = 0; index < offset; index += 1) {
30
- if (!state.equal(first[index], second[index])) {
31
- return false;
32
- }
33
- }
34
- }
35
- length -= offset;
36
- for (let index = offset; index < length; index += 1) {
37
- if (!state.equal(first[index], second[index])) {
38
- return false;
39
- }
40
- }
41
- return true;
42
- }
43
- function getValue(state) {
44
- if (value_computed.activeComputed != null) {
45
- state.computeds.add(value_computed.activeComputed);
46
- }
47
- if (effect.activeEffect != null) {
48
- state.effects.add(effect.activeEffect);
49
- }
50
- return state.value;
51
- }
52
- exports.emitValue = emitValue;
53
- exports.equalArrays = equalArrays;
54
- exports.getValue = getValue;
package/dist/index.cjs DELETED
@@ -1,21 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const batch = require("./batch.cjs");
4
- const effect = require("./effect.cjs");
5
- const helpers_is = require("./helpers/is.cjs");
6
- const value_array = require("./value/array.cjs");
7
- const value_computed = require("./value/computed.cjs");
8
- const value_signal = require("./value/signal.cjs");
9
- const value_store = require("./value/store.cjs");
10
- exports.startBatch = batch.startBatch;
11
- exports.stopBatch = batch.stopBatch;
12
- exports.effect = effect.effect;
13
- exports.isArray = helpers_is.isArray;
14
- exports.isComputed = helpers_is.isComputed;
15
- exports.isEffect = helpers_is.isEffect;
16
- exports.isReactive = helpers_is.isReactive;
17
- exports.isSignal = helpers_is.isSignal;
18
- exports.array = value_array.array;
19
- exports.computed = value_computed.computed;
20
- exports.signal = value_signal.signal;
21
- exports.store = value_store.store;
@@ -1,17 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- function isKey(value) {
4
- return typeof value === "number" || typeof value === "string";
5
- }
6
- function isPlainObject(value) {
7
- if (value === null || typeof value !== "object") {
8
- return false;
9
- }
10
- if (Symbol.toStringTag in value || Symbol.iterator in value) {
11
- return false;
12
- }
13
- const prototype = Object.getPrototypeOf(value);
14
- return prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null;
15
- }
16
- exports.isKey = isKey;
17
- exports.isPlainObject = isPlainObject;
@@ -1,17 +0,0 @@
1
- function isKey(value) {
2
- return typeof value === "number" || typeof value === "string";
3
- }
4
- function isPlainObject(value) {
5
- if (value === null || typeof value !== "object") {
6
- return false;
7
- }
8
- if (Symbol.toStringTag in value || Symbol.iterator in value) {
9
- return false;
10
- }
11
- const prototype = Object.getPrototypeOf(value);
12
- return prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null;
13
- }
14
- export {
15
- isKey,
16
- isPlainObject
17
- };
@@ -1,32 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- class Subscription {
4
- constructor(state, callback) {
5
- this.state = state;
6
- this.callback = callback;
7
- callback(state.value);
8
- }
9
- }
10
- function noop() {
11
- }
12
- function subscribe(state, callback) {
13
- if (typeof callback !== "function" || state.subscriptions.has(callback)) {
14
- return noop;
15
- }
16
- state.subscriptions.set(callback, new Subscription(state, callback));
17
- return () => {
18
- unsubscribe(state, callback);
19
- };
20
- }
21
- function unsubscribe(state, callback) {
22
- const subscription = state.subscriptions.get(callback);
23
- state.subscriptions.delete(callback);
24
- if (subscription != null) {
25
- subscription.callback = noop;
26
- subscription.state = void 0;
27
- }
28
- }
29
- exports.Subscription = Subscription;
30
- exports.noop = noop;
31
- exports.subscribe = subscribe;
32
- exports.unsubscribe = unsubscribe;
@@ -1,181 +0,0 @@
1
- "use strict";
2
- var __typeError = (msg) => {
3
- throw TypeError(msg);
4
- };
5
- var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
6
- var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
7
- var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
8
- var _indiced, _size;
9
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
10
- const helpers_is = require("../helpers/is.cjs");
11
- const helpers_proxy = require("../helpers/proxy.cjs");
12
- const helpers_value = require("../helpers/value.cjs");
13
- const subscription = require("../subscription.cjs");
14
- const value_computed = require("./computed.cjs");
15
- const value_reactive = require("./reactive.cjs");
16
- const value_signal = require("./signal.cjs");
17
- class ReactiveArray extends value_reactive.Reactive {
18
- constructor(value, options) {
19
- super(
20
- helpers_is.arrayName,
21
- new Proxy(value, {
22
- get: (target, property) => updateMethods.has(property) ? updateArray(property, target, this.state, __privateGet(this, _size)) : Reflect.get(target, property),
23
- set: (target, property, value2) => helpers_proxy.setValueInProxy(
24
- target,
25
- property,
26
- value2,
27
- this.state,
28
- true,
29
- __privateGet(this, _size)
30
- )
31
- }),
32
- options
33
- );
34
- __privateAdd(this, _indiced, /* @__PURE__ */ new Map());
35
- __privateAdd(this, _size, value_signal.signal(0));
36
- __privateGet(this, _size).set(value.length);
37
- }
38
- /**
39
- * The length of the array
40
- */
41
- get length() {
42
- return __privateGet(this, _size).get();
43
- }
44
- /**
45
- * Set the length of the array
46
- */
47
- set length(value) {
48
- if (typeof value === "number" && value >= 0 && value !== this.state.value.length) {
49
- this.get().length = value;
50
- }
51
- }
52
- /**
53
- * Clear the array
54
- */
55
- clear() {
56
- this.length = 0;
57
- }
58
- get(first) {
59
- if (typeof first === "number") {
60
- return helpers_proxy.getReactiveValueInProxy(this, __privateGet(this, _indiced), first, true).get();
61
- }
62
- if (first === "length") {
63
- return this.length;
64
- }
65
- return helpers_value.getValue(this.state);
66
- }
67
- /**
68
- * Create a computed, filtered array
69
- */
70
- filter(callback) {
71
- return value_computed.computed(() => this.get().filter(callback));
72
- }
73
- /**
74
- * Create a computed, mapped array
75
- */
76
- map(callback) {
77
- return value_computed.computed(() => this.get().map(callback));
78
- }
79
- peek(length) {
80
- return length === true ? __privateGet(this, _size).peek() : [...this.state.value];
81
- }
82
- /**
83
- * Remove and return the last item of the array
84
- */
85
- pop() {
86
- return this.state.value.pop();
87
- }
88
- /**
89
- * Add items to the end of the array
90
- */
91
- push(...items) {
92
- return this.state.value.push(...items);
93
- }
94
- set(first, second) {
95
- if (first == null || Array.isArray(first)) {
96
- this.state.value.splice(0, this.state.value.length, ...first ?? []);
97
- } else if (first === "length") {
98
- this.length = second;
99
- } else if (typeof first === "number") {
100
- this.state.value[first] = second;
101
- }
102
- }
103
- /**
104
- * Remove and return the first item of the array
105
- */
106
- shift() {
107
- return this.state.value.shift();
108
- }
109
- /**
110
- * Remove and return items from the array _(and optionally add new items)_
111
- */
112
- splice(from, to, ...items) {
113
- return this.state.value.splice(
114
- from,
115
- to ?? this.state.value.length,
116
- ...items
117
- );
118
- }
119
- subscribe(first, second) {
120
- if (typeof first === "number" && typeof second === "function") {
121
- return helpers_proxy.getReactiveValueInProxy(
122
- this,
123
- __privateGet(this, _indiced),
124
- first,
125
- true
126
- ).subscribe(second);
127
- }
128
- return typeof first === "function" ? subscription.subscribe(this.state, first) : subscription.noop;
129
- }
130
- /**
131
- * Add items to the beginning of the array
132
- */
133
- unshift(...items) {
134
- return this.state.value.unshift(...items);
135
- }
136
- /**
137
- * Update the value _(based on the current value)_
138
- */
139
- update(callback) {
140
- const updated = callback(this.state.value);
141
- if (updated == null || Array.isArray(updated)) {
142
- this.set(updated);
143
- }
144
- }
145
- }
146
- _indiced = new WeakMap();
147
- _size = new WeakMap();
148
- function array(value, options) {
149
- return new ReactiveArray(Array.isArray(value) ? value : [], options);
150
- }
151
- function updateArray(type, array2, state, length) {
152
- const affectsLength = lengthAffectingMethods.has(type);
153
- const previousArray = affectsLength ? [] : [...array2];
154
- const previousLength = array2.length;
155
- return (...args) => {
156
- const result = array2[type](
157
- ...args
158
- );
159
- if (affectsLength ? array2.length !== previousLength : !helpers_value.equalArrays(state, previousArray, array2)) {
160
- helpers_value.emitValue(state);
161
- length.set(array2.length);
162
- }
163
- return result;
164
- };
165
- }
166
- const lengthAffectingMethods = /* @__PURE__ */ new Set([
167
- "pop",
168
- "push",
169
- "shift",
170
- "unshift"
171
- ]);
172
- const updateMethods = /* @__PURE__ */ new Set([
173
- ...lengthAffectingMethods,
174
- "copyWithin",
175
- "fill",
176
- "reverse",
177
- "sort",
178
- "splice"
179
- ]);
180
- exports.ReactiveArray = ReactiveArray;
181
- exports.array = array;
@@ -1,60 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
4
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
6
- const batch = require("../batch.cjs");
7
- const effect = require("../effect.cjs");
8
- const helpers_is = require("../helpers/is.cjs");
9
- const value_reactive = require("./reactive.cjs");
10
- exports.activeComputed = void 0;
11
- class Computed extends value_reactive.Reactive {
12
- constructor(callback, options) {
13
- super(helpers_is.computedName, void 0, options);
14
- __publicField(this, "effect", {
15
- dirty: true,
16
- instance: void 0
17
- });
18
- this.effect.instance = effect.effect(() => {
19
- if (this.effect.dirty) {
20
- const previousComputed = exports.activeComputed;
21
- exports.activeComputed = this;
22
- const value = callback();
23
- exports.activeComputed = previousComputed;
24
- if (!this.state.equal(this.state.value, value)) {
25
- this.state.value = value;
26
- for (const computed2 of this.state.computeds) {
27
- computed2.effect.dirty = true;
28
- }
29
- for (const effect2 of this.state.effects) {
30
- batch.batchedHandlers.add(effect2);
31
- }
32
- for (const [, subscription] of this.state.subscriptions) {
33
- subscription.callback(value);
34
- }
35
- }
36
- this.effect.dirty = false;
37
- }
38
- });
39
- }
40
- /**
41
- * @inheritdoc
42
- */
43
- get() {
44
- if (exports.activeComputed != null && this !== exports.activeComputed) {
45
- this.state.computeds.add(exports.activeComputed);
46
- }
47
- if (effect.activeEffect != null && effect.activeEffect !== this.effect.instance) {
48
- this.state.effects.add(effect.activeEffect);
49
- }
50
- if (this.effect.dirty && batch.batchDepth === 0) {
51
- effect.runEffect(this.effect.instance);
52
- }
53
- return this.state.value;
54
- }
55
- }
56
- function computed(callback, options) {
57
- return new Computed(callback, options);
58
- }
59
- exports.Computed = Computed;
60
- exports.computed = computed;