@sveltebase/utils 0.2.0 → 0.2.2

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.ts CHANGED
@@ -17,19 +17,9 @@ export declare function timestamps<T extends boolean>(updateOnly: T): T extends
17
17
  createdAt: number;
18
18
  updatedAt: number;
19
19
  };
20
- export type TryCatchReturn = {
21
- success: string;
22
- error?: never;
23
- } | {
24
- error: string;
25
- success?: never;
26
- } | null | void;
20
+ import type { TryCatchReturn } from "./async.svelte.js";
21
+ export type { TryCatchReturn } from "./async.svelte.js";
27
22
  export declare function tryCatch(task: () => Promise<TryCatchReturn> | TryCatchReturn): Promise<void>;
28
23
  export declare const wait: (ms: number) => Promise<unknown>;
29
- export declare function createAsync<T extends (...args: any[]) => Promise<TryCatchReturn> | Promise<void>>(asyncFn: T): {
30
- isLoading(key?: string): boolean;
31
- readonly error: Error | null;
32
- run: (...args: Parameters<T>) => Promise<TryCatchReturn>;
33
- runWithKey: (key: string, ...args: Parameters<T>) => Promise<TryCatchReturn>;
34
- };
24
+ export { createAsync } from "./async.svelte.js";
35
25
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,aAAa;IAC5B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,QAAQ,CAAC,EAAE,KAAK,GAAG,QAAQ,GAAG,MAAM,CAAC;IACrC,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;AAuCD,eAAO,MAAM,OAAO;cACR,MAAM,SAAS,MAAM,YAAW,aAAa,GAAQ,IAAI;cA6CzD,MAAM,GAAG,MAAM,GAAG,IAAI;iBAYnB,MAAM,YAAW,IAAI,CAAC,aAAa,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eAAO,MAAM,OAAO;cACR,MAAM,SAAS,MAAM,YAAW,aAAa,GAAQ,IAAI;cA6CzD,MAAM,GAAG,MAAM,GAAG,IAAI;iBAYnB,MAAM,YAAW,IAAI,CAAC,aAAa,EAAE,MAAM,GAAG,QAAQ,CAAC,GAAQ,IAAI;CAGjF,CAAC;AAEF,wBAAgB,UAAU,CAAC,CAAC,SAAS,OAAO,EAC1C,UAAU,EAAE,CAAC,GACZ,CAAC,SAAS,IAAI,GAAG;IAAE,SAAS,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAInF;AAED,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACxD,YAAY,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAExD,wBAAsB,QAAQ,CAAC,IAAI,EAAE,MAAM,OAAO,CAAC,cAAc,CAAC,GAAG,cAAc,iBAkBlF;AAED,eAAO,MAAM,IAAI,GAAI,IAAI,MAAM,qBAAsD,CAAC;AAEtF,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC"}
package/dist/index.js CHANGED
@@ -1,5 +1,4 @@
1
1
  import { DEV } from "esm-env";
2
- import { SvelteMap } from "svelte/reactivity";
3
2
  let toastModulePromise = null;
4
3
  function hasBrowser() {
5
4
  return typeof window !== "undefined" && typeof document !== "undefined";
@@ -94,53 +93,4 @@ export async function tryCatch(task) {
94
93
  }
95
94
  }
96
95
  export const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
97
- const GLOBAL_KEY = "__global__";
98
- export function createAsync(asyncFn) {
99
- const loadingStates = $state(new SvelteMap());
100
- let error = $state(null);
101
- async function execute(id, args) {
102
- try {
103
- loadingStates.set(id, true);
104
- error = null;
105
- const response = await asyncFn(...args);
106
- if (response?.success) {
107
- await toastSuccess(response.success);
108
- }
109
- else if (response?.error) {
110
- await toastError(response.error);
111
- }
112
- return response;
113
- }
114
- catch (err) {
115
- const e = err instanceof Error ? err : new Error(String(err));
116
- error = e;
117
- if (DEV) {
118
- await toastError(e.name, { description: e.message });
119
- console.error("[Dev Error]:", e);
120
- }
121
- else {
122
- await toastError("Something went wrong");
123
- }
124
- throw e;
125
- }
126
- finally {
127
- loadingStates.set(id, false);
128
- }
129
- }
130
- async function run(...args) {
131
- return execute(GLOBAL_KEY, args);
132
- }
133
- async function runWithKey(key, ...args) {
134
- return execute(key || GLOBAL_KEY, args);
135
- }
136
- return {
137
- isLoading(key) {
138
- return loadingStates.get(key ?? GLOBAL_KEY) ?? false;
139
- },
140
- get error() {
141
- return error;
142
- },
143
- run,
144
- runWithKey
145
- };
146
- }
96
+ export { createAsync } from "./async.svelte.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltebase/utils",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "type": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"