tbonelee commented on code in PR #5266: URL: https://github.com/apache/zeppelin/pull/5266#discussion_r3407696734
########## zeppelin-web-angular/src/app/share/react-mount/react-remote-loader.service.ts: ########## @@ -0,0 +1,92 @@ +/* + * 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 { Injectable } from '@angular/core'; +import { environment } from '../../../environments/environment'; +import { AnyExposedModule } from './react-mount-handle'; + +interface RemoteContainer { + get<T>(key: string): Promise<() => T>; + init?: (shareScope: unknown) => Promise<void>; +} + +declare global { + interface Window { + reactApp?: RemoteContainer; + } +} + +@Injectable({ providedIn: 'root' }) +export class ReactRemoteLoaderService { + private containerPromise: Promise<RemoteContainer> | null = null; + private readonly modulePromises = new Map<string, Promise<AnyExposedModule>>(); + + loadContainer(): Promise<RemoteContainer> { + if (this.containerPromise) { + return this.containerPromise; + } + + this.containerPromise = new Promise<RemoteContainer>((resolve, reject) => { + if (window.reactApp) { + resolve(window.reactApp); + return; + } + + const script = document.createElement('script'); + script.src = environment.reactRemoteEntryUrl; + script.async = true; + script.onload = () => { + if (!window.reactApp) { + reject(new Error('window.reactApp not registered after script load')); + return; + } + resolve(window.reactApp); + }; + script.onerror = () => { + reject(new Error(`Failed to load React remote at ${script.src}`)); + }; Review Comment: Handled both the script error and `window.reactApp` load failure cases. (see https://github.com/tbonelee/zeppelin/blob/e3a30842a9050ddb66c077cc7392ee177cd006c7/zeppelin-web-angular/src/app/share/react-mount/react-remote-loader.service.ts#L50) -- 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]
