2020-10-02 15:10:00 +00:00
|
|
|
/**
|
|
|
|
* DocData maintains all underlying data for a Grist document, knows how to load it,
|
|
|
|
* subscribes to actions which change it, and forwards those actions to individual tables.
|
|
|
|
* It also provides the interface to apply actions to data.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {DocComm} from 'app/client/components/DocComm';
|
2021-12-07 11:21:16 +00:00
|
|
|
import {MetaTableData, TableData} from 'app/client/models/TableData';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {ApplyUAOptions, ApplyUAResult} from 'app/common/ActiveDocAPI';
|
|
|
|
import {CellValue, TableDataAction, UserAction} from 'app/common/DocActions';
|
|
|
|
import {DocData as BaseDocData} from 'app/common/DocData';
|
2021-12-07 11:21:16 +00:00
|
|
|
import {SchemaTypes} from 'app/common/schema';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {ColTypeMap} from 'app/common/TableData';
|
|
|
|
import * as bluebird from 'bluebird';
|
|
|
|
import {Emitter} from 'grainjs';
|
|
|
|
import defaults = require('lodash/defaults');
|
|
|
|
|
|
|
|
const gristNotify = (window as any).gristNotify;
|
|
|
|
|
|
|
|
export class DocData extends BaseDocData {
|
|
|
|
public readonly sendActionsEmitter = new Emitter();
|
|
|
|
public readonly sendActionsDoneEmitter = new Emitter();
|
|
|
|
|
2020-11-09 23:40:43 +00:00
|
|
|
private _bundlesPending: number = 0; // How many bundles are currently pending.
|
|
|
|
private _lastBundlePromise?: Promise<void>; // Promise for completion of the last pending bundle.
|
|
|
|
private _triggerBundleFinalize?: () => void; // When a bundle is pending, trigger its finalize() callback.
|
|
|
|
|
|
|
|
// When a bundle is pending and actions should be checked, the callback to check them.
|
|
|
|
private _shouldIncludeInBundle?: (actions: UserAction[]) => boolean;
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
private _nextDesc: string|null = null; // The description for the next incoming action.
|
|
|
|
private _lastActionNum: number|null = null; // ActionNum of the last action in the current bundle, or null.
|
|
|
|
private _bundleSender: BundleSender;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for DocData.
|
2022-02-19 09:46:49 +00:00
|
|
|
* @param {Object} docComm: A map of server methods available on this document.
|
2020-10-02 15:10:00 +00:00
|
|
|
* @param {Object} metaTableData: A map from tableId to table data, presented as an action,
|
|
|
|
* equivalent to BulkAddRecord, i.e. ["TableData", tableId, rowIds, columnValues].
|
|
|
|
*/
|
|
|
|
constructor(public readonly docComm: DocComm, metaTableData: {[tableId: string]: TableDataAction}) {
|
|
|
|
super((tableId) => docComm.fetchTable(tableId), metaTableData);
|
|
|
|
this._bundleSender = new BundleSender(this.docComm);
|
|
|
|
}
|
|
|
|
|
|
|
|
public createTableData(tableId: string, tableData: TableDataAction|null, colTypes: ColTypeMap): TableData {
|
|
|
|
return new TableData(this, tableId, tableData, colTypes);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version of inherited getTable() which returns the enhance TableData type.
|
|
|
|
public getTable(tableId: string): TableData|undefined {
|
|
|
|
return super.getTable(tableId) as TableData;
|
|
|
|
}
|
|
|
|
|
2021-12-07 11:21:16 +00:00
|
|
|
// Version of inherited getMetaTable() which returns the enhanced TableData type.
|
|
|
|
public getMetaTable<TableId extends keyof SchemaTypes>(tableId: TableId): MetaTableData<TableId> {
|
|
|
|
return super.getMetaTable(tableId) as any;
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
/**
|
|
|
|
* Finds up to n most likely target columns for the given values in the document.
|
|
|
|
*/
|
|
|
|
public async findColFromValues(values: any[], n: number, optTableId?: string): Promise<number[]> {
|
|
|
|
try {
|
|
|
|
return await this.docComm.findColFromValues(values, n, optTableId);
|
|
|
|
} catch (e) {
|
|
|
|
gristNotify(`Error finding matching columns: ${e.message}`);
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns error message (traceback) for one invalid formula cell.
|
|
|
|
*/
|
|
|
|
public getFormulaError(tableId: string, colId: string, rowId: number): Promise<CellValue> {
|
|
|
|
return this.docComm.getFormulaError(tableId, colId, rowId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets a bundle to collect all incoming actions. Throws an error if any actions which
|
|
|
|
// do not match the verification callback are sent.
|
2020-11-09 23:40:43 +00:00
|
|
|
public startBundlingActions<T>(options: BundlingOptions<T>): BundlingInfo<T> {
|
|
|
|
if (this._bundlesPending >= 2) {
|
|
|
|
// We don't expect a full-blown queue of bundles or actions at any point. If a bundle is
|
|
|
|
// pending, a new bundle should immediately finalize it. Here we refuse to queue up more
|
|
|
|
// actions than that. (This could crop up in theory while disconnected, but is hard to
|
|
|
|
// trigger to test.)
|
|
|
|
throw new Error('Too many actions already pending');
|
|
|
|
}
|
|
|
|
this._bundlesPending++;
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2020-11-09 23:40:43 +00:00
|
|
|
// Promise to allow waiting for the result of prepare() callback before it's even called.
|
2020-12-15 04:19:38 +00:00
|
|
|
let prepareResolve!: (value: T|Promise<T>) => void;
|
2020-11-09 23:40:43 +00:00
|
|
|
const preparePromise = new Promise<T>(resolve => { prepareResolve = resolve; });
|
|
|
|
|
|
|
|
// Manually-triggered promise for when finalize() should be called. It's triggered by user,
|
|
|
|
// and when an unrelated action or a new bundle is started.
|
|
|
|
let triggerFinalize!: () => void;
|
|
|
|
const triggerFinalizePromise = new Promise<void>(resolve => { triggerFinalize = resolve; });
|
|
|
|
|
|
|
|
const doBundleActions = async () => {
|
|
|
|
if (this._lastBundlePromise) {
|
|
|
|
this._triggerBundleFinalize?.();
|
|
|
|
await this._lastBundlePromise;
|
|
|
|
}
|
|
|
|
try {
|
2020-12-04 23:29:29 +00:00
|
|
|
this._nextDesc = options.description;
|
|
|
|
this._lastActionNum = null;
|
|
|
|
this._triggerBundleFinalize = triggerFinalize;
|
2021-04-26 21:54:09 +00:00
|
|
|
prepareResolve(options.prepare());
|
2020-12-04 23:29:29 +00:00
|
|
|
this._shouldIncludeInBundle = options.shouldIncludeInBundle;
|
|
|
|
|
2022-02-24 19:16:56 +00:00
|
|
|
// If finalize is triggered, we must wait for preparePromise to fulfill before proceeding.
|
|
|
|
await Promise.all([triggerFinalizePromise, preparePromise]);
|
|
|
|
|
2020-11-09 23:40:43 +00:00
|
|
|
// Unset _shouldIncludeInBundle so that actions sent by finalize() are included in the
|
|
|
|
// bundle. If they were checked and incorrectly failed the check, we'd have a deadlock.
|
|
|
|
// TODO The downside is that when sending multiple unrelated actions quickly, the first
|
|
|
|
// can trigger finalize, and subsequent ones can get bundled in while finalize() is
|
|
|
|
// running. This changes the order of actions and may create problems (e.g. with undo).
|
|
|
|
this._shouldIncludeInBundle = undefined;
|
|
|
|
await options.finalize();
|
|
|
|
} finally {
|
|
|
|
// In all cases, reset the bundle-specific values we set above
|
|
|
|
this._shouldIncludeInBundle = undefined;
|
|
|
|
this._triggerBundleFinalize = undefined;
|
|
|
|
this._bundlesPending--;
|
|
|
|
if (this._bundlesPending === 0) {
|
|
|
|
this._lastBundlePromise = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-15 04:19:38 +00:00
|
|
|
const completionPromise = this._lastBundlePromise = doBundleActions();
|
|
|
|
return {preparePromise, triggerFinalize, completionPromise};
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute a callback that may send multiple actions, and bundle those actions together. The
|
|
|
|
// callback may return a promise, in which case bundleActions() will wait for it to resolve.
|
2020-11-09 23:40:43 +00:00
|
|
|
// If nestInActiveBundle is true, and there is an active bundle, then simply calls callback()
|
|
|
|
// without starting a new bundle.
|
|
|
|
public async bundleActions<T>(desc: string|null, callback: () => T|Promise<T>,
|
|
|
|
options: {nestInActiveBundle?: boolean} = {}): Promise<T> {
|
|
|
|
if (options.nestInActiveBundle && this._bundlesPending) {
|
2020-10-02 15:10:00 +00:00
|
|
|
return await callback();
|
2020-11-09 23:40:43 +00:00
|
|
|
}
|
|
|
|
const bundlingInfo = this.startBundlingActions<T>({
|
|
|
|
description: desc,
|
|
|
|
shouldIncludeInBundle: () => true,
|
|
|
|
prepare: callback,
|
|
|
|
finalize: async () => undefined,
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
return await bundlingInfo.preparePromise;
|
2020-10-02 15:10:00 +00:00
|
|
|
} finally {
|
2020-11-09 23:40:43 +00:00
|
|
|
bundlingInfo.triggerFinalize();
|
2020-12-15 04:19:38 +00:00
|
|
|
await bundlingInfo.completionPromise;
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends actions to the server to be applied.
|
|
|
|
* @param {String} optDesc: Optional description of the actions to be shown in the log.
|
|
|
|
*
|
|
|
|
* sendActions also emits two events:
|
|
|
|
* 'sendActions': emitted before the action is sent, with { actions } object as data.
|
|
|
|
* 'sendActionsDone': emitted on success, with the same data object.
|
|
|
|
* Note that it allows a handler for 'sendActions' to pass along information to the handler
|
|
|
|
* for the corresponding 'sendActionsDone', by tacking it onto the event data object.
|
|
|
|
*/
|
|
|
|
public sendActions(actions: UserAction[], optDesc?: string): Promise<any[]> {
|
|
|
|
// Some old code relies on this promise being a bluebird Promise.
|
|
|
|
// TODO Remove bluebird and this cast.
|
2021-04-26 21:54:09 +00:00
|
|
|
return bluebird.Promise.resolve(this._sendActionsImpl(actions, optDesc)) as unknown as Promise<any[]>;
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a single action to the server to be applied. Calls this.sendActions to manage the
|
|
|
|
* optional bundle.
|
|
|
|
* @param {String} optDesc: Optional description of the actions to be shown in the log.
|
|
|
|
*/
|
|
|
|
public sendAction(action: UserAction, optDesc?: string): Promise<any> {
|
|
|
|
return this.sendActions([action], optDesc).then((retValues) => retValues[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// See documentation of sendActions().
|
|
|
|
private async _sendActionsImpl(actions: UserAction[], optDesc?: string): Promise<any[]> {
|
|
|
|
const eventData = {actions};
|
|
|
|
this.sendActionsEmitter.emit(eventData);
|
|
|
|
const options = { desc: optDesc };
|
2020-11-09 23:40:43 +00:00
|
|
|
if (this._shouldIncludeInBundle && !this._shouldIncludeInBundle(actions)) {
|
|
|
|
this._triggerBundleFinalize?.();
|
|
|
|
await this._lastBundlePromise;
|
|
|
|
}
|
|
|
|
if (this._bundlesPending) {
|
2020-10-02 15:10:00 +00:00
|
|
|
defaults(options, {
|
|
|
|
desc: this._nextDesc,
|
|
|
|
linkId: this._lastActionNum,
|
|
|
|
});
|
|
|
|
this._nextDesc = null;
|
|
|
|
}
|
2020-11-09 23:40:43 +00:00
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
const result: ApplyUAResult = await this._bundleSender.applyUserActions(actions, options);
|
|
|
|
this._lastActionNum = result.actionNum;
|
|
|
|
this.sendActionsDoneEmitter.emit(eventData);
|
|
|
|
return result.retValues;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* BundleSender helper class collects multiple applyUserActions() calls that happen on the same
|
|
|
|
* tick, and sends them to the server all at once.
|
|
|
|
*/
|
|
|
|
class BundleSender {
|
|
|
|
private _options = {};
|
|
|
|
private _actions: UserAction[] = [];
|
|
|
|
private _sendPromise?: Promise<ApplyUAResult>;
|
|
|
|
|
|
|
|
constructor(private _docComm: DocComm) {}
|
|
|
|
|
|
|
|
public applyUserActions(actions: UserAction[], options: ApplyUAOptions): Promise<ApplyUAResult> {
|
|
|
|
defaults(this._options, options);
|
|
|
|
const start = this._actions.length;
|
|
|
|
this._actions.push(...actions);
|
|
|
|
const end = this._actions.length;
|
|
|
|
return this._getSendPromise()
|
|
|
|
.then(result => ({
|
|
|
|
actionNum: result.actionNum,
|
|
|
|
retValues: result.retValues.slice(start, end),
|
|
|
|
isModification: result.isModification
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
public _getSendPromise(): Promise<ApplyUAResult> {
|
|
|
|
if (!this._sendPromise) {
|
|
|
|
// Note that the first Promise.resolve() ensures that the next step (actual send) happens on
|
|
|
|
// the next tick. By that time, more actions may have been added to this._actions array.
|
|
|
|
this._sendPromise = Promise.resolve()
|
|
|
|
.then(() => {
|
|
|
|
this._sendPromise = undefined;
|
|
|
|
const ret = this._docComm.applyUserActions(this._actions, this._options);
|
|
|
|
this._options = {};
|
|
|
|
this._actions = [];
|
|
|
|
return ret;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return this._sendPromise;
|
|
|
|
}
|
|
|
|
}
|
2020-11-09 23:40:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Options to startBundlingAction().
|
|
|
|
*/
|
|
|
|
export interface BundlingOptions<T = unknown> {
|
|
|
|
// Description of the action bundle.
|
|
|
|
description: string|null;
|
|
|
|
|
|
|
|
// Checker for whether an action belongs in the current bundle. If not, finalize() will be
|
|
|
|
// called immediately. Note that this checker is NOT applied for actions sent from prepare()
|
|
|
|
// or finalize() callbacks, only those in between.
|
|
|
|
shouldIncludeInBundle: (actions: UserAction[]) => boolean;
|
|
|
|
|
|
|
|
// Callback to start this action bundle.
|
|
|
|
prepare: () => T|Promise<T>;
|
|
|
|
|
|
|
|
// Callback to finalize this action bundle.
|
|
|
|
finalize: () => Promise<void>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Result of startBundlingActions(), to allow waiting for prepare() to complete, and to trigger
|
2020-12-15 04:19:38 +00:00
|
|
|
* finalize() manually, and to wait for the full bundle to complete.
|
2020-11-09 23:40:43 +00:00
|
|
|
*/
|
|
|
|
export interface BundlingInfo<T = unknown> {
|
|
|
|
// Promise for when the prepare() has completed. Note that sometimes it's delayed until the
|
|
|
|
// previous bundle has been finalized.
|
|
|
|
preparePromise: Promise<T>;
|
|
|
|
|
|
|
|
// Ask DocData to call the finalize callback immediately.
|
|
|
|
triggerFinalize: () => void;
|
2020-12-15 04:19:38 +00:00
|
|
|
|
|
|
|
// Promise for when the bundle has been finalized.
|
|
|
|
completionPromise: Promise<void>;
|
2020-11-09 23:40:43 +00:00
|
|
|
}
|