yuustate 0.0.1 → 3.10.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/LICENSE +21 -0
- package/README.md +92 -3
- package/esm/index.d.mts +1 -0
- package/esm/index.mjs +1 -0
- package/esm/react/create-mutation.d.mts +143 -0
- package/esm/react/create-query.d.mts +519 -0
- package/esm/react/create-store.d.mts +35 -0
- package/esm/react/create-stores.d.mts +76 -0
- package/esm/react/create-stream.d.mts +343 -0
- package/esm/react/use-isomorphic-layout-effect.d.mts +6 -0
- package/esm/react/use-mutation.d.mts +84 -0
- package/esm/react/use-store.d.mts +34 -0
- package/esm/react.d.mts +8 -0
- package/esm/react.mjs +1031 -0
- package/esm/vanilla/basic.d.mts +13 -0
- package/esm/vanilla/hash.d.mts +7 -0
- package/esm/vanilla/store.d.mts +121 -0
- package/esm/vanilla.d.mts +3 -0
- package/esm/vanilla.mjs +90 -0
- package/index.d.ts +1 -0
- package/index.js +12 -0
- package/package.json +50 -3
- package/react/create-mutation.d.ts +143 -0
- package/react/create-query.d.ts +519 -0
- package/react/create-store.d.ts +35 -0
- package/react/create-stores.d.ts +76 -0
- package/react/create-stream.d.ts +343 -0
- package/react/use-isomorphic-layout-effect.d.ts +6 -0
- package/react/use-mutation.d.ts +84 -0
- package/react/use-store.d.ts +34 -0
- package/react.d.ts +8 -0
- package/react.js +1040 -0
- package/ts_version_4.5_and_above_is_required.d.ts +0 -0
- package/vanilla/basic.d.ts +13 -0
- package/vanilla/hash.d.ts +7 -0
- package/vanilla/store.d.ts +121 -0
- package/vanilla.d.ts +3 -0
- package/vanilla.js +97 -0
|
File without changes
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check if this runs on browser.
|
|
3
|
+
*/
|
|
4
|
+
export declare const isClient: boolean;
|
|
5
|
+
/**
|
|
6
|
+
* Empty function.
|
|
7
|
+
*/
|
|
8
|
+
export declare const noop: () => void;
|
|
9
|
+
/**
|
|
10
|
+
* If the value is a function, it will invoke the function.\
|
|
11
|
+
* If the value is not a function, it will just return it.
|
|
12
|
+
*/
|
|
13
|
+
export declare const getValue: <T, P extends any[]>(valueOrComputeValueFn: T | ((...params: P) => T), ...params: P) => T;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const isPlainObject: (value: any) => boolean;
|
|
2
|
+
/**
|
|
3
|
+
* Get stable hash string from any value.
|
|
4
|
+
*
|
|
5
|
+
* Reference: https://github.com/TanStack/query/blob/v5.90.3/packages/query-core/src/utils.ts#L216
|
|
6
|
+
*/
|
|
7
|
+
export declare const getHash: (value?: any) => string;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
export type SetStateInput<TState> = Partial<TState> | ((state: TState) => Partial<TState>);
|
|
2
|
+
/**
|
|
3
|
+
* A subscriber function that is called whenever the state updates.
|
|
4
|
+
*
|
|
5
|
+
* @param state - The latest state
|
|
6
|
+
* @param prevState - The previous state before the update
|
|
7
|
+
* @param changedKeys - The top-level keys that changed (shallow diff)
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* - Subscribers are only called when at least one field changes.
|
|
11
|
+
* - Change detection is performed per key using `Object.is`.
|
|
12
|
+
* - `changedKeys` only includes top-level keys; nested changes must be inferred by the consumer.
|
|
13
|
+
*/
|
|
14
|
+
export type Subscriber<TState> = (state: TState, prevState: TState, changedKeys: Array<keyof TState>) => void;
|
|
15
|
+
/**
|
|
16
|
+
* Core store API for managing state.
|
|
17
|
+
*
|
|
18
|
+
* @remarks
|
|
19
|
+
* - The store performs **shallow change detection per key** before notifying subscribers.
|
|
20
|
+
* - Subscribers are only notified when at least one field changes.
|
|
21
|
+
* - State is treated as **immutable**. Mutating nested values directly will not trigger updates.
|
|
22
|
+
* - Designed to be framework-agnostic (React bindings are built separately).
|
|
23
|
+
* - By default, `setState` is **disabled on the server** to prevent accidental shared state between requests.
|
|
24
|
+
*/
|
|
25
|
+
export type StoreApi<TState extends Record<string, any>> = {
|
|
26
|
+
/**
|
|
27
|
+
* Updates the state.
|
|
28
|
+
*
|
|
29
|
+
* @param value - Partial state or updater function
|
|
30
|
+
*
|
|
31
|
+
* @remarks
|
|
32
|
+
* - Accepts either a partial object or a function `(state) => partial`
|
|
33
|
+
* - Updates are shallowly merged into the current state
|
|
34
|
+
*/
|
|
35
|
+
setState: (value: SetStateInput<TState>) => void;
|
|
36
|
+
/**
|
|
37
|
+
* Get the current state.
|
|
38
|
+
*
|
|
39
|
+
* @returns The latest state snapshot
|
|
40
|
+
*/
|
|
41
|
+
getState: () => TState;
|
|
42
|
+
/**
|
|
43
|
+
* Subscribes to state changes.
|
|
44
|
+
*
|
|
45
|
+
* @param subscriber - Function called when state updates
|
|
46
|
+
*
|
|
47
|
+
* @returns Unsubscribe function
|
|
48
|
+
*
|
|
49
|
+
* @remarks
|
|
50
|
+
* - Subscribers are only called when at least one root key changes
|
|
51
|
+
* - Receives `(state, prevState, changedKeys)`
|
|
52
|
+
*/
|
|
53
|
+
subscribe: (subscriber: Subscriber<TState>) => () => void;
|
|
54
|
+
/**
|
|
55
|
+
* Returns the number of active subscribers.
|
|
56
|
+
*
|
|
57
|
+
* @returns Total subscriber count
|
|
58
|
+
*
|
|
59
|
+
* @remarks
|
|
60
|
+
* - Can be used to determine if the store is currently in use
|
|
61
|
+
*/
|
|
62
|
+
getSubscriberCount: () => number;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Lifecycle hooks for the store.
|
|
66
|
+
*
|
|
67
|
+
* These hooks allow you to attach side effects based on subscription lifecycle.
|
|
68
|
+
*
|
|
69
|
+
* @remarks
|
|
70
|
+
* Useful for:
|
|
71
|
+
* - Lazy initialization (e.g. start fetching on first subscribe)
|
|
72
|
+
* - Cleanup (e.g. cancel timers, disconnect sockets)
|
|
73
|
+
* - Resource management (e.g. garbage collection)
|
|
74
|
+
*/
|
|
75
|
+
export type InitStoreOptions<TState extends Record<string, any>, TStoreProps extends Record<string, any> = object> = {
|
|
76
|
+
onFirstSubscribe?: (state: TState, store: StoreApi<TState> & TStoreProps) => void;
|
|
77
|
+
onSubscribe?: (state: TState, store: StoreApi<TState> & TStoreProps) => void;
|
|
78
|
+
onUnsubscribe?: (state: TState, store: StoreApi<TState> & TStoreProps) => void;
|
|
79
|
+
onLastUnsubscribe?: (state: TState, store: StoreApi<TState> & TStoreProps) => void;
|
|
80
|
+
/**
|
|
81
|
+
* Called whenever the state changes, without counting as a subscriber.
|
|
82
|
+
* Acts like a "spy" on state updates.
|
|
83
|
+
* Useful for devtools, logging, or debugging state changes.
|
|
84
|
+
*/
|
|
85
|
+
onStateChange?: (state: TState, prevState: TState, changedKeys: Array<keyof TState>) => void;
|
|
86
|
+
/**
|
|
87
|
+
* By default, calling `setState` on the server is disallowed to prevent shared state across requests.
|
|
88
|
+
* Set this to `true` only if you explicitly intend to mutate state during server execution.
|
|
89
|
+
*/
|
|
90
|
+
allowSetStateServerSide?: boolean;
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Creates a vanilla store with pub-sub capabilities.
|
|
94
|
+
*
|
|
95
|
+
* The store state must be an **object**.\
|
|
96
|
+
* Updates are applied as shallow merges, so non-object states are not supported.
|
|
97
|
+
*
|
|
98
|
+
* @param initialState - The initial state of the store
|
|
99
|
+
* @param options - Optional lifecycle hooks
|
|
100
|
+
*
|
|
101
|
+
* @returns A store API for managing state and subscriptions
|
|
102
|
+
*
|
|
103
|
+
* @remarks
|
|
104
|
+
* - State updates are **shallowly compared per key** before notifying subscribers.
|
|
105
|
+
* - Subscribers are only notified when at least one updated field changes (using `Object.is` comparison).
|
|
106
|
+
* - Subscribers receive the new state, previous state, and changed top-level keys.
|
|
107
|
+
* - State is expected to be treated as **immutable**.
|
|
108
|
+
* - Mutating nested values directly will not trigger updates.
|
|
109
|
+
* - Lifecycle hooks allow side-effect management tied to subscription count.
|
|
110
|
+
* - By default, `setState` is **not allowed on the server** to prevent accidental shared state between requests.
|
|
111
|
+
* - This helps avoid leaking data between users in server environments.
|
|
112
|
+
* - If you intentionally want to allow this behavior, set `allowSetStateServerSide: true`.
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* const store = initStore({ count: 0 });
|
|
116
|
+
*
|
|
117
|
+
* store.subscribe((state) => console.log(state.count));
|
|
118
|
+
* store.setState({ count: 1 }); // triggers subscriber
|
|
119
|
+
* store.setState({ count: 1 }); // no-op (no change)
|
|
120
|
+
*/
|
|
121
|
+
export declare const initStore: <TState extends Record<string, any>>(initialState: TState, options?: InitStoreOptions<TState>) => StoreApi<TState>;
|
package/vanilla.d.ts
ADDED
package/vanilla.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const isClient = typeof window !== "undefined" && !("Deno" in window);
|
|
4
|
+
const noop = () => {
|
|
5
|
+
};
|
|
6
|
+
const getValue = (valueOrComputeValueFn, ...params) => {
|
|
7
|
+
if (typeof valueOrComputeValueFn === "function") {
|
|
8
|
+
return valueOrComputeValueFn(...params);
|
|
9
|
+
}
|
|
10
|
+
return valueOrComputeValueFn;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const hasObjectPrototype = (value) => {
|
|
14
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
15
|
+
};
|
|
16
|
+
const isPlainObject = (value) => {
|
|
17
|
+
if (!hasObjectPrototype(value)) return false;
|
|
18
|
+
const ctor = value.constructor;
|
|
19
|
+
if (typeof ctor === "undefined") return true;
|
|
20
|
+
const prot = ctor.prototype;
|
|
21
|
+
if (!hasObjectPrototype(prot)) return false;
|
|
22
|
+
if (!prot.hasOwnProperty("isPrototypeOf")) return false;
|
|
23
|
+
if (Object.getPrototypeOf(value) !== Object.prototype) return false;
|
|
24
|
+
return true;
|
|
25
|
+
};
|
|
26
|
+
const getHash = (value) => JSON.stringify(
|
|
27
|
+
value,
|
|
28
|
+
(_, val) => isPlainObject(val) ? Object.keys(val).sort().reduce((result, key) => {
|
|
29
|
+
result[key] = val[key];
|
|
30
|
+
return result;
|
|
31
|
+
}, {}) : val
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
const initStore = (initialState, options = {}) => {
|
|
35
|
+
const {
|
|
36
|
+
onFirstSubscribe = noop,
|
|
37
|
+
onSubscribe = noop,
|
|
38
|
+
onUnsubscribe = noop,
|
|
39
|
+
onLastUnsubscribe = noop,
|
|
40
|
+
onStateChange = noop,
|
|
41
|
+
allowSetStateServerSide = false
|
|
42
|
+
} = options;
|
|
43
|
+
const subscribers = /* @__PURE__ */ new Set();
|
|
44
|
+
const getSubscriberCount = () => subscribers.size;
|
|
45
|
+
const subscribe = (subscriber) => {
|
|
46
|
+
subscribers.add(subscriber);
|
|
47
|
+
if (subscribers.size === 1) onFirstSubscribe(state, storeApi);
|
|
48
|
+
onSubscribe(state, storeApi);
|
|
49
|
+
return () => {
|
|
50
|
+
subscribers.delete(subscriber);
|
|
51
|
+
onUnsubscribe(state, storeApi);
|
|
52
|
+
if (subscribers.size === 0) onLastUnsubscribe(state, storeApi);
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
let state = initialState;
|
|
56
|
+
const getState = () => state;
|
|
57
|
+
const setState = (value) => {
|
|
58
|
+
if (!isClient && !allowSetStateServerSide) {
|
|
59
|
+
console.error(
|
|
60
|
+
"setState on the server is not allowed by default. Set `allowSetStateServerSide: true` to allow it."
|
|
61
|
+
);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const prevState = state;
|
|
65
|
+
const newValue = getValue(value, state);
|
|
66
|
+
const changedKeys = [];
|
|
67
|
+
for (const key of Object.keys(newValue)) {
|
|
68
|
+
if (key === "__proto__" || key === "constructor" || key === "prototype") {
|
|
69
|
+
console.warn(
|
|
70
|
+
`Ignored unsafe key "${String(key)}" in setState(). This key is reserved and may indicate a prototype pollution attempt or malformed payload.`
|
|
71
|
+
);
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (!Object.is(prevState[key], newValue[key])) {
|
|
75
|
+
changedKeys.push(key);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (changedKeys.length === 0) return;
|
|
79
|
+
state = { ...prevState, ...newValue };
|
|
80
|
+
onStateChange(state, prevState, changedKeys);
|
|
81
|
+
[...subscribers].forEach((subscriber) => subscriber(state, prevState, changedKeys));
|
|
82
|
+
};
|
|
83
|
+
const storeApi = {
|
|
84
|
+
getState,
|
|
85
|
+
setState,
|
|
86
|
+
subscribe,
|
|
87
|
+
getSubscriberCount
|
|
88
|
+
};
|
|
89
|
+
return storeApi;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
exports.getHash = getHash;
|
|
93
|
+
exports.getValue = getValue;
|
|
94
|
+
exports.initStore = initStore;
|
|
95
|
+
exports.isClient = isClient;
|
|
96
|
+
exports.isPlainObject = isPlainObject;
|
|
97
|
+
exports.noop = noop;
|