Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

imperative.ts

48 lines2.4 KB

1import * as Effect from "effect/Effect";
2​
3import { runtime } from "../lib/runtime";
4import * as MobilePreferences from "./mobile-preferences";
5import * as MobileStorage from "./mobile-storage";
6​
7export type { Preferences } from "./mobile-preferences";
8export type { AgentAwarenessRegistrationRecord, RecentThreadShortcut } from "./mobile-storage";
9export { MobilePreferencesLoadError, MobilePreferencesSaveError } from "./mobile-preferences";
10export {
11 MobileDeviceIdGenerationError,
12 MobileStorageDecodeError,
13 MobileStorageEncodeError,
14} from "./mobile-storage";
15export { MobileSecureStorageError } from "./mobile-secure-storage";
16​
17const runStorage = <A, E>(
18 use: (storage: MobileStorage.MobileStorage["Service"]) => Effect.Effect<A, E>,
19) => runtime.runPromise(MobileStorage.MobileStorage.pipe(Effect.flatMap(use)));
20​
21const runPreferences = <A, E>(
22 use: (store: MobilePreferences.MobilePreferencesStore["Service"]) => Effect.Effect<A, E>,
23) => runtime.runPromise(MobilePreferences.MobilePreferencesStore.pipe(Effect.flatMap(use)));
24​
25export const loadPreferences = () => runPreferences((store) => store.load);
26export const savePreferencesPatch = (patch: Partial<MobilePreferences.Preferences>) =>
27 runPreferences((store) => store.savePatch(patch));
28export const updatePreferences = (
29 transform: (current: MobilePreferences.Preferences) => Partial<MobilePreferences.Preferences>,
30) => runPreferences((store) => store.update(transform));
31​
32export const loadOrCreateAgentAwarenessDeviceId = () =>
33 runStorage((storage) => storage.loadOrCreateAgentAwarenessDeviceId);
34export const loadAgentAwarenessDeviceId = () =>
35 runStorage((storage) => storage.loadAgentAwarenessDeviceId);
36export const loadAgentAwarenessRegistrationRecord = () =>
37 runStorage((storage) => storage.loadAgentAwarenessRegistrationRecord);
38export const saveAgentAwarenessRegistrationRecord = (
39 record: MobileStorage.AgentAwarenessRegistrationRecord,
40) => runStorage((storage) => storage.saveAgentAwarenessRegistrationRecord(record));
41export const clearAgentAwarenessRegistrationRecord = () =>
42 runStorage((storage) => storage.clearAgentAwarenessRegistrationRecord);
43​
44export const loadRecentThreadShortcuts = () =>
45 runStorage((storage) => storage.loadRecentThreadShortcuts);
46export const saveRecentThreadShortcuts = (
47 threads: ReadonlyArray<MobileStorage.RecentThreadShortcut>,
48) => runStorage((storage) => storage.saveRecentThreadShortcuts(threads));