You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gristlabs_grist-core/app/common/DocDataCache.ts

52 lines
1.7 KiB

import { AlternateActions, AlternateStorage, ProcessedAction} from 'app/common/AlternateActions';
import { DocAction, UserAction } from 'app/common/DocActions';
import { DocData } from 'app/common/DocData';
import max from 'lodash/max';
/**
* An implementation of an in-memory storage that can handle UserActions,
* generating DocActions and retValues that work as for regular storage.
* It shares an implementation with on-demand tables.
*/
export class DocDataCache implements AlternateStorage {
public docData: DocData;
private _altActions: AlternateActions;
constructor(actions?: DocAction[]) {
this.docData = new DocData(
async (tableId) => {
throw new Error(`no ${tableId}`);
},
null,
);
this._altActions = new AlternateActions(this);
for (const action of actions || []) {
this.docData.receiveAction(action);
}
}
public async sendTableActions(actions: UserAction[]): Promise<ProcessedAction[]> {
const results: ProcessedAction[] = [];
for (const userAction of actions) {
const processedAction = await this._altActions.processUserAction(userAction);
results.push(processedAction);
for (const storedAction of processedAction.stored) {
this.docData.receiveAction(storedAction);
}
}
return results;
}
public async fetchActionData(tableId: string, rowIds: number[], colIds?: string[]) {
const table = await this.docData.requireTable(tableId);
return table.getTableDataAction(
rowIds,
colIds,
);
}
public async getNextRowId(tableId: string): Promise<number> {
const table = await this.docData.requireTable(tableId);
return (max(table.getRowIds()) || 0) + 1;
}
}