mirror of
https://github.com/hanzoai/papers.git
synced 2026-10-29 07:34:31 +00:00
92 lines
2.4 KiB
TeX
92 lines
2.4 KiB
TeX
\section{Implementation Details}
|
|
|
|
|
|
\subsection{Event Persistence}
|
|
|
|
Events persist to IndexedDB for offline support and session recovery:
|
|
|
|
\begin{lstlisting}[language=JavaScript,caption=IndexedDB persistence]
|
|
class EventStore {
|
|
async append(event) {
|
|
const db = await this.getDB();
|
|
const tx = db.transaction("events", "readwrite");
|
|
await tx.objectStore("events").add(event);
|
|
}
|
|
|
|
async replay(fromTimestamp) {
|
|
const db = await this.getDB();
|
|
const events = await db.getAll("events");
|
|
return events
|
|
.filter(e => e.timestamp >= fromTimestamp)
|
|
.sort((a, b) => a.timestamp - b.timestamp);
|
|
}
|
|
}
|
|
\end{lstlisting}
|
|
|
|
\subsection{Conflict Resolution}
|
|
|
|
When offline edits conflict with server state, Astle.js applies last-writer-wins with vector clocks for ordering:
|
|
|
|
\begin{equation}
|
|
\text{resolve}(e_1, e_2) = \begin{cases}
|
|
e_1 & \text{if } \text{vc}(e_1) > \text{vc}(e_2) \\
|
|
e_2 & \text{otherwise}
|
|
\end{cases}
|
|
\end{equation}
|
|
|
|
For inventory conflicts, server state always wins:
|
|
|
|
\begin{lstlisting}[language=JavaScript,caption=Inventory conflict resolution]
|
|
function resolveInventoryConflict(local, remote) {
|
|
// Server inventory is authoritative
|
|
if (remote.available < local.quantity) {
|
|
return {
|
|
type: "cart/adjustQuantity",
|
|
payload: {
|
|
itemId: local.itemId,
|
|
quantity: remote.available,
|
|
reason: "inventory_adjusted",
|
|
},
|
|
};
|
|
}
|
|
return null;
|
|
}
|
|
\end{lstlisting}
|
|
|
|
\subsection{Performance Optimizations}
|
|
|
|
\textbf{Structural Sharing.} Immutable updates share unchanged subtrees:
|
|
|
|
\begin{lstlisting}[language=JavaScript,caption=Structural sharing]
|
|
function addItem(state, item) {
|
|
return {
|
|
...state,
|
|
items: [...state.items, item],
|
|
// subtotal, total recomputed
|
|
};
|
|
}
|
|
\end{lstlisting}
|
|
|
|
\textbf{Batched Updates.} Multiple synchronous dispatches batch into single render:
|
|
|
|
\begin{lstlisting}[language=JavaScript,caption=Update batching]
|
|
store.batch(() => {
|
|
store.dispatch(actions.cart.clear());
|
|
store.dispatch(actions.checkout.reset());
|
|
store.dispatch(actions.ui.showConfirmation());
|
|
});
|
|
// Single render after all three
|
|
\end{lstlisting}
|
|
|
|
\textbf{Lazy Computation.} Expensive derivations compute on access:
|
|
|
|
\begin{lstlisting}[language=JavaScript,caption=Lazy selectors]
|
|
const getShippingOptions = createLazySelector(
|
|
[getCart, getAddress],
|
|
async (cart, address) => {
|
|
return await shippingApi.getOptions(cart, address);
|
|
}
|
|
);
|
|
\end{lstlisting}
|
|
|