@oscarpalmer/mora 0.18.1 → 0.19.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.
@@ -1,7 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const batch = require("../batch.cjs");
4
+ const value_computed = require("../value/computed.cjs");
4
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.get();
16
+ }
5
17
  function setProxyValue(proxy, value) {
6
18
  batch.startBatch();
7
19
  const proxyKeys = Object.keys(proxy);
@@ -39,5 +51,6 @@ function setValueInProxy(target, property, value, state, isArray, length) {
39
51
  }
40
52
  return true;
41
53
  }
54
+ exports.getReactiveValueInProxy = getReactiveValueInProxy;
42
55
  exports.setProxyValue = setProxyValue;
43
56
  exports.setValueInProxy = setValueInProxy;
@@ -1,5 +1,17 @@
1
1
  import { startBatch, stopBatch } from "../batch.js";
2
+ import { computed } from "../value/computed.js";
2
3
  import { emitValue } from "./value.js";
4
+ function getReactiveValueInProxy(reactive, mapped, key, isArray) {
5
+ let item = mapped.get(key);
6
+ if (item == null) {
7
+ item = computed(() => {
8
+ const value = reactive.get();
9
+ return isArray ? value.at(key) : value[key];
10
+ });
11
+ mapped.set(key, item);
12
+ }
13
+ return item.get();
14
+ }
3
15
  function setProxyValue(proxy, value) {
4
16
  startBatch();
5
17
  const proxyKeys = Object.keys(proxy);
@@ -38,6 +50,7 @@ function setValueInProxy(target, property, value, state, isArray, length) {
38
50
  return true;
39
51
  }
40
52
  export {
53
+ getReactiveValueInProxy,
41
54
  setProxyValue,
42
55
  setValueInProxy
43
56
  };
package/dist/mora.full.js CHANGED
@@ -278,6 +278,19 @@ function getValue(state) {
278
278
  return state.value;
279
279
  }
280
280
 
281
+ function getReactiveValueInProxy(reactive, mapped, key, isArray) {
282
+ let item = mapped.get(key);
283
+ if (item == null) {
284
+ item = computed(() => {
285
+ const value = reactive.get();
286
+ return isArray
287
+ ? value.at(key)
288
+ : value[key];
289
+ });
290
+ mapped.set(key, item);
291
+ }
292
+ return item.get();
293
+ }
281
294
  function setProxyValue(proxy, value) {
282
295
  startBatch();
283
296
  const proxyKeys = Object.keys(proxy);
@@ -352,6 +365,7 @@ function signal(value, options) {
352
365
  }
353
366
 
354
367
  class ReactiveArray extends Reactive {
368
+ #indiced = new Map();
355
369
  #size = signal(0);
356
370
  /**
357
371
  * The length of the array
@@ -378,22 +392,19 @@ class ReactiveArray extends Reactive {
378
392
  }), options);
379
393
  this.#size.set(value.length);
380
394
  }
381
- /**
382
- * Get an indexed item
383
- */
384
- at(index) {
385
- return this.get().at(index);
386
- }
387
395
  /**
388
396
  * Clear the array
389
397
  */
390
398
  clear() {
391
399
  this.length = 0;
392
400
  }
393
- /**
394
- * @inheritdoc
395
- */
396
- get() {
401
+ get(first) {
402
+ if (typeof first === 'number') {
403
+ return getReactiveValueInProxy(this, this.#indiced, first, true);
404
+ }
405
+ if (first === 'length') {
406
+ return this.length;
407
+ }
397
408
  return getValue(this.state);
398
409
  }
399
410
  /**
@@ -504,13 +515,16 @@ function isPlainObject(value) {
504
515
  }
505
516
 
506
517
  class Store extends Reactive {
518
+ #keyed = new Map();
507
519
  constructor(value, options) {
508
520
  super(storeName, new Proxy(value, {
509
521
  set: (target, property, value) => setValueInProxy(target, property, value, this.state, false),
510
522
  }), options);
511
523
  }
512
524
  get(key) {
513
- return isKey(key) ? this.state.value[key] : getValue(this.state);
525
+ return isKey(key)
526
+ ? getReactiveValueInProxy(this, this.#keyed, key, false)
527
+ : getValue(this.state);
514
528
  }
515
529
  peek(key) {
516
530
  return isKey(key) ? this.state.value[key] : { ...this.state.value };
@@ -5,7 +5,7 @@ var __typeError = (msg) => {
5
5
  var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
6
6
  var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
7
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 _size;
8
+ var _indiced, _size;
9
9
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
10
10
  const helpers_is = require("../helpers/is.cjs");
11
11
  const helpers_proxy = require("../helpers/proxy.cjs");
@@ -30,6 +30,7 @@ class ReactiveArray extends value_reactive.Reactive {
30
30
  }),
31
31
  options
32
32
  );
33
+ __privateAdd(this, _indiced, /* @__PURE__ */ new Map());
33
34
  __privateAdd(this, _size, value_signal.signal(0));
34
35
  __privateGet(this, _size).set(value.length);
35
36
  }
@@ -47,22 +48,19 @@ class ReactiveArray extends value_reactive.Reactive {
47
48
  this.get().length = value;
48
49
  }
49
50
  }
50
- /**
51
- * Get an indexed item
52
- */
53
- at(index) {
54
- return this.get().at(index);
55
- }
56
51
  /**
57
52
  * Clear the array
58
53
  */
59
54
  clear() {
60
55
  this.length = 0;
61
56
  }
62
- /**
63
- * @inheritdoc
64
- */
65
- get() {
57
+ get(first) {
58
+ if (typeof first === "number") {
59
+ return helpers_proxy.getReactiveValueInProxy(this, __privateGet(this, _indiced), first, true);
60
+ }
61
+ if (first === "length") {
62
+ return this.length;
63
+ }
66
64
  return helpers_value.getValue(this.state);
67
65
  }
68
66
  /**
@@ -124,6 +122,7 @@ class ReactiveArray extends value_reactive.Reactive {
124
122
  return this.state.value.unshift(...items);
125
123
  }
126
124
  }
125
+ _indiced = new WeakMap();
127
126
  _size = new WeakMap();
128
127
  function array(value, options) {
129
128
  return new ReactiveArray(Array.isArray(value) ? value : [], options);
@@ -4,9 +4,9 @@ var __typeError = (msg) => {
4
4
  var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
5
5
  var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
6
6
  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);
7
- var _size;
7
+ var _indiced, _size;
8
8
  import { arrayName } from "../helpers/is.js";
9
- import { setValueInProxy } from "../helpers/proxy.js";
9
+ import { setValueInProxy, getReactiveValueInProxy } from "../helpers/proxy.js";
10
10
  import { getValue, equalArrays, emitValue } from "../helpers/value.js";
11
11
  import { computed } from "./computed.js";
12
12
  import { Reactive } from "./reactive.js";
@@ -28,6 +28,7 @@ class ReactiveArray extends Reactive {
28
28
  }),
29
29
  options
30
30
  );
31
+ __privateAdd(this, _indiced, /* @__PURE__ */ new Map());
31
32
  __privateAdd(this, _size, signal(0));
32
33
  __privateGet(this, _size).set(value.length);
33
34
  }
@@ -45,22 +46,19 @@ class ReactiveArray extends Reactive {
45
46
  this.get().length = value;
46
47
  }
47
48
  }
