Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert history store to Typescript #15352

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,50 @@
Add: "Add",
Delete: "Delete",
Change: "Change",
} as const

interface HistoryDocument {
_id: string
_rev?: string
[key: string]: any
}

interface HistoryOperation {
id?: string | number | undefined
type: (typeof Operations)[keyof typeof Operations]
doc: HistoryDocument
forwardPatch?: any[]
backwardsPatch?: any[]
}

interface HistoryState {
history: HistoryOperation[]
position: number
loading?: boolean
}

export const initialState = {
interface DerivedHistoryState extends HistoryState {

Check failure on line 30 in packages/builder/src/stores/builder/history.ts

View workflow job for this annotation

GitHub Actions / lint

'DerivedHistoryState' is defined but never used. Allowed unused vars must match /^_/u
canUndo: boolean
canRedo: boolean
}

interface HistoryStoreOptions {
getDoc: (id: string) => HistoryDocument | undefined
selectDoc?: (id: string) => void
beforeAction?: (operation: HistoryOperation) => void | Promise<void>
afterAction?: (operation: HistoryOperation) => void | Promise<void>
}

type SaveFn = (
doc: HistoryDocument,
operationId?: string | number
) => Promise<HistoryDocument>
type DeleteFn = (
doc: HistoryDocument,
operationId?: string | number
) => Promise<void>

export const initialState: HistoryState = {
history: [],
position: 0,
loading: false,
Expand All @@ -18,9 +59,9 @@
selectDoc,
beforeAction,
afterAction,
}) => {
}: HistoryStoreOptions) => {
// Use a derived store to check if we are able to undo or redo any operations
const store = writable(initialState)
const store = writable<HistoryState>(initialState)
const derivedStore = derived(store, $store => {
return {
...$store,
Expand All @@ -31,8 +72,8 @@

// Wrapped versions of essential functions which we call ourselves when using
// undo and redo
let saveFn
let deleteFn
let saveFn: SaveFn
let deleteFn: DeleteFn

/**
* Internal util to set the loading flag
Expand Down Expand Up @@ -66,7 +107,7 @@
* For internal use only.
* @param operation the operation to save
*/
const saveOperation = operation => {
const saveOperation = (operation: HistoryOperation) => {
store.update(state => {
// Update history
let history = state.history
Expand All @@ -93,7 +134,7 @@
* @param fn the save function
* @returns {function} a wrapped version of the save function
*/
const wrapSaveDoc = fn => {
const wrapSaveDoc = (fn: SaveFn): SaveFn => {
saveFn = async (doc, operationId) => {
// Only works on a single doc at a time
if (!doc || Array.isArray(doc)) {
Expand Down Expand Up @@ -141,7 +182,7 @@
* @param fn the delete function
* @returns {function} a wrapped version of the delete function
*/
const wrapDeleteDoc = fn => {
const wrapDeleteDoc = (fn: DeleteFn): DeleteFn => {
deleteFn = async (doc, operationId) => {
// Only works on a single doc at a time
if (!doc || Array.isArray(doc)) {
Expand Down
Loading