|
4 | 4 |
|
5 | 5 | /** user-defined commands **/ |
6 | 6 |
|
| 7 | + |
7 | 8 | export const commands = { |
8 | | - async event(payload: AnalyticsPayload): Promise<Result<null, string>> { |
| 9 | +async event(payload: AnalyticsPayload) : Promise<Result<null, string>> { |
9 | 10 | try { |
10 | | - return { status: "ok", data: await TAURI_INVOKE("plugin:analytics|event", { payload }) }; |
11 | | - } catch (e) { |
12 | | - if (e instanceof Error) throw e; |
13 | | - else return { status: "error", error: e as any }; |
14 | | - } |
15 | | - }, |
16 | | - async setProperties(payload: PropertiesPayload): Promise<Result<null, string>> { |
| 11 | + return { status: "ok", data: await TAURI_INVOKE("plugin:analytics|event", { payload }) }; |
| 12 | +} catch (e) { |
| 13 | + if(e instanceof Error) throw e; |
| 14 | + else return { status: "error", error: e as any }; |
| 15 | +} |
| 16 | +}, |
| 17 | +async setProperties(payload: PropertiesPayload) : Promise<Result<null, string>> { |
17 | 18 | try { |
18 | | - return { |
19 | | - status: "ok", |
20 | | - data: await TAURI_INVOKE("plugin:analytics|set_properties", { payload }), |
21 | | - }; |
22 | | - } catch (e) { |
23 | | - if (e instanceof Error) throw e; |
24 | | - else return { status: "error", error: e as any }; |
25 | | - } |
26 | | - }, |
27 | | - async setDisabled(disabled: boolean): Promise<Result<null, string>> { |
| 19 | + return { status: "ok", data: await TAURI_INVOKE("plugin:analytics|set_properties", { payload }) }; |
| 20 | +} catch (e) { |
| 21 | + if(e instanceof Error) throw e; |
| 22 | + else return { status: "error", error: e as any }; |
| 23 | +} |
| 24 | +}, |
| 25 | +async setDisabled(disabled: boolean) : Promise<Result<null, string>> { |
28 | 26 | try { |
29 | | - return { |
30 | | - status: "ok", |
31 | | - data: await TAURI_INVOKE("plugin:analytics|set_disabled", { disabled }), |
32 | | - }; |
33 | | - } catch (e) { |
34 | | - if (e instanceof Error) throw e; |
35 | | - else return { status: "error", error: e as any }; |
36 | | - } |
37 | | - }, |
38 | | - async isDisabled(): Promise<Result<boolean, string>> { |
| 27 | + return { status: "ok", data: await TAURI_INVOKE("plugin:analytics|set_disabled", { disabled }) }; |
| 28 | +} catch (e) { |
| 29 | + if(e instanceof Error) throw e; |
| 30 | + else return { status: "error", error: e as any }; |
| 31 | +} |
| 32 | +}, |
| 33 | +async isDisabled() : Promise<Result<boolean, string>> { |
39 | 34 | try { |
40 | | - return { status: "ok", data: await TAURI_INVOKE("plugin:analytics|is_disabled") }; |
41 | | - } catch (e) { |
42 | | - if (e instanceof Error) throw e; |
43 | | - else return { status: "error", error: e as any }; |
44 | | - } |
45 | | - }, |
46 | | -}; |
| 35 | + return { status: "ok", data: await TAURI_INVOKE("plugin:analytics|is_disabled") }; |
| 36 | +} catch (e) { |
| 37 | + if(e instanceof Error) throw e; |
| 38 | + else return { status: "error", error: e as any }; |
| 39 | +} |
| 40 | +} |
| 41 | +} |
47 | 42 |
|
48 | 43 | /** user-defined events **/ |
49 | 44 |
|
| 45 | + |
| 46 | + |
50 | 47 | /** user-defined constants **/ |
51 | 48 |
|
| 49 | + |
| 50 | + |
52 | 51 | /** user-defined types **/ |
53 | 52 |
|
54 | | -export type AnalyticsPayload = Partial<{ |
55 | | - [key in string]: |
56 | | - | null |
57 | | - | boolean |
58 | | - | number |
59 | | - | string |
60 | | - | JsonValue[] |
61 | | - | Partial<{ [key in string]: JsonValue }>; |
62 | | -}> & { event: string }; |
63 | | -export type JsonValue = |
64 | | - | null |
65 | | - | boolean |
66 | | - | number |
67 | | - | string |
68 | | - | JsonValue[] |
69 | | - | Partial<{ [key in string]: JsonValue }>; |
70 | | -export type PropertiesPayload = { |
71 | | - set?: Partial<{ [key in string]: JsonValue }>; |
72 | | - set_once?: Partial<{ [key in string]: JsonValue }>; |
73 | | -}; |
| 53 | +export type AnalyticsPayload = (Partial<{ [key in string]: null | boolean | number | string | JsonValue[] | Partial<{ [key in string]: JsonValue }> }>) & { event: string } |
| 54 | +export type JsonValue = null | boolean | number | string | JsonValue[] | Partial<{ [key in string]: JsonValue }> |
| 55 | +export type PropertiesPayload = { set?: Partial<{ [key in string]: JsonValue }>; set_once?: Partial<{ [key in string]: JsonValue }> } |
74 | 56 |
|
75 | 57 | /** tauri-specta globals **/ |
76 | 58 |
|
77 | | -import { invoke as TAURI_INVOKE, Channel as TAURI_CHANNEL } from "@tauri-apps/api/core"; |
| 59 | +import { |
| 60 | + invoke as TAURI_INVOKE, |
| 61 | + Channel as TAURI_CHANNEL, |
| 62 | +} from "@tauri-apps/api/core"; |
78 | 63 | import * as TAURI_API_EVENT from "@tauri-apps/api/event"; |
79 | 64 | import { type WebviewWindow as __WebviewWindow__ } from "@tauri-apps/api/webviewWindow"; |
80 | 65 |
|
81 | 66 | type __EventObj__<T> = { |
82 | | - listen: (cb: TAURI_API_EVENT.EventCallback<T>) => ReturnType<typeof TAURI_API_EVENT.listen<T>>; |
83 | | - once: (cb: TAURI_API_EVENT.EventCallback<T>) => ReturnType<typeof TAURI_API_EVENT.once<T>>; |
84 | | - emit: null extends T |
85 | | - ? (payload?: T) => ReturnType<typeof TAURI_API_EVENT.emit> |
86 | | - : (payload: T) => ReturnType<typeof TAURI_API_EVENT.emit>; |
| 67 | + listen: ( |
| 68 | + cb: TAURI_API_EVENT.EventCallback<T>, |
| 69 | + ) => ReturnType<typeof TAURI_API_EVENT.listen<T>>; |
| 70 | + once: ( |
| 71 | + cb: TAURI_API_EVENT.EventCallback<T>, |
| 72 | + ) => ReturnType<typeof TAURI_API_EVENT.once<T>>; |
| 73 | + emit: null extends T |
| 74 | + ? (payload?: T) => ReturnType<typeof TAURI_API_EVENT.emit> |
| 75 | + : (payload: T) => ReturnType<typeof TAURI_API_EVENT.emit>; |
87 | 76 | }; |
88 | 77 |
|
89 | | -export type Result<T, E> = { status: "ok"; data: T } | { status: "error"; error: E }; |
90 | | - |
91 | | -function __makeEvents__<T extends Record<string, any>>(mappings: Record<keyof T, string>) { |
92 | | - return new Proxy( |
93 | | - {} as unknown as { |
94 | | - [K in keyof T]: __EventObj__<T[K]> & { |
95 | | - (handle: __WebviewWindow__): __EventObj__<T[K]>; |
96 | | - }; |
97 | | - }, |
98 | | - { |
99 | | - get: (_, event) => { |
100 | | - const name = mappings[event as keyof T]; |
101 | | - |
102 | | - return new Proxy((() => {}) as any, { |
103 | | - apply: (_, __, [window]: [__WebviewWindow__]) => ({ |
104 | | - listen: (arg: any) => window.listen(name, arg), |
105 | | - once: (arg: any) => window.once(name, arg), |
106 | | - emit: (arg: any) => window.emit(name, arg), |
107 | | - }), |
108 | | - get: (_, command: keyof __EventObj__<any>) => { |
109 | | - switch (command) { |
110 | | - case "listen": |
111 | | - return (arg: any) => TAURI_API_EVENT.listen(name, arg); |
112 | | - case "once": |
113 | | - return (arg: any) => TAURI_API_EVENT.once(name, arg); |
114 | | - case "emit": |
115 | | - return (arg: any) => TAURI_API_EVENT.emit(name, arg); |
116 | | - } |
117 | | - }, |
118 | | - }); |
119 | | - }, |
120 | | - }, |
121 | | - ); |
| 78 | +export type Result<T, E> = |
| 79 | + | { status: "ok"; data: T } |
| 80 | + | { status: "error"; error: E }; |
| 81 | + |
| 82 | +function __makeEvents__<T extends Record<string, any>>( |
| 83 | + mappings: Record<keyof T, string>, |
| 84 | +) { |
| 85 | + return new Proxy( |
| 86 | + {} as unknown as { |
| 87 | + [K in keyof T]: __EventObj__<T[K]> & { |
| 88 | + (handle: __WebviewWindow__): __EventObj__<T[K]>; |
| 89 | + }; |
| 90 | + }, |
| 91 | + { |
| 92 | + get: (_, event) => { |
| 93 | + const name = mappings[event as keyof T]; |
| 94 | + |
| 95 | + return new Proxy((() => {}) as any, { |
| 96 | + apply: (_, __, [window]: [__WebviewWindow__]) => ({ |
| 97 | + listen: (arg: any) => window.listen(name, arg), |
| 98 | + once: (arg: any) => window.once(name, arg), |
| 99 | + emit: (arg: any) => window.emit(name, arg), |
| 100 | + }), |
| 101 | + get: (_, command: keyof __EventObj__<any>) => { |
| 102 | + switch (command) { |
| 103 | + case "listen": |
| 104 | + return (arg: any) => TAURI_API_EVENT.listen(name, arg); |
| 105 | + case "once": |
| 106 | + return (arg: any) => TAURI_API_EVENT.once(name, arg); |
| 107 | + case "emit": |
| 108 | + return (arg: any) => TAURI_API_EVENT.emit(name, arg); |
| 109 | + } |
| 110 | + }, |
| 111 | + }); |
| 112 | + }, |
| 113 | + }, |
| 114 | + ); |
122 | 115 | } |
0 commit comments