Skip to content

Commit

Permalink
Remove history tracking on client
Browse files Browse the repository at this point in the history
This was not utilized anymore, since the history view feature was
removed long time ago.
  • Loading branch information
raimohanska committed Feb 2, 2024
1 parent 507d043 commit 74693e3
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 31 deletions.
3 changes: 1 addition & 2 deletions common/src/board-history-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ export function boardHistoryReducer(
appEvent: BoardHistoryEntry,
): [BoardWithHistory, PersistableBoardItemEvent | null] {
const [updatedBoard, undoAction] = boardReducer(board.board, appEvent)
const history = updatedBoard !== board.board ? addToHistory(board.history, appEvent) : board.history
const updatedBoardWithHistory = { board: updatedBoard, history }

return [updatedBoardWithHistory, undoAction ? undoAction() : null]
}

Expand Down
1 change: 0 additions & 1 deletion frontend/src/board/BoardView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ export const BoardView = ({
L.filter((b: Board) => !!b, componentScope()),
)
const accessLevel = L.view(boardState, "accessLevel")
const history = L.view(boardState, "serverHistory")
const locks = L.view(boardState, (s) => s.locks)
const sessionId = L.view(sessionState, (s) => s.sessionId)
const sessions = L.view(boardState, (s) => s.users)
Expand Down
4 changes: 0 additions & 4 deletions frontend/src/board/ItemView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ import * as L from "lonna"
import {
AccessLevel,
Board,
BoardHistoryEntry,
canWrite,
Connection,
getAlign,
getHorizontalAlign,
getItemBackground,
getItemIds,
getItemShape,
getVerticalAlign,
Id,
isTextItem,
Item,
ItemType,
Expand Down Expand Up @@ -46,7 +43,6 @@ export const ItemView = ({
}: {
board: L.Property<Board>
accessLevel: L.Property<AccessLevel>
history: L.Property<BoardHistoryEntry[]>
id: string
type: ItemType
item: L.Property<Item>
Expand Down
2 changes: 0 additions & 2 deletions frontend/src/store/board-local-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { migrateBoard, migrateEvent } from "../../../common/src/migration"
export type LocalStorageBoard = {
serverShadow: Board
queue: BoardHistoryEntry[] // serverShadow + queue = current board
serverHistory: BoardHistoryEntry[] // history until serverShadow (queued events not included)
}

const BOARD_STORAGE_KEY_PREFIX = "board_"
Expand All @@ -31,7 +30,6 @@ async function getStoredState(localStorageKey: string): Promise<LocalStorageBoar
return {
serverShadow: migrateBoard(state.serverShadow),
queue: state.queue.map(migrateEvent),
serverHistory: state.serverHistory.map(migrateEvent),
}
} catch (e) {
console.error(`Fetching local state ${localStorageKey} from IndexedDB failed`, e)
Expand Down
2 changes: 0 additions & 2 deletions frontend/src/store/board-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ describe("With stored local state", () => {
locallyStoredBoard: {
serverShadow: board1,
queue: [],
serverHistory: [],
},
})

Expand All @@ -137,7 +136,6 @@ describe("With stored local state", () => {
locallyStoredBoard: {
serverShadow: board1,
queue: [],
serverHistory: [],
},
})

Expand Down
30 changes: 10 additions & 20 deletions frontend/src/store/board-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import _ from "lodash"
import * as L from "lonna"
import { globalScope } from "lonna"
import { addOrReplaceEvent, foldActions } from "../../../common/src/action-folding"
import { boardHistoryReducer } from "../../../common/src/board-history-reducer"
import { boardReducer } from "../../../common/src/board-reducer"
import {
AccessLevel,
Expand Down Expand Up @@ -54,7 +53,6 @@ export type BoardState = {
board: Board | undefined
serverShadow: Board | undefined
queue: (BoardHistoryEntry | CursorMove)[] // serverShadow + queue = current board
serverHistory: BoardHistoryEntry[] // history until serverShadow (queued events not included)
sent: (BoardHistoryEntry | CursorMove)[]
locks: ItemLocks
users: UserSessionInfo[]
Expand Down Expand Up @@ -168,7 +166,7 @@ export function BoardStore(
let redoStack = CommandStack()

let initialServerSyncEventBuffer: BoardHistoryEntry[] = []

let eventsSinceInit = 0
const eventsReducer = (state: BoardState, event: BoardStoreEvent): BoardState => {
const storedInitialState = boardStateFromLocalStorage.get()?.storedInitialState

Expand All @@ -181,6 +179,10 @@ export function BoardStore(
} else if (event.action === "ui.redo") {
return redoStack.pop(state, undoStack)
} else if (isPersistableBoardItemEvent(event)) {
if (eventsSinceInit == 0) {
console.log("First event since init at serial", event.serial)
}
eventsSinceInit++
try {
if (event.serial == undefined) {
if (!canWrite(state.accessLevel)) return state
Expand All @@ -191,18 +193,15 @@ export function BoardStore(
return flushIfPossible({ ...state, board, queue: addOrReplaceEvent(event, state.queue) })
} else {
// Remote event
const [{ board: newServerShadow, history: newServerHistory }] = boardHistoryReducer(
{ board: state.serverShadow!, history: state.serverHistory },
event,
)
const [newServerShadow] = boardReducer(state.serverShadow!, event)
// Rebase local events on top of new server shadow
// TODO: what if fails?
const localEvents = [...state.sent, ...state.queue]
const board = localEvents
.filter(isBoardHistoryEntry)
.reduce((b, e) => boardReducer(b, e)[0], newServerShadow)
//console.log(`Processed remote board event and rebased ${localEvents.length} local events on top`, event)
return { ...state, serverShadow: newServerShadow, board, serverHistory: newServerHistory }
return { ...state, serverShadow: newServerShadow, board }
}
} catch (e) {
console.error("Error applying event. Fetching as new board...", e)
Expand All @@ -224,9 +223,8 @@ export function BoardStore(
//console.log("Got ack")
return flushIfPossible({ ...state, sent: [] })
} else {
// Our sent events now acknowledged and will be incorporated into serverShadow and serverHistory
// Our sent events now acknowledged and will be incorporated into serverShadow
const newServerEvents = state.sent.filter(isBoardHistoryEntry)
const newServerHistory = [...state.serverHistory, ...newServerEvents]
const newServerShadow =
state.queue.length > 0
? newServerEvents.reduce((b, e) => boardReducer(b, e)[0], state.serverShadow!)
Expand All @@ -238,7 +236,6 @@ export function BoardStore(
...state,
board: { ...state.board!, serial: newSerial },
serverShadow: { ...newServerShadow, serial: newSerial },
serverHistory: newServerHistory,
sent: [],
})
}
Expand Down Expand Up @@ -307,7 +304,6 @@ export function BoardStore(
status: "offline",
queue: storedInitialState.queue,
sent: [],
serverHistory: storedInitialState.serverHistory,
serverShadow: storedInitialState.serverShadow,
board,
}
Expand Down Expand Up @@ -341,6 +337,7 @@ export function BoardStore(
const users = state.users.map((u) => (u.sessionId === event.sessionId ? event : u))
return { ...state, users }
} else if (event.action === "board.init") {
eventsSinceInit = 0
console.log(`Going to online mode. Init as new board at serial ${event.board.serial}`)
return {
...state,
Expand All @@ -349,12 +346,9 @@ export function BoardStore(
accessLevel: event.accessLevel,
serverShadow: event.board,
sent: [],
serverHistory: [
// Create a bootstrap event to make the local history consistent even though we don't have the full history from server.
mkBootStrapEvent(event.board.id, event.board, event.board.serial),
],
}
} else if (event.action === "board.init.diff") {
eventsSinceInit = 0
if (event.first) {
// Ensure local buffer empty on first chunk even if an earlier init was aborted.
initialServerSyncEventBuffer = []
Expand Down Expand Up @@ -413,7 +407,6 @@ export function BoardStore(
)
// Local board = server shadow + local queue
const board = queue.reduce((b, e) => boardReducer(b, e)[0], newServerShadow)
const newServerHistory = [...state.serverHistory, ...initialServerSyncEventBuffer]

initialServerSyncEventBuffer = []

Expand All @@ -424,7 +417,6 @@ export function BoardStore(
board,
serverShadow: newServerShadow,
queue,
serverHistory: newServerHistory,
})
} catch (e) {
console.error("Error initializing board. Fetching as new board...", e)
Expand Down Expand Up @@ -454,7 +446,6 @@ export function BoardStore(
accessLevel: "none",
serverShadow: undefined,
board: undefined,
serverHistory: [],
locks: {},
users: [],
queue: [],
Expand All @@ -481,7 +472,6 @@ export function BoardStore(
L.map((state) => {
return {
serverShadow: state.serverShadow!,
serverHistory: state.serverHistory,
queue: state.queue.filter(isBoardHistoryEntry),
}
}),
Expand Down

0 comments on commit 74693e3

Please sign in to comment.