Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

7.9 KB
Raw Blame
/**
 * Copyright 2023-present DreamNum Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import type { BaseDataModel, IDisposable, IMutation, IMutationInfo, Workbook } from '@univerjs/core';
import type { IRemoteSyncMutationOptions } from '../../services/remote-instance/remote-instance.service';
import {
    CommandType,
    ICommandService,
    ILogService,
    Inject,
    Injector,
    IUniverInstanceService,
    RxDisposable,
    toDisposable,
    UniverInstanceType,
} from '@univerjs/core';
import { takeUntil } from 'rxjs/operators';
import {
    IRemoteInstanceService,
    IRemoteSyncService,
    RemoteInstanceServiceName,
    RemoteSyncServiceName,
} from '../../services/remote-instance/remote-instance.service';
import { IRPCChannelService } from '../../services/rpc/channel.service';
import { ChannelClientDisposedError, fromModule, toModule } from '../../services/rpc/rpc.service';

/**
 * This controller is responsible for syncing data from the primary thread to
 * the worker thread.
 *
 * Note that only spreadsheets will be synced to the remote calculation instance by default.
 */
export class DataSyncPrimaryController extends RxDisposable {
    private _remoteInstanceService!: IRemoteInstanceService;

    private readonly _syncingUnits = new Set<string>();

    private readonly _syncingMutations = new Set<string>();

    private _syncMutationQueue: Promise<unknown> = Promise.resolve();

    private _remoteReady: Promise<true> = Promise.resolve(true);

    constructor(
        @Inject(Injector) private readonly _injector: Injector,
        @ICommandService private readonly _commandService: ICommandService,
        @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
        @IRPCChannelService private readonly _rpcChannelService: IRPCChannelService,
        @IRemoteSyncService private readonly _remoteSyncService: IRemoteSyncService,
        @ILogService private readonly _logService: ILogService
    ) {
        super();

        this._initRPCChannels();
        this._init();
    }

    registerSyncingMutations(mutation: IMutation<object>): void {
        this._syncingMutations.add(mutation.id);
    }

    /**
     * Only spreadsheets would be synced to the web worker in normal situations. If you would like to
     * sync other types of documents, you should manually call this method with that document's id.
     */
    syncUnit(unitId: string): IDisposable {
        const alreadySyncing = this._syncingUnits.has(unitId);
        this._syncingUnits.add(unitId);
        const unit = this._univerInstanceService.getUnit<Workbook>(unitId, UniverInstanceType.UNIVER_SHEET)
            ?? this._univerInstanceService.getUnit<BaseDataModel>(unitId, UniverInstanceType.UNIVER_BASE);
        if (!alreadySyncing && unit) {
            this._handleRemoteRequest(this._remoteInstanceService.createInstance({
                unitID: unit.getUnitId(),
                type: unit.type,
                snapshot: unit.getSnapshot(),
            }), 'create the remote unit');
        }

        return toDisposable(() => {
            if (!alreadySyncing) {
                this._syncingUnits.delete(unitId);
            }
            if (!alreadySyncing && unit) {
                this._handleRemoteRequest(this._remoteInstanceService.disposeInstance({
                    unitID: unit.getUnitId(),
                }), 'dispose the remote unit');
            }
        });
    }

    /** Sync registered mutations for a unit without creating a replica unit. */
    syncUnitMutations(unitId: string): IDisposable {
        const alreadySyncing = this._syncingUnits.has(unitId);
        this._syncingUnits.add(unitId);
        return toDisposable(() => {
            if (!alreadySyncing) {
                this._syncingUnits.delete(unitId);
            }
        });
    }

    async syncMutation(commandInfo: IMutationInfo, options?: IRemoteSyncMutationOptions): Promise<boolean> {
        const sync = () => this._remoteReady.then(() =>
            this._remoteInstanceService.syncMutation({ mutationInfo: commandInfo }, options)
        );
        const result = this._syncMutationQueue.then(sync, sync);
        this._syncMutationQueue = result.catch(() => false);
        return result;
    }

    async waitForPendingMutations(): Promise<void> {
        await this._syncMutationQueue.catch(() => false);
    }

    private _initRPCChannels(): void {
        // for the worker to call
        this._rpcChannelService.registerChannel(RemoteSyncServiceName, fromModule(this._remoteSyncService));

        // to call the worker
        this._injector.add([
            IRemoteInstanceService,
            // eslint-disable-next-line react/no-unnecessary-use-prefix
            { useFactory: () => toModule<IRemoteInstanceService>(this._rpcChannelService.requestChannel(RemoteInstanceServiceName)) },
        ]);
        this._remoteInstanceService = this._injector.get(IRemoteInstanceService);
        this._remoteReady = this._remoteInstanceService.whenReady();
        this._handleRemoteRequest(this._remoteReady, 'initialize the remote instance service');
    }

    private _init(): void {
        this._univerInstanceService.getTypeOfUnitAdded$<Workbook>(UniverInstanceType.UNIVER_SHEET).pipe(takeUntil(this.dispose$)).subscribe((event) => {
            const { unit: sheet } = event;
            this._syncingUnits.add(sheet.getUnitId());

            // If a sheet is created, it should sync the data to the worker thread.
            this._handleRemoteRequest(this._remoteInstanceService.createInstance({
                unitID: sheet.getUnitId(),
                type: UniverInstanceType.UNIVER_SHEET,
                snapshot: sheet.getSnapshot(),
            }), 'create the remote workbook');
        });

        this._univerInstanceService.getTypeOfUnitDisposed$<Workbook>(UniverInstanceType.UNIVER_SHEET).pipe(takeUntil(this.dispose$)).subscribe((workbook) => {
            this._syncingUnits.delete(workbook.getUnitId());
            // If a sheet is disposed, it should sync the data to the worker thread.
            this._handleRemoteRequest(this._remoteInstanceService.disposeInstance({
                unitID: workbook.getUnitId(),
            }), 'dispose the remote workbook');
        });

        // Mutations executed on the main thread should be synced to the worker thread.
        this.disposeWithMe(this._commandService.onCommandExecuted((commandInfo, options) => {
            const { type, params, id } = commandInfo;
            const unitId = (params as { unitId: string })?.unitId || '';

            if (type === CommandType.MUTATION &&
                // only sync mutations to the worker thread
                (!unitId || this._syncingUnits.has(unitId)) &&
                // do not sync mutations from the web worker back to the web worker
                !(options as IRemoteSyncMutationOptions)?.fromSync &&
                // do not sync mutations those are not meant to be synced
                this._syncingMutations.has(id)) {
                this._handleRemoteRequest(
                    this.syncMutation(commandInfo as IMutationInfo, options),
                    `sync mutation ${id}`
                );
            }
        }));
    }

    private _handleRemoteRequest(request: Promise<unknown>, operation: string): void {
        request.catch((error: unknown) => {
            if (error instanceof ChannelClientDisposedError) {
                return;
            }

            this._logService.error(`[DataSyncPrimaryController]: Failed to ${operation}.`, error);
        });
    }
}