@oscarpalmer/mora 0.33.0 → 0.35.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.
@@ -6,42 +6,51 @@ import {
6
6
  SYMBOL_STATE,
7
7
  } from '../constants';
8
8
  import {subscribeToReadonly} from '../helpers/subscription';
9
- import {getFrozenValue, getSignalValue, getStringValue, peekSignalValue} from '../helpers/value';
10
- import type {ReadonlyFrozenSignal, ReadonlySignal, SignalState, InternalSignal} from '../models';
9
+ import {
10
+ getFrozenValue,
11
+ getJsonValue,
12
+ getSignalValue,
13
+ getStringValue,
14
+ peekSignalValue,
15
+ } from '../helpers/value';
16
+ import type {InternalSignal, ReadonlyFrozenSignal, ReadonlySignal, SignalState} from '../models';
17
+
18
+ // #region Types
19
+
20
+ type InternalReadonly = {
21
+ [SUBSCRIPTION_TYPE_FROZEN]: boolean;
22
+ } & InternalSignal;
23
+
24
+ // #endregion
11
25
 
12
26
  // #region Instance
13
27
 
14
28
  function Readonly(this: any, state: SignalState, frozen: boolean) {
29
+ this[SUBSCRIPTION_TYPE_FROZEN] = frozen;
15
30
  this[SYMBOL_STATE] = state;
16
-
17
- Object.defineProperty(this, SUBSCRIPTION_TYPE_FROZEN, {
18
- enumerable: true,
19
- value: frozen,
20
- });
21
31
  }
22
32
 
23
33
  Readonly.prototype[NAME_MORA] = NAME_READONLY;
24
34
 
35
+ Readonly.prototype.get = getReadonlyValue;
36
+ Readonly.prototype.peek = peekReadonlyValue;
25
37
  Readonly.prototype.subscribe = subscribeToReadonly;
38
+ Readonly.prototype.toJSON = getJsonValue;
26
39
  Readonly.prototype.toString = getStringValue;
27
40
 
28
- Readonly.prototype.get = function () {
29
- return getReadonlyValue(this, false, this[SUBSCRIPTION_TYPE_FROZEN]);
30
- };
31
-
32
- Readonly.prototype.peek = function (copy?: boolean) {
33
- return getReadonlyValue(this, true, this[SUBSCRIPTION_TYPE_FROZEN], copy);
34
- };
35
-
36
- Readonly.prototype.toJSON = function () {
37
- return this[SYMBOL_STATE].value;
38
- };
39
-
40
41
  // #endregion
41
42
 
42
43
  // #region Functions
43
44
 
