refactor pkgs store (#581)

Co-authored-by: neil molina <neil@neils-MacBook-Pro.local>
This commit is contained in:
Neil 2023-05-10 13:15:29 +08:00 committed by GitHub
parent fb854d85a1
commit dcc9a34e2c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 298 additions and 285 deletions

View file

@ -0,0 +1,26 @@
import type { GUIPackage, InstalledPackage, Packages } from "./types";
import Fuse from "fuse.js";
import log from "$libs/logger";
let packagesIndex: Fuse<GUIPackage>;
export function indexPackages(packages: GUIPackage[]) {
try {
packagesIndex = new Fuse(packages, {
keys: ["name", "full_name", "desc", "categories"],
minMatchCharLength: 3,
threshold: 0.3
});
log.info("refreshed packages fuse index");
} catch (error) {
log.error(error);
}
}
export function searchPackages(term: string, limit = 5): GUIPackage[] {
if (!term || !packagesIndex) return [];
// TODO: if online, use algolia else use Fuse
const res = packagesIndex.search(term, { limit });
const matchingPackages: GUIPackage[] = res.map((v) => v.item);
return matchingPackages;
}

View file

@ -7,14 +7,14 @@ import type { GUIPackage } from "$libs/types";
import { getFeaturedPackages, getPackageReviews } from "@native";
import initAuthStore from "./stores/auth";
import initNavStore from "./stores/nav";
import initPackagesStore from "./stores/pkgs";
import pkgStore from "./stores/pkgs";
import initNotificationStore from "./stores/notifications";
import initAppUpdateStore from "./stores/update";
import { trackSearch } from "./analytics";
export const featuredPackages = writable<Package[]>([]);
export const packagesStore = initPackagesStore();
export const packagesStore = pkgStore;
export const initializeFeaturedPackages = async () => {
console.log("intialize featured packages");

View file

@ -1,7 +1,6 @@
import { derived, writable } from "svelte/store";
import type { GUIPackage, InstalledPackage, Packages } from "../types";
import { PackageStates } from "../types";
import Fuse from "fuse.js";
import {
getPackage,
getDistPackages,
@ -29,9 +28,10 @@ import log from "$libs/logger";
import { isPackageUpToDate } from "../packages/pkg-utils";
import withDelay from "$libs/utils/delay";
import { indexPackages, searchPackages } from "$libs/search-index";
const packageRefreshInterval = 1000 * 60 * 60; // 1 hour
export default function initPackagesStore() {
let initialized = false;
let isDestroyed = false;
let refreshTimeoutId: ReturnType<typeof setTimeout> | null = null;
@ -46,8 +46,6 @@ export default function initPackagesStore() {
})
);
let packagesIndex: Fuse<GUIPackage>;
const updateAllPackages = (guiPkgs: GUIPackage[]) => {
packageMap.update((pkgs) => {
guiPkgs.forEach((pkg) => {
@ -174,12 +172,8 @@ To read more about this package go to [${guiPkg.homepage}](${guiPkg.homepage}).
log.info("initialized packages store with ", guiPkgs.length);
}
packagesIndex = new Fuse(guiPkgs, {
keys: ["name", "full_name", "desc", "categories"],
minMatchCharLength: 3,
threshold: 0.3
});
log.info("refreshed packages fuse index");
// initialize Fuse index for fuzzy search
indexPackages(guiPkgs);
try {
const installedPkgs: InstalledPackage[] = await getInstalledPackages();
@ -312,25 +306,6 @@ To read more about this package go to [${guiPkg.homepage}](${guiPkg.homepage}).
updatePackage(full_name, {}, version);
});
return {
packageList,
search: async (term: string, limit = 5): Promise<GUIPackage[]> => {
if (!term || !packagesIndex) return [];
// TODO: if online, use algolia else use Fuse
const res = packagesIndex.search(term, { limit });
const matchingPackages: GUIPackage[] = res.map((v) => v.item);
return matchingPackages;
},
init,
installPkg,
uninstallPkg,
syncPackageData,
deletePkg,
destroy,
cachePkgImage
};
}
// This is only used for uninstall now
export const withFakeLoader = (
pkg: GUIPackage,
@ -364,3 +339,15 @@ const setBadgeCountFromPkgs = (pkgs: Packages) => {
log.error(error);
}
};
export default {
packageList,
search: searchPackages,
init,
installPkg,
uninstallPkg,
syncPackageData,
deletePkg,
destroy,
cachePkgImage
};