Something went wrong. Try again.
Hopefully feature-complete Android Bluesky client written in Expo
atproto bluesky
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960import { ResourceUri } from "@atcute/lexicons";
interface StateObj { like?: ResourceUri | false; repost?: ResourceUri | false; bookmark?: boolean;}
export type InteractionType = keyof StateObj;
export class InteractionCache { #state = new Map<ResourceUri, StateObj>(); #optimisticState = new Map<ResourceUri, StateObj>();
get(uri: ResourceUri) { return this.#state.get(uri); }
addOptimistic(type: InteractionType, uri: ResourceUri, interaction: ResourceUri | boolean) { this.#getAndSet(this.#optimisticState, uri, (val) => { val ??= {}; val[type] = interaction as any; return val; }); }
override(type: InteractionType, uri: ResourceUri, interaction: ResourceUri | boolean) { this.#getAndSet(this.#state, uri, (val) => { val ??= {}; val[type] = interaction as any; return val; }); this.discard(type, uri); }
commit(type: InteractionType, uri: ResourceUri) { if (this.#optimisticState.has(uri)) { this.override(type, uri, this.#optimisticState.get(uri)![type]!); } }
discard(type: InteractionType, uri: ResourceUri) { const newVal = this.#getAndSet(this.#optimisticState, uri, (val) => { val ??= {}; val[type] = undefined; return val; });
if (!Object.values(newVal).reduce((a, b) => a || b)) { this.#optimisticState.delete(uri); } }
#getAndSet<T, U extends object>(map: Map<T, U>, key: T, fun: (val: U | undefined) => U): U { const newVal = fun(map.get(key)); map.set(key, newVal); return newVal; }}