44
- export function getReadonlyInstance<Value>(
45
+ export function getReadonlyInstance(
46
+ state: SignalState,
47
+ frozen: boolean,
48
+ ): ReadonlySignal<unknown> | ReadonlyFrozenSignal<unknown> {
49
+ // @ts-expect-error All good, no worries :-)
50
+ return new Readonly(state, frozen);
51
+ }
52
+
53
+ export function getReadonlySignal<Value>(
45
54
  this: InternalSignal,
46
55
  frozen?: never,
47
56
  ): ReadonlySignal<Value> {
@@ -49,7 +58,7 @@ export function getReadonlyInstance<Value>(
49
58
 
50
59
  this[SYMBOL_STATE].readonlies ??= {};
51
60
 
52
- this[SYMBOL_STATE].readonlies[key] ??= getReadonlySignal(
61
+ this[SYMBOL_STATE].readonlies[key] ??= getReadonlyInstance(
53
62
  this[SYMBOL_STATE],
54
63
  frozen === true,
55
64
  ) as never;
@@ -57,16 +66,12 @@ export function getReadonlyInstance<Value>(
57
66
  return this[SYMBOL_STATE].readonlies[key] as never;
58
67
  }
59
68
 
60
- export function getReadonlySignal(
61
- state: SignalState,
62
- frozen: boolean,
63
- ): ReadonlySignal<unknown> | ReadonlyFrozenSignal<unknown> {
64
- // @ts-expect-error All good, no worries :-)
65
- return new Readonly(state, frozen);
69
+ function getReadonlyValue(this: InternalReadonly) {
70
+ return handleReadonlyValue(this, false, this[SUBSCRIPTION_TYPE_FROZEN]);
66
71
  }
67
72
 
68
- function getReadonlyValue(
69
- instance: InternalSignal,
73
+ function handleReadonlyValue(
74
+ instance: InternalReadonly,
70
75
  peek: boolean,
71
76
  frozen: boolean,
72
77
  copy?: boolean,
@@ -82,4 +87,8 @@ function getReadonlyValue(
82
87
  return frozen ? getFrozenValue(value) : value;
83
88
  }
84
89
 
90
+ function peekReadonlyValue(this: InternalReadonly, copy?: boolean) {
91
+ return handleReadonlyValue(this, true, this[SUBSCRIPTION_TYPE_FROZEN], copy);
92
+ }
93
+
85
94
  // #endregion
@@ -3,57 +3,53 @@ import {getState} from '../helpers/misc';
3
3
  import {subscribeToSignal} from '../helpers/subscription';
4
4
  import {
5
5
  emitValue,
6
+ getJsonValue,
6
7
  getSignalValue,
7
8
  getStringValue,
8
- handleSignalValue,
9
+ handleSignal,
9
10
  peekSignalValue,
10
- updateSignalValue,
11
+ updateSignal,
11
12
  } from '../helpers/value';
12
- import type {ReactiveOptions, Signal, InternalSignal} from '../models';
13
- import {getReadonlyInstance} from './readonly';
13
+ import type {InternalSignal, InternalStateful, ReactiveOptions, Signal} from '../models';
14
+ import {getReadonlySignal} from './readonly';
14
15
 
15
16
  // #region Instance
16
17
 
17
18
  function Signal(this: any, value: unknown, options?: ReactiveOptions<unknown>) {
18
19
  this[SYMBOL_STATE] = getState(undefined, options);
19
20
 
20
- handleSignalValue(this, value, setAndEmit);
21
+ handleSignal(this, value, setAndEmit);
21
22
  }
22
23
 
23
24
  Signal.prototype[NAME_MORA] = NAME_SIGNAL;
24
25
 
25
- Signal.prototype.asReadonly = getReadonlyInstance;
26
+ Signal.prototype.asReadonly = getReadonlySignal;
26
27
  Signal.prototype.get = getSignalValue;
27
28
  Signal.prototype.peek = peekSignalValue;
29
+ Signal.prototype.set = setSignalValue;
28
30
  Signal.prototype.subscribe = subscribeToSignal;
29
31
  Signal.prototype.toString = getStringValue;
30
-
31
- Signal.prototype.set = function (value: never) {
32
- return handleSignalValue(this, value, setAndEmit);
33
- };
34
-
35
- Signal.prototype.toJSON = function () {
36
- return this[SYMBOL_STATE].value;
37
- };
38
-
39
- Signal.prototype.update = function (callback: never) {
40
- return updateSignalValue(this, callback, setAndEmit);
41
- };
32
+ Signal.prototype.toJSON = getJsonValue;
33
+ Signal.prototype.update = updateSignalValue;
42
34
 
43
35
  // #endregion
44
36
 
45
37
  // #region Functions
46
38
 
47
- function setAndEmit(instance: InternalSignal, value: unknown): void {
39
+ function setAndEmit(instance: InternalStateful, value: unknown): void {
48
40
  const state = instance[SYMBOL_STATE];
49
41
 
50
- if (!(state.equal ?? Object.is)(state.value as never, value as never)) {
42
+ if (!(state.equal ?? Object.is)(state.value, value)) {
51
43
  state.value = value;
52
44
 
53
45
  emitValue(instance);
54
46
  }
55
47
  }
56
48
 
49
+ function setSignalValue(this: InternalSignal, value: unknown): void {
50
+ handleSignal(this, value, setAndEmit);
51
+ }
52
+
57
53
  /**
58
54
  * Create a reactive value from a function result
59
55
  *
@@ -92,4 +88,8 @@ export function signal(value: unknown, options?: ReactiveOptions<unknown>): Sign
92
88
  return new Signal(value, options);
93
89
  }
94
90
 
91
+ function updateSignalValue(this: InternalSignal, callback: never): void {
92
+ updateSignal(this, callback, setAndEmit);
93
+ }
94
+
95
95
  // #endregion
@@ -10,9 +10,9 @@ import {
10
10
  updateProxyValue,
11
11
  } from '../helpers/proxy';
12
12
  import {subscribeToProxy} from '../helpers/subscription';
13
- import {getStringValue} from '../helpers/value';
13
+ import {getJsonValue, getStringValue} from '../helpers/value';
14
14
  import type {ReactiveOptions, ReactiveStore} from '../models';
15
- import {getReadonlyInstance} from './readonly';
15
+ import {getReadonlySignal} from './readonly';
16
16
 
17
17
  // #region Instance
18
18
 
@@ -34,19 +34,16 @@ function ReactiveStore(this: any, value: never, options?: ReactiveOptions<unknow
34
34
 
35
35
  ReactiveStore.prototype[NAME_MORA] = NAME_STORE;
36
36
 
37
- ReactiveStore.prototype.asReadonly = getReadonlyInstance;
37
+ ReactiveStore.prototype.asReadonly = getReadonlySignal;
38
38
  ReactiveStore.prototype.get = getValueInProxy;
39
39
  ReactiveStore.prototype.notify = emitProxyValues;
40
40
  ReactiveStore.prototype.peek = peekValueInProxy;
41
41
  ReactiveStore.prototype.set = setProxyValue;
42
42
  ReactiveStore.prototype.subscribe = subscribeToProxy;
43
+ ReactiveStore.prototype.toJSON = getJsonValue;
43
44
  ReactiveStore.prototype.toString = getStringValue;
44
45
  ReactiveStore.prototype.update = updateProxyValue;
45
46
 
46
- ReactiveStore.prototype.toJSON = function () {
47
- return this[SYMBOL_STATE].value;
48
- };
49
-
50
47
  // #endregion
51
48
 
52
49
  // #region Functions