@pinia/colada-plugin-delay 0.1.5 → 0.2.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/dist/index.d.mts CHANGED
@@ -7,26 +7,87 @@ import { PiniaColadaPlugin } from "@pinia/colada";
7
7
  */
8
8
  interface PiniaColadaDelayOptions {
9
9
  /**
10
- * Delay in milliseconds to wait before letting the `asyncStatus` become `'loading'`. Set to `false` or 0 to disable. Requires the {@link PiniaColadaDelay} plugin.
10
+ * Delay in milliseconds to wait before letting the `asyncStatus` become `'loading'`. Set to `false` or 0 to disable.
11
+ *
11
12
  * @default 200
12
13
  */
13
14
  delay?: number | false;
15
+ /**
16
+ * Query-specific delay override. Overrides the top-level `delay` for queries
17
+ * only.
18
+ */
19
+ query?: {
20
+ delay?: number | false;
21
+ };
22
+ /**
23
+ * Mutation-specific delay override. Overrides the top-level `delay` for
24
+ * mutations only.
25
+ */
26
+ mutations?: {
27
+ delay?: number | false;
28
+ };
14
29
  }
15
30
  /**
16
- * Delays the `asyncStatus` of a query by a certain amount of time to avoid flickering between refreshes.
17
- * @param options - Pinia Colada Delay Loading plugin options
31
+ * Delays the `asyncStatus` of queries by a certain amount of time to avoid
32
+ * flickering between refreshes.
33
+ *
34
+ * @param options - Plugin options
35
+ */
36
+ declare function PiniaColadaDelayQuery(options?: Pick<PiniaColadaDelayOptions, 'delay'>): PiniaColadaPlugin;
37
+ /**
38
+ * Delays the `asyncStatus` of mutations by a certain amount of time to avoid
39
+ * flickering.
40
+ *
41
+ * @param options - Plugin options
42
+ */
43
+ declare function PiniaColadaDelayMutations(options?: Pick<PiniaColadaDelayOptions, 'delay'>): PiniaColadaPlugin;
44
+ /**
45
+ * Delays the `asyncStatus` of both queries and mutations by a certain amount
46
+ * of time to avoid flickering. Options apply to both, with optional `query`
47
+ * and `mutations` nested overrides.
48
+ *
49
+ * @param options - Plugin options
18
50
  */
19
51
  declare function PiniaColadaDelay(options?: PiniaColadaDelayOptions): PiniaColadaPlugin;
20
52
  declare module '@pinia/colada' {
21
- interface UseQueryOptions<TData, TError, TDataInitial> extends PiniaColadaDelayOptions {}
22
- interface UseQueryOptionsGlobal extends PiniaColadaDelayOptions {}
53
+ interface UseQueryOptions<TData, TError, TDataInitial> extends Pick<PiniaColadaDelayOptions, 'delay'> {}
54
+ interface UseQueryOptionsGlobal extends Pick<PiniaColadaDelayOptions, 'delay'> {}
23
55
  interface UseQueryEntryExtensions<TData, TError, TDataInitial> {
24
56
  /**
25
- * Returns whether the query is currently delaying its `asyncStatus` from becoming `'loading'`. Requires the {@link PiniaColadaDelay} plugin.
57
+ * Returns whether the query is currently delaying its `asyncStatus` from
58
+ * becoming `'loading'`. Requires the {@link PiniaColadaDelay} plugin.
59
+ */
60
+ isDelaying: ShallowRef<boolean>;
61
+ }
62
+ interface UseMutationOptions<TData, TVars, TError, TContext> {
63
+ /**
64
+ * Delay in milliseconds to wait before letting the `asyncStatus` become
65
+ * `'loading'`. Set to `false` or 0 to disable. Requires the
66
+ * {@link PiniaColadaDelay} or {@link PiniaColadaDelayMutations} plugin.
67
+ *
68
+ * @default 200
69
+ */
70
+ delay?: number | false;
71
+ }
72
+ interface UseMutationOptionsGlobal {
73
+ /**
74
+ * Delay in milliseconds to wait before letting the `asyncStatus` become
75
+ * `'loading'`. Set to `false` or 0 to disable. Requires the
76
+ * {@link PiniaColadaDelay} or {@link PiniaColadaDelayMutations} plugin.
77
+ *
78
+ * @default 200
79
+ */
80
+ delay?: number | false;
81
+ }
82
+ interface UseMutationEntryExtensions<TData, TVars, TError, TContext> {
83
+ /**
84
+ * Returns whether the mutation is currently delaying its `asyncStatus`
85
+ * from becoming `'loading'`. Requires the {@link PiniaColadaDelay} or
86
+ * {@link PiniaColadaDelayMutations} plugin.
26
87
  */
27
88
  isDelaying: ShallowRef<boolean>;
28
89
  }
29
90
  }
