import { createPubSub } from "create-pubsub"; import throttle from "throttleit"; import { addLogEntry } from "./logEntries"; import { defaultSettings } from "./settings"; import type { ImageSearchResults, SearchResults, SearchState, TextGenerationState, TextSearchResults, } from "./types"; function createLocalStoragePubSub(localStorageKey: string, defaultValue: T) { const localStorageValue = localStorage.getItem(localStorageKey); const localStoragePubSub = createPubSub( localStorageValue ? (JSON.parse(localStorageValue) as T) : defaultValue, ); const [, onValueChange] = localStoragePubSub; onValueChange((value) => localStorage.setItem(localStorageKey, JSON.stringify(value)), ); return localStoragePubSub; } const querySuggestionsPubSub = createLocalStoragePubSub( "querySuggestions", [], ); const lastSearchTokenHashPubSub = createLocalStoragePubSub( "lastSearchTokenHash", "", ); export const [updateLastSearchTokenHash, , getLastSearchTokenHash] = lastSearchTokenHashPubSub; export const [updateQuerySuggestions, , getQuerySuggestions] = querySuggestionsPubSub; export const queryPubSub = createPubSub( new URLSearchParams(self.location.search).get("q") ?? "", ); export const [, , getQuery] = queryPubSub; export const responsePubSub = createPubSub(""); export const updateResponse = throttle(responsePubSub[0], 1000 / 12); const searchResultsPubSub = createPubSub({ textResults: [], imageResults: [], }); export const [updateSearchResults, , getSearchResults] = searchResultsPubSub; export const [updateSearchPromise, , getSearchPromise] = createPubSub< Promise >(Promise.resolve({ textResults: [], imageResults: [] })); export const textGenerationStatePubSub = createPubSub("idle"); export const [updateTextGenerationState, , getTextGenerationState] = textGenerationStatePubSub; const [, listenToTextGenerationStateChanges] = textGenerationStatePubSub; listenToTextGenerationStateChanges((textGenerationState) => { addLogEntry(`Text generation state changed to '${textGenerationState}'`); }); export const modelLoadingProgressPubSub = createPubSub(0); export const [updateModelLoadingProgress] = modelLoadingProgressPubSub; export const settingsPubSub = createLocalStoragePubSub( "settings", defaultSettings, ); export const [, listenToSettingsChanges, getSettings] = settingsPubSub; export const modelSizeInMegabytesPubSub = createPubSub(0); export const [updateModelSizeInMegabytes] = modelSizeInMegabytesPubSub; export const textSearchStatePubSub = createPubSub("idle"); export const imageSearchStatePubSub = createPubSub("idle"); export const [updateTextSearchState] = textSearchStatePubSub; const [, subscribeToTextSearchState] = textSearchStatePubSub; subscribeToTextSearchState((textSearchState) => { addLogEntry(`Text search state changed to '${textSearchState}'`); }); export const [updateImageSearchState] = imageSearchStatePubSub; const [, subscribeToImageSearchState] = imageSearchStatePubSub; subscribeToImageSearchState((imageSearchState) => { addLogEntry(`Image search state changed to '${imageSearchState}'`); }); export const textSearchResultsPubSub = createPubSub([]); export const imageSearchResultsPubSub = createPubSub([]); export const [updateTextSearchResults] = textSearchResultsPubSub; export const [updateImageSearchResults] = imageSearchResultsPubSub;