@rise-x/apps-sdk 0.20.0 → 0.21.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.
@@ -11,11 +11,26 @@ export interface SdkQueryOptions {
11
11
  retry?: boolean | number;
12
12
  }
13
13
  export interface SdkMutationOptions<TData, TVariables> {
14
+ /**
15
+ * Whether the hook's built-in invalidation of cached searches (`useWorkSearch`,
16
+ * `useAssetSearch`) runs after the write. Default `true`.
17
+ *
18
+ * A search is cached on the server for a couple of minutes, so reading it again right after
19
+ * a write can return the rows as they were before it, and overwrite a list the app has already
20
+ * brought up to date from live events. An app that does that (see `@rise-x/apps-sdk/live`)
21
+ * passes `"unlessLive"`: the searches are left alone while the host's live connection is
22
+ * `connected`, and invalidated as before when it is not. `false` never invalidates them.
23
+ * Everything else the hook invalidates (the work's detail and data, the audit trail, row
24
+ * listings, other scopes) is unaffected.
25
+ */
26
+ invalidateSearches?: boolean | "unlessLive";
14
27
  onSuccess?: (data: TData, variables: TVariables) => unknown;
15
28
  onError?: (error: ConnectorError, variables: TVariables) => unknown;
16
29
  onSettled?: (data: TData | undefined, error: ConnectorError | null, variables: TVariables) => unknown;
17
30
  }
18
31
  export declare function getLiveEnvId(): QueryEnvId;
