-
-
Notifications
You must be signed in to change notification settings - Fork 722
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(engine-formula): add global computing status service (#4602)
- Loading branch information
Showing
7 changed files
with
152 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
packages/engine-formula/src/controller/computing-status.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* Copyright 2023-present DreamNum Inc. | ||
* | ||
* 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 { ICommandInfo } from '@univerjs/core'; | ||
import type { ISetFormulaCalculationNotificationMutation } from '../commands/mutations/set-formula-calculation.mutation'; | ||
import { Disposable, DisposableCollection, ICommandService, Inject } from '@univerjs/core'; | ||
import { BehaviorSubject, distinctUntilChanged, Observable, shareReplay } from 'rxjs'; | ||
import { SetFormulaCalculationNotificationMutation } from '../commands/mutations/set-formula-calculation.mutation'; | ||
import { GlobalComputingStatusService } from '../services/global-computing-status.service'; | ||
import { FormulaExecuteStageType } from '../services/runtime.service'; | ||
|
||
// TODO@wzhudev: move logics in Facade to this place. | ||
|
||
/** | ||
* This controller monitors the calculating status of the formula engine, | ||
* and expose some internal status to the outside. | ||
* | ||
* @ignore | ||
*/ | ||
export class ComputingStatusReporterController extends Disposable { | ||
private _computingCompleted$ = new Observable<boolean>((observe) => { | ||
this._commandService.onCommandExecuted((command: ICommandInfo) => { | ||
if (command.id !== SetFormulaCalculationNotificationMutation.id) return; | ||
|
||
const params = command.params as ISetFormulaCalculationNotificationMutation; | ||
if (params.stageInfo) { | ||
return observe.next( | ||
params.stageInfo.stage === FormulaExecuteStageType.IDLE | ||
|| params.stageInfo.stage === FormulaExecuteStageType.CALCULATION_COMPLETED | ||
); | ||
} | ||
}); | ||
}).pipe( | ||
distinctUntilChanged(), | ||
shareReplay() | ||
); | ||
|
||
constructor( | ||
@ICommandService private readonly _commandService: ICommandService, | ||
@Inject(GlobalComputingStatusService) private readonly _globalComputingSrv: GlobalComputingStatusService | ||
) { | ||
super(); | ||
|
||
const disposables = new DisposableCollection(); | ||
const subject = new BehaviorSubject(true); | ||
disposables.add(this._globalComputingSrv.pushComputingStatusSubject(subject)); | ||
disposables.add(this._computingCompleted$.subscribe((completed) => subject.next(completed))); | ||
disposables.add(() => subject.complete()); | ||
this.disposeWithMe(disposables); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
packages/engine-formula/src/services/global-computing-status.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* Copyright 2023-present DreamNum Inc. | ||
* | ||
* 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 { IDisposable, Nullable } from '@univerjs/core'; | ||
import type { Subscription } from 'rxjs'; | ||
import { Disposable } from '@univerjs/core'; | ||
import { BehaviorSubject, combineLatest, distinctUntilChanged, map } from 'rxjs'; | ||
|
||
export type ComputingStatus = boolean; | ||
|
||
export class GlobalComputingStatusService extends Disposable { | ||
private _allSubjects: BehaviorSubject<ComputingStatus>[] = []; | ||
|
||
private readonly _computingStatus$ = new BehaviorSubject<ComputingStatus>(true); | ||
readonly computingStatus$ = this._computingStatus$.pipe(distinctUntilChanged()); | ||
|
||
private _computingSubscription: Nullable<Subscription>; | ||
|
||
override dispose(): void { | ||
super.dispose(); | ||
|
||
this._computingSubscription?.unsubscribe(); | ||
|
||
this._computingStatus$.next(true); | ||
this._computingStatus$.complete(); | ||
} | ||
|
||
pushComputingStatusSubject(subject: BehaviorSubject<ComputingStatus>): IDisposable { | ||
this._allSubjects.push(subject); | ||
this._updateComputingObservable(); | ||
|
||
return { | ||
dispose: () => { | ||
const index = this._allSubjects.indexOf(subject); | ||
if (index !== -1) { | ||
this._allSubjects.splice(index, 1); | ||
} | ||
|
||
this._updateComputingObservable(); | ||
}, | ||
}; | ||
} | ||
|
||
private _updateComputingObservable(): void { | ||
this._computingSubscription?.unsubscribe(); | ||
|
||
if (this._allSubjects.length === 0) { | ||
this._computingStatus$.next(true); | ||
return; | ||
} | ||
|
||
this._computingSubscription = combineLatest(this._allSubjects) | ||
.pipe(map((values) => values.every((v) => v))) | ||
.subscribe((computing) => this._computingStatus$.next(computing)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters