@oscarpalmer/mora 0.5.0 → 0.6.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/dist/batch.cjs CHANGED
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const effect = require("./effect.cjs");
4
4
  function flushEffects() {
5
- while (effect.dirtyEffects.size > 0) {
5
+ while (exports.batchDepth === 0 && effect.dirtyEffects.size > 0) {
6
6
  const effects = [...effect.dirtyEffects];
7
7
  effect.dirtyEffects.clear();
8
8
  for (const effect$1 of effects) {
@@ -17,9 +17,7 @@ function stopBatch() {
17
17
  if (exports.batchDepth > 0) {
18
18
  exports.batchDepth -= 1;
19
19
  }
20
- if (exports.batchDepth === 0) {
21
- flushEffects();
22
- }
20
+ flushEffects();
23
21
  }
24
22
  exports.batchDepth = 0;
25
23
  exports.flushEffects = flushEffects;
package/dist/batch.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { dirtyEffects, runEffect } from "./effect.js";
2
2
  function flushEffects() {
3
- while (dirtyEffects.size > 0) {
3
+ while (batchDepth === 0 && dirtyEffects.size > 0) {
4
4
  const effects = [...dirtyEffects];
5
5
  dirtyEffects.clear();
6
6
  for (const effect of effects) {
@@ -15,9 +15,7 @@ function stopBatch() {
15
15
  if (batchDepth > 0) {
16
16
  batchDepth -= 1;
17
17
  }
18
- if (batchDepth === 0) {
19
- flushEffects();
20
- }
18
+ flushEffects();
21
19
  }
22
20
  let batchDepth = 0;
23
21
  export {
package/dist/computed.cjs CHANGED
@@ -3,6 +3,7 @@ var __defProp = Object.defineProperty;
3
3
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
4
4
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
5
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
6
+ const batch = require("./batch.cjs");
6
7
  const effect = require("./effect.cjs");
7
8
  exports.activeComputed = void 0;
8
9
  class Computed {
@@ -46,7 +47,11 @@ class Computed {
46
47
  this.state.effects.add(effect.activeEffect);
47
48
  }
48
49
  if (this.state.dirty) {
49
- effect.runEffect(this.state.effect);
50
+ if (batch.batchDepth === 0) {
51
+ effect.runEffect(this.state.effect);
52
+ } else {
53
+ effect.dirtyEffects.add(this.state.effect);
54
+ }
50
55
  }
51
56
  return this.state.value;
52
57
  }
package/dist/computed.js CHANGED
@@ -1,6 +1,7 @@
1
1
  var __defProp = Object.defineProperty;
2
2
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
3
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
+ import { batchDepth } from "./batch.js";
4
5
  import { effect, dirtyEffects, activeEffect, runEffect } from "./effect.js";
5
6
  let activeComputed;
6
7
  class Computed {
@@ -44,7 +45,11 @@ class Computed {
44
45
  this.state.effects.add(activeEffect);
45
46
  }
46
47
  if (this.state.dirty) {
47
- runEffect(this.state.effect);
48
+ if (batchDepth === 0) {
49
+ runEffect(this.state.effect);
50
+ } else {
51
+ dirtyEffects.add(this.state.effect);
52
+ }
48
53
  }
49
54
  return this.state.value;
50
55
  }
package/dist/mora.full.js CHANGED
@@ -27,7 +27,7 @@ let activeEffect;
27
27
  const dirtyEffects = new Set();
28
28
 
29
29
  function flushEffects() {
30
- while (dirtyEffects.size > 0) {
30
+ while (batchDepth === 0 && dirtyEffects.size > 0) {
31
31
  const effects = [...dirtyEffects];
32
32
  dirtyEffects.clear();
33
33
  for (const effect of effects) {
@@ -48,9 +48,7 @@ function stopBatch() {
48
48
  if (batchDepth > 0) {
49
49
  batchDepth -= 1;
50
50
  }
51
- if (batchDepth === 0) {
52
- flushEffects();
53
- }
51
+ flushEffects();
54
52
  }
55
53
  let batchDepth = 0;
56
54
 
@@ -96,7 +94,12 @@ class Computed {
96
94
  this.state.effects.add(activeEffect);
97
95
  }
98
96
  if (this.state.dirty) {
99
- runEffect(this.state.effect);
97
+ if (batchDepth === 0) {
98
+ runEffect(this.state.effect);
99
+ }
100
+ else {
101
+ dirtyEffects.add(this.state.effect);
102
+ }
100
103
  }
101
104
  return this.state.value;
102
105
  }
@@ -137,9 +140,9 @@ class Signal {
137
140
  return;
138
141
  }
139
142
  this.state.value = value;
140
- for (const computed of this.state.computeds) {
141
- computed.state.dirty = true;
142
- dirtyEffects.add(computed.state.effect);
143
+ for (const comp of this.state.computeds) {
144
+ comp.state.dirty = true;
145
+ dirtyEffects.add(comp.state.effect);
143
146
  }
144
147
  for (const effect of this.state.effects) {
145
148
  dirtyEffects.add(effect);
package/dist/signal.cjs CHANGED
@@ -35,9 +35,9 @@ class Signal {
35
35
  return;
36
36
  }
37
37
  this.state.value = value;
38
- for (const computed2 of this.state.computeds) {
39
- computed2.state.dirty = true;
40
- effect.dirtyEffects.add(computed2.state.effect);
38
+ for (const comp of this.state.computeds) {
39
+ comp.state.dirty = true;
40
+ effect.dirtyEffects.add(comp.state.effect);
41
41
  }
42
42
  for (const effect$1 of this.state.effects) {
43
43
  effect.dirtyEffects.add(effect$1);
package/dist/signal.js CHANGED
@@ -33,9 +33,9 @@ class Signal {
33
33
  return;
34
34
  }
35
35
  this.state.value = value;
36
- for (const computed of this.state.computeds) {
37
- computed.state.dirty = true;
38
- dirtyEffects.add(computed.state.effect);
36
+ for (const comp of this.state.computeds) {
37
+ comp.state.dirty = true;
38
+ dirtyEffects.add(comp.state.effect);
39
39
  }
40
40
  for (const effect of this.state.effects) {
41
41
  dirtyEffects.add(effect);
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "description": "Signals and stuff…",
7
7
  "devDependencies": {
8
8
  "@biomejs/biome": "^1.9",
9
- "@oscarpalmer/atoms": "^0.99",
9
+ "@oscarpalmer/atoms": "^0.100",
10
10
  "@rollup/plugin-node-resolve": "^16",
11
11
  "@rollup/plugin-typescript": "^12.1",
12
12
  "@types/node": "^22.15",
@@ -52,5 +52,5 @@
52
52
  },
53
53
  "type": "module",
54
54
  "types": "./types/index.d.ts",
55
- "version": "0.5.0"
55
+ "version": "0.6.0"
56
56
  }
package/src/batch.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import {dirtyEffects, runEffect} from './effect';
2
2
 
3
3
  export function flushEffects(): void {
4
- while (dirtyEffects.size > 0) {
4
+ while (batchDepth === 0 && dirtyEffects.size > 0) {
5
5
  const effects = [...dirtyEffects];
6
6
 
7
7
  dirtyEffects.clear();
@@ -27,9 +27,7 @@ export function stopBatch(): void {
27
27
  batchDepth -= 1;
28
28
  }
29
29
 
30
- if (batchDepth === 0) {
31
- flushEffects();
32
- }
30
+ flushEffects();
33
31
  }
34
32
 
35
33
  export let batchDepth = 0;
package/src/computed.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import {batchDepth} from './batch';
1
2
  import {
2
3
  type Effect,
3
4
  activeEffect,
@@ -14,10 +15,14 @@ type ComputedState<Value> = {
14
15
  value: Value;
15
16
  };
16
17
 
18
+ export type InternalComputed = {
19
+ readonly state: ComputedState<unknown>;
20
+ };
21
+
17
22
  export let activeComputed: Computed<unknown> | undefined;
18
23
 
19
24
  export class Computed<Value> {
20
- readonly state: ComputedState<Value>;
25
+ private readonly state: ComputedState<Value>;
21
26
 
22
27
  constructor(callback: () => Value) {
23
28
  this.state = {
@@ -70,7 +75,11 @@ export class Computed<Value> {
70
75
  }
71
76
 
72
77
  if (this.state.dirty) {
73
- runEffect(this.state.effect);
78
+ if (batchDepth === 0) {
79
+ runEffect(this.state.effect);
80
+ } else {
81
+ dirtyEffects.add(this.state.effect);
82
+ }
74
83
  }
75
84
 
76
85
  return this.state.value;
package/src/effect.ts CHANGED
@@ -4,8 +4,12 @@ type EffectState = {
4
4
  callback: GenericCallback;
5
5
  };
6
6
 
7
+ type InternalEffect = {
8
+ state: EffectState;
9
+ };
10
+
7
11
  export class Effect {
8
- readonly state: EffectState;
12
+ private readonly state: EffectState;
9
13
 
10
14
  constructor(callback: GenericCallback) {
11
15
  this.state = {
@@ -22,7 +26,7 @@ export function runEffect(effect: Effect): void {
22
26
  activeEffect = effect;
23
27
 
24
28
  try {
25
- effect.state.callback();
29
+ (effect as unknown as InternalEffect).state.callback();
26
30
  } finally {
27
31
  activeEffect = previousEffect;
28
32
  }
package/src/signal.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import {batchDepth, flushEffects} from './batch';
2
- import {type Computed, activeComputed} from './computed';
2
+ import {type Computed, type InternalComputed, activeComputed} from './computed';
3
3
  import {type Effect, activeEffect, dirtyEffects} from './effect';
4
4
 
5
5
  type SignalState<Value> = {
@@ -9,7 +9,7 @@ type SignalState<Value> = {
9
9
  };
10
10
 
11
11
  export class Signal<Value> {
12
- readonly state: SignalState<Value>;
12
+ private readonly state: SignalState<Value>;
13
13
 
14
14
  constructor(value: Value) {
15
15
  this.state = {
@@ -44,10 +44,10 @@ export class Signal<Value> {
44
44
 
45
45
  this.state.value = value;
46
46
 
47
- for (const computed of this.state.computeds) {
48
- computed.state.dirty = true;
47
+ for (const comp of this.state.computeds) {
48
+ (comp as unknown as InternalComputed).state.dirty = true;
49
49
 
50
- dirtyEffects.add(computed.state.effect);
50
+ dirtyEffects.add((comp as unknown as InternalComputed).state.effect);
51
51
  }
52
52
 
53
53
  for (const effect of this.state.effects) {
@@ -1,11 +1,8 @@
1
1
  // Generated by dts-bundle-generator v9.5.1
2
2
 
3
3
  export type GenericCallback = (...args: any[]) => any;
4
- export type EffectState = {
5
- callback: GenericCallback;
6
- };
7
4
  declare class Effect {
8
- readonly state: EffectState;
5
+ private readonly state;
9
6
  constructor(callback: GenericCallback);
10
7
  }
11
8
  export type ComputedState<Value> = {
@@ -15,9 +12,12 @@ export type ComputedState<Value> = {
15
12
  effects: Set<Effect>;
16
13
  value: Value;
17
14
  };
15
+ export type InternalComputed = {
16
+ readonly state: ComputedState<unknown>;
17
+ };
18
18
  export declare let activeComputed: Computed<unknown> | undefined;
19
19
  export declare class Computed<Value> {
20
- readonly state: ComputedState<Value>;
20
+ private readonly state;
21
21
  constructor(callback: () => Value);
22
22
  /**
23
23
  * Get the value
@@ -6,9 +6,12 @@ type ComputedState<Value> = {
6
6
  effects: Set<Effect>;
7
7
  value: Value;
8
8
  };
9
+ export type InternalComputed = {
10
+ readonly state: ComputedState<unknown>;
11
+ };
9
12
  export declare let activeComputed: Computed<unknown> | undefined;
10
13
  export declare class Computed<Value> {
11
- readonly state: ComputedState<Value>;
14
+ private readonly state;
12
15
  constructor(callback: () => Value);
13
16
  /**
14
17
  * Get the value
@@ -1,11 +1,8 @@
1
1
  // Generated by dts-bundle-generator v9.5.1
2
2
 
3
3
  export type GenericCallback = (...args: any[]) => any;
4
- export type EffectState = {
5
- callback: GenericCallback;
6
- };
7
4
  export declare class Effect {
8
- readonly state: EffectState;
5
+ private readonly state;
9
6
  constructor(callback: GenericCallback);
10
7
  }
11
8
  export declare function runEffect(effect: Effect): void;
package/types/effect.d.ts CHANGED
@@ -1,9 +1,6 @@
1
1
  import type { GenericCallback } from '@oscarpalmer/atoms/models';
2
- type EffectState = {
3
- callback: GenericCallback;
4
- };
5
2
  export declare class Effect {
6
- readonly state: EffectState;
3
+ private readonly state;
7
4
  constructor(callback: GenericCallback);
8
5
  }
9
6
  export declare function runEffect(effect: Effect): void;
@@ -13,4 +10,3 @@ export declare function runEffect(effect: Effect): void;
13
10
  export declare function effect(callback: GenericCallback): Effect;
14
11
  export declare let activeEffect: Effect | undefined;
15
12
  export declare const dirtyEffects: Set<Effect>;
16
- export {};
package/types/index.d.cts CHANGED
@@ -1,43 +1,7 @@
1
1
  // Generated by dts-bundle-generator v9.5.1
2
2
 
3
- export type GenericCallback = (...args: any[]) => any;
4
- export type EffectState = {
5
- callback: GenericCallback;
6
- };
7
- export declare class Effect {
8
- readonly state: EffectState;
9
- constructor(callback: GenericCallback);
10
- }
11
- /**
12
- * Create an effect
13
- */
14
- export declare function effect(callback: GenericCallback): Effect;
15
- export type ComputedState<Value> = {
16
- computeds: Set<Computed<unknown>>;
17
- dirty: boolean;
18
- effect: Effect;
19
- effects: Set<Effect>;
20
- value: Value;
21
- };
22
- export declare class Computed<Value> {
23
- readonly state: ComputedState<Value>;
24
- constructor(callback: () => Value);
25
- /**
26
- * Get the value
27
- */
28
- get(): Value;
29
- }
30
- /**
31
- * Create a computed value
32
- */
33
- export declare function computed<Value>(callback: () => Value): Computed<Value>;
34
- export type SignalState<Value> = {
35
- computeds: Set<Computed<unknown>>;
36
- effects: Set<Effect>;
37
- value: Value;
38
- };
39
3
  export declare class Signal<Value> {
40
- readonly state: SignalState<Value>;
4
+ private readonly state;
41
5
  constructor(value: Value);
42
6
  /**
43
7
  * Get the value
@@ -61,5 +25,26 @@ export declare function startBatch(): void;
61
25
  * Stop batching effects and flush _(run)_ them
62
26
  */
63
27
  export declare function stopBatch(): void;
28
+ export type GenericCallback = (...args: any[]) => any;
29
+ export declare class Effect {
30
+ private readonly state;
31
+ constructor(callback: GenericCallback);
32
+ }
33
+ /**
34
+ * Create an effect
35
+ */
36
+ export declare function effect(callback: GenericCallback): Effect;
37
+ export declare class Computed<Value> {
38
+ private readonly state;
39
+ constructor(callback: () => Value);
40
+ /**
41
+ * Get the value
42
+ */
43
+ get(): Value;
44
+ }
45
+ /**
46
+ * Create a computed value
47
+ */
48
+ export declare function computed<Value>(callback: () => Value): Computed<Value>;
64
49
 
65
50
  export {};
@@ -1,35 +1,7 @@
1
1
  // Generated by dts-bundle-generator v9.5.1
2
2
 
3
- export type GenericCallback = (...args: any[]) => any;
4
- export type EffectState = {
5
- callback: GenericCallback;
6
- };
7
- declare class Effect {
8
- readonly state: EffectState;
9
- constructor(callback: GenericCallback);
10
- }
11
- export type ComputedState<Value> = {
12
- computeds: Set<Computed<unknown>>;
13
- dirty: boolean;
14
- effect: Effect;
15
- effects: Set<Effect>;
16
- value: Value;
17
- };
18
- declare class Computed<Value> {
19
- readonly state: ComputedState<Value>;
20
- constructor(callback: () => Value);
21
- /**
22
- * Get the value
23
- */
24
- get(): Value;
25
- }
26
- export type SignalState<Value> = {
27
- computeds: Set<Computed<unknown>>;
28
- effects: Set<Effect>;
29
- value: Value;
30
- };
31
3
  export declare class Signal<Value> {
32
- readonly state: SignalState<Value>;
4
+ private readonly state;
33
5
  constructor(value: Value);
34
6
  /**
35
7
  * Get the value
package/types/signal.d.ts CHANGED
@@ -1,12 +1,5 @@
1
- import { type Computed } from './computed';
2
- import { type Effect } from './effect';
3
- type SignalState<Value> = {
4
- computeds: Set<Computed<unknown>>;
5
- effects: Set<Effect>;
6
- value: Value;
7
- };
8
1
  export declare class Signal<Value> {
9
- readonly state: SignalState<Value>;
2
+ private readonly state;
10
3
  constructor(value: Value);
11
4
  /**
12
5
  * Get the value
@@ -22,4 +15,3 @@ export declare class Signal<Value> {
22
15
  update(callback: (value: Value) => Value): void;
23
16
  }
24
17
  export declare function signal<Value>(value: Value): Signal<Value>;
25
- export {};