Skip to content

Commit

Permalink
Fix periodic indexing for multi package manager configs
Browse files Browse the repository at this point in the history
Closes #40
  • Loading branch information
ThatsJustCheesy committed Feb 28, 2023
1 parent 9e2e487 commit 8965ab1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
7 changes: 4 additions & 3 deletions src/renderer/preload/platform/Windows/winget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ const winget: IPCWinget = {
} catch (e) {}

const nowTime = new Date().getTime();
const lastFullIndexTime = cacheDB_lastFullIndexJsTimestamp("winget");
const secondsSinceIndexBuilt = (nowTime - lastFullIndexTime) / 1000;
const indexTooOld =
(nowTime - cacheDB_lastFullIndexJsTimestamp()) / 1000 >
(await getFullIndexIntervalInSeconds());
secondsSinceIndexBuilt > (await getFullIndexIntervalInSeconds());

if (
!indexExists ||
Expand All @@ -82,7 +83,7 @@ const winget: IPCWinget = {
(winget as any)._ingestPackageInfo(undefined, manifests);

(winget as any)._postIndexing();
cacheDB_updateLastFullIndexJsTimestamp();
cacheDB_updateLastFullIndexJsTimestamp("winget");
},

async indexOutdated(): Promise<void> {
Expand Down
7 changes: 4 additions & 3 deletions src/renderer/preload/platform/macOS/brew.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ const brew: IPCBrew = {
} catch (e) {}

const nowTime = new Date().getTime();
const lastFullIndexTime = cacheDB_lastFullIndexJsTimestamp("brew");
const secondsSinceIndexBuilt = (nowTime - lastFullIndexTime) / 1000;
const indexTooOld =
(nowTime - cacheDB_lastFullIndexJsTimestamp()) / 1000 >
(await getFullIndexIntervalInSeconds());
secondsSinceIndexBuilt > (await getFullIndexIntervalInSeconds());

if (
!indexExists ||
Expand All @@ -102,7 +103,7 @@ const brew: IPCBrew = {
]);

(brew as any)._postIndexing();
cacheDB_updateLastFullIndexJsTimestamp();
cacheDB_updateLastFullIndexJsTimestamp("brew");
},

async indexOutdated(): Promise<void> {
Expand Down
8 changes: 4 additions & 4 deletions src/renderer/preload/platform/macOS/brewCask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ const brewCask: IPCBrewCask = {
} catch (e) {}

const nowTime = new Date().getTime();
const timeSinceIndexBuilt =
(nowTime - cacheDB_lastFullIndexJsTimestamp()) / 1000;
const lastFullIndexTime = cacheDB_lastFullIndexJsTimestamp("brew-cask");
const secondsSinceIndexBuilt = (nowTime - lastFullIndexTime) / 1000;
const indexTooOld =
timeSinceIndexBuilt > (await getFullIndexIntervalInSeconds());
secondsSinceIndexBuilt > (await getFullIndexIntervalInSeconds());

if (
!indexExists ||
Expand All @@ -196,7 +196,7 @@ const brewCask: IPCBrewCask = {
]);

(brewCask as any)._postIndexing();
cacheDB_updateLastFullIndexJsTimestamp();
cacheDB_updateLastFullIndexJsTimestamp("brew-cask");
},

async indexOutdated(): Promise<void> {
Expand Down
23 changes: 17 additions & 6 deletions src/renderer/preload/shared/db/cacheDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function cacheDB_addSchema(schema: string) {
}

type CacheDBMeta = {
lastFullIndexJsTimestamp?: number;
lastFullIndexJsTimestamp?: Record<string /* package manager key */, number>;
};

function cacheDB_loadMeta(): CacheDBMeta {
Expand All @@ -58,13 +58,24 @@ function cacheDB_saveMeta(meta: CacheDBMeta) {
fs.writeFileSync(cacheMetaPath, JSON.stringify(meta));
}

export function cacheDB_lastFullIndexJsTimestamp(): number {
return cacheDB_loadMeta()?.lastFullIndexJsTimestamp ?? new Date().getTime();
export function cacheDB_lastFullIndexJsTimestamp(
packageManagerKey: string
): number {
return (
cacheDB_loadMeta()?.lastFullIndexJsTimestamp?.[packageManagerKey] ??
new Date().getTime()
);
}
export function cacheDB_updateLastFullIndexJsTimestamp(): void {
export function cacheDB_updateLastFullIndexJsTimestamp(
packageManagerKey: string
): void {
const oldMeta = cacheDB_loadMeta();
cacheDB_saveMeta({
...cacheDB_loadMeta(),
lastFullIndexJsTimestamp: new Date().getTime(),
...oldMeta,
lastFullIndexJsTimestamp: {
...oldMeta.lastFullIndexJsTimestamp,
[packageManagerKey]: new Date().getTime(),
},
});
}

Expand Down

0 comments on commit 8965ab1

Please sign in to comment.