Skip to content

Commit

Permalink
#737 fix offline search use
Browse files Browse the repository at this point in the history
  • Loading branch information
getneil committed Jul 24, 2023
1 parent 07f0d55 commit 2b97eda
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 36 deletions.
37 changes: 13 additions & 24 deletions svelte/src/libs/native-electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,36 @@
* - connect to a local platform api and returns a data
*/

import type { Package, Review, AirtablePost } from "$libs/types";
import type { Package, AirtablePost } from "$libs/types";
import type { GUIPackage, Session, Packages, AutoUpdateStatus } from "./types";

import * as mock from "./native-mock";
import { PackageStates, type InstalledPackage } from "./types";
import type { InstalledPackage } from "./types";

import { get as apiGet } from "$libs/v1-client";
import withRetry from "./utils/retry";
import log from "./logger";
const { ipcRenderer, shell } = window.require("electron");

export async function getDistPackages(): Promise<Package[]> {
let pkgs: Package[] = [];
try {
return withRetry(async () => {
const pkgs = await withRetry(async () => {
const packages = await apiGet<Package[]>("packages");
log.info("packages received:", packages?.length);
return packages || [];
});
} catch (error) {
log.error("getDistPackagesList:", error);
return [];

// rebuild packages[] from cache
const cachedPkgs = await loadPackageCache();
if (cachedPkgs?.packages) {
pkgs = Object.values(cachedPkgs.packages);
}
}

return pkgs;
}

export async function getInstalledPackages(): Promise<InstalledPackage[]> {
Expand All @@ -56,25 +64,6 @@ export async function getInstalledVersionsForPackage(fullName: string): Promise<
return result as InstalledPackage;
}

export async function getPackages(): Promise<GUIPackage[]> {
const [packages, installedPackages] = await Promise.all([
getDistPackages(),
ipcRenderer.invoke("get-installed-packages") as InstalledPackage[]
]);

// NOTE: its not ideal to get bottles or set package states here maybe do it async in the package store init
// --- it has noticeable slowness
log.info(`native: installed ${installedPackages.length} out of ${(packages || []).length}`);
return (packages || []).map((pkg) => {
const installedPkg = installedPackages.find((p) => p.full_name === pkg.full_name);
return {
...pkg,
state: installedPkg ? PackageStates.INSTALLED : PackageStates.AVAILABLE,
installed_versions: installedPkg?.installed_versions || []
};
});
}

export async function installPackage(pkg: GUIPackage, version?: string) {
const latestVersion = pkg.version;
const specificVersion = version || latestVersion;
Expand Down Expand Up @@ -231,7 +220,7 @@ export const deletePackage = async (args: { fullName: string; version: string })
}
};

export const loadPackageCache = async () => {
export const loadPackageCache = async (): Promise<Packages | void> => {
try {
return await ipcRenderer.invoke("load-package-cache");
} catch (error) {
Expand Down
9 changes: 0 additions & 9 deletions svelte/src/libs/native-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,6 @@ export async function getInstalledVersionsForPackage(full_name: string): Promise
}) as Package;
}

export async function getPackages(): Promise<GUIPackage[]> {
return packages.map((pkg) => {
return {
...pkg,
state: PackageStates.AVAILABLE
};
});
}

export async function installPackage(pkg: GUIPackage, version?: string) {
console.log("installing: ", pkg.full_name, version);
await delay(10000);
Expand Down
8 changes: 5 additions & 3 deletions svelte/src/libs/stores/pkgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,11 @@ const init = async function () {
log.info("packages store: try initialize");

if (!initialized) {
const cachedPkgs: Packages = await loadPackageCache();
log.info(`Loaded ${Object.keys(cachedPkgs.packages).length} packages from cache`);
packageMap.set(cachedPkgs);
const cachedPkgs = await loadPackageCache();
if (cachedPkgs) {
log.info(`Loaded ${Object.keys(cachedPkgs.packages).length} packages from cache`);
packageMap.set(cachedPkgs);
}

await refreshPackages();
await monitorTeaDir();
Expand Down

0 comments on commit 2b97eda

Please sign in to comment.