@pathscale/ui 2.11.13 → 2.12.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.
@@ -1,3 +1,4 @@
1
+ import type { JSX } from "@solidjs/web";
1
2
  import type { DataGridColumn, DataGridRow } from "./createDataGrid";
2
3
  export declare const formatCell: (value: unknown) => string;
3
4
  export declare const columnSpan: (visible: number, hasSelection: boolean) => number;
@@ -5,7 +6,7 @@ export declare const columnSpan: (visible: number, hasSelection: boolean) => num
5
6
  export declare const pageLabel: (page: number, pageCount: number) => string;
6
7
  export declare const rangeLabel: (page: number, pageSize: number, total: number) => string;
7
8
  export declare const searchPlaceholder: (label: string) => string;
8
- export declare const cellContent: <Row extends DataGridRow>(column: DataGridColumn<Row>, row: Row, index: number) => string | number | boolean | import("solid-js/types/types.js").RenderedElement | Node | import("@solidjs/web").JSX.ArrayElement | null | undefined;
9
+ export declare const cellContent: <Row extends DataGridRow>(column: DataGridColumn<Row>, row: Row, index: number) => JSX.Element;
9
10
  export declare const readInputValue: (event: {
10
11
  currentTarget: HTMLInputElement;
11
12
  }) => string;
