Skip to content

Commit

Permalink
fix(cli): apploader only emits the latest result to prevent doubble l…
Browse files Browse the repository at this point in the history
…oading app (#2606)
  • Loading branch information
eikeland authored Nov 25, 2024
1 parent fdf3e3d commit 00fb17d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 33 deletions.
12 changes: 12 additions & 0 deletions .changeset/thin-baboons-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@equinor/fusion-framework-cli': patch
---

### Modified Files

`AppLoader.tsx`

### Changes

- Added import for last operator from rxjs/operators.
- Updated the initialize subscription to use the last operator.
72 changes: 39 additions & 33 deletions packages/cli/src/bin/dev-portal/AppLoader.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect, useMemo, useRef, useState } from 'react';

import { Subscription } from 'rxjs';
import { last } from 'rxjs/operators';

import { useFramework } from '@equinor/fusion-framework-react';

Expand Down Expand Up @@ -52,39 +53,44 @@ export const AppLoader = (props: { readonly appKey: string }) => {

/** make sure that initialize is canceled and disposed if current app changes */
subscription.add(
currentApp?.initialize().subscribe({
next: ({ manifest, script, config }) => {
/** generate basename for application */
const [basename] = window.location.pathname.match(
/\/?apps\/[a-z|-]+(\/)?/g,
) ?? [''];

/** create a 'private' element for the application */
const el = document.createElement('div');
if (!ref.current) {
throw Error('Missing application mounting point');
}

ref.current.appendChild(el);

/** extract render callback function from javascript module */
const render = script.renderApp ?? script.default;

/** add application teardown to current render effect teardown */
subscription.add(render(el, { fusion, env: { basename, config, manifest } }));

/** remove app element when application unmounts */
subscription.add(() => el.remove());
},
complete: () => {
/** flag that application is no longer loading */
setLoading(false);
},
error: (err) => {
/** set error if initialization of application fails */
setError(err);
},
}),
currentApp
?.initialize()
.pipe(last())
.subscribe({
next: ({ manifest, script, config }) => {
/** generate basename for application */
const [basename] = window.location.pathname.match(
/\/?apps\/[a-z|-]+(\/)?/g,
) ?? [''];

/** create a 'private' element for the application */
const el = document.createElement('div');
if (!ref.current) {
throw Error('Missing application mounting point');
}

ref.current.appendChild(el);

/** extract render callback function from javascript module */
const render = script.renderApp ?? script.default;

/** add application teardown to current render effect teardown */
subscription.add(
render(el, { fusion, env: { basename, config, manifest } }),
);

/** remove app element when application unmounts */
subscription.add(() => el.remove());
},
complete: () => {
/** flag that application is no longer loading */
setLoading(false);
},
error: (err) => {
/** set error if initialization of application fails */
setError(err);
},
}),
);

/** teardown application when hook unmounts */
Expand Down

0 comments on commit 00fb17d

Please sign in to comment.