pull/351/head
Lucas Nogueira 2 years ago
parent 16abdbfcf8
commit 954787ee2e
No known key found for this signature in database
GPG Key ID: FFEA6C72E73482F1

@ -1,41 +1,41 @@
import { invoke, transformCallback } from '@tauri-apps/api/tauri'
import { invoke, transformCallback } from "@tauri-apps/api/tauri";
interface CheckOptions {
/**
* Request headers
*/
headers?: Record<string, unknown>
headers?: Record<string, unknown>;
/**
* Timeout in seconds
*/
timeout?: number
timeout?: number;
/**
* Target identifier for the running application. This is sent to the backend.
*/
target?: string
target?: string;
}
interface UpdateResponse {
available: boolean
currentVersion: string
latestVersion: string
date?: string
body?: string
available: boolean;
currentVersion: string;
latestVersion: string;
date?: string;
body?: string;
}
// TODO: use channel from @tauri-apps/api on v2
class Channel<T = unknown> {
id: number
id: number;
// @ts-expect-error field used by the IPC serializer
private readonly __TAURI_CHANNEL_MARKER__ = true
private readonly __TAURI_CHANNEL_MARKER__ = true;
#onmessage: (response: T) => void = () => {
// no-op
}
};
constructor() {
this.id = transformCallback((response: T) => {
this.#onmessage(response)
})
this.#onmessage(response);
});
}
set onmessage(handler: (response: T) => void) {
@ -43,38 +43,42 @@ class Channel<T = unknown> {
}
get onmessage(): (response: T) => void {
return this.#onmessage
return this.#onmessage;
}
toJSON(): string {
return `__CHANNEL__:${this.id}`
return `__CHANNEL__:${this.id}`;
}
}
type DownloadEvent =
{ event: 'Started', data: { contentLength?: number } } |
{ event: 'Progress', data: { chunkLength: number } } |
{ event: 'Finished' }
| { event: "Started"; data: { contentLength?: number } }
| { event: "Progress"; data: { chunkLength: number } }
| { event: "Finished" };
class Update {
response: UpdateResponse
response: UpdateResponse;
constructor(response: UpdateResponse) {
this.response = response
this.response = response;
}
async downloadAndInstall(onEvent?: (progress: DownloadEvent) => void): Promise<void> {
const channel = new Channel<DownloadEvent>()
async downloadAndInstall(
onEvent?: (progress: DownloadEvent) => void
): Promise<void> {
const channel = new Channel<DownloadEvent>();
if (onEvent != null) {
channel.onmessage = onEvent
channel.onmessage = onEvent;
}
return invoke('plugin:updater|download_and_install', { onEvent: channel })
return invoke("plugin:updater|download_and_install", { onEvent: channel });
}
}
async function check(options?: CheckOptions): Promise<Update> {
return invoke<UpdateResponse>('plugin:updater|check', { ...options }).then(response => new Update(response))
return invoke<UpdateResponse>("plugin:updater|check", { ...options }).then(
(response) => new Update(response)
);
}
export type { CheckOptions, UpdateResponse, DownloadEvent }
export { check, Update }
export type { CheckOptions, UpdateResponse, DownloadEvent };
export { check, Update };

@ -29,4 +29,4 @@
"dependencies": {
"@tauri-apps/api": "^1.2.0"
}
}
}

@ -29,12 +29,10 @@
"updater": {
"active": true,
"pubkey": "dW50cnVzdGVkIGNvbW1lbnQ6IG1pbmlzaWduIHB1YmxpYyBrZXk6IDE5QzMxNjYwNTM5OEUwNTgKUldSWTRKaFRZQmJER1h4d1ZMYVA3dnluSjdpN2RmMldJR09hUFFlZDY0SlFqckkvRUJhZDJVZXAK",
"endpoints": [
"http://localhost:3007"
],
"endpoints": ["http://localhost:3007"],
"windows": {
"installMode": "quiet"
}
}
}
}
}

Loading…
Cancel
Save