what-core 0.4.2 → 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/dist/store.js CHANGED
@@ -1,128 +1,83 @@
1
- // What Framework - Store
2
- // Lightweight global state management. Signal-based, type-safe, ergonomic.
3
- // Like Zustand meets signals — define a store, use it anywhere.
4
-
5
- import { signal, computed, batch } from './reactive.js';
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
-
1
+ import { signal, computed, batch, __DEV__ } from './reactive.js';
18
2
  export function derived(fn) {
19
- fn._storeComputed = true;
20
- return fn;
3
+ fn._storeComputed = true;
4
+ return fn;
21
5
  }
22
-
23
- // Deprecated alias — use derived() instead
24
6
  let _storeComputedWarned = false;
25
7
  export function storeComputed(fn) {
26
- if (!_storeComputedWarned) {
27
- _storeComputedWarned = true;
28
- console.warn('[what] storeComputed() is deprecated. Use derived() instead.');
29
- }
30
- return derived(fn);
8
+ if (!_storeComputedWarned) {
9
+ _storeComputedWarned = true;
10
+ console.warn('[what] storeComputed() is deprecated. Use derived() instead.');
11
+ }
12
+ return derived(fn);
31
13
  }
32
-
33
- // --- createStore ---
34
- // Creates a reactive store with actions. Each key becomes a signal.
35
- //
36
- // Usage:
37
- // const useCounter = createStore({
38
- // count: 0,
39
- // doubled: storeComputed(state => state.count * 2), // computed
40
- // increment() { this.count++; }, // action
41
- // decrement() { this.count--; },
42
- // addItem(item) { this.items.push(item); }, // action (not confused with computed)
43
- // });
44
- //
45
- // function Counter() {
46
- // const { count, doubled, increment } = useCounter();
47
- // return h('div', null, count, ' / ', doubled, h('button', { onClick: increment }, '+'));
48
- // }
49
-
50
14
  export function createStore(definition) {
51
- const signals = {};
52
- const computeds = {};
53
- const actions = {};
54
- const state = {};
55
-
56
- // Separate state, computeds, and actions
57
- // Use explicit _storeComputed marker instead of function.length heuristic
58
- for (const [key, value] of Object.entries(definition)) {
59
- if (typeof value === 'function' && value._storeComputed) {
60
- // Computed: explicitly marked with storeComputed()
61
- computeds[key] = value;
62
- } else if (typeof value === 'function') {
63
- // Action: any other function
64
- actions[key] = value;
65
- } else {
66
- // State: initial value
67
- signals[key] = signal(value);
68
- }
69
- }
70
-
71
- // Build computed signals
72
- for (const [key, fn] of Object.entries(computeds)) {
73
- const proxy = new Proxy({}, {
74
- get(_, prop) {
75
- if (signals[prop]) return signals[prop]();
76
- if (computeds[prop]) return computeds[prop]();
77
- return undefined;
78
- },
79
- });
80
- computeds[key] = computed(() => fn(proxy));
81
- }
82
-
83
- // Build action functions bound to signals
84
- for (const [key, fn] of Object.entries(actions)) {
85
- actions[key] = (...args) => {
86
- batch(() => {
87
- const proxy = new Proxy({}, {
88
- get(_, prop) {
89
- if (signals[prop]) return signals[prop].peek();
90
- if (computeds[prop]) return computeds[prop].peek();
91
- if (actions[prop]) return actions[prop];
92
- return undefined;
93
- },
94
- set(_, prop, val) {
95
- if (signals[prop]) signals[prop].set(val);
96
- return true;
97
- },
98
- });
99
- fn.apply(proxy, args);
100
- });
101
- };
102
- }
103
-
104
- // Return a hook-like function
105
- return function useStore() {
106
- const result = {};
107
- for (const [key, s] of Object.entries(signals)) {
108
- Object.defineProperty(result, key, { get: () => s(), enumerable: true });
109
- }
110
- for (const [key, c] of Object.entries(computeds)) {
111
- Object.defineProperty(result, key, { get: () => c(), enumerable: true });
112
- }
113
- for (const [key, fn] of Object.entries(actions)) {
114
- result[key] = fn;
115
- }
116
- return result;
117
- };
15
+ const signals = {};
16
+ const computeds = {};
17
+ const actions = {};
18
+ const state = {};
19
+ for (const [key, value] of Object.entries(definition)) {
20
+ if (typeof value === 'function' && value._storeComputed) {
21
+ if (__DEV__ && value.length === 0) {
22
+ console.warn(
23
+ `[what] derived() for "${key}" should accept the state parameter, e.g. derived(state => ...).`
24
+ );
25
+ }
26
+ computeds[key] = value;
27
+ } else if (typeof value === 'function') {
28
+ actions[key] = value;
29
+ } else {
30
+ signals[key] = signal(value);
31
+ }
32
+ }
33
+ for (const [key, fn] of Object.entries(computeds)) {
34
+ const proxy = new Proxy({}, {
35
+ get(_, prop) {
36
+ if (signals[prop]) return signals[prop]();
37
+ if (computeds[prop]) return computeds[prop]();
38
+ return undefined;
39
+ },
40
+ });
41
+ computeds[key] = computed(() => fn(proxy));
42
+ }
43
+ for (const [key, fn] of Object.entries(actions)) {
44
+ actions[key] = (...args) => {
45
+ batch(() => {
46
+ const proxy = new Proxy({}, {
47
+ get(_, prop) {
48
+ if (signals[prop]) return signals[prop].peek();
49
+ if (computeds[prop]) return computeds[prop].peek();
50
+ if (actions[prop]) return actions[prop];
51
+ return undefined;
52
+ },
53
+ set(_, prop, val) {
54
+ if (signals[prop]) signals[prop].set(val);
55
+ return true;
56
+ },
57
+ });
58
+ fn.apply(proxy, args);
59
+ });
60
+ };
61
+ }
62
+ return function useStore() {
63
+ const result = {};
64
+ for (const [key, s] of Object.entries(signals)) {
65
+ Object.defineProperty(result, key, { get: () => s(), enumerable: true });
66
+ }
67
+ for (const [key, c] of Object.entries(computeds)) {
68
+ Object.defineProperty(result, key, { get: () => c(), enumerable: true });
69
+ }
70
+ for (const [key, fn] of Object.entries(actions)) {
71
+ result[key] = fn;
72
+ }
73
+ return result;
74
+ };
118
75
  }
