import { shallowRef } from "vue"; import type { NativeTransaction, NativeViewEntry } from "../types"; export class NativeHistoryLedger { readonly keys = shallowRef([]); get length() { return this.keys.value.length; } get previousKey() { return this.keys.value.at(-2); } initialize(key: string) { this.keys.value = [key]; } contains(key: string) { return this.keys.value.includes(key); } indexOf(key: string) { return this.keys.value.lastIndexOf(key); } accept(target: NativeViewEntry, transaction: NativeTransaction | null) { const history = this.keys.value; if (!history.length) return this.initialize(target.key); if ( transaction?.kind === "pop" || transaction?.kind === "dismiss" || transaction?.replace ) { const targetIndex = history.lastIndexOf(target.key); this.keys.value = targetIndex >= 0 ? history.slice(0, targetIndex + 1) : [...history.slice(0, -1), target.key]; return; } const existingIndex = history.lastIndexOf(target.key); this.keys.value = transaction ? [...history, target.key] : existingIndex >= 0 ? history.slice(0, existingIndex + 1) : [...history, target.key]; } }