48
- /**
49
- * Get an indexed item
50
- */
51
- at(index) {
52
- return this.get().at(index);
53
- }
54
49
  /**
55
50
  * Clear the array
56
51
  */
57
52
  clear() {
58
53
  this.length = 0;
59
54
  }
60
- /**
61
- * @inheritdoc
62
- */
63
- get() {
55
+ get(first) {
56
+ if (typeof first === "number") {
57
+ return getReactiveValueInProxy(this, __privateGet(this, _indiced), first, true);
58
+ }
59
+ if (first === "length") {
60
+ return this.length;
61
+ }
64
62
  return getValue(this.state);
65
63
  }
66
64
  /**
@@ -122,6 +120,7 @@ class ReactiveArray extends Reactive {
122
120
  return this.state.value.unshift(...items);
123
121
  }
124
122
  }
123
+ _indiced = new WeakMap();
125
124
  _size = new WeakMap();
126
125
  function array(value, options) {
127
126
  return new ReactiveArray(Array.isArray(value) ? value : [], options);
@@ -1,4 +1,11 @@
1
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 _keyed;
2
9
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
10
  const is = require("../node_modules/@oscarpalmer/atoms/dist/internal/is.cjs");
4
11
  const helpers_is = require("../helpers/is.cjs");
@@ -14,9 +21,10 @@ class Store extends value_reactive.Reactive {
14
21
  }),
15
22
  options
16
23
  );
24
+ __privateAdd(this, _keyed, /* @__PURE__ */ new Map());
17
25
  }