30
91
  //#endregion
31
- export { PiniaColadaDelay };
92
+ export { PiniaColadaDelay, PiniaColadaDelayMutations, type PiniaColadaDelayOptions, PiniaColadaDelayQuery };
32
93
  //# sourceMappingURL=index.d.mts.map
package/dist/index.mjs CHANGED
@@ -1,11 +1,44 @@
1
1
  import { customRef, shallowRef } from "vue";
2
-
2
+ import { useMutationCache } from "@pinia/colada";
3
3
  //#region src/delay.ts
4
4
  /**
5
- * Delays the `asyncStatus` of a query by a certain amount of time to avoid flickering between refreshes.
6
- * @param options - Pinia Colada Delay Loading plugin options
5
+ * Creates a delayed `asyncStatus` customRef that waits before switching to
6
+ * `'loading'`.
7
7
  */
8
- function PiniaColadaDelay(options) {
8
+ function createDelayedAsyncStatus(initialValue, delay, isDelaying) {
9
+ return customRef((track, trigger) => {
10
+ let value = initialValue;
11
+ let timeout;
12
+ return {
13
+ get() {
14
+ track();
15
+ return value;
16
+ },
17
+ set(newValue) {
18
+ clearTimeout(timeout);
19
+ if (newValue === "loading") {
20
+ isDelaying.value = true;
21
+ timeout = setTimeout(() => {
22
+ isDelaying.value = false;
23
+ value = newValue;
24
+ trigger();
25
+ }, delay);
26
+ } else {
27
+ isDelaying.value = false;
28
+ value = newValue;
29
+ trigger();
30
+ }
31
+ }
32
+ };
33
+ });
34
+ }
35
+ /**
36
+ * Delays the `asyncStatus` of queries by a certain amount of time to avoid
37
+ * flickering between refreshes.
38
+ *
39
+ * @param options - Plugin options
40
+ */
41
+ function PiniaColadaDelayQuery(options) {
9
42
  return ({ queryCache, scope }) => {
10
43
  queryCache.$onAction(({ name, args }) => {
11
44
  if (name === "extend") {
@@ -16,37 +49,51 @@ function PiniaColadaDelay(options) {
16
49
  entry.ext.isDelaying = isDelaying;
17
50
  if (!delay) return;
18
51
  const initialValue = entry.asyncStatus.value;
19
- entry.asyncStatus = customRef((track, trigger) => {
20
- let value = initialValue;
21
- let timeout;
22
- return {
23
- get() {
24
- track();
25
- return value;
26
- },
27
- set(newValue) {
28
- clearTimeout(timeout);
29
- if (newValue === "loading") {
30
- isDelaying.value = true;
31
- timeout = setTimeout(() => {
32
- isDelaying.value = false;
33
- value = newValue;
34
- trigger();
35
- }, delay);
36
- } else {
37
- isDelaying.value = false;
38
- value = newValue;
39
- trigger();
40
- }
41
- }
42
- };
43
- });
52
+ entry.asyncStatus = createDelayedAsyncStatus(initialValue, delay, isDelaying);
44
53
  });
45
54
  }
46
55
  });
47
56
  };
48
57
  }
