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

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

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

Loading…
Cancel
Save