@oscarpalmer/mora 0.3.0 → 0.5.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.
package/types/signal.d.ts CHANGED
@@ -1,14 +1,24 @@
1
- import { Computed } from './computed';
1
+ import { type Computed } from './computed';
2
2
  import { type Effect } from './effect';
3
3
  type SignalState<Value> = {
4
- observers: Set<Computed<never> | Effect>;
4
+ computeds: Set<Computed<unknown>>;
5
+ effects: Set<Effect>;
5
6
  value: Value;
6
7
  };
7
8
  export declare class Signal<Value> {
8
9
  readonly state: SignalState<Value>;
9
10
  constructor(value: Value);
11
+ /**
12
+ * Get the value
13
+ */
10
14
  get(): Value;
15
+ /**
16
+ * Set the value
17
+ */
11
18
  set(value: Value): void;
19
+ /**
20
+ * Update the value
21
+ */
12
22
  update(callback: (value: Value) => Value): void;
13
23
  }
14
24
  export declare function signal<Value>(value: Value): Signal<Value>;
package/dist/mora.iife.js DELETED
@@ -1,129 +0,0 @@
1
- var Mora = (function (exports) {
2
- 'use strict';
3
-
4
- let activeEffect;
5
- class Effect {
6
- callback;
7
- signals = new Set();
8
- }
9
- function runEffect(effect) {
10
- for (const signal of effect.signals) {
11
- signal.state.observers.delete(effect);
12
- }
13
- effect.signals.clear();
14
- const previousEffect = activeEffect;
15
- activeEffect = effect;
16
- try {
17
- effect.callback();
18
- }
19
- finally {
20
- activeEffect = previousEffect;
21
- }
22
- }
23
- function effect(callback) {
24
- const instance = new Effect();
25
- instance.callback = callback;
26
- runEffect(instance);
27
- return instance;
28
- }
29
-
30
- class Computed extends Effect {
31
- dirty = true;
32
- effects = new Set();
33
- value;
34
- constructor(callback) {
35
- super();
36
- this.callback = () => {
37
- if (this.dirty) {
38
- this.value = callback();
39
- this.dirty = false;
40
- }
41
- };
42
- runEffect(this);
43
- }
44
- get() {
45
- if (activeEffect != null && activeEffect !== this) {
46
- this.effects.add(activeEffect);
47
- }
48
- if (this.dirty) {
49
- runEffect(this);
50
- }
51
- return this.value;
52
- }
53
- }
54
- function computed(callback) {
55
- return new Computed(callback);
56
- }
57
-
58
- const dirtyEffects = new Set();
59
- let batchDepth = 0;
60
- class Signal {
61
- state;
62
- constructor(value) {
63
- this.state = {
64
- value,
65
- observers: new Set(),
66
- };
67
- }
68
- get() {
69
- if (activeEffect != null) {
70
- this.state.observers.add(activeEffect);
71
- activeEffect.signals.add(this);
72
- }
73
- return this.state.value;
74
- }
75
- set(value) {
76
- if (Object.is(this.state.value, value)) {
77
- return;
78
- }
79
- this.state.value = value;
80
- const isOutermostBatch = batchDepth === 0;
81
- batchDepth += 1;
82
- try {
83
- for (const observer of this.state.observers) {
84
- if (observer instanceof Computed) {
85
- observer.dirty = true;
86
- dirtyEffects.add(observer);
87
- for (const effect of observer.effects) {
88
- dirtyEffects.add(effect);
89
- }
90
- }
91
- else {
92
- dirtyEffects.add(observer);
93
- }
94
- }
95
- if (isOutermostBatch) {
96
- queueMicrotask(() => {
97
- try {
98
- for (const effect of dirtyEffects) {
99
- runEffect(effect);
100
- }
101
- }
102
- finally {
103
- dirtyEffects.clear();
104
- batchDepth = 0;
105
- }
106
- });
107
- }
108
- }
109
- finally {
110
- if (isOutermostBatch) {
111
- batchDepth -= 1;
112
- }
113
- }
114
- }
115
- update(callback) {
116
- this.set(callback(this.state.value));
117
- }
118
- }
119
- function signal(value) {
120
- return new Signal(value);
121
- }
122
-
123
- exports.computed = computed;
124
- exports.effect = effect;
125
- exports.signal = signal;
126
-
127
- return exports;
128
-
129
- })({});