32
+ export declare function shouldInvalidateSearches(option: SdkMutationOptions<unknown, unknown>["invalidateSearches"]): boolean;
33
+ export declare function invalidateScope(client: QueryClient, queryKey: readonly unknown[], searches: boolean): Promise<void>;
19
34
  export interface RowsPage {
20
35
  items: {
21
36
  id: string;
@@ -39,5 +54,5 @@ interface BuiltInfiniteQuery<TPage> {
39
54
  enabled?: unknown;
40
55
  }
41
56
  export declare function useSdkInfiniteQuery<TPage>(built: BuiltInfiniteQuery<TPage>, options?: SdkQueryOptions): UseInfiniteQueryResult<InfiniteData<TPage>, ConnectorError>;
42
- export declare function useSdkMutation<TData, TVariables>(mutationFn: (variables: TVariables) => Promise<TData>, effects: (client: QueryClient, data: TData, variables: TVariables) => void, options?: SdkMutationOptions<TData, TVariables>): import("@tanstack/react-query").UseMutationResult<TData, ConnectorError, TVariables, unknown>;
57
+ export declare function useSdkMutation<TData, TVariables>(mutationFn: (variables: TVariables) => Promise<TData>, effects: (client: QueryClient, data: TData, variables: TVariables, searches: boolean) => void, options?: SdkMutationOptions<TData, TVariables>): import("@tanstack/react-query").UseMutationResult<TData, ConnectorError, TVariables, unknown>;
43
58
  export {};
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getLiveEnvId = getLiveEnvId;
4
+ exports.shouldInvalidateSearches = shouldInvalidateSearches;
5
+ exports.invalidateScope = invalidateScope;
4
6
  exports.getNextRowsPageParam = getNextRowsPageParam;
5
7
  exports.invalidateAppSdkQueries = invalidateAppSdkQueries;
6
8
  exports.useEnvSubscription = useEnvSubscription;
@@ -28,6 +30,20 @@ const context_1 = require("./context");
28
30
  function getLiveEnvId() {
29
31
  return (0, accessors_1.getShellEnvironment)()?.id ?? null;
30
32
  }
33
+ // Resolved when the write succeeds, so "unlessLive" reflects the connection at that moment.
34
+ function shouldInvalidateSearches(option) {
35
+ if (option === "unlessLive")
36
+ return (0, accessors_1.getShellLive)()?.getStatus() !== "connected";
37
+ return option !== false;
38
+ }
39
+ // Invalidates a scope of the cache, leaving out its v4 search listings when `searches` is false.
40
+ // Every key is [root, envId, domain, kind, ...], so a search is `kind === "search"`.
41
+ function invalidateScope(client, queryKey, searches) {
42
+ return client.invalidateQueries({
43
+ queryKey,
44
+ predicate: searches ? undefined : (query) => query.queryKey[3] !== "search",
45
+ });
46
+ }
31
47
  // Shared getNextPageParam for the domains' skip-paged infinite rows queries.
32
48
  // Mirrors the connectors' iterate() bail/advance rule exactly: stop when the
33
49
  // server reports no more rows, or when a "full" page turned out to be
@@ -112,12 +128,14 @@ function useSdkInfiniteQuery(built, options) {
112
128
  };
113
129
  return (0, react_query_1.useInfiniteQuery)(merged, client);
114
130
  }
115
- function useSdkMutation(mutationFn, effects, options) {
131
+ function useSdkMutation(mutationFn,
132
+ // `searches` is whether the caller's `invalidateSearches` allows invalidating the v4 searches.
133
+ effects, options) {
116
134
  const client = (0, context_1.useAppQueryClient)();
117
135
  return (0, react_query_1.useMutation)({
118
136
  mutationFn,
119
137
  onSuccess: (data, variables) => {
120
- effects(client, data, variables);
138
+ effects(client, data, variables, shouldInvalidateSearches(options?.invalidateSearches));
121
139
  return options?.onSuccess?.(data, variables);
122
140
  },
123
141
  onError: (error, variables) => options?.onError?.(error, variables),
@@ -125,18 +125,20 @@ function useWorkAudit(workId, options) {
125
125
  return (0, shared_1.useSdkQuery)(exports.workQueries.audit(workId), options);
126
126
  }
127
127
  function useStartWork(options) {
128
- return (0, shared_1.useSdkMutation)((vars) => work_1.work.start(vars), (client) => {
128
+ return (0, shared_1.useSdkMutation)((vars) => work_1.work.start(vars), (client, _data, _vars, searches) => {
129
129
  const envId = (0, shared_1.getLiveEnvId)();
130
130
  void client.invalidateQueries({
131
131
  queryKey: keys_1.queryKeys.work.allRows(envId),
132
132
  });
133
- void client.invalidateQueries({
134
- queryKey: keys_1.queryKeys.work.searchAll(envId),
135
- });
133
+ if (searches) {
134
+ void client.invalidateQueries({
135
+ queryKey: keys_1.queryKeys.work.searchAll(envId),
136
+ });
137
+ }
136
138
  }, options);
137
139
  }
138
140
  function usePatchWorkData(options) {
139
- return (0, shared_1.useSdkMutation)(({ workId, ...patch }) => work_1.work.patchData(workId, patch), (client, _data, vars) => {
141
+ return (0, shared_1.useSdkMutation)(({ workId, ...patch }) => work_1.work.patchData(workId, patch), (client, _data, vars, searches) => {
140
142
  const envId = (0, shared_1.getLiveEnvId)();
141
143
  void client.invalidateQueries({
142
144
  queryKey: keys_1.queryKeys.work.dataAll(envId, vars.workId),
@@ -148,13 +150,15 @@ function usePatchWorkData(options) {
148
150
  void client.invalidateQueries({
149
151
  queryKey: keys_1.queryKeys.work.allRows(envId),
150
152
  });
151
- void client.invalidateQueries({
152
- queryKey: keys_1.queryKeys.work.searchAll(envId),
153
- });
153
+ if (searches) {
154
+ void client.invalidateQueries({
155
+ queryKey: keys_1.queryKeys.work.searchAll(envId),
156
+ });
157
+ }
154
158
  }, options);
155
159
  }
156
160
  function useSubmitWork(options) {
157
- return (0, shared_1.useSdkMutation)((vars) => work_1.work.submit(vars), (client, data, vars) => {
161
+ return (0, shared_1.useSdkMutation)((vars) => work_1.work.submit(vars), (client, data, vars, searches) => {
158
162
  const envId = (0, shared_1.getLiveEnvId)();
159
163
  client.setQueryData(keys_1.queryKeys.work.detail(envId, vars.workId), data);
160
164
  void client.invalidateQueries({
@@ -169,17 +173,19 @@ function useSubmitWork(options) {
169
173
  void client.invalidateQueries({
170
174
  queryKey: keys_1.queryKeys.work.allRows(envId),
171
175
  });
172
- void client.invalidateQueries({
173
- queryKey: keys_1.queryKeys.work.searchAll(envId),
174
- });
176
+ if (searches) {
177
+ void client.invalidateQueries({
178
+ queryKey: keys_1.queryKeys.work.searchAll(envId),
179
+ });
180
+ }
175
181
  // CROSS-DOMAIN: asset create/edit drafts materialize via work.submit —
176
182
  // the whole assets scope goes stale. Over-broad but correct; only
177
183
  // active asset queries actually refetch.
178
- void client.invalidateQueries({ queryKey: keys_1.queryKeys.assets.all(envId) });
184
+ void (0, shared_1.invalidateScope)(client, keys_1.queryKeys.assets.all(envId), searches);
179
185
  }, options);
180
186
  }
181
187
  function useDeleteWork(options) {
182
- return (0, shared_1.useSdkMutation)((vars) => work_1.work.delete(vars.workId), (client, _data, vars) => {
188
+ return (0, shared_1.useSdkMutation)((vars) => work_1.work.delete(vars.workId), (client, _data, vars, searches) => {
183
189
  const envId = (0, shared_1.getLiveEnvId)();
184
190
  client.removeQueries({
185
191
  queryKey: keys_1.queryKeys.work.detail(envId, vars.workId),
@@ -193,9 +199,11 @@ function useDeleteWork(options) {
193
199
  void client.invalidateQueries({
194
200
  queryKey: keys_1.queryKeys.work.allRows(envId),
195
201
  });
196
- void client.invalidateQueries({
197
- queryKey: keys_1.queryKeys.work.searchAll(envId),
198
- });
202
+ if (searches) {
203
+ void client.invalidateQueries({
204
+ queryKey: keys_1.queryKeys.work.searchAll(envId),
205
+ });
206
+ }
199
207
  void client.invalidateQueries({
200
208
  queryKey: keys_1.queryKeys.work.relatedAll(envId),
201
209
  });