49
-
58
+ /**
59
+ * Delays the `asyncStatus` of mutations by a certain amount of time to avoid
60
+ * flickering.
61
+ *
62
+ * @param options - Plugin options
63
+ */
64
+ function PiniaColadaDelayMutations(options) {
65
+ return ({ pinia, scope }) => {
66
+ useMutationCache(pinia).$onAction(({ name, args }) => {
67
+ if (name === "extend") {
68
+ const [entry] = args;
69
+ const delay = entry.options?.delay ?? options?.delay ?? 200;
70
+ scope.run(() => {
71
+ const isDelaying = shallowRef(false);
72
+ entry.ext.isDelaying = isDelaying;
73
+ if (!delay) return;
74
+ const initialValue = entry.asyncStatus.value;
75
+ entry.asyncStatus = createDelayedAsyncStatus(initialValue, delay, isDelaying);
76
+ });
77
+ }
78
+ });
79
+ };
80
+ }
81
+ /**
82
+ * Delays the `asyncStatus` of both queries and mutations by a certain amount
83
+ * of time to avoid flickering. Options apply to both, with optional `query`
84
+ * and `mutations` nested overrides.
85
+ *
86
+ * @param options - Plugin options
87
+ */
88
+ function PiniaColadaDelay(options) {
89
+ const removeQueryDelays = PiniaColadaDelayQuery({ delay: options?.query?.delay ?? options?.delay });
90
+ const removeMutationDelays = PiniaColadaDelayMutations({ delay: options?.mutations?.delay ?? options?.delay });
91
+ return (context) => {
92
+ removeQueryDelays(context);
93
+ removeMutationDelays(context);
94
+ };
95
+ }
50
96
  //#endregion
