what-core 0.1.1 → 0.2.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/src/store.js CHANGED
@@ -4,15 +4,32 @@
4
4
 
5
5
  import { signal, computed, batch } from './reactive.js';
6
6
 
7
+ // --- storeComputed ---
8
+ // Marker wrapper to explicitly tag a function as a computed in createStore.
9
+ // Without this, createStore can't distinguish computed(state => ...) from action(item => ...).
10
+ //
11
+ // Usage:
12
+ // const useCounter = createStore({
13
+ // count: 0,
14
+ // doubled: storeComputed(state => state.count * 2),
15
+ // addItem(item) { /* this is an action */ },
16
+ // });
17
+
18
+ export function storeComputed(fn) {
19
+ fn._storeComputed = true;
20
+ return fn;
21
+ }
22
+
7
23
  // --- createStore ---
8
24
  // Creates a reactive store with actions. Each key becomes a signal.
9
25
  //
10
26
  // Usage:
11
27
  // const useCounter = createStore({
12
28
  // count: 0,
13
- // doubled: (state) => state.count * 2, // computed
14
- // increment() { this.count++; }, // action
29
+ // doubled: storeComputed(state => state.count * 2), // computed
30
+ // increment() { this.count++; }, // action
15
31
  // decrement() { this.count--; },
32
+ // addItem(item) { this.items.push(item); }, // action (not confused with computed)
16
33
  // });
17
34
  //
18
35
  // function Counter() {
@@ -27,12 +44,13 @@ export function createStore(definition) {
27
44
  const state = {};
28
45
 
29
46
  // Separate state, computeds, and actions
47
+ // Use explicit _storeComputed marker instead of function.length heuristic
30
48
  for (const [key, value] of Object.entries(definition)) {
31
- if (typeof value === 'function' && value.length > 0 && key !== 'constructor') {
32
- // Computed: function that takes state
49
+ if (typeof value === 'function' && value._storeComputed) {
50
+ // Computed: explicitly marked with storeComputed()
33
51
  computeds[key] = value;
34
52
  } else if (typeof value === 'function') {
35
- // Action: function with no args that uses `this`
53
+ // Action: any other function
36
54
  actions[key] = value;
37
55
  } else {
38
56
  // State: initial value