@uniqbit/mate-core 0.20.0 → 0.21.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/claude-capability-plugins/companion-mentions/.claude-plugin/plugin.json +1 -1
- package/claude-capability-plugins/companion-mentions/hooks/register.ts +1 -1
- package/claude-plugin/hooks/hooks.json +10 -1
- package/claude-plugin/hooks/rtk-worktree-git.mjs +58 -0
- package/opencode-plugin/src/server.ts +9 -2
- package/package.json +2 -1
- package/src/cli/commands/artifact/delivery/environment.ts +28 -8
- package/src/cli/commands/artifact/delivery/link.ts +28 -9
- package/src/cli/commands/artifact/delivery/output.ts +7 -0
- package/src/cli/commands/companion/update.ts +2 -19
- package/src/cli/commands/factory/factory.ts +11 -1
- package/src/cli/commands/install.ts +47 -36
- package/src/cli/commands/launch/claude.ts +2 -0
- package/src/cli/commands/launch/shared.ts +33 -6
- package/src/cli/commands/plugin/install.ts +27 -11
- package/src/cli/commands/report/index.ts +180 -184
- package/src/cli/commands/report/markdown.ts +67 -72
- package/src/cli/commands/shared/companion-selection.ts +3 -1
- package/src/cli/commands/studio/access.ts +6 -14
- package/src/cli/commands/studio/areas.ts +112 -33
- package/src/cli/commands/studio/companion-add.ts +87 -0
- package/src/cli/commands/studio/companion-removal.ts +15 -8
- package/src/cli/commands/studio/factory-credential.ts +1 -15
- package/src/cli/commands/studio/factory-git-tokens.ts +40 -0
- package/src/cli/commands/studio/factory-relay.ts +18 -0
- package/src/cli/commands/studio/graph-model.ts +61 -0
- package/src/cli/commands/studio/graph.ts +336 -0
- package/src/cli/commands/studio/index.ts +4 -4
- package/src/cli/commands/studio/payload.ts +42 -15
- package/src/cli/commands/studio/repository-add.ts +116 -0
- package/src/cli/commands/studio/routes.ts +6 -0
- package/src/cli/commands/studio/selection.ts +12 -3
- package/src/cli/commands/studio/server.ts +127 -5
- package/src/cli/commands/studio/services.ts +40 -0
- package/src/cli/commands/studio/terminal.ts +45 -3
- package/src/cli/commands/studio/views/vault/markdown-tree.ts +84 -3
- package/src/cli/commands/studio/workset-session.ts +47 -14
- package/src/cli/commands/unwrap.ts +3 -0
- package/src/cli/commands/update.ts +2 -0
- package/src/cli/commands/vscode/release-vsix.json +4 -0
- package/src/cli/commands/vscode/setup.ts +71 -13
- package/src/cli/commands/vscode/vsix.ts +48 -7
- package/src/cli/commands/working/cleanup.ts +40 -8
- package/src/cli/commands/working/worktree.ts +176 -35
- package/src/cli/main.ts +17 -2
- package/src/cli/usage.ts +1 -1
- package/src/hooks/rtk-worktree-git.ts +48 -0
- package/src/hooks/validate-artifact-path.ts +38 -7
- package/src/lib/delivery/branch-template.ts +122 -0
- package/src/lib/delivery/branches.ts +21 -16
- package/src/lib/delivery/checkout.ts +102 -0
- package/src/lib/delivery/environment.ts +2 -0
- package/src/lib/delivery/merge-git.ts +13 -1
- package/src/lib/delivery/record-delivery.ts +83 -29
- package/src/lib/delivery/record.ts +15 -3
- package/src/lib/delivery/resolve-branch.ts +157 -110
- package/src/lib/factory/attach-client.ts +11 -1
- package/src/lib/factory/broker.ts +37 -40
- package/src/lib/factory/client.ts +22 -1
- package/src/lib/factory/clone-url.ts +39 -0
- package/src/lib/factory/command.ts +21 -5
- package/src/lib/factory/git-auth.ts +61 -0
- package/src/lib/factory/git-credential-store.ts +181 -0
- package/src/lib/factory/ids.ts +12 -2
- package/src/lib/factory/outcomes/actions.ts +20 -4
- package/src/lib/factory/outcomes/index.ts +77 -11
- package/src/lib/factory/outcomes/lifecycle.ts +9 -2
- package/src/lib/factory/outcomes/sessions.ts +23 -1
- package/src/lib/factory/prepare.ts +9 -3
- package/src/lib/factory/runner/scheduler.ts +96 -14
- package/src/lib/factory/runner/workflow.ts +5 -1
- package/src/lib/factory/runtime.ts +143 -43
- package/src/lib/orchestrator/companion-hub.ts +6 -1
- package/src/lib/orchestrator/engine-floor.ts +48 -0
- package/src/lib/orchestrator/launcher.ts +34 -1
- package/src/lib/orchestrator/projection-claude-commands.ts +2 -2
- package/src/lib/orchestrator/projection-claude-entry.ts +42 -21
- package/src/lib/orchestrator/projection-claude-links.ts +32 -8
- package/src/lib/orchestrator/projection-claude-skills.ts +2 -2
- package/src/lib/orchestrator/projection-entries.ts +22 -18
- package/src/lib/orchestrator/projection-opencode-copies.ts +65 -21
- package/src/lib/orchestrator/projection-runtime-documents.ts +66 -43
- package/src/lib/orchestrator/projection-types.ts +28 -1
- package/src/lib/orchestrator/refresh-working-claude-links.ts +3 -1
- package/src/lib/orchestrator/session-prepare.ts +6 -1
- package/src/lib/orchestrator/setup-compatibilities.ts +1 -1
- package/src/lib/orchestrator/types.ts +4 -0
- package/src/lib/orchestrator/umbrella-checkouts.ts +102 -0
- package/src/lib/orchestrator/working-repo-projection.ts +24 -4
- package/src/lib/orchestrator/wrap-pass.ts +1 -0
- package/src/lib/public-npm.ts +95 -50
- package/src/lib/session-state.ts +72 -2
- package/src/lib/update-checker.ts +7 -1
- package/src/lib/usage/aggregate.ts +202 -0
- package/src/lib/usage/cache.ts +87 -0
- package/src/lib/usage/claude-pricing.ts +71 -0
- package/src/lib/usage/claude.ts +189 -0
- package/src/lib/usage/collect.ts +56 -0
- package/src/lib/usage/opencode.ts +188 -0
- package/src/lib/usage/records.ts +80 -0
- package/src/lib/usage/rtk-database.ts +72 -0
- package/src/lib/usage/savings.ts +209 -0
- package/src/lib/usage/scope.ts +182 -0
- package/src/lib/usage/source.ts +102 -0
- package/src/lib/usage/types.ts +143 -0
- package/src/lib/usage/window.ts +37 -0
- package/src/lib/workset/factory-local.ts +157 -0
- package/src/lib/workset/record.ts +25 -11
- package/src/lib/workset/repositories.ts +118 -25
- package/src/lib/workset/worktree-include.ts +161 -0
- package/src/lib/workset/worktree-index.ts +6 -2
- package/src/lib/workset/worktree.ts +591 -101
- package/src/opencode/companion-hooks.ts +46 -8
- package/src/opencode/companion-policy.ts +18 -5
- package/src/opencode/enter-worktree.ts +254 -6
- package/src/opencode/session-register.ts +2 -2
- package/src/runtime/claude-bg-isolation.ts +138 -0
- package/src/runtime/companion-git.ts +10 -2
- package/src/runtime/companion-guidance.ts +17 -4
- package/src/runtime/current-branch.ts +16 -2
- package/src/runtime/entered-worktree.ts +24 -0
- package/src/runtime/git-tracked.ts +60 -0
- package/src/runtime/repo-destination.ts +72 -0
- package/src/runtime/repo-local.ts +25 -3
- package/src/runtime/rtk-git-guard.ts +66 -0
- package/src/runtime/runtime-document-manifest.ts +229 -19
- package/src/runtime/runtime-document-manifest.typecheck.ts +10 -0
- package/src/runtime/secret-guard.ts +9 -2
- package/src/runtime/secret-policy-weakening.ts +122 -0
- package/src/runtime/serving-claude-wrap.ts +15 -2
- package/src/runtime/umbrella-folder.ts +56 -0
- package/src/runtime/wrap-state.ts +5 -13
- package/src/studio.ts +37 -0
- package/src/templates/mate-skills/agents/mate-apply-change/SKILL.md +61 -6
- package/src/templates/mate-skills/agents/mate-explore-change/SKILL.md +8 -4
- package/src/templates/mate-skills/agents/mate-propose-change/SKILL.md +9 -5
- package/src/templates/mate-skills/agents/mate-publish-artifact/SKILL.md +5 -4
- package/src/templates/mate-skills/agents/mate-show-me/SKILL.md +10 -4
- package/src/templates/mate-skills/agents/mate-simplify-code/SKILL.md +5 -1
- package/src/templates/mate-skills/claude/mate-apply-change/SKILL.md +61 -6
- package/src/templates/mate-skills/claude/mate-explore-change/SKILL.md +8 -4
- package/src/templates/mate-skills/claude/mate-propose-change/SKILL.md +9 -5
- package/src/templates/mate-skills/claude/mate-publish-artifact/SKILL.md +5 -4
- package/src/templates/mate-skills/claude/mate-show-me/SKILL.md +10 -4
- package/src/templates/mate-skills/claude/mate-simplify-code/SKILL.md +5 -1
- package/src/tools/setup/__snapshots__/runtime-surface-golden.test.ts.snap +307 -42
- package/src/tools/setup/capabilities/rtk.ts +93 -1
- package/src/tools/setup/dynamic-plugins/audiences.ts +65 -21
- package/src/tools/setup/dynamic-plugins/frozen.ts +32 -13
- package/src/tools/setup/dynamic-plugins/hydrate.ts +4 -4
- package/src/tools/setup/dynamic-plugins/install.ts +24 -22
- package/src/tools/setup/dynamic-plugins/loader.ts +11 -7
- package/src/tools/setup/dynamic-plugins/paths.ts +30 -10
- package/src/tools/setup/dynamic-plugins/verify.ts +10 -7
- package/src/tools/setup/dynamic-plugins/workspaces.ts +153 -0
- package/src/tools/setup/mate.ts +3 -3
- package/src/tools/setup/plugins/gitignore.ts +15 -9
- package/src/tools/setup/providers/claude-plugin-hooks.ts +12 -0
- package/src/tools/setup/providers/claude.ts +25 -3
- package/src/tools/setup/utils.ts +4 -2
- package/src/tools/setup/working-repo-cleanup.ts +2 -2
- package/src/tools/setup/working-repo-local-state.ts +2 -0
- package/src/tools/setup.ts +25 -14
- package/studio-dist/client/assets/app-D9cw6szy.css +2 -0
- package/studio-dist/client/assets/artifacts-WD2ZXYrx.js +1 -0
- package/studio-dist/client/assets/button-Br0O1sXw.js +1 -0
- package/studio-dist/client/assets/combobox-DA7RAUUZ.js +1 -0
- package/studio-dist/client/assets/companion-view-CrabvxS8.js +1 -0
- package/studio-dist/client/assets/factory-DGS0E16C.js +6 -0
- package/studio-dist/client/assets/fields-6aXBpFhJ.js +1 -0
- package/studio-dist/client/assets/getting-started-Clde4w6P.js +1 -0
- package/studio-dist/client/assets/getting-started-D6kgWwWZ.js +1 -0
- package/studio-dist/client/assets/getting-started._topic-BiunccbT.js +1 -0
- package/studio-dist/client/assets/getting-started.index-DpiNoGw8.js +1 -0
- package/studio-dist/client/assets/graph-B_TCBVbu.js +2 -0
- package/studio-dist/client/assets/graph-canvas-Ba37Y5AE.js +3242 -0
- package/studio-dist/client/assets/guides-Du_RtJxP.js +1 -0
- package/studio-dist/client/assets/index-Df8L1LQa.js +40 -0
- package/studio-dist/client/assets/jsx-runtime-MZURdZre.js +1 -0
- package/studio-dist/client/assets/markdown-view-BOX6x0rL.js +1 -0
- package/studio-dist/client/assets/panel-dbmCx7Mx.js +1 -0
- package/studio-dist/client/assets/queries-RvHeXY12.js +1 -0
- package/studio-dist/client/assets/react-3QStrcDO.js +27 -0
- package/studio-dist/client/assets/report-url-XRL0x1SY.js +1 -0
- package/studio-dist/client/assets/reports-r3IK7Doz.js +1 -0
- package/studio-dist/client/assets/routes-C0A5LTFc.js +1 -0
- package/studio-dist/client/assets/select-CnaMd617.js +1 -0
- package/studio-dist/client/assets/skills-CzjzHpBT.js +1 -0
- package/studio-dist/client/assets/specs-B0ldF_6J.js +1 -0
- package/studio-dist/client/assets/specs-view-BnCLA0gW.js +1 -0
- package/studio-dist/client/assets/spinner-Dcm3fqK1.js +1 -0
- package/studio-dist/client/assets/steps-C0p2N1Pb.js +1 -0
- package/studio-dist/client/assets/store-Dw34catY.js +1 -0
- package/studio-dist/client/assets/studio-Cpz_HRHp.js +100 -0
- package/studio-dist/client/assets/studio-queries-ibmpFo6R.js +2 -0
- package/studio-dist/client/assets/usage-C2QJ_0IZ.js +55 -0
- package/studio-dist/client/assets/useAnchoredPopupScrollLock-Bj-FWb8z.js +1 -0
- package/studio-dist/client/assets/useMutation-aAr2uiay.js +1 -0
- package/studio-dist/client/assets/useSelector-AYcH7lB0.js +2 -0
- package/studio-dist/client/assets/vault-BGcxkSIY.js +1 -0
- package/studio-dist/client/assets/warnings-R1cE1rLf.js +1 -0
- package/studio-dist/client/assets/workflow-C2qTrMDH.js +1 -0
- package/studio-dist/server/assets/__23tanstack-start-server-fn-resolver-Bn896ZYj.js +122 -0
- package/studio-dist/server/assets/_tanstack-start-manifest_v.js +102 -35
- package/studio-dist/server/assets/{actions-Bvgj9eb8.js → actions-DYSv-9VV.js} +10 -4
- package/studio-dist/server/assets/artifacts-DnF9L3dZ.js +216 -0
- package/studio-dist/server/assets/{button-CIJqAhni.js → button-CgOIVdmD.js} +13 -1702
- package/studio-dist/server/assets/combobox-DV_s9W0s.js +483 -0
- package/studio-dist/server/assets/{companion-view-DISASvdJ.js → companion-view-BKVXD3TO.js} +3 -3
- package/studio-dist/server/assets/companions-C7fctnU9.js +31 -0
- package/studio-dist/server/assets/{createServerRpc-tK2lE9R2.js → createServerRpc-bFKFpGFG.js} +1 -1
- package/studio-dist/server/assets/{factory-DUTa0eDs.js → factory-C7Ybg0xG.js} +1256 -353
- package/studio-dist/server/assets/{factory-DGPmNGbo.js → factory-Df5uAHjb.js} +53 -6
- package/studio-dist/server/assets/fields-BbieQTD0.js +1190 -0
- package/studio-dist/server/assets/getting-started-B136TP57.js +4 -0
- package/studio-dist/server/assets/getting-started-CRxMWorQ.js +67 -0
- package/studio-dist/server/assets/getting-started._topic-DJKQAzYl.js +13 -0
- package/studio-dist/server/assets/getting-started.index-D84gec1t.js +13 -0
- package/studio-dist/server/assets/graph-CSje3029.js +801 -0
- package/studio-dist/server/assets/graph-DDZzXM1L.js +16 -0
- package/studio-dist/server/assets/graph-canvas-22bWB5-2.js +21066 -0
- package/studio-dist/server/assets/graph-wire-BdSUx0G5.js +58 -0
- package/studio-dist/server/assets/guides-D9dk1Frq.js +13 -0
- package/studio-dist/server/assets/guides-IE-19POz.js +5 -0
- package/studio-dist/server/assets/guides.server-vcYRY5wx.js +66 -0
- package/studio-dist/server/assets/jsx-runtime-Bt0s0YKS.js +451 -0
- package/studio-dist/server/assets/{markdown-view-Bn7xPdks.js → markdown-view-BR8dJ7aR.js} +28 -9
- package/studio-dist/server/assets/{panel-3Jmn4awF.js → panel-BR_t9iMM.js} +4 -4
- package/studio-dist/server/assets/queries-DWoTin-q.js +32 -0
- package/studio-dist/server/assets/react-CdqSUhaL.js +89 -0
- package/studio-dist/server/assets/redirect-BaZ_QyqF.js +39 -0
- package/studio-dist/server/assets/report-url-BUXPcE_8.js +9 -0
- package/studio-dist/server/assets/{reports-D6YMqhnO.js → reports-CQ9qQ1-n.js} +9 -10
- package/studio-dist/server/assets/{reports-C06RXVE2.js → reports-dYDEoBeq.js} +3 -3
- package/studio-dist/server/assets/{router-C8MGqlAY.js → router-DkRHNTpE.js} +787 -2862
- package/studio-dist/server/assets/{routes-BcFgvqf7.js → routes-B6GfOew4.js} +22 -171
- package/studio-dist/server/assets/{search-JAKurDNh.js → search-BJnWLfkG.js} +21 -2
- package/studio-dist/server/assets/select-Af7Wibii.js +1609 -0
- package/studio-dist/server/assets/{shell-DS3wUexh.js → shell-Hq-hXgnT.js} +6 -5
- package/studio-dist/server/assets/{skills-BEnOxiDl.js → skills-B7nEuVbx.js} +9 -8
- package/studio-dist/server/assets/{skills-CtrBAtX3.js → skills-B_YYL7q8.js} +3 -3
- package/studio-dist/server/assets/specs-BpLUFntA.js +51 -0
- package/studio-dist/server/assets/specs-view-0peMVd22.js +426 -0
- package/studio-dist/server/assets/spinner-CdwJW4VB.js +20 -0
- package/studio-dist/server/assets/{steps-Dq_CwY_s.js → steps-D56DApvK.js} +1 -1
- package/studio-dist/server/assets/store-Cs17okNG.js +280 -0
- package/studio-dist/server/assets/studio-CAcKp3bM.js +10340 -0
- package/studio-dist/server/assets/studio-queries-BtRGzDdh.js +1451 -0
- package/studio-dist/server/assets/usage-COdjd1gD.js +25570 -0
- package/studio-dist/server/assets/usage-f6pqE1tm.js +24 -0
- package/studio-dist/server/assets/useAnchoredPopupScrollLock-DlBKHRw7.js +756 -0
- package/studio-dist/server/assets/useMutation-DzTZGenE.js +690 -0
- package/studio-dist/server/assets/{__23tanstack-start-server-fn-resolver-M2GBs8s3.js → useSelector-DUmCUXAs.js} +4 -668
- package/studio-dist/server/assets/{vault-Fvxt2Tzl.js → vault-BDuIUq0F.js} +8 -5
- package/studio-dist/server/assets/{vault-BmWnryh3.js → vault-BVLp6zu3.js} +3 -3
- package/studio-dist/server/assets/{views-BgYa-za2.js → views-flRUr21z.js} +4 -4
- package/studio-dist/server/assets/{warnings-DzR6fyZh.js → warnings-CDy9pdgL.js} +3 -2
- package/studio-dist/server/assets/{delivery-path-C29fBynY.js → workflow-D9GKI2EV.js} +132 -106
- package/studio-dist/server/server.js +582 -8
- package/src/cli/commands/report/collector.ts +0 -288
- package/studio-dist/client/assets/app-CdPiFv59.css +0 -2
- package/studio-dist/client/assets/button-BIIHE0EQ.js +0 -3
- package/studio-dist/client/assets/companion-view-DM6YyXaR.js +0 -1
- package/studio-dist/client/assets/delivery-path-DELxj0Av.js +0 -1
- package/studio-dist/client/assets/factory-BZy0E8ty.js +0 -4
- package/studio-dist/client/assets/index-B3wkMzjA.js +0 -40
- package/studio-dist/client/assets/markdown-view-pQ2Qp_i5.js +0 -1
- package/studio-dist/client/assets/panel-CbajlC3p.js +0 -1
- package/studio-dist/client/assets/prepaint-CwSrpdFA.js +0 -27
- package/studio-dist/client/assets/react-CN2GSx7x.js +0 -1
- package/studio-dist/client/assets/reports-DllhYQ5_.js +0 -1
- package/studio-dist/client/assets/rolldown-runtime-WkwZ5CU-.js +0 -1
- package/studio-dist/client/assets/routes-COm-G3mK.js +0 -1
- package/studio-dist/client/assets/skills-DmWAy-jZ.js +0 -1
- package/studio-dist/client/assets/specs-BG3Wi3ln.js +0 -1
- package/studio-dist/client/assets/steps-DFsH3Neu.js +0 -1
- package/studio-dist/client/assets/vault-B2q1nBY5.js +0 -1
- package/studio-dist/client/assets/warnings-BSXIyuSQ.js +0 -1
- package/studio-dist/client/assets/workflow-DlsQD6Hc.js +0 -1
- package/studio-dist/server/assets/companions-BYbHWKOZ.js +0 -15
- package/studio-dist/server/assets/createServerFn-F5sAOF8V.js +0 -459
- package/studio-dist/server/assets/prepaint-DJfTQ_VH.js +0 -45
- package/studio-dist/server/assets/react-Ce6dj15C.js +0 -46
- package/studio-dist/server/assets/rolldown-runtime-DYf_mQCB.js +0 -34
- package/studio-dist/server/assets/specs-C8rv34-5.js +0 -211
- package/studio-dist/server/assets/studio-BeltxxKt.js +0 -216
- package/studio-dist/server/assets/workflow-liRYecHk.js +0 -73
|
@@ -0,0 +1,690 @@
|
|
|
1
|
+
import { Fr as notifyManager, Lr as Subscribable, Mr as Removable, Nr as createRetryer, zr as useQueryClient } from "./button-CgOIVdmD.js";
|
|
2
|
+
import { n as require_react, o as __toESM } from "./jsx-runtime-Bt0s0YKS.js";
|
|
3
|
+
import { G as hashKey, Q as noop, nt as shallowEqualObjects, rt as shouldThrowError } from "./studio-queries-BtRGzDdh.js";
|
|
4
|
+
// ../../node_modules/.bun/@tanstack+query-core@5.105.3/node_modules/@tanstack/query-core/build/modern/mutation.js
|
|
5
|
+
/**
|
|
6
|
+
* Represents a single mutation attempt. A `Mutation` holds the mutation's
|
|
7
|
+
* options, state (data/error/status), and the `MutationObserver`s currently
|
|
8
|
+
* subscribed to it.
|
|
9
|
+
*
|
|
10
|
+
* Instances are created and managed internally by `MutationCache`; application
|
|
11
|
+
* code typically interacts with mutations indirectly through `QueryClient` or
|
|
12
|
+
* a framework hook like `useMutation`. Direct access to a `Mutation` instance
|
|
13
|
+
* is possible via `mutationCache.find()`/`getAll()` for inspecting cache state.
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* const mutationCache = queryClient.getMutationCache()
|
|
17
|
+
*
|
|
18
|
+
* const mutation = mutationCache.find({ mutationKey: ['addPost'] })
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
var Mutation = class extends Removable {
|
|
22
|
+
#client;
|
|
23
|
+
#observers;
|
|
24
|
+
#mutationCache;
|
|
25
|
+
#retryer;
|
|
26
|
+
constructor(config) {
|
|
27
|
+
super();
|
|
28
|
+
this.#client = config.client;
|
|
29
|
+
this.mutationId = config.mutationId;
|
|
30
|
+
this.#mutationCache = config.mutationCache;
|
|
31
|
+
this.#observers = [];
|
|
32
|
+
this.state = config.state || getDefaultState();
|
|
33
|
+
this.setOptions(config.options);
|
|
34
|
+
this.scheduleGc();
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Replaces the mutation's options and applies their `gcTime`.
|
|
38
|
+
* @internal
|
|
39
|
+
* @param options - The new mutation options.
|
|
40
|
+
*/
|
|
41
|
+
setOptions(options) {
|
|
42
|
+
this.options = options;
|
|
43
|
+
this.updateGcTime(this.options.gcTime);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The `meta` object passed in the mutation's options, if any.
|
|
47
|
+
* @returns The mutation's `meta`, or `undefined` if none was set.
|
|
48
|
+
*/
|
|
49
|
+
get meta() {
|
|
50
|
+
return this.options.meta;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Subscribes an observer to the mutation and stops its garbage collection.
|
|
54
|
+
* @internal
|
|
55
|
+
* @param observer - The observer to add. Adding an observer that is already subscribed does
|
|
56
|
+
* nothing.
|
|
57
|
+
*/
|
|
58
|
+
addObserver(observer) {
|
|
59
|
+
if (!this.#observers.includes(observer)) {
|
|
60
|
+
this.#observers.push(observer);
|
|
61
|
+
this.clearGcTimeout();
|
|
62
|
+
this.#mutationCache.notify({
|
|
63
|
+
type: "observerAdded",
|
|
64
|
+
mutation: this,
|
|
65
|
+
observer
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Unsubscribes an observer from the mutation.
|
|
71
|
+
* @internal
|
|
72
|
+
* @param observer - The observer to remove. Garbage collection is scheduled afterwards.
|
|
73
|
+
*/
|
|
74
|
+
removeObserver(observer) {
|
|
75
|
+
this.#observers = this.#observers.filter((x) => x !== observer);
|
|
76
|
+
this.scheduleGc();
|
|
77
|
+
this.#mutationCache.notify({
|
|
78
|
+
type: "observerRemoved",
|
|
79
|
+
mutation: this,
|
|
80
|
+
observer
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
optionalRemove() {
|
|
84
|
+
if (!this.#observers.length) {
|
|
85
|
+
if (this.state.status === "pending") this.scheduleGc();
|
|
86
|
+
else this.#mutationCache.remove(this);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Resumes a mutation that is currently paused or was restored from a
|
|
91
|
+
* dehydrated, still-`pending` state.
|
|
92
|
+
*
|
|
93
|
+
* - If this mutation has an active retryer (it paused mid-attempt, e.g. due
|
|
94
|
+
* to the network mode or scope-based queuing), its retryer is resumed.
|
|
95
|
+
* - Otherwise, if the mutation's status is still `pending` (e.g. it was
|
|
96
|
+
* dehydrated while an attempt was in flight and never got a retryer in
|
|
97
|
+
* this instance), `execute` is called again with the last known variables.
|
|
98
|
+
* - Otherwise the mutation has already settled and this resolves immediately
|
|
99
|
+
* without running anything again.
|
|
100
|
+
* @returns A promise that settles with the resumed mutation: it rejects if the mutation fails.
|
|
101
|
+
* It resolves immediately if the mutation has already settled.
|
|
102
|
+
* @see {@link Mutation#execute}
|
|
103
|
+
* @example
|
|
104
|
+
* ```ts
|
|
105
|
+
* // typically driven by reconnect handling, e.g. queryClient.resumePausedMutations()
|
|
106
|
+
* const mutation = mutationCache.find({ mutationKey: ['addPost'] })
|
|
107
|
+
* await mutation?.continue()
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
continue() {
|
|
111
|
+
return this.#retryer?.continue() ?? (this.state.status === "pending" ? this.execute(this.state.variables) : Promise.resolve());
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Runs the mutation function for the given variables through a retryer, and
|
|
115
|
+
* drives the mutation's state and lifecycle callbacks through to settlement.
|
|
116
|
+
*
|
|
117
|
+
* If this mutation's state is already `pending` when `execute` is called
|
|
118
|
+
* (i.e. it was restored, still in-flight, from a dehydrated state), the
|
|
119
|
+
* `onMutate` step is skipped and a `continue` action is dispatched to
|
|
120
|
+
* unpause it; otherwise a `pending` action is dispatched first, then the
|
|
121
|
+
* mutation cache's `onMutate` and the mutation's own `onMutate` option are
|
|
122
|
+
* awaited in that order, and the resulting context is stored.
|
|
123
|
+
*
|
|
124
|
+
* The mutation function is then run (subject to `retry`/`retryDelay`/
|
|
125
|
+
* `networkMode`, and to the mutation cache's scope-based serialization).
|
|
126
|
+
* On success, the cache's `onSuccess`/`onSettled` callbacks run before the
|
|
127
|
+
* mutation's own `onSuccess`/`onSettled` options, a `success` action is
|
|
128
|
+
* dispatched, and the resolved data is returned. On failure, the same
|
|
129
|
+
* cache-then-option ordering is used for `onError`/`onSettled`, but each of
|
|
130
|
+
* those four callbacks is individually caught so that a throwing callback
|
|
131
|
+
* cannot mask the original error; an `error` action is then dispatched and
|
|
132
|
+
* the original error is re-thrown.
|
|
133
|
+
* @param variables - The variables passed to the `mutationFn`.
|
|
134
|
+
* @returns A promise that resolves with the mutation's data, or rejects with its error.
|
|
135
|
+
* @see {@link Mutation#continue}
|
|
136
|
+
* @example
|
|
137
|
+
* ```ts
|
|
138
|
+
* // Called internally by `MutationObserver.mutate` and `Mutation.continue` —
|
|
139
|
+
* // applications normally trigger mutations through those, not this method.
|
|
140
|
+
* const data = await mutation.execute(variables)
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
async execute(variables) {
|
|
144
|
+
const onContinue = () => {
|
|
145
|
+
this.#dispatch({ type: "continue" });
|
|
146
|
+
};
|
|
147
|
+
const mutationFnContext = {
|
|
148
|
+
client: this.#client,
|
|
149
|
+
meta: this.options.meta,
|
|
150
|
+
mutationKey: this.options.mutationKey
|
|
151
|
+
};
|
|
152
|
+
const retryer = this.#retryer = createRetryer({
|
|
153
|
+
fn: () => {
|
|
154
|
+
if (!this.options.mutationFn) return Promise.reject(/* @__PURE__ */ new Error("No mutationFn found"));
|
|
155
|
+
return this.options.mutationFn(variables, mutationFnContext);
|
|
156
|
+
},
|
|
157
|
+
onFail: (failureCount, error) => {
|
|
158
|
+
this.#dispatch({
|
|
159
|
+
type: "failed",
|
|
160
|
+
failureCount,
|
|
161
|
+
error
|
|
162
|
+
});
|
|
163
|
+
},
|
|
164
|
+
onPause: () => {
|
|
165
|
+
this.#dispatch({ type: "pause" });
|
|
166
|
+
},
|
|
167
|
+
onContinue,
|
|
168
|
+
retry: this.options.retry ?? 0,
|
|
169
|
+
retryDelay: this.options.retryDelay,
|
|
170
|
+
networkMode: this.options.networkMode,
|
|
171
|
+
canRun: () => this.#mutationCache.canRun(this)
|
|
172
|
+
});
|
|
173
|
+
const restored = this.state.status === "pending";
|
|
174
|
+
const isPaused = !retryer.canStart();
|
|
175
|
+
try {
|
|
176
|
+
if (restored) onContinue();
|
|
177
|
+
else {
|
|
178
|
+
this.#dispatch({
|
|
179
|
+
type: "pending",
|
|
180
|
+
variables,
|
|
181
|
+
isPaused
|
|
182
|
+
});
|
|
183
|
+
if (this.#mutationCache.config.onMutate) await this.#mutationCache.config.onMutate(variables, this, mutationFnContext);
|
|
184
|
+
const context = await this.options.onMutate?.(variables, mutationFnContext);
|
|
185
|
+
if (context !== this.state.context) this.#dispatch({
|
|
186
|
+
type: "pending",
|
|
187
|
+
context,
|
|
188
|
+
variables,
|
|
189
|
+
isPaused
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
if (this.state.isPaused && retryer.canStart()) onContinue();
|
|
193
|
+
const data = await retryer.start();
|
|
194
|
+
await this.#mutationCache.config.onSuccess?.(data, variables, this.state.context, this, mutationFnContext);
|
|
195
|
+
await this.options.onSuccess?.(data, variables, this.state.context, mutationFnContext);
|
|
196
|
+
await this.#mutationCache.config.onSettled?.(data, null, this.state.variables, this.state.context, this, mutationFnContext);
|
|
197
|
+
await this.options.onSettled?.(data, null, variables, this.state.context, mutationFnContext);
|
|
198
|
+
this.#dispatch({
|
|
199
|
+
type: "success",
|
|
200
|
+
data
|
|
201
|
+
});
|
|
202
|
+
return data;
|
|
203
|
+
} catch (error) {
|
|
204
|
+
try {
|
|
205
|
+
await this.#mutationCache.config.onError?.(error, variables, this.state.context, this, mutationFnContext);
|
|
206
|
+
} catch (e) {
|
|
207
|
+
Promise.reject(e);
|
|
208
|
+
}
|
|
209
|
+
try {
|
|
210
|
+
await this.options.onError?.(error, variables, this.state.context, mutationFnContext);
|
|
211
|
+
} catch (e) {
|
|
212
|
+
Promise.reject(e);
|
|
213
|
+
}
|
|
214
|
+
try {
|
|
215
|
+
await this.#mutationCache.config.onSettled?.(void 0, error, this.state.variables, this.state.context, this, mutationFnContext);
|
|
216
|
+
} catch (e) {
|
|
217
|
+
Promise.reject(e);
|
|
218
|
+
}
|
|
219
|
+
try {
|
|
220
|
+
await this.options.onSettled?.(void 0, error, variables, this.state.context, mutationFnContext);
|
|
221
|
+
} catch (e) {
|
|
222
|
+
Promise.reject(e);
|
|
223
|
+
}
|
|
224
|
+
this.#dispatch({
|
|
225
|
+
type: "error",
|
|
226
|
+
error
|
|
227
|
+
});
|
|
228
|
+
throw error;
|
|
229
|
+
} finally {
|
|
230
|
+
if (this.#retryer === retryer) this.#retryer = void 0;
|
|
231
|
+
this.#mutationCache.runNext(this);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
#dispatch(action) {
|
|
235
|
+
const reducer = (state) => {
|
|
236
|
+
switch (action.type) {
|
|
237
|
+
case "failed": return {
|
|
238
|
+
...state,
|
|
239
|
+
failureCount: action.failureCount,
|
|
240
|
+
failureReason: action.error
|
|
241
|
+
};
|
|
242
|
+
case "pause": return {
|
|
243
|
+
...state,
|
|
244
|
+
isPaused: true
|
|
245
|
+
};
|
|
246
|
+
case "continue": return {
|
|
247
|
+
...state,
|
|
248
|
+
isPaused: false
|
|
249
|
+
};
|
|
250
|
+
case "pending": return {
|
|
251
|
+
...state,
|
|
252
|
+
context: action.context,
|
|
253
|
+
data: void 0,
|
|
254
|
+
failureCount: 0,
|
|
255
|
+
failureReason: null,
|
|
256
|
+
error: null,
|
|
257
|
+
isPaused: action.isPaused,
|
|
258
|
+
status: "pending",
|
|
259
|
+
variables: action.variables,
|
|
260
|
+
submittedAt: Date.now()
|
|
261
|
+
};
|
|
262
|
+
case "success": return {
|
|
263
|
+
...state,
|
|
264
|
+
data: action.data,
|
|
265
|
+
failureCount: 0,
|
|
266
|
+
failureReason: null,
|
|
267
|
+
error: null,
|
|
268
|
+
status: "success",
|
|
269
|
+
isPaused: false
|
|
270
|
+
};
|
|
271
|
+
case "error": return {
|
|
272
|
+
...state,
|
|
273
|
+
data: void 0,
|
|
274
|
+
error: action.error,
|
|
275
|
+
failureCount: state.failureCount + 1,
|
|
276
|
+
failureReason: action.error,
|
|
277
|
+
isPaused: false,
|
|
278
|
+
status: "error"
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
this.state = reducer(this.state);
|
|
283
|
+
notifyManager.batch(() => {
|
|
284
|
+
this.#observers.forEach((observer) => {
|
|
285
|
+
observer.onMutationUpdate(action);
|
|
286
|
+
});
|
|
287
|
+
this.#mutationCache.notify({
|
|
288
|
+
mutation: this,
|
|
289
|
+
type: "updated",
|
|
290
|
+
action
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
/**
|
|
296
|
+
* Returns the initial state of a mutation: `'idle'`, with no data, error, or variables.
|
|
297
|
+
* @returns The initial mutation state.
|
|
298
|
+
*/
|
|
299
|
+
function getDefaultState() {
|
|
300
|
+
return {
|
|
301
|
+
context: void 0,
|
|
302
|
+
data: void 0,
|
|
303
|
+
error: null,
|
|
304
|
+
failureCount: 0,
|
|
305
|
+
failureReason: null,
|
|
306
|
+
isPaused: false,
|
|
307
|
+
status: "idle",
|
|
308
|
+
variables: void 0,
|
|
309
|
+
submittedAt: 0
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
// ../../node_modules/.bun/@tanstack+query-core@5.105.3/node_modules/@tanstack/query-core/build/modern/mutationObserver.js
|
|
313
|
+
/**
|
|
314
|
+
* Observes a single mutation and derives a `MutationObserverResult` from it.
|
|
315
|
+
* A framework hook like `useMutation` creates one `MutationObserver` per hook
|
|
316
|
+
* call, keeps it stable across re-renders, calls `setOptions` when the options
|
|
317
|
+
* passed to the hook change, subscribes to it to re-render on updates, and
|
|
318
|
+
* reads `getCurrentResult()` for the value to return. Calling `mutate()`
|
|
319
|
+
* builds a new underlying `Mutation` in the `MutationCache` and executes it.
|
|
320
|
+
* @example
|
|
321
|
+
* ```ts
|
|
322
|
+
* const observer = new MutationObserver(queryClient, {
|
|
323
|
+
* mutationFn: (variables: { title: string }) => addPost(variables),
|
|
324
|
+
* })
|
|
325
|
+
* ```
|
|
326
|
+
*/
|
|
327
|
+
var MutationObserver = class extends Subscribable {
|
|
328
|
+
#client;
|
|
329
|
+
#currentResult = void 0;
|
|
330
|
+
#currentMutation;
|
|
331
|
+
#mutateOptions;
|
|
332
|
+
constructor(client, options) {
|
|
333
|
+
super();
|
|
334
|
+
this.#client = client;
|
|
335
|
+
this.setOptions(options);
|
|
336
|
+
this.bindMethods();
|
|
337
|
+
this.#updateResult();
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Binds the methods of the result (`mutate` and `reset`) to the observer, so they keep working when
|
|
341
|
+
* destructured from it.
|
|
342
|
+
*/
|
|
343
|
+
bindMethods() {
|
|
344
|
+
this.mutate = this.mutate.bind(this);
|
|
345
|
+
this.reset = this.reset.bind(this);
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Updates the observer's options.
|
|
349
|
+
*
|
|
350
|
+
* If the new `mutationKey` differs from the previous one (and both were
|
|
351
|
+
* defined), the observer is reset, detaching it from the mutation it was
|
|
352
|
+
* observing. Otherwise, if the currently observed mutation is still
|
|
353
|
+
* `pending`, its options are updated in place as well.
|
|
354
|
+
* @param options - The new mutation observer options. They are defaulted with {@link
|
|
355
|
+
* QueryClient#defaultMutationOptions} before being applied.
|
|
356
|
+
* @example
|
|
357
|
+
* ```ts
|
|
358
|
+
* observer.setOptions({
|
|
359
|
+
* mutationFn: (variables: { title: string }) => addPost(variables),
|
|
360
|
+
* onSuccess: (data) => console.log(data),
|
|
361
|
+
* })
|
|
362
|
+
* ```
|
|
363
|
+
*/
|
|
364
|
+
setOptions(options) {
|
|
365
|
+
const prevOptions = this.options;
|
|
366
|
+
this.options = this.#client.defaultMutationOptions(options);
|
|
367
|
+
if (!shallowEqualObjects(this.options, prevOptions)) this.#client.getMutationCache().notify({
|
|
368
|
+
type: "observerOptionsUpdated",
|
|
369
|
+
mutation: this.#currentMutation,
|
|
370
|
+
observer: this
|
|
371
|
+
});
|
|
372
|
+
if (prevOptions?.mutationKey && this.options.mutationKey && hashKey(prevOptions.mutationKey) !== hashKey(this.options.mutationKey)) this.reset();
|
|
373
|
+
else if (this.#currentMutation?.state.status === "pending") this.#currentMutation.setOptions(this.options);
|
|
374
|
+
}
|
|
375
|
+
onSubscribe() {
|
|
376
|
+
if (this.listeners.size === 1 && this.#currentMutation) {
|
|
377
|
+
this.#currentMutation.addObserver(this);
|
|
378
|
+
this.#updateResult();
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
onUnsubscribe() {
|
|
382
|
+
if (!this.hasListeners()) this.#currentMutation?.removeObserver(this);
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Updates the observer's result when the observed mutation changes, and notifies its listeners.
|
|
386
|
+
* @internal
|
|
387
|
+
* @param action - The action that updated the observed mutation, passed on to the observer's
|
|
388
|
+
* callbacks.
|
|
389
|
+
*/
|
|
390
|
+
onMutationUpdate(action) {
|
|
391
|
+
this.#updateResult();
|
|
392
|
+
this.#notify(action);
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Returns the observer's current result, derived from the observed
|
|
396
|
+
* mutation's state (or the default, `idle` state if no mutation has been
|
|
397
|
+
* built yet, e.g. before the first `mutate()` call or after `reset()`).
|
|
398
|
+
* @returns The observer's latest result.
|
|
399
|
+
*/
|
|
400
|
+
getCurrentResult() {
|
|
401
|
+
return this.#currentResult;
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Detaches the observer from the mutation it is currently observing (if
|
|
405
|
+
* any) and resets the observed result back to its default, `idle` state.
|
|
406
|
+
*
|
|
407
|
+
* This does not cancel an in-flight mutation; the mutation itself keeps
|
|
408
|
+
* running to completion and its own callbacks still fire, but this
|
|
409
|
+
* observer stops reflecting its state and a subsequent `mutate()` call
|
|
410
|
+
* will build a brand new mutation.
|
|
411
|
+
* @see {@link MutationObserver#mutate}
|
|
412
|
+
* @example
|
|
413
|
+
* ```ts
|
|
414
|
+
* observer.reset()
|
|
415
|
+
* ```
|
|
416
|
+
*/
|
|
417
|
+
reset() {
|
|
418
|
+
this.#currentMutation?.removeObserver(this);
|
|
419
|
+
this.#currentMutation = void 0;
|
|
420
|
+
this.#updateResult();
|
|
421
|
+
this.#notify();
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* Builds a new `Mutation` in the `MutationCache` using the observer's
|
|
425
|
+
* current options, detaches this observer from any previously observed
|
|
426
|
+
* mutation, attaches it to the new one, and executes it with the given
|
|
427
|
+
* variables.
|
|
428
|
+
*
|
|
429
|
+
* The optional per-call `options` (`onSuccess`/`onError`/`onSettled`) are
|
|
430
|
+
* invoked once the mutation settles, in addition to any callbacks defined
|
|
431
|
+
* on the observer's own options.
|
|
432
|
+
* @param variables - The variables passed to the `mutationFn`.
|
|
433
|
+
* @param options - Per-call `onSuccess`, `onError`, and `onSettled` callbacks.
|
|
434
|
+
* @returns A promise that resolves with the mutation's data, or rejects with its error.
|
|
435
|
+
* @example
|
|
436
|
+
* ```ts
|
|
437
|
+
* await observer.mutate(
|
|
438
|
+
* { title: 'New post' },
|
|
439
|
+
* { onSuccess: (data) => console.log(data) },
|
|
440
|
+
* )
|
|
441
|
+
* ```
|
|
442
|
+
*/
|
|
443
|
+
mutate(variables, options) {
|
|
444
|
+
this.#mutateOptions = options;
|
|
445
|
+
this.#currentMutation?.removeObserver(this);
|
|
446
|
+
this.#currentMutation = this.#client.getMutationCache().build(this.#client, this.options);
|
|
447
|
+
this.#currentMutation.addObserver(this);
|
|
448
|
+
return this.#currentMutation.execute(variables);
|
|
449
|
+
}
|
|
450
|
+
#updateResult() {
|
|
451
|
+
const state = this.#currentMutation?.state ?? getDefaultState();
|
|
452
|
+
this.#currentResult = {
|
|
453
|
+
...state,
|
|
454
|
+
isPending: state.status === "pending",
|
|
455
|
+
isSuccess: state.status === "success",
|
|
456
|
+
isError: state.status === "error",
|
|
457
|
+
isIdle: state.status === "idle",
|
|
458
|
+
mutate: this.mutate,
|
|
459
|
+
reset: this.reset
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
#notify(action) {
|
|
463
|
+
notifyManager.batch(() => {
|
|
464
|
+
const mutateOptions = this.#mutateOptions;
|
|
465
|
+
if (mutateOptions && this.hasListeners()) {
|
|
466
|
+
const variables = this.#currentResult.variables;
|
|
467
|
+
const onMutateResult = this.#currentResult.context;
|
|
468
|
+
const context = {
|
|
469
|
+
client: this.#client,
|
|
470
|
+
meta: this.options.meta,
|
|
471
|
+
mutationKey: this.options.mutationKey
|
|
472
|
+
};
|
|
473
|
+
if (action?.type === "success") {
|
|
474
|
+
try {
|
|
475
|
+
mutateOptions.onSuccess?.(action.data, variables, onMutateResult, context);
|
|
476
|
+
} catch (e) {
|
|
477
|
+
Promise.reject(e);
|
|
478
|
+
}
|
|
479
|
+
try {
|
|
480
|
+
mutateOptions.onSettled?.(action.data, null, variables, onMutateResult, context);
|
|
481
|
+
} catch (e) {
|
|
482
|
+
Promise.reject(e);
|
|
483
|
+
}
|
|
484
|
+
} else if (action?.type === "error") {
|
|
485
|
+
try {
|
|
486
|
+
mutateOptions.onError?.(action.error, variables, onMutateResult, context);
|
|
487
|
+
} catch (e) {
|
|
488
|
+
Promise.reject(e);
|
|
489
|
+
}
|
|
490
|
+
try {
|
|
491
|
+
mutateOptions.onSettled?.(void 0, action.error, variables, onMutateResult, context);
|
|
492
|
+
} catch (e) {
|
|
493
|
+
Promise.reject(e);
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
this.listeners.forEach((listener) => {
|
|
498
|
+
listener(this.#currentResult);
|
|
499
|
+
});
|
|
500
|
+
});
|
|
501
|
+
}
|
|
502
|
+
};
|
|
503
|
+
// ../../node_modules/.bun/@tanstack+react-query@5.105.3+cef22a3c7e8b4cef/node_modules/@tanstack/react-query/build/modern/useMutation.js
|
|
504
|
+
var import_react = /* @__PURE__ */ __toESM(require_react(), 1);
|
|
505
|
+
/**
|
|
506
|
+
* Unlike queries, mutations are typically used to create/update/delete data or perform server side-effects.
|
|
507
|
+
* `useMutation` is the hook for that.
|
|
508
|
+
* @param options - The {@link UseMutationOptions} to use — everything you can pass to `useMutation`.
|
|
509
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
510
|
+
* be used.
|
|
511
|
+
* @returns `mutate`/`mutateAsync` also accept per-call `onSuccess`/`onError`/`onSettled` callbacks as a second
|
|
512
|
+
* argument, useful for triggering call-site side effects (e.g. navigation) without coupling them to the shared
|
|
513
|
+
* mutation definition. Hook-level callbacks (passed to `options`) fire for every mutation; per-call callbacks
|
|
514
|
+
* fire only for the latest call you've made, and only while the component is still mounted — unmounting before
|
|
515
|
+
* the mutation settles removes the subscription and prevents them from firing.
|
|
516
|
+
* @throws {Error} The mutation error, when `throwOnError` is `true` or returns `true` for it, so that it is
|
|
517
|
+
* thrown to the nearest error boundary.
|
|
518
|
+
* @see {@link mutationOptions} to share these options across multiple `useMutation` call sites, or to look
|
|
519
|
+
* the mutation up elsewhere via its `mutationKey` (e.g. with `useMutationState`).
|
|
520
|
+
* @example
|
|
521
|
+
* ```tsx
|
|
522
|
+
* import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
523
|
+
*
|
|
524
|
+
* function AddTodo() {
|
|
525
|
+
* const queryClient = useQueryClient()
|
|
526
|
+
*
|
|
527
|
+
* const addMutation = useMutation({
|
|
528
|
+
* mutationFn: addTodo,
|
|
529
|
+
* onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
|
|
530
|
+
* })
|
|
531
|
+
*
|
|
532
|
+
* return (
|
|
533
|
+
* <button
|
|
534
|
+
* onClick={() =>
|
|
535
|
+
* addMutation.mutate('Item', {
|
|
536
|
+
* onError: (error) => console.error('Failed to add item:', error),
|
|
537
|
+
* })
|
|
538
|
+
* }
|
|
539
|
+
* >
|
|
540
|
+
* Add
|
|
541
|
+
* </button>
|
|
542
|
+
* )
|
|
543
|
+
* }
|
|
544
|
+
* ```
|
|
545
|
+
* @example
|
|
546
|
+
* Rendering the mutation's own state, rather than just firing it off:
|
|
547
|
+
* ```tsx
|
|
548
|
+
* import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
549
|
+
*
|
|
550
|
+
* function AddTodo() {
|
|
551
|
+
* const queryClient = useQueryClient()
|
|
552
|
+
*
|
|
553
|
+
* const addMutation = useMutation({
|
|
554
|
+
* mutationFn: addTodo,
|
|
555
|
+
* onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
|
|
556
|
+
* })
|
|
557
|
+
*
|
|
558
|
+
* return (
|
|
559
|
+
* <div>
|
|
560
|
+
* {addMutation.isPending ? (
|
|
561
|
+
* 'Adding todo...'
|
|
562
|
+
* ) : (
|
|
563
|
+
* <>
|
|
564
|
+
* {addMutation.isError ? (
|
|
565
|
+
* <div>An error occurred: {addMutation.error.message}</div>
|
|
566
|
+
* ) : null}
|
|
567
|
+
* <button onClick={() => addMutation.mutate('Item')}>Add</button>
|
|
568
|
+
* </>
|
|
569
|
+
* )}
|
|
570
|
+
* </div>
|
|
571
|
+
* )
|
|
572
|
+
* }
|
|
573
|
+
* ```
|
|
574
|
+
* @example
|
|
575
|
+
* Optimistic update via `onMutate`, rolling back on `onError`:
|
|
576
|
+
* ```tsx
|
|
577
|
+
* import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
578
|
+
*
|
|
579
|
+
* function AddTodo() {
|
|
580
|
+
* const queryClient = useQueryClient()
|
|
581
|
+
*
|
|
582
|
+
* const addMutation = useMutation({
|
|
583
|
+
* mutationFn: addTodo,
|
|
584
|
+
* onMutate: async (newTodo) => {
|
|
585
|
+
* await queryClient.cancelQueries({ queryKey: ['todos'] })
|
|
586
|
+
* const previousTodos = queryClient.getQueryData<Array<string>>(['todos'])
|
|
587
|
+
*
|
|
588
|
+
* queryClient.setQueryData<Array<string>>(['todos'], (old) => [
|
|
589
|
+
* ...(old ?? []),
|
|
590
|
+
* newTodo,
|
|
591
|
+
* ])
|
|
592
|
+
*
|
|
593
|
+
* // Passed to `onError` as `onMutateResult` if the mutation fails.
|
|
594
|
+
* return { previousTodos }
|
|
595
|
+
* },
|
|
596
|
+
* onError: (_err, _newTodo, onMutateResult) => {
|
|
597
|
+
* queryClient.setQueryData(['todos'], onMutateResult?.previousTodos)
|
|
598
|
+
* },
|
|
599
|
+
* onSettled: () => {
|
|
600
|
+
* queryClient.invalidateQueries({ queryKey: ['todos'] })
|
|
601
|
+
* },
|
|
602
|
+
* })
|
|
603
|
+
*
|
|
604
|
+
* return (
|
|
605
|
+
* <button onClick={() => addMutation.mutate('Item')}>Add</button>
|
|
606
|
+
* )
|
|
607
|
+
* }
|
|
608
|
+
* ```
|
|
609
|
+
* @example
|
|
610
|
+
* Callbacks passed per call to `mutate` only fire for the last call — `mutateAsync` gives you a
|
|
611
|
+
* promise per call instead, so you can wait for all of them when they succeed:
|
|
612
|
+
* ```tsx
|
|
613
|
+
* import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
614
|
+
*
|
|
615
|
+
* function AddTodos() {
|
|
616
|
+
* const queryClient = useQueryClient()
|
|
617
|
+
*
|
|
618
|
+
* const addMutation = useMutation({
|
|
619
|
+
* mutationFn: addTodo,
|
|
620
|
+
* onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
|
|
621
|
+
* })
|
|
622
|
+
*
|
|
623
|
+
* async function handleAddAll(todos: Array<string>) {
|
|
624
|
+
* try {
|
|
625
|
+
* await Promise.all(todos.map((todo) => addMutation.mutateAsync(todo)))
|
|
626
|
+
* } catch (error) {
|
|
627
|
+
* console.error('Failed to add todos:', error)
|
|
628
|
+
* }
|
|
629
|
+
* }
|
|
630
|
+
*
|
|
631
|
+
* return (
|
|
632
|
+
* <button onClick={() => handleAddAll(['Todo 1', 'Todo 2', 'Todo 3'])}>
|
|
633
|
+
* Add all
|
|
634
|
+
* </button>
|
|
635
|
+
* )
|
|
636
|
+
* }
|
|
637
|
+
* ```
|
|
638
|
+
* @example
|
|
639
|
+
* If some of the mutations above can fail independently of the others, and you want to know which ones
|
|
640
|
+
* did — rather than losing that information the moment the first one rejects — swap `Promise.all` for
|
|
641
|
+
* `Promise.allSettled`:
|
|
642
|
+
* ```tsx
|
|
643
|
+
* import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
644
|
+
*
|
|
645
|
+
* function AddTodos() {
|
|
646
|
+
* const queryClient = useQueryClient()
|
|
647
|
+
*
|
|
648
|
+
* const addMutation = useMutation({
|
|
649
|
+
* mutationFn: addTodo,
|
|
650
|
+
* onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
|
|
651
|
+
* })
|
|
652
|
+
*
|
|
653
|
+
* async function handleAddAll(todos: Array<string>) {
|
|
654
|
+
* const addResults = await Promise.allSettled(
|
|
655
|
+
* todos.map((todo) => addMutation.mutateAsync(todo)),
|
|
656
|
+
* )
|
|
657
|
+
*
|
|
658
|
+
* addResults.forEach((addResult, index) => {
|
|
659
|
+
* if (addResult.status === 'rejected') {
|
|
660
|
+
* console.error(`Failed to add "${todos[index]}":`, addResult.reason)
|
|
661
|
+
* }
|
|
662
|
+
* })
|
|
663
|
+
* }
|
|
664
|
+
*
|
|
665
|
+
* return (
|
|
666
|
+
* <button onClick={() => handleAddAll(['Todo 1', 'Todo 2', 'Todo 3'])}>
|
|
667
|
+
* Add all
|
|
668
|
+
* </button>
|
|
669
|
+
* )
|
|
670
|
+
* }
|
|
671
|
+
* ```
|
|
672
|
+
*/
|
|
673
|
+
function useMutation(options, queryClient) {
|
|
674
|
+
const client = useQueryClient(queryClient);
|
|
675
|
+
const [observer] = import_react.useState(() => new MutationObserver(client, options));
|
|
676
|
+
import_react.useEffect(() => {
|
|
677
|
+
observer.setOptions(options);
|
|
678
|
+
}, [observer, options]);
|
|
679
|
+
const result = import_react.useSyncExternalStore(import_react.useCallback((onStoreChange) => observer.subscribe(notifyManager.batchCalls(onStoreChange)), [observer]), () => observer.getCurrentResult(), () => observer.getCurrentResult());
|
|
680
|
+
const mutate = import_react.useCallback((...args) => {
|
|
681
|
+
observer.mutate(args[0], args[1]).catch(noop);
|
|
682
|
+
}, [observer]);
|
|
683
|
+
if (result.error && shouldThrowError(observer.options.throwOnError, [result.error])) throw result.error;
|
|
684
|
+
return {
|
|
685
|
+
...result,
|
|
686
|
+
mutate,
|
|
687
|
+
mutateAsync: result.mutate
|
|
688
|
+
};
|
|
689
|
+
}
|
|
690
|
+
export { Mutation as n, useMutation as t };
|