51
- export { PiniaColadaDelay };
97
+ export { PiniaColadaDelay, PiniaColadaDelayMutations, PiniaColadaDelayQuery };
98
+
52
99
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":[],"sources":["../src/delay.ts"],"sourcesContent":["/**\n * Pinia Colada Delay Loading plugin.\n *\n * Allows delaying the `loading` value for `asyncStatus` to improve _perceived performance_.\n *\n * @module @pinia/colada-plugin-delay\n */\n\nimport type { ShallowRef } from 'vue'\nimport { customRef, shallowRef } from 'vue'\nimport type { PiniaColadaPlugin, AsyncStatus } from '@pinia/colada'\n\n/**\n * Options for the {@link PiniaColadaDelay} plugin.\n */\ninterface PiniaColadaDelayOptions {\n /**\n * Delay in milliseconds to wait before letting the `asyncStatus` become `'loading'`. Set to `false` or 0 to disable. Requires the {@link PiniaColadaDelay} plugin.\n * @default 200\n */\n delay?: number | false\n}\n\n/**\n * Delays the `asyncStatus` of a query by a certain amount of time to avoid flickering between refreshes.\n * @param options - Pinia Colada Delay Loading plugin options\n */\nexport function PiniaColadaDelay(options?: PiniaColadaDelayOptions): PiniaColadaPlugin {\n return ({ queryCache, scope }) => {\n queryCache.$onAction(({ name, args }) => {\n if (name === 'extend') {\n const [entry] = args\n const delay = entry.options?.delay ?? options?.delay ?? 200\n scope.run(() => {\n const isDelaying = shallowRef(false)\n entry.ext.isDelaying = isDelaying\n if (!delay) return\n\n const initialValue = entry.asyncStatus.value\n entry.asyncStatus = customRef<AsyncStatus>((track, trigger) => {\n let value = initialValue\n let timeout: ReturnType<typeof setTimeout> | undefined\n return {\n get() {\n track()\n return value\n },\n set(newValue) {\n clearTimeout(timeout)\n if (newValue === 'loading') {\n isDelaying.value = true\n timeout = setTimeout(() => {\n isDelaying.value = false\n value = newValue\n trigger()\n }, delay)\n } else {\n isDelaying.value = false\n value = newValue\n trigger()\n }\n },\n }\n })\n })\n }\n })\n }\n}\n\ndeclare module '@pinia/colada' {\n // eslint-disable-next-line unused-imports/no-unused-vars\n interface UseQueryOptions<TData, TError, TDataInitial> extends PiniaColadaDelayOptions {}\n\n interface UseQueryOptionsGlobal extends PiniaColadaDelayOptions {}\n\n // eslint-disable-next-line unused-imports/no-unused-vars\n interface UseQueryEntryExtensions<TData, TError, TDataInitial> {\n /**\n * Returns whether the query is currently delaying its `asyncStatus` from becoming `'loading'`. Requires the {@link PiniaColadaDelay} plugin.\n */\n isDelaying: ShallowRef<boolean>\n }\n}\n"],"mappings":";;;;;;;AA2BA,SAAgB,iBAAiB,SAAsD;AACrF,SAAQ,EAAE,YAAY,YAAY;AAChC,aAAW,WAAW,EAAE,MAAM,WAAW;AACvC,OAAI,SAAS,UAAU;IACrB,MAAM,CAAC,SAAS;IAChB,MAAM,QAAQ,MAAM,SAAS,SAAS,SAAS,SAAS;AACxD,UAAM,UAAU;KACd,MAAM,aAAa,WAAW,MAAM;AACpC,WAAM,IAAI,aAAa;AACvB,SAAI,CAAC,MAAO;KAEZ,MAAM,eAAe,MAAM,YAAY;AACvC,WAAM,cAAc,WAAwB,OAAO,YAAY;MAC7D,IAAI,QAAQ;MACZ,IAAI;AACJ,aAAO;OACL,MAAM;AACJ,eAAO;AACP,eAAO;;OAET,IAAI,UAAU;AACZ,qBAAa,QAAQ;AACrB,YAAI,aAAa,WAAW;AAC1B,oBAAW,QAAQ;AACnB,mBAAU,iBAAiB;AACzB,qBAAW,QAAQ;AACnB,kBAAQ;AACR,mBAAS;YACR,MAAM;eACJ;AACL,oBAAW,QAAQ;AACnB,iBAAQ;AACR,kBAAS;;;OAGd;OACD;MACF;;IAEJ"}
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/delay.ts"],"sourcesContent":["/**\n * Pinia Colada Delay Loading plugin.\n *\n * Allows delaying the `loading` value for `asyncStatus` to improve _perceived performance_.\n *\n * @module @pinia/colada-plugin-delay\n */\n\nimport type { ShallowRef } from 'vue'\nimport { customRef, shallowRef } from 'vue'\nimport type { PiniaColadaPlugin, AsyncStatus } from '@pinia/colada'\nimport { useMutationCache } from '@pinia/colada'\n\n/**\n * Options for the {@link PiniaColadaDelay} plugin.\n */\nexport interface PiniaColadaDelayOptions {\n /**\n * Delay in milliseconds to wait before letting the `asyncStatus` become `'loading'`. Set to `false` or 0 to disable.\n *\n * @default 200\n */\n delay?: number | false\n\n /**\n * Query-specific delay override. Overrides the top-level `delay` for queries\n * only.\n */\n query?: { delay?: number | false }\n\n /**\n * Mutation-specific delay override. Overrides the top-level `delay` for\n * mutations only.\n */\n mutations?: { delay?: number | false }\n}\n\n/**\n * Creates a delayed `asyncStatus` customRef that waits before switching to\n * `'loading'`.\n */\nfunction createDelayedAsyncStatus(\n initialValue: AsyncStatus,\n delay: number,\n isDelaying: ShallowRef<boolean>,\n) {\n return customRef<AsyncStatus>((track, trigger) => {\n let value = initialValue\n let timeout: ReturnType<typeof setTimeout> | undefined\n return {\n get() {\n track()\n return value\n },\n set(newValue) {\n clearTimeout(timeout)\n if (newValue === 'loading') {\n isDelaying.value = true\n timeout = setTimeout(() => {\n isDelaying.value = false\n value = newValue\n trigger()\n }, delay)\n } else {\n isDelaying.value = false\n value = newValue\n trigger()\n }\n },\n }\n })\n}\n\n/**\n * Delays the `asyncStatus` of queries by a certain amount of time to avoid\n * flickering between refreshes.\n *\n * @param options - Plugin options\n */\nexport function PiniaColadaDelayQuery(\n options?: Pick<PiniaColadaDelayOptions, 'delay'>,\n): PiniaColadaPlugin {\n return ({ queryCache, scope }) => {\n queryCache.$onAction(({ name, args }) => {\n if (name === 'extend') {\n const [entry] = args\n const delay = entry.options?.delay ?? options?.delay ?? 200\n scope.run(() => {\n const isDelaying = shallowRef(false)\n entry.ext.isDelaying = isDelaying\n if (!delay) return\n\n const initialValue = entry.asyncStatus.value\n entry.asyncStatus = createDelayedAsyncStatus(initialValue, delay, isDelaying)\n })\n }\n })\n }\n}\n\n/**\n * Delays the `asyncStatus` of mutations by a certain amount of time to avoid\n * flickering.\n *\n * @param options - Plugin options\n */\nexport function PiniaColadaDelayMutations(\n options?: Pick<PiniaColadaDelayOptions, 'delay'>,\n): PiniaColadaPlugin {\n return ({ pinia, scope }) => {\n const mutationCache = useMutationCache(pinia)\n mutationCache.$onAction(({ name, args }) => {\n if (name === 'extend') {\n const [entry] = args\n const delay = entry.options?.delay ?? options?.delay ?? 200\n scope.run(() => {\n const isDelaying = shallowRef(false)\n entry.ext.isDelaying = isDelaying\n if (!delay) return\n\n const initialValue = entry.asyncStatus.value\n entry.asyncStatus = createDelayedAsyncStatus(initialValue, delay, isDelaying)\n })\n }\n })\n }\n}\n\n/**\n * Delays the `asyncStatus` of both queries and mutations by a certain amount\n * of time to avoid flickering. Options apply to both, with optional `query`\n * and `mutations` nested overrides.\n *\n * @param options - Plugin options\n */\nexport function PiniaColadaDelay(options?: PiniaColadaDelayOptions): PiniaColadaPlugin {\n const removeQueryDelays = PiniaColadaDelayQuery({\n delay: options?.query?.delay ?? options?.delay,\n })\n const removeMutationDelays = PiniaColadaDelayMutations({\n delay: options?.mutations?.delay ?? options?.delay,\n })\n return (context) => {\n removeQueryDelays(context)\n removeMutationDelays(context)\n }\n}\n\ndeclare module '@pinia/colada' {\n // eslint-disable-next-line unused-imports/no-unused-vars\n interface UseQueryOptions<TData, TError, TDataInitial> extends Pick<\n PiniaColadaDelayOptions,\n 'delay'\n > {}\n\n interface UseQueryOptionsGlobal extends Pick<PiniaColadaDelayOptions, 'delay'> {}\n\n // eslint-disable-next-line unused-imports/no-unused-vars\n interface UseQueryEntryExtensions<TData, TError, TDataInitial> {\n /**\n * Returns whether the query is currently delaying its `asyncStatus` from\n * becoming `'loading'`. Requires the {@link PiniaColadaDelay} plugin.\n */\n isDelaying: ShallowRef<boolean>\n }\n\n // eslint-disable-next-line unused-imports/no-unused-vars\n interface UseMutationOptions<TData, TVars, TError, TContext> {\n /**\n * Delay in milliseconds to wait before letting the `asyncStatus` become\n * `'loading'`. Set to `false` or 0 to disable. Requires the\n * {@link PiniaColadaDelay} or {@link PiniaColadaDelayMutations} plugin.\n *\n * @default 200\n */\n delay?: number | false\n }\n\n interface UseMutationOptionsGlobal {\n /**\n * Delay in milliseconds to wait before letting the `asyncStatus` become\n * `'loading'`. Set to `false` or 0 to disable. Requires the\n * {@link PiniaColadaDelay} or {@link PiniaColadaDelayMutations} plugin.\n *\n * @default 200\n */\n delay?: number | false\n }\n\n // eslint-disable-next-line unused-imports/no-unused-vars\n interface UseMutationEntryExtensions<TData, TVars, TError, TContext> {\n /**\n * Returns whether the mutation is currently delaying its `asyncStatus`\n * from becoming `'loading'`. Requires the {@link PiniaColadaDelay} or\n * {@link PiniaColadaDelayMutations} plugin.\n */\n isDelaying: ShallowRef<boolean>\n }\n}\n"],"mappings":";;;;;;;AAyCA,SAAS,yBACP,cACA,OACA,YACA;AACA,QAAO,WAAwB,OAAO,YAAY;EAChD,IAAI,QAAQ;EACZ,IAAI;AACJ,SAAO;GACL,MAAM;AACJ,WAAO;AACP,WAAO;;GAET,IAAI,UAAU;AACZ,iBAAa,QAAQ;AACrB,QAAI,aAAa,WAAW;AAC1B,gBAAW,QAAQ;AACnB,eAAU,iBAAiB;AACzB,iBAAW,QAAQ;AACnB,cAAQ;AACR,eAAS;QACR,MAAM;WACJ;AACL,gBAAW,QAAQ;AACnB,aAAQ;AACR,cAAS;;;GAGd;GACD;;;;;;;;AASJ,SAAgB,sBACd,SACmB;AACnB,SAAQ,EAAE,YAAY,YAAY;AAChC,aAAW,WAAW,EAAE,MAAM,WAAW;AACvC,OAAI,SAAS,UAAU;IACrB,MAAM,CAAC,SAAS;IAChB,MAAM,QAAQ,MAAM,SAAS,SAAS,SAAS,SAAS;AACxD,UAAM,UAAU;KACd,MAAM,aAAa,WAAW,MAAM;AACpC,WAAM,IAAI,aAAa;AACvB,SAAI,CAAC,MAAO;KAEZ,MAAM,eAAe,MAAM,YAAY;AACvC,WAAM,cAAc,yBAAyB,cAAc,OAAO,WAAW;MAC7E;;IAEJ;;;;;;;;;AAUN,SAAgB,0BACd,SACmB;AACnB,SAAQ,EAAE,OAAO,YAAY;AACL,mBAAiB,MAAM,CAC/B,WAAW,EAAE,MAAM,WAAW;AAC1C,OAAI,SAAS,UAAU;IACrB,MAAM,CAAC,SAAS;IAChB,MAAM,QAAQ,MAAM,SAAS,SAAS,SAAS,SAAS;AACxD,UAAM,UAAU;KACd,MAAM,aAAa,WAAW,MAAM;AACpC,WAAM,IAAI,aAAa;AACvB,SAAI,CAAC,MAAO;KAEZ,MAAM,eAAe,MAAM,YAAY;AACvC,WAAM,cAAc,yBAAyB,cAAc,OAAO,WAAW;MAC7E;;IAEJ;;;;;;;;;;AAWN,SAAgB,iBAAiB,SAAsD;CACrF,MAAM,oBAAoB,sBAAsB,EAC9C,OAAO,SAAS,OAAO,SAAS,SAAS,OAC1C,CAAC;CACF,MAAM,uBAAuB,0BAA0B,EACrD,OAAO,SAAS,WAAW,SAAS,SAAS,OAC9C,CAAC;AACF,SAAQ,YAAY;AAClB,oBAAkB,QAAQ;AAC1B,uBAAqB,QAAQ"}
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.1.5",
7
+ "version": "0.2.1",
8
8
  "description": "Delay async status in Pinia Colada",
9
9
  "author": {
10
10
  "name": "Eduardo San Martin Morote",
@@ -46,10 +46,10 @@
46
46
  "dist"
47
47
  ],
48
48
  "peerDependencies": {
49
- "@pinia/colada": ">=0.21.5"
49
+ "@pinia/colada": ">=1.1.0"
50
50
  },
51
51
  "devDependencies": {
52
- "@pinia/colada": "^0.21.5"
52
+ "@pinia/colada": "^1.1.0"
53
53
  },
54
54
  "scripts": {
55
55
  "build": "tsdown",