@@ -0,0 +1,28 @@
1
+ import type { Accessor } from "solid-js";
2
+ /**
3
+ * A write, without a query library. The companion to `createQuery`.
4
+ *
5
+ * Replaces `useMutation`. The same rule applies as there: reading this never
6
+ * suspends and never throws. `mutate` reports failure through `error()`;
7
+ * `mutateAsync` rejects, for a caller that wants to await and handle it.
8
+ */
9
+ export interface CreateMutationOptions<TArgs extends unknown[], TResult> {
10
+ mutationFn: (...args: TArgs) => Promise<TResult>;
11
+ onSuccess?: (result: TResult, ...args: TArgs) => void | Promise<void>;
12
+ onError?: (error: unknown, ...args: TArgs) => void;
13
+ /** Runs after success or failure, like TanStack's `onSettled`. */
14
+ onSettled?: () => void | Promise<void>;
15
+ }
16
+ export interface MutationResult<TArgs extends unknown[], TResult> {
17
+ /** Fire and forget. Failure lands on `error()` rather than as a rejection. */
18
+ mutate: (...args: TArgs) => void;
19
+ /** Fire and await. Rejects on failure. */
20
+ mutateAsync: (...args: TArgs) => Promise<TResult>;
21
+ isPending: Accessor<boolean>;
22
+ error: Accessor<unknown>;
23
+ /** The last successful result. */
24
+ data: Accessor<TResult | undefined>;
25
+ /** Clear `error` and `data`. */
26
+ reset: () => void;
27
+ }
28
+ export declare const createMutation: <TArgs extends unknown[], TResult>(options: () => CreateMutationOptions<TArgs, TResult>) => MutationResult<TArgs, TResult>;
@@ -0,0 +1,41 @@
1
+ import { createSignal } from "solid-js";
2
+ const createMutation = (options)=>{
3
+ const [isPending, setIsPending] = createSignal(false);
4
+ const [error, setError] = createSignal(void 0);
5
+ const [data, setData] = createSignal(void 0);
6
+ let inFlight = 0;
7
+ const mutateAsync = async (...args)=>{
8
+ const { mutationFn, onSuccess, onError, onSettled } = options();
9
+ inFlight++;
10
+ setIsPending(true);
11
+ setError(void 0);
12
+ try {
13
+ const result = await mutationFn(...args);
14
+ setData(()=>result);
15
+ await onSuccess?.(result, ...args);
16
+ return result;
17
+ } catch (caught) {
18
+ setError(()=>caught);
19
+ onError?.(caught, ...args);
20
+ throw caught;
21
+ } finally{
22
+ inFlight--;
23
+ if (0 === inFlight) setIsPending(false);
24
+ await onSettled?.();
25
+ }
26
+ };
27
+ return {
28
+ mutate: (...args)=>{
29
+ mutateAsync(...args).catch(()=>{});
30
+ },
31
+ mutateAsync,
32
+ isPending,
33
+ error,
34
+ data,
35
+ reset: ()=>{
36
+ setError(void 0);
37
+ setData(()=>void 0);
38
+ }
39
+ };
40
+ };
41
+ export { createMutation };
@@ -0,0 +1,56 @@
1
+ import type { Accessor } from "solid-js";
2
+ /**
3
+ * Asynchronous reads, without a query library.
4
+ *
5
+ * This exists to replace `@tanstack/solid-query`, and the replacement is not a
6
+ * like-for-like port. One behaviour is deliberately different, and it is the
7
+ * reason this file exists rather than a wrapper around the old one:
8
+ *
9
+ * **A query that has not run is not pending, and reading it never suspends.**
10
+ *
11
+ * TanStack keeps a query that has never fetched -- including one held back by
12
+ * `enabled: false` -- at `status: "pending"` forever. Under Solid 2, reading a
13
+ * pending query throws `NotReadyError` to suspend. A widget whose query was
14
+ * disabled therefore suspended for the lifetime of the page, and because
15
+ * `NotReadyError` extends `Error` with no message, a boundary that caught it
16
+ * had nothing to print. That is how a support-chat button that had not
17
+ * connected replaced an entire application with a blank error page.
18
+ *
19
+ * Here, `data()` is `undefined` until there is data, `isLoading()` is true only
20
+ * while a fetch is actually in flight, and neither ever throws. A caller that
21
+ * wants to suspend can do so explicitly; a caller that forgets cannot take the
22
+ * page down.
23
+ */
24
+ export interface CreateQueryOptions<T> {
25
+ /**
26
+ * Identity, for invalidation. Compared by value, and matched by prefix, so
27
+ * `["users"]` invalidates `["users", 1]` as well.
28
+ */
29
+ key: readonly unknown[];
30
+ /** The read itself. Only called when `enabled` is not false. */
31
+ fetcher: () => Promise<T>;
32
+ /** Held back while false. Default true. */
33
+ enabled?: boolean;
34
+ }
35
+ export interface QueryResult<T> {
36
+ /** The last value read, or `undefined` before the first one arrives. */
37
+ data: Accessor<T | undefined>;
38
+ /** The last failure, cleared by the next successful read. */
39
+ error: Accessor<unknown>;
40
+ /** True only while a fetch is in flight. Never true for a disabled query. */
41
+ isLoading: Accessor<boolean>;
42
+ /** True once a value has arrived at least once. */
43
+ isReady: Accessor<boolean>;
44
+ /** Read again now, regardless of `enabled`. */
45
+ refetch: () => Promise<void>;
46
+ }
47
+ /**
48
+ * Re-read every live query whose key starts with `prefix`.
49
+ *
50
+ * The replacement for `useQueryClient().invalidateQueries({ queryKey })`. It is
51
+ * a plain function rather than something read from context: invalidation is
52
+ * usually wanted from a mutation handler or a store, which are not components
53
+ * and have no context to read.
54
+ */
55
+ export declare const invalidateQueries: (prefix: readonly unknown[]) => void;
56
+ export declare const createQuery: <T>(options: () => CreateQueryOptions<T>) => QueryResult<T>;
@@ -0,0 +1,55 @@
1
+ import { createRenderEffect, createSignal, onCleanup, untrack } from "solid-js";
2
+ const registry = new Set();
3
+ const isPrefix = (prefix, key)=>prefix.length <= key.length && prefix.every((part, index)=>Object.is(part, key[index]));
4
+ const invalidateQueries = (prefix)=>{
5
+ for (const entry of registry)if (isPrefix(prefix, entry.key)) entry.refetch();
6
+ };
7
+ const createQuery = (options)=>{
8
+ const [data, setData] = createSignal(void 0);
9
+ const [error, setError] = createSignal(void 0);
10
+ const [isLoading, setIsLoading] = createSignal(false);
11
+ const [isReady, setIsReady] = createSignal(false);
12
+ let generation = 0;
13
+ const run = async ()=>{
14
+ const mine = ++generation;
15
+ const { fetcher } = untrack(options);
16
+ setIsLoading(true);
17
+ try {
18
+ const value = await fetcher();
19
+ if (mine !== generation) return;
20
+ setData(()=>value);
21
+ setError(void 0);
22
+ setIsReady(true);
23
+ } catch (caught) {
24
+ if (mine !== generation) return;
25
+ setError(()=>caught);
26
+ } finally{
27
+ if (mine === generation) setIsLoading(false);
28
+ }
29
+ };
30
+ createRenderEffect(()=>options(), ({ key, enabled = true })=>{
31
+ if (!enabled) {
32
+ generation++;
33
+ setIsLoading(false);
34
+ return;
35
+ }
36
+ const entry = {
37
+ key,
38
+ refetch: ()=>void run()
39
+ };
40
+ registry.add(entry);
41
+ onCleanup(()=>registry.delete(entry));
42
+ run();
43
+ });
44
+ onCleanup(()=>{
45
+ generation++;
46
+ });
47
+ return {
48
+ data,
49
+ error,
50
+ isLoading,
51
+ isReady,
52
+ refetch: run
53
+ };
54
+ };
55
+ export { createQuery, invalidateQueries };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,180 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { createRoot, flush } from "solid-js";
3
+ import { createMutation } from "./createMutation.js";
4
+ import { createQuery, invalidateQueries } from "./createQuery.js";
5
+ const tick = ()=>new Promise((resolve)=>setTimeout(resolve, 0));
6
+ describe("createQuery", ()=>{
7
+ test("a disabled query is not loading, and never throws", async ()=>{
8
+ let calls = 0;
9
+ const dispose = createRoot((d)=>{
10
+ const q = createQuery(()=>({
11
+ key: [
12
+ "never"
13
+ ],
14
+ enabled: false,
15
+ fetcher: async ()=>{
16
+ calls++;
17
+ return "value";
18
+ }
19
+ }));
20
+ expect(q.isLoading()).toBe(false);
21
+ expect(q.data()).toBeUndefined();
22
+ expect(q.isReady()).toBe(false);
23
+ return d;
24
+ });
25
+ await tick();
26
+ expect(calls).toBe(0);
27
+ dispose();
28
+ });
29
+ test("reads, and reports readiness", async ()=>{
30
+ let resolveFetch;
31
+ const result = await createRoot(async (dispose)=>{
32
+ const q = createQuery(()=>({
33
+ key: [
34
+ "thing"
35
+ ],
36
+ fetcher: ()=>new Promise((r)=>resolveFetch = r)
37
+ }));
38
+ flush();
39
+ const whileLoading = {
40
+ loading: q.isLoading(),
41
+ ready: q.isReady()
42
+ };
43
+ resolveFetch?.("hello");
44
+ await tick();
45
+ return {
46
+ whileLoading,
47
+ data: q.data(),
48
+ ready: q.isReady(),
49
+ dispose
50
+ };
51
+ });
52
+ expect(result.whileLoading).toEqual({
53
+ loading: true,
54
+ ready: false
55
+ });
56
+ expect(result.data).toBe("hello");
57
+ expect(result.ready).toBe(true);
58
+ result.dispose();
59
+ });
60
+ test("a failure lands on error() rather than being thrown", async ()=>{
61
+ const result = await createRoot(async (dispose)=>{
62
+ const q = createQuery(()=>({
63
+ key: [
64
+ "bad"
65
+ ],
66
+ fetcher: async ()=>{
67
+ throw new Error("nope");
68
+ }
69
+ }));
70
+ flush();
71
+ await tick();
72
+ return {
73
+ error: q.error(),
74
+ loading: q.isLoading(),
75
+ dispose
76
+ };
77
+ });
78
+ expect(result.error.message).toBe("nope");
79
+ expect(result.loading).toBe(false);
80
+ result.dispose();
81
+ });
82
+ test("invalidateQueries matches by key prefix", async ()=>{
83
+ let calls = 0;
84
+ const dispose = createRoot((d)=>{
85
+ createQuery(()=>({
86
+ key: [
87
+ "users",
88
+ 1
89
+ ],
90
+ fetcher: async ()=>{
91
+ calls++;
92
+ return calls;
93
+ }
94
+ }));
95
+ flush();
96
+ return d;
97
+ });
98
+ await tick();
99
+ expect(calls).toBe(1);
100
+ invalidateQueries([
101
+ "users"
102
+ ]);
103
+ await tick();
104
+ expect(calls).toBe(2);
105
+ invalidateQueries([
106
+ "apps"
107
+ ]);
108
+ await tick();
109
+ expect(calls).toBe(2);
110
+ dispose();
111
+ });
112
+ test("a disposed query deregisters, so invalidation cannot reach it", async ()=>{
113
+ let calls = 0;
114
+ const dispose = createRoot((d)=>{
115
+ createQuery(()=>({
116
+ key: [
117
+ "gone"
118
+ ],
119
+ fetcher: async ()=>{
120
+ calls++;
121
+ return calls;
122
+ }
123
+ }));
124
+ flush();
125
+ return d;
126
+ });
127
+ await tick();
128
+ expect(calls).toBe(1);
129
+ dispose();
130
+ invalidateQueries([
131
+ "gone"
132
+ ]);
133
+ await tick();
134
+ expect(calls).toBe(1);
135
+ });
136
+ });
137
+ describe("createMutation", ()=>{
138
+ test("mutate reports failure without an unhandled rejection", async ()=>{
139
+ const result = await createRoot(async (dispose)=>{
140
+ const m = createMutation(()=>({
141
+ mutationFn: async ()=>{
142
+ throw new Error("write failed");
143
+ }
144
+ }));
145
+ m.mutate();
146
+ await tick();
147
+ return {
148
+ error: m.error(),
149
+ pending: m.isPending(),
150
+ dispose
151
+ };
152
+ });
153
+ expect(result.error.message).toBe("write failed");
154
+ expect(result.pending).toBe(false);
155
+ result.dispose();
156
+ });
157
+ test("mutateAsync rejects, and onSuccess sees the result", async ()=>{
158
+ const seen = [];
159
+ const result = await createRoot(async (dispose)=>{
160
+ const m = createMutation(()=>({
161
+ mutationFn: async (name)=>`made ${name}`,
162
+ onSuccess: (r)=>{
163
+ seen.push(r);
164
+ }
165
+ }));
166
+ const value = await m.mutateAsync("app");
167
+ return {
168
+ value,
169
+ data: m.data(),
170
+ dispose
171
+ };
172
+ });
173
+ expect(result.value).toBe("made app");
174
+ expect(result.data).toBe("made app");
175
+ expect(seen).toEqual([
176
+ "made app"
177
+ ]);
178
+ result.dispose();
179
+ });
180
+ });
@@ -0,0 +1,4 @@
1
+ export type { CreateQueryOptions, QueryResult, } from "./createQuery";
2
+ export { createQuery, invalidateQueries } from "./createQuery";
3
+ export type { CreateMutationOptions, MutationResult, } from "./createMutation";
4
+ export { createMutation } from "./createMutation";
@@ -0,0 +1,2 @@
1
+ export { createQuery, invalidateQueries } from "./createQuery.js";
2
+ export { createMutation } from "./createMutation.js";
package/dist/index.d.ts CHANGED
@@ -121,6 +121,8 @@ export type { Align, CapabilityProps, ChangeReason, Constraint, Controlled, Dire
121
121
  export { FLAVORS, isInvalid, resolveState, SIZES, SPACES, STATES, VARIANTS, } from "./components/vocabulary";
122
122
  export type { AnyFormApi, CreateFormOptions, FormApi, UseFieldResult, } from "./hooks/form";
123
123
  export { createForm, FormContext, getFirstFieldError, useField, useFormContext, } from "./hooks/form";
124
+ export type { CreateMutationOptions, CreateQueryOptions, MutationResult, QueryResult, } from "./hooks/data";
125
+ export { createMutation, createQuery, invalidateQueries, } from "./hooks/data";
124
126
  export { useDesktop } from "./hooks/layout";
125
127
  export type { UseAnchoredOverlayPositionOptions } from "./hooks/table";
126
128
  export { useAnchoredOverlayPosition } from "./hooks/table";
package/dist/index.js CHANGED
@@ -72,6 +72,7 @@ export { DEFAULT_GAP as DEFAULT_TOAST_GAP, DEFAULT_MAX_VISIBLE_TOAST, DEFAULT_SC
72
72
  export { TooltipArrow, TooltipContent, TooltipTrigger, default as Tooltip } from "./components/tooltip/index.js";
73
73
  export { FLAVORS, SIZES, SPACES, STATES, VARIANTS, isInvalid, resolveState } from "./components/vocabulary.js";
74
74
  export { FormContext, createForm, getFirstFieldError, useField, useFormContext } from "./hooks/form/index.js";
75
+ export { createMutation, createQuery, invalidateQueries } from "./hooks/data/index.js";
75
76
  export { useDesktop } from "./hooks/layout/index.js";
76
77
  export { useAnchoredOverlayPosition } from "./hooks/table/index.js";
77
78
  export { evaluatePasswordRules, matchPasswordConfirmation } from "./passwordRules.js";
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "format": "solid-layouts-library-v2",
3
3
  "package": "@pathscale/ui",
4
- "version": "2.11.13",
4
+ "version": "2.12.0",
5
5
  "components": {
6
6
  "Accordion": {
7
7
  "kind": "embedded"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pathscale/ui",
3
- "version": "2.11.13",
3
+ "version": "2.12.0",
4
4
  "author": "pathscale",
5
5
  "repository": {
6
6
  "type": "git",