Skip to content

Commit

Permalink
chore: make getter fn for chrome.storage
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranjamie authored and fbwoolf committed Oct 25, 2023
1 parent c7c5c3e commit d9bd157
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/shared/logger-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ const maxLogLength = 2_000;

const logStorageKey = 'logs';

const storageAdapter = chrome.storage.local;
function getStorageAdapter() {
return chrome.storage.local;
}

function truncateLogToMaxSize(logs: LogItem[]) {
if (logs.length <= maxLogLength) return logs;
Expand All @@ -15,17 +17,17 @@ function truncateLogToMaxSize(logs: LogItem[]) {

export async function getLogSizeInBytes(): Promise<number> {
return new Promise(resolve =>
storageAdapter.getBytesInUse([logStorageKey], bytes => resolve(bytes))
getStorageAdapter().getBytesInUse([logStorageKey], bytes => resolve(bytes))
);
}

export async function clearBrowserStorageLogs(): Promise<void> {
return new Promise(resolve => storageAdapter.set({ [logStorageKey]: [] }, () => resolve()));
return new Promise(resolve => getStorageAdapter().set({ [logStorageKey]: [] }, () => resolve()));
}

export async function getLogsFromBrowserStorage(): Promise<LogItem[]> {
return new Promise(resolve =>
storageAdapter.get([logStorageKey], ({ logs }) => resolve(Array.isArray(logs) ? logs : []))
getStorageAdapter().get([logStorageKey], ({ logs }) => resolve(Array.isArray(logs) ? logs : []))
);
}

Expand All @@ -39,8 +41,9 @@ export async function appendLogToBrowserStorage(logEvent: pino.LogEvent): Promis
const { ts, level, messages } = logEvent;
const formattedLogItem = [new Date(ts).toISOString(), level.label, ...messages] as LogItem;
return new Promise(resolve =>
storageAdapter.set({ [logStorageKey]: truncateLogToMaxSize([formattedLogItem, ...logs]) }, () =>
resolve(formattedLogItem)
getStorageAdapter().set(
{ [logStorageKey]: truncateLogToMaxSize([formattedLogItem, ...logs]) },
() => resolve(formattedLogItem)
)
);
}

0 comments on commit d9bd157

Please sign in to comment.