carloea2 commented on code in PR #5043: URL: https://github.com/apache/texera/pull/5043#discussion_r3277991940
########## frontend/src/app/workspace/service/code-editor/ui-udf-parameters-sync.service.ts: ########## @@ -0,0 +1,115 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 { Injectable } from "@angular/core"; +import { isEqual } from "lodash-es"; +import { ReplaySubject, Subject } from "rxjs"; +import { debounceTime } from "rxjs/operators"; +import { WorkflowActionService } from "../workflow-graph/model/workflow-action.service"; +import { UiUdfParametersParseError, UiUdfParametersParserService } from "./ui-udf-parameters-parser.service"; +import type { UiUdfParameter } from "./ui-udf-parameters-parser.service"; +import { isDefined } from "../../../common/util/predicate"; +import { isPythonUdf } from "../workflow-graph/model/workflow-graph"; +import type { Text as YText } from "yjs"; + +const UI_PARAMETER_SYNC_DEBOUNCE_TIME_MS = 200; + +@Injectable({ providedIn: "root" }) +export class UiUdfParametersSyncService { + private readonly uiParametersChangedSubject = new ReplaySubject<{ operatorId: string; parameters: UiUdfParameter[] }>( + 1 + ); + private readonly uiParametersParseErrorSubject = new ReplaySubject<{ operatorId: string; message?: string }>(1); + + readonly uiParametersChanged$ = this.uiParametersChangedSubject.asObservable(); + readonly uiParametersParseError$ = this.uiParametersParseErrorSubject.asObservable(); + + constructor( + private workflowActionService: WorkflowActionService, + private uiUdfParametersParserService: UiUdfParametersParserService + ) {} + + attachToYCode(operatorId: string, yCode: YText): () => void { + const codeChanges = new Subject<string>(); + const subscription = codeChanges + .pipe(debounceTime(UI_PARAMETER_SYNC_DEBOUNCE_TIME_MS)) + .subscribe(latestCode => this.syncStructureFromCode(operatorId, latestCode)); + const handler = () => codeChanges.next(yCode.toString()); + + yCode.observe(handler); + this.syncStructureFromCode(operatorId, yCode.toString()); + + return () => { + yCode.unobserve(handler); + subscription.unsubscribe(); + codeChanges.complete(); + }; + } + + syncStructureFromCode(operatorId: string, codeFromEditor?: string): void { + const operator = this.workflowActionService.getTexeraGraph().getOperator(operatorId); + + if (!operator || !isPythonUdf(operator)) return; + + const code = codeFromEditor ?? this.getSharedCode(operatorId); + if (!isDefined(code)) return; + + const existingParameters = (operator.operatorProperties?.uiParameters ?? []) as UiUdfParameter[]; + let mergedParameters: UiUdfParameter[]; + + try { + mergedParameters = this.buildParsedShapeWithPreservedValues(code, existingParameters); + } catch (error) { + if (error instanceof UiUdfParametersParseError) { + this.uiParametersParseErrorSubject.next({ operatorId, message: error.message }); + return; + } + throw error; + } + + this.uiParametersParseErrorSubject.next({ operatorId }); Review Comment: Fixed with the smaller option: I kept the observable contract unchanged, but moved the clear event behind an explicit helper so the intent is visible at the call site. ```ts this.clearParseError(operatorId); ``` ```ts private clearParseError(operatorId: string): void { this.uiParametersParseErrorSubject.next({ operatorId }); } ``` I also documented `uiParametersParseError$` so PR #2 consumers know that an event without `message` means “clear the current parse error for this operator.” -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
