import * as React from 'react' ; import { Provider } from "polar-shared/src/util/Providers" ; import { createObservableStore , SetStore } from '../react/store/ObservableStore' ; import { UndoQueues } from "./UndoQueues" ; import UndoFunction = UndoQueues . UndoFunction ; import IPushResult = UndoQueues . IPushResult ; import UndoResult = UndoQueues . UndoResult ; import RedoResult = UndoQueues . RedoResult ; interface IUndoStore { readonly undoQueue : UndoQueues . UndoQueue ; } interface IUndoCallbacks { readonly push : ( action : UndoFunction ) => Promise < IPushResult > ; readonly undo : ( ) => Promise < UndoResult > ; readonly redo : ( ) => Promise < RedoResult > ; } const initialStore = { undoQueue : UndoQueues . create ( ) } ; interface Mutation { } interface Mutator { } function mutatorFactory ( storeProvider , setStore ) { return { } ; } function useCallbacksFactory ( storeProvider , setStore , mutator ) { return React . useMemo ( ( ) => { type WithQueueHandler < T > = ( undoQueue : UndoQueues . UndoQueue ) => Promise < T > ; async function withQueue < T > ( handler ) { const store = storeProvider ( ) ; const { undoQueue } = store ; return await handler ( undoQueue ) ; } async function push ( action ) { return await withQueue ( undoQueue => undoQueue . push ( action ) ) ; } async function undo ( ) { return await withQueue ( undoQueue => undoQueue . undo ( ) ) ; } async function redo ( ) { return await withQueue ( undoQueue => undoQueue . redo ( ) ) ; } return { push , undo , redo } ; } , [ storeProvider ] ) ; } export const [ UndoStoreProviderDelegate , useUndoStore , useUndoCallbacks , useUndoMutator ] = createObservableStore < IUndoStore , Mutator , IUndoCallbacks > ( { initialValue : initialStore , mutatorFactory , callbacksFactory : useCallbacksFactory , enableShallowEquals : true } ) ;