18
26
  get(key) {
19
- return is.isKey(key) ? this.state.value[key] : helpers_value.getValue(this.state);
27
+ return is.isKey(key) ? helpers_proxy.getReactiveValueInProxy(this, __privateGet(this, _keyed), key, false) : helpers_value.getValue(this.state);
20
28
  }
21
29
  peek(key) {
22
30
  return is.isKey(key) ? this.state.value[key] : { ...this.state.value };
@@ -29,6 +37,7 @@ class Store extends value_reactive.Reactive {
29
37
  }
30
38
  }
31
39
  }
40
+ _keyed = new WeakMap();
32
41
  function store(value, options) {
33
42
  return new Store(is.isPlainObject(value) ? value : {}, options);
34
43
  }
@@ -1,6 +1,13 @@
1
+ var __typeError = (msg) => {
2
+ throw TypeError(msg);
3
+ };
4
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
5
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
6
+ 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);
7
+ var _keyed;
1
8
  import { isPlainObject, isKey } from "../node_modules/@oscarpalmer/atoms/dist/internal/is.js";
2
9
  import { storeName } from "../helpers/is.js";
3
- import { setValueInProxy, setProxyValue } from "../helpers/proxy.js";
10
+ import { setValueInProxy, getReactiveValueInProxy, setProxyValue } from "../helpers/proxy.js";
4
11
  import { getValue } from "../helpers/value.js";
5
12
  import { Reactive } from "./reactive.js";
6
13
  class Store extends Reactive {
@@ -12,9 +19,10 @@ class Store extends Reactive {
12
19
  }),
13
20
  options
14
21
  );
22
+ __privateAdd(this, _keyed, /* @__PURE__ */ new Map());
15
23
  }
16
24
  get(key) {
17
- return isKey(key) ? this.state.value[key] : getValue(this.state);
25
+ return isKey(key) ? getReactiveValueInProxy(this, __privateGet(this, _keyed), key, false) : getValue(this.state);
18
26
  }
19
27
  peek(key) {
20
28
  return isKey(key) ? this.state.value[key] : { ...this.state.value };
@@ -27,6 +35,7 @@ class Store extends Reactive {
27
35
  }
28
36
  }
29
37
  }
38
+ _keyed = new WeakMap();
30
39
  function store(value, options) {
31
40
  return new Store(isPlainObject(value) ? value : {}, options);
32
41
  }
package/package.json CHANGED
@@ -54,5 +54,5 @@
54
54
  },
55
55
  "type": "module",
56
56
  "types": "./types/index.d.ts",
57
- "version": "0.18.1"
57
+ "version": "0.19.0"
58
58
  }
@@ -1,9 +1,53 @@
1
- import type {ArrayOrPlainObject, PlainObject} from '@oscarpalmer/atoms/models';
1
+ import type {
2
+ ArrayOrPlainObject,
3
+ Key,
4
+ PlainObject,
5
+ } from '@oscarpalmer/atoms/models';
2
6
  import {startBatch, stopBatch} from '../batch';
7
+ import type {ReactiveArray} from '../value/array';
8
+ import {type Computed, computed} from '../value/computed';
3
9
  import type {ReactiveState} from '../value/reactive';
4
10
  import type {Signal} from '../value/signal';
11
+ import type {Store} from '../value/store';
5
12
  import {emitValue} from './value';
6
13
 