119
-
120
- // --- Simple atom --- [DEPRECATED: use signal() directly]
121
76
  let _atomWarned = false;
122
77
  export function atom(initial) {
123
- if (!_atomWarned) {
124
- _atomWarned = true;
125
- console.warn('[what] atom() is deprecated. Use signal() directly instead.');
126
- }
127
- return signal(initial);
78
+ if (!_atomWarned) {
79
+ _atomWarned = true;
80
+ console.warn('[what] atom() is deprecated. Use signal() directly instead.');
128
81
  }
82
+ return signal(initial);
83
+ }
package/dist/what.js CHANGED
@@ -1,4 +1,5 @@
1
- export { signal, computed, effect, batch, untrack } from './reactive.js';
1
+ export { signal, computed, effect, memo as signalMemo, batch, untrack, flushSync, createRoot } from './reactive.js';
2
+ export { template, insert, mapArray, spread, delegateEvents, on, classList } from './render.js';
2
3
  export { h, Fragment, html } from './h.js';
3
4
  export { mount } from './dom.js';
4
5
  export {
@@ -17,10 +18,9 @@ onCleanup,
17
18
  createResource,
18
19
  } from './hooks.js';
19
20
  export { memo, lazy, Suspense, ErrorBoundary, Show, For, Switch, Match, Island } from './components.js';
20
- export { createStore, storeComputed, atom } from './store.js';
21
+ export { createStore, derived, storeComputed, atom } from './store.js';
21
22
  export { Head, clearHead } from './head.js';
22
23
  export {
23
- show,
24
24
  each,
25
25
  cls,
26
26
  style,
@@ -28,6 +28,7 @@ debounce,
28
28
  throttle,
29
29
  useMediaQuery,
30
30
  useLocalStorage,
31
+ useClickOutside,
31
32
  Portal,
32
33
  transition,
33
34
  } from './helpers.js';
@@ -56,6 +57,7 @@ cssTransition,
56
57
  } from './animation.js';
57
58
  export {
58
59
  useFocus,
60
+ useFocusRestore,
59
61
  useFocusTrap,
60
62
  FocusTrap,
61
63
  announce,