floppy-disk 3.0.0-experimental.1 → 3.0.1
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/README.md +256 -676
- package/esm/index.d.mts +1 -0
- package/esm/index.mjs +1 -0
- package/esm/react/create-mutation.d.mts +151 -0
- package/esm/react/create-query.d.mts +344 -0
- package/esm/react/create-store.d.mts +28 -0
- package/esm/react/create-stores.d.mts +39 -0
- package/esm/react/use-isomorphic-layout-effect.d.mts +6 -0
- package/esm/react/use-mutation.d.mts +82 -0
- package/esm/react/use-store.d.mts +28 -0
- package/esm/react.d.mts +7 -0
- package/esm/react.mjs +697 -0
- package/esm/vanilla/basic.d.mts +13 -0
- package/esm/vanilla/hash.d.mts +7 -0
- package/esm/vanilla/store.d.mts +89 -0
- package/esm/vanilla.d.mts +3 -0
- package/esm/vanilla.mjs +82 -0
- package/index.d.ts +1 -0
- package/index.js +12 -0
- package/package.json +47 -45
- package/react/create-mutation.d.ts +151 -0
- package/react/create-query.d.ts +344 -0
- package/react/create-store.d.ts +28 -0
- package/react/create-stores.d.ts +39 -0
- package/react/use-isomorphic-layout-effect.d.ts +6 -0
- package/react/use-mutation.d.ts +82 -0
- package/react/use-store.d.ts +28 -0
- package/react.d.ts +7 -0
- package/react.js +705 -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 +89 -0
- package/vanilla.d.ts +3 -0
- package/vanilla.js +89 -0
- package/esm/index.d.ts +0 -8
- package/esm/index.js +0 -8
- package/esm/react/create-bi-direction-query.d.ts +0 -166
- package/esm/react/create-bi-direction-query.js +0 -74
- package/esm/react/create-mutation.d.ts +0 -39
- package/esm/react/create-mutation.js +0 -56
- package/esm/react/create-query.d.ts +0 -319
- package/esm/react/create-query.js +0 -434
- package/esm/react/create-store.d.ts +0 -38
- package/esm/react/create-store.js +0 -38
- package/esm/react/create-stores.d.ts +0 -61
- package/esm/react/create-stores.js +0 -99
- package/esm/react/with-context.d.ts +0 -5
- package/esm/react/with-context.js +0 -14
- package/esm/utils.d.ts +0 -24
- package/esm/utils.js +0 -31
- package/esm/vanilla/fetcher.d.ts +0 -27
- package/esm/vanilla/fetcher.js +0 -95
- package/esm/vanilla/init-store.d.ts +0 -24
- package/esm/vanilla/init-store.js +0 -51
- package/lib/index.d.ts +0 -8
- package/lib/index.js +0 -11
- package/lib/react/create-bi-direction-query.d.ts +0 -166
- package/lib/react/create-bi-direction-query.js +0 -78
- package/lib/react/create-mutation.d.ts +0 -39
- package/lib/react/create-mutation.js +0 -60
- package/lib/react/create-query.d.ts +0 -319
- package/lib/react/create-query.js +0 -438
- package/lib/react/create-store.d.ts +0 -38
- package/lib/react/create-store.js +0 -42
- package/lib/react/create-stores.d.ts +0 -61
- package/lib/react/create-stores.js +0 -104
- package/lib/react/with-context.d.ts +0 -5
- package/lib/react/with-context.js +0 -18
- package/lib/utils.d.ts +0 -24
- package/lib/utils.js +0 -39
- package/lib/vanilla/fetcher.d.ts +0 -27
- package/lib/vanilla/fetcher.js +0 -99
- package/lib/vanilla/init-store.d.ts +0 -24
- package/lib/vanilla/init-store.js +0 -55
- package/utils/package.json +0 -6
|
@@ -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,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a partial state update.
|
|
3
|
+
*
|
|
4
|
+
* Can be either:
|
|
5
|
+
* - A partial object to merge into the current state
|
|
6
|
+
* - A function that receives the current state and returns a partial update
|
|
7
|
+
*/
|
|
8
|
+
export type SetState<TState> = Partial<TState> | ((state: TState) => Partial<TState>);
|
|
9
|
+
/**
|
|
10
|
+
* A subscriber function that is called whenever the state updates.
|
|
11
|
+
*
|
|
12
|
+
* @param state - The latest state
|
|
13
|
+
* @param prevState - The previous state before the update
|
|
14
|
+
* @param changedKeys - The top-level keys that changed (shallow diff)
|
|
15
|
+
*
|
|
16
|
+
* @remarks
|
|
17
|
+
* - Subscribers are only called when at least one field changes.
|
|
18
|
+
* - Change detection is performed per key using `Object.is`.
|
|
19
|
+
* - `changedKeys` only includes top-level keys; nested changes must be inferred by the consumer.
|
|
20
|
+
*/
|
|
21
|
+
export type Subscriber<TState> = (state: TState, prevState: TState, changedKeys: Array<keyof TState>) => void;
|
|
22
|
+
/**
|
|
23
|
+
* Core store API for managing state.
|
|
24
|
+
*
|
|
25
|
+
* @remarks
|
|
26
|
+
* - The store performs **shallow change detection per key** before notifying subscribers.
|
|
27
|
+
* - Subscribers are only notified when at least one field changes.
|
|
28
|
+
* - State is treated as **immutable**. Mutating nested values directly will not trigger updates.
|
|
29
|
+
* - Designed to be framework-agnostic (React bindings are built separately).
|
|
30
|
+
* - By default, `setState` is **disabled on the server** to prevent accidental shared state between requests.
|
|
31
|
+
*/
|
|
32
|
+
export type StoreApi<TState extends Record<string, any>> = {
|
|
33
|
+
setState: (value: SetState<TState>) => void;
|
|
34
|
+
getState: () => TState;
|
|
35
|
+
subscribe: (subscriber: Subscriber<TState>) => () => void;
|
|
36
|
+
getSubscribers: () => Set<Subscriber<TState>>;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Lifecycle hooks for the store.
|
|
40
|
+
*
|
|
41
|
+
* These hooks allow you to attach side effects based on subscription lifecycle.
|
|
42
|
+
*
|
|
43
|
+
* @remarks
|
|
44
|
+
* Useful for:
|
|
45
|
+
* - Lazy initialization (e.g. start fetching on first subscribe)
|
|
46
|
+
* - Cleanup (e.g. cancel timers, disconnect sockets)
|
|
47
|
+
* - Resource management (e.g. garbage collection)
|
|
48
|
+
*/
|
|
49
|
+
export type InitStoreOptions<TState extends Record<string, any>> = {
|
|
50
|
+
onFirstSubscribe?: (state: TState, store: StoreApi<TState>) => void;
|
|
51
|
+
onSubscribe?: (state: TState, store: StoreApi<TState>) => void;
|
|
52
|
+
onUnsubscribe?: (state: TState, store: StoreApi<TState>) => void;
|
|
53
|
+
onLastUnsubscribe?: (state: TState, store: StoreApi<TState>) => void;
|
|
54
|
+
/**
|
|
55
|
+
* By default, calling `setState` on the server is disallowed to prevent shared state across requests.
|
|
56
|
+
* Set this to `true` only if you explicitly intend to mutate state during server execution.
|
|
57
|
+
*/
|
|
58
|
+
allowSetStateServerSide?: boolean;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Creates a vanilla store with pub-sub capabilities.
|
|
62
|
+
*
|
|
63
|
+
* The store state must be an **object**.\
|
|
64
|
+
* Updates are applied as shallow merges, so non-object states are not supported.
|
|
65
|
+
*
|
|
66
|
+
* @param initialState - The initial state of the store
|
|
67
|
+
* @param options - Optional lifecycle hooks
|
|
68
|
+
*
|
|
69
|
+
* @returns A store API for managing state and subscriptions
|
|
70
|
+
*
|
|
71
|
+
* @remarks
|
|
72
|
+
* - State updates are **shallowly compared per key** before notifying subscribers.
|
|
73
|
+
* - Subscribers are only notified when at least one updated field changes (using `Object.is` comparison).
|
|
74
|
+
* - Subscribers receive the new state, previous state, and changed top-level keys.
|
|
75
|
+
* - State is expected to be treated as **immutable**.
|
|
76
|
+
* - Mutating nested values directly will not trigger updates.
|
|
77
|
+
* - Lifecycle hooks allow side-effect management tied to subscription count.
|
|
78
|
+
* - By default, `setState` is **not allowed on the server** to prevent accidental shared state between requests.
|
|
79
|
+
* - This helps avoid leaking data between users in server environments.
|
|
80
|
+
* - If you intentionally want to allow this behavior, set `allowSetStateServerSide: true`.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* const store = initStore({ count: 0 });
|
|
84
|
+
*
|
|
85
|
+
* store.subscribe((state) => console.log(state.count));
|
|
86
|
+
* store.setState({ count: 1 }); // triggers subscriber
|
|
87
|
+
* store.setState({ count: 1 }); // no-op (no change)
|
|
88
|
+
*/
|
|
89
|
+
export declare const initStore: <TState extends Record<string, any>>(initialState: TState, options?: InitStoreOptions<TState>) => StoreApi<TState>;
|
package/esm/vanilla.mjs
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
const isClient = typeof window !== "undefined" && !("Deno" in window);
|
|
2
|
+
const noop = () => {
|
|
3
|
+
};
|
|
4
|
+
const getValue = (valueOrComputeValueFn, ...params) => {
|
|
5
|
+
if (typeof valueOrComputeValueFn === "function") {
|
|
6
|
+
return valueOrComputeValueFn(...params);
|
|
7
|
+
}
|
|
8
|
+
return valueOrComputeValueFn;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const hasObjectPrototype = (value) => {
|
|
12
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
13
|
+
};
|
|
14
|
+
const isPlainObject = (value) => {
|
|
15
|
+
if (!hasObjectPrototype(value)) return false;
|
|
16
|
+
const ctor = value.constructor;
|
|
17
|
+
if (typeof ctor === "undefined") return true;
|
|
18
|
+
const prot = ctor.prototype;
|
|
19
|
+
if (!hasObjectPrototype(prot)) return false;
|
|
20
|
+
if (!prot.hasOwnProperty("isPrototypeOf")) return false;
|
|
21
|
+
if (Object.getPrototypeOf(value) !== Object.prototype) return false;
|
|
22
|
+
return true;
|
|
23
|
+
};
|
|
24
|
+
const getHash = (value) => JSON.stringify(
|
|
25
|
+
value,
|
|
26
|
+
(_, val) => isPlainObject(val) ? Object.keys(val).sort().reduce((result, key) => {
|
|
27
|
+
result[key] = val[key];
|
|
28
|
+
return result;
|
|
29
|
+
}, {}) : val
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
const initStore = (initialState, options = {}) => {
|
|
33
|
+
const {
|
|
34
|
+
onFirstSubscribe = noop,
|
|
35
|
+
onSubscribe = noop,
|
|
36
|
+
onUnsubscribe = noop,
|
|
37
|
+
onLastUnsubscribe = noop,
|
|
38
|
+
allowSetStateServerSide = false
|
|
39
|
+
} = options;
|
|
40
|
+
const subscribers = /* @__PURE__ */ new Set();
|
|
41
|
+
const getSubscribers = () => subscribers;
|
|
42
|
+
const subscribe = (subscriber) => {
|
|
43
|
+
subscribers.add(subscriber);
|
|
44
|
+
if (subscribers.size === 1) onFirstSubscribe(state, storeApi);
|
|
45
|
+
onSubscribe(state, storeApi);
|
|
46
|
+
return () => {
|
|
47
|
+
subscribers.delete(subscriber);
|
|
48
|
+
onUnsubscribe(state, storeApi);
|
|
49
|
+
if (subscribers.size === 0) onLastUnsubscribe(state, storeApi);
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
let state = initialState;
|
|
53
|
+
const getState = () => state;
|
|
54
|
+
const setState = (value) => {
|
|
55
|
+
if (!isClient && !allowSetStateServerSide) {
|
|
56
|
+
console.error(
|
|
57
|
+
"setState on the server is not allowed by default. Set `allowSetStateServerSide: true` to allow it."
|
|
58
|
+
);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const prevState = state;
|
|
62
|
+
const newValue = getValue(value, state);
|
|
63
|
+
const changedKeys = [];
|
|
64
|
+
for (const key in newValue) {
|
|
65
|
+
if (!Object.is(prevState[key], newValue[key])) {
|
|
66
|
+
changedKeys.push(key);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (changedKeys.length === 0) return;
|
|
70
|
+
state = { ...prevState, ...newValue };
|
|
71
|
+
[...subscribers].forEach((subscriber) => subscriber(state, prevState, changedKeys));
|
|
72
|
+
};
|
|
73
|
+
const storeApi = {
|
|
74
|
+
getState,
|
|
75
|
+
setState,
|
|
76
|
+
subscribe,
|
|
77
|
+
getSubscribers
|
|
78
|
+
};
|
|
79
|
+
return storeApi;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export { getHash, getValue, initStore, isClient, isPlainObject, noop };
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from 'floppy-disk/vanilla';
|
package/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var vanilla = require('floppy-disk/vanilla');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
Object.keys(vanilla).forEach(function (k) {
|
|
8
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
get: function () { return vanilla[k]; }
|
|
11
|
+
});
|
|
12
|
+
});
|
package/package.json
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "floppy-disk",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
3
|
+
"description": "Lightweight, simple, and powerful state management library",
|
|
4
|
+
"private": false,
|
|
5
|
+
"version": "3.0.1",
|
|
5
6
|
"keywords": [
|
|
7
|
+
"utilities",
|
|
8
|
+
"store",
|
|
6
9
|
"state",
|
|
7
|
-
"manager",
|
|
8
|
-
"management",
|
|
9
10
|
"react",
|
|
10
11
|
"hooks",
|
|
11
|
-
"store",
|
|
12
|
-
"utilities",
|
|
13
12
|
"query"
|
|
14
13
|
],
|
|
15
14
|
"author": "Muhammad Afifudin",
|
|
@@ -24,59 +23,62 @@
|
|
|
24
23
|
"homepage": "https://github.com/afiiif/floppy-disk#readme",
|
|
25
24
|
"sideEffects": false,
|
|
26
25
|
"files": [
|
|
27
|
-
"
|
|
28
|
-
"esm/",
|
|
29
|
-
"utils/"
|
|
26
|
+
"**"
|
|
30
27
|
],
|
|
31
|
-
"main": "./
|
|
32
|
-
"
|
|
33
|
-
"
|
|
28
|
+
"main": "./index.js",
|
|
29
|
+
"types": "./index.d.ts",
|
|
30
|
+
"typesVersions": {
|
|
31
|
+
">=4.5": {
|
|
32
|
+
"esm/*": [
|
|
33
|
+
"esm/*"
|
|
34
|
+
],
|
|
35
|
+
"*": [
|
|
36
|
+
"*"
|
|
37
|
+
]
|
|
38
|
+
},
|
|
39
|
+
"*": {
|
|
40
|
+
"esm/*": [
|
|
41
|
+
"ts_version_4.5_and_above_is_required.d.ts"
|
|
42
|
+
],
|
|
43
|
+
"*": [
|
|
44
|
+
"ts_version_4.5_and_above_is_required.d.ts"
|
|
45
|
+
]
|
|
46
|
+
}
|
|
47
|
+
},
|
|
34
48
|
"exports": {
|
|
35
49
|
"./package.json": "./package.json",
|
|
36
50
|
".": {
|
|
37
51
|
"import": {
|
|
38
|
-
"types": "./esm/index.d.
|
|
39
|
-
"default": "./esm/index.
|
|
40
|
-
},
|
|
41
|
-
"module": {
|
|
42
|
-
"types": "./esm/index.d.ts",
|
|
43
|
-
"default": "./esm/index.js"
|
|
52
|
+
"types": "./esm/index.d.mts",
|
|
53
|
+
"default": "./esm/index.mjs"
|
|
44
54
|
},
|
|
45
55
|
"default": {
|
|
46
|
-
"types": "./
|
|
47
|
-
"default": "./
|
|
56
|
+
"types": "./index.d.ts",
|
|
57
|
+
"default": "./index.js"
|
|
48
58
|
}
|
|
49
59
|
},
|
|
50
|
-
"
|
|
60
|
+
"./*": {
|
|
51
61
|
"import": {
|
|
52
|
-
"types": "./esm
|
|
53
|
-
"default": "./esm
|
|
54
|
-
},
|
|
55
|
-
"module": {
|
|
56
|
-
"types": "./esm/utils.d.ts",
|
|
57
|
-
"default": "./esm/utils.js"
|
|
62
|
+
"types": "./esm/*.d.mts",
|
|
63
|
+
"default": "./esm/*.mjs"
|
|
58
64
|
},
|
|
59
65
|
"default": {
|
|
60
|
-
"types": "
|
|
61
|
-
"default": "
|
|
66
|
+
"types": "./*.d.ts",
|
|
67
|
+
"default": "./*.js"
|
|
62
68
|
}
|
|
63
69
|
}
|
|
64
70
|
},
|
|
65
|
-
"
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
"name": "experimental",
|
|
78
|
-
"prerelease": true
|
|
79
|
-
}
|
|
80
|
-
]
|
|
71
|
+
"packageManager": "pnpm@10.32.1",
|
|
72
|
+
"peerDependencies": {
|
|
73
|
+
"@types/react": ">=17.0",
|
|
74
|
+
"react": ">=17.0"
|
|
75
|
+
},
|
|
76
|
+
"peerDependenciesMeta": {
|
|
77
|
+
"@types/react": {
|
|
78
|
+
"optional": true
|
|
79
|
+
},
|
|
80
|
+
"react": {
|
|
81
|
+
"optional": true
|
|
82
|
+
}
|
|
81
83
|
}
|
|
82
84
|
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { type InitStoreOptions, type SetState } from 'floppy-disk/vanilla';
|
|
2
|
+
/**
|
|
3
|
+
* Represents the state of a mutation.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* A mutation does not cache results and only tracks the latest execution.
|
|
7
|
+
*
|
|
8
|
+
* The state transitions are:
|
|
9
|
+
* - `INITIAL` → before any execution
|
|
10
|
+
* - `SUCCESS` → when mutation resolves successfully
|
|
11
|
+
* - `ERROR` → when mutation fails
|
|
12
|
+
*
|
|
13
|
+
* Unlike queries:
|
|
14
|
+
* - No retry mechanism
|
|
15
|
+
* - No caching across executions
|
|
16
|
+
*/
|
|
17
|
+
export type MutationState<TData, TVariable, TError> = {
|
|
18
|
+
isPending: boolean;
|
|
19
|
+
} & ({
|
|
20
|
+
state: 'INITIAL';
|
|
21
|
+
isSuccess: false;
|
|
22
|
+
isError: false;
|
|
23
|
+
variable: undefined;
|
|
24
|
+
data: undefined;
|
|
25
|
+
dataUpdatedAt: undefined;
|
|
26
|
+
error: undefined;
|
|
27
|
+
errorUpdatedAt: undefined;
|
|
28
|
+
} | {
|
|
29
|
+
state: 'SUCCESS';
|
|
30
|
+
isSuccess: true;
|
|
31
|
+
isError: false;
|
|
32
|
+
variable: TVariable;
|
|
33
|
+
data: TData;
|
|
34
|
+
dataUpdatedAt: number;
|
|
35
|
+
error: undefined;
|
|
36
|
+
errorUpdatedAt: undefined;
|
|
37
|
+
} | {
|
|
38
|
+
state: 'ERROR';
|
|
39
|
+
isSuccess: false;
|
|
40
|
+
isError: true;
|
|
41
|
+
variable: TVariable;
|
|
42
|
+
data: undefined;
|
|
43
|
+
dataUpdatedAt: undefined;
|
|
44
|
+
error: TError;
|
|
45
|
+
errorUpdatedAt: number;
|
|
46
|
+
});
|
|
47
|
+
export declare const INITIAL_STATE: {
|
|
48
|
+
state: string;
|
|
49
|
+
isPending: boolean;
|
|
50
|
+
isSuccess: boolean;
|
|
51
|
+
isError: boolean;
|
|
52
|
+
variable: undefined;
|
|
53
|
+
data: undefined;
|
|
54
|
+
dataUpdatedAt: undefined;
|
|
55
|
+
error: undefined;
|
|
56
|
+
errorUpdatedAt: undefined;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Configuration options for a mutation.
|
|
60
|
+
*
|
|
61
|
+
* @remarks
|
|
62
|
+
* Lifecycle callbacks are triggered for each execution.
|
|
63
|
+
*/
|
|
64
|
+
export type MutationOptions<TData, TVariable, TError = Error> = InitStoreOptions<MutationState<TData, TVariable, TError>> & {
|
|
65
|
+
/**
|
|
66
|
+
* Called when the mutation succeeds.\
|
|
67
|
+
* If multiple concurrent executions happened, only the latest execution triggers this callback.
|
|
68
|
+
*/
|
|
69
|
+
onSuccess?: (data: TData, variable: TVariable, stateBeforeExecute: MutationState<TData, TVariable, TError>) => void;
|
|
70
|
+
/**
|
|
71
|
+
* Called when the mutation fails.\
|
|
72
|
+
* If multiple concurrent executions happened, only the latest execution triggers this callback.
|
|
73
|
+
*/
|
|
74
|
+
onError?: (error: TError, variable: TVariable, stateBeforeExecute: MutationState<TData, TVariable, TError>) => void;
|
|
75
|
+
/**
|
|
76
|
+
* Called after the mutation settles (either success or error).\
|
|
77
|
+
* If multiple concurrent executions happened, only the latest execution triggers this callback.
|
|
78
|
+
*/
|
|
79
|
+
onSettled?: (variable: TVariable, stateBeforeExecute: MutationState<TData, TVariable, TError>) => void;
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Creates a mutation store for handling async operations that modify data.
|
|
83
|
+
*
|
|
84
|
+
* @param mutationFn - Async function that performs the mutation
|
|
85
|
+
* @param options - Optional configuration and lifecycle callbacks
|
|
86
|
+
*
|
|
87
|
+
* @returns A function that acts as both:
|
|
88
|
+
* - A React hook for subscribing to mutation state
|
|
89
|
+
* - A mutation controller (execute, reset, etc.)
|
|
90
|
+
*
|
|
91
|
+
* @remarks
|
|
92
|
+
* - Mutations are **not cached** and only track the latest execution.
|
|
93
|
+
* - Designed for operations that change data (e.g. create, update, delete).
|
|
94
|
+
* - No retry mechanism is provided by default.
|
|
95
|
+
* - The mutation always resolves (never throws): the result contains either `data` or `error`.
|
|
96
|
+
* - If multiple executions triggered at the same time:
|
|
97
|
+
* - Only the latest execution is allowed to update the state.
|
|
98
|
+
* - Results from previous executions are ignored if a newer one exists.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* const useCreateUser = createMutation(async (input) => {
|
|
102
|
+
* return api.createUser(input);
|
|
103
|
+
* });
|
|
104
|
+
*
|
|
105
|
+
* const { isPending } = useCreateUser();
|
|
106
|
+
* const result = await useCreateUser.execute({ name: 'John' });
|
|
107
|
+
*/
|
|
108
|
+
export declare const createMutation: <TData, TVariable = undefined, TError = Error>(mutationFn: (variable: TVariable, stateBeforeExecute: MutationState<TData, TVariable, TError>) => Promise<TData>, options?: MutationOptions<TData, TVariable, TError>) => (() => MutationState<TData, TVariable, TError>) & {
|
|
109
|
+
subscribe: (subscriber: import("../vanilla.ts").Subscriber<MutationState<TData, TVariable, TError>>) => () => void;
|
|
110
|
+
getSubscribers: () => Set<import("../vanilla.ts").Subscriber<MutationState<TData, TVariable, TError>>>;
|
|
111
|
+
getState: () => MutationState<TData, TVariable, TError>;
|
|
112
|
+
/**
|
|
113
|
+
* Manually updates the mutation state.
|
|
114
|
+
*
|
|
115
|
+
* @remarks
|
|
116
|
+
* - Intended for advanced use cases.
|
|
117
|
+
* - Prefer using provided mutation actions (`execute`, `reset`) instead.
|
|
118
|
+
*/
|
|
119
|
+
setState: (value: SetState<MutationState<TData, TVariable, TError>>) => void;
|
|
120
|
+
/**
|
|
121
|
+
* Executes the mutation.
|
|
122
|
+
*
|
|
123
|
+
* @param variable - Input passed to the mutation function
|
|
124
|
+
*
|
|
125
|
+
* @returns A promise that always resolves with:
|
|
126
|
+
* - `{ data, variable }` on success
|
|
127
|
+
* - `{ error, variable }` on failure
|
|
128
|
+
*
|
|
129
|
+
* @remarks
|
|
130
|
+
* - The promise never rejects to simplify async handling.
|
|
131
|
+
* - If a mutation is already in progress, a warning is logged.
|
|
132
|
+
* - When a new execution starts, all previous pending executions will resolve with the result of the latest execution.
|
|
133
|
+
*/
|
|
134
|
+
execute: TVariable extends undefined ? () => Promise<{
|
|
135
|
+
variable: undefined;
|
|
136
|
+
data?: TData;
|
|
137
|
+
error?: TError;
|
|
138
|
+
}> : (variable: TVariable) => Promise<{
|
|
139
|
+
variable: TVariable;
|
|
140
|
+
data?: TData;
|
|
141
|
+
error?: TError;
|
|
142
|
+
}>;
|
|
143
|
+
/**
|
|
144
|
+
* Resets the mutation state back to its initial state.
|
|
145
|
+
*
|
|
146
|
+
* @remarks
|
|
147
|
+
* - Does not cancel any ongoing execution.
|
|
148
|
+
* - If an execution is still pending, its result may override the reset state.
|
|
149
|
+
*/
|
|
150
|
+
reset: () => void;
|
|
151
|
+
};
|