14
+ export function getReactiveValueInProxy<Value>(
15
+ array: ReactiveArray<Value>,
16
+ mapped: Map<Key, Computed<unknown>>,
17
+ index: number,
18
+ isArray: true,
19
+ ): unknown;
20
+
21
+ export function getReactiveValueInProxy<Value extends PlainObject>(
22
+ store: Store<Value>,
23
+ mapped: Map<Key, Computed<unknown>>,
24
+ key: Key,
25
+ isArray: false,
26
+ ): unknown;
27
+
28
+ export function getReactiveValueInProxy(
29
+ reactive: ReactiveArray<unknown> | Store<PlainObject>,
30
+ mapped: Map<Key, Computed<unknown>>,
31
+ key: Key,
32
+ isArray: boolean,
33
+ ): unknown {
34
+ let item = mapped.get(key);
35
+
36
+ if (item == null) {
37
+ item = computed(() => {
38
+ const value = reactive.get();
39
+
40
+ return isArray
41
+ ? (value as unknown[]).at(key as number)
42
+ : (value as PlainObject)[key];
43
+ }) as Computed<unknown>;
44
+
45
+ mapped.set(key, item);
46
+ }
47
+
48
+ return item.get();
49
+ }
50
+
7
51
  export function setProxyValue(
8
52
  proxy: ArrayOrPlainObject,
9
53
  value: ArrayOrPlainObject,
package/src/index.ts CHANGED
@@ -12,3 +12,4 @@ 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,11 +1,12 @@
1
1
  import {arrayName} from '../helpers/is';
2
- import {setValueInProxy} from '../helpers/proxy';
2
+ import {getReactiveValueInProxy, setValueInProxy} from '../helpers/proxy';
3
3
  import {emitValue, equalArrays, getValue} from '../helpers/value';
4
4
  import {type Computed, computed} from './computed';
5
5
  import {Reactive, type ReactiveOptions, type ReactiveState} from './reactive';
6
6
  import {type Signal, signal} from './signal';
7
7
 
8
8
  export class ReactiveArray<Item> extends Reactive<Item[], Item> {
9
+ #indiced = new Map<number, Computed<unknown>>();
9
10
  #size = signal(0);
10
11
 
11
12
  /**
@@ -52,13 +53,6 @@ export class ReactiveArray<Item> extends Reactive<Item[], Item> {
52
53
  this.#size.set(value.length);
53
54
  }
54
55
 
55
- /**
56
- * Get an indexed item
57
- */
58
- at(index: number): Item | undefined {
59
- return this.get().at(index);
60
- }
61
-
62
56
  /**
63
57
  * Clear the array
64
58
  */
@@ -69,7 +63,21 @@ export class ReactiveArray<Item> extends Reactive<Item[], Item> {
69
63
  /**
70
64
  * @inheritdoc
71
65
  */
72
- get(): Item[] {
66
+ get(): Item[];
67
+
68
+ get(index: number): Item | undefined;
69
+
70
+ get(property: 'length'): number;
71
+
72
+ get(first?: unknown): unknown {
73
+ if (typeof first === 'number') {
74
+ return getReactiveValueInProxy(this, this.#indiced, first, true);
75
+ }
76
+
77
+ if (first === 'length') {
78
+ return this.length;
79
+ }
80
+
73
81
  return getValue(this.state);
74
82
  }
75
83
 
@@ -1,11 +1,18 @@
1
1
  import {isKey, isPlainObject} from '@oscarpalmer/atoms/is';
2
2
  import type {Key, PlainObject} from '@oscarpalmer/atoms/models';
3
3
  import {storeName} from '../helpers/is';
4
- import {setProxyValue, setValueInProxy} from '../helpers/proxy';
4
+ import {
5
+ getReactiveValueInProxy,
6
+ setProxyValue,
7
+ setValueInProxy,
8
+ } from '../helpers/proxy';
5
9
  import {getValue} from '../helpers/value';
10
+ import type {Computed} from './computed';
6
11
  import {Reactive, type ReactiveOptions} from './reactive';
7
12
 
8
13
  export class Store<Value extends PlainObject> extends Reactive<Value, Value> {
14
+ #keyed = new Map<Key, Computed<unknown>>();
15
+
9
16
  constructor(value: Value, options?: ReactiveOptions<Value>) {
10
17
  super(
11
18
  storeName,
@@ -33,7 +40,9 @@ export class Store<Value extends PlainObject> extends Reactive<Value, Value> {
33
40
  get(key: Key): unknown;
34
41
 
35
42
  get(key?: unknown): unknown {
36
- return isKey(key) ? this.state.value[key] : getValue(this.state);
43
+ return isKey(key)
44
+ ? getReactiveValueInProxy(this, this.#keyed, key, false)
45
+ : getValue(this.state);
37
46
  }
38
47
 
39
48
  /**
@@ -0,0 +1,79 @@
1
+ // Generated by dts-bundle-generator v9.5.1
2
+
3
+ export type GenericCallback = (...args: any[]) => any;
4
+ declare class Effect {
5
+ private readonly $mora;
6
+ private readonly state;
7
+ constructor(callback: GenericCallback);
8
+ }
9
+ declare class Computed<Value> extends Reactive<Value> {
10
+ private readonly effect;
11
+ constructor(callback: () => Value, options?: ReactiveOptions<Value>);
12
+ /**
13
+ * @inheritdoc
14
+ */
15
+ get(): Value;
16
+ }
17
+ declare abstract class Reactive<Value, Equal = Value> {
18
+ protected readonly state: ReactiveState<Value, Equal>;
19
+ constructor(name: string, value: Value, options?: ReactiveOptions<Equal>);
20
+ /**
21
+ * Get the value
22
+ */
23
+ abstract get(): Value;
24
+ /**
25
+ * Get the value _(without reactivity)_
26
+ */
27
+ peek(): Value;
28
+ /**
29
+ * Subscribe to changes
30
+ */
31
+ subscribe(callback: (value: Value) => void): Unsubscribe;
32
+ /**
33
+ * JSON representation of the value
34
+ */
35
+ toJSON(): Value;
36
+ /**
37
+ * String representation of the value
38
+ */
39
+ toString(): string;
40
+ /**
41
+ * Unsubscribe from changes
42
+ */
43
+ unsubscribe(callback: (value: Value) => void): void;
44
+ }
45
+ export type ReactiveOptions<Value> = {
46
+ /**
47
+ * Method to compare values for equality
48
+ */
49
+ equal?: (first: Value, second: Value) => boolean;
50
+ };
51
+ export type ReactiveState<Value, Equal> = {
52
+ computeds: Set<Computed<unknown>>;
53
+ effects: Set<Effect>;
54
+ equal: (first: Equal, second: Equal) => boolean;
55
+ subscriptions: Map<(value: Value) => void, Subscription<Value>>;
56
+ value: Value;
57
+ };
58
+ declare class Subscription<Value> {
59
+ state: ReactiveState<Value, never>;
60
+ callback: GenericCallback;
61
+ constructor(state: ReactiveState<Value, never>, callback: GenericCallback);
62
+ }
63
+ /**
64
+ * Unsubscribe from changes
65
+ */
66
+ export type Unsubscribe = () => void;
67
+ export declare function flushHandlers(): void;
68
+ /**
69
+ * Start batching effects _(use `stopBatch` to flush and run batched effects)_
70
+ */
71
+ export declare function startBatch(): void;
72
+ /**
73
+ * Stop batching effects and flush _(run)_ them
74
+ */
75
+ export declare function stopBatch(): void;
76
+ export declare const batchedHandlers: Set<Effect | Subscription<unknown>>;
77
+ export declare let batchDepth: number;
78
+
79
+ export {};
@@ -0,0 +1,16 @@
1
+ // Generated by dts-bundle-generator v9.5.1
2
+
3
+ export type GenericCallback = (...args: any[]) => any;
4
+ export declare class Effect {
5
+ private readonly $mora;
6
+ private readonly state;
7
+ constructor(callback: GenericCallback);
8
+ }
9
+ export declare function runEffect(effect: Effect): void;
10
+ /**
11
+ * Create an effect
12
+ */
13
+ export declare function effect(callback: GenericCallback): Effect;
14
+ export declare let activeEffect: Effect | undefined;
15
+
16
+ export {};