Skip to content

Commit 0c132a7

Browse files
author
Joel Denning
authored
Wait for mount to finish before unmounting parcels. Resolves single-spa#656. (single-spa#660)
1 parent d12eb34 commit 0c132a7

File tree

3 files changed

+55
-13
lines changed

3 files changed

+55
-13
lines changed

spec/parcels/app-mounts-parcel.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,37 @@ describe("applications mounting parcels :", () => {
294294
expect(p2.getStatus()).toBe(singleSpa.NOT_MOUNTED);
295295
});
296296
});
297+
298+
// https://github.com/single-spa/single-spa/issues/656
299+
it(`successfully unmounts a parcel that is unmounted before mount finishes`, async () => {
300+
let shouldAppBeMounted = false,
301+
parcel;
302+
303+
singleSpa.registerApplication(
304+
"immediate-unmount",
305+
app,
306+
() => shouldAppBeMounted
307+
);
308+
309+
parcelConfig = {
310+
mount: () =>
311+
new Promise((resolve) => {
312+
setTimeout(resolve, 50);
313+
}),
314+
async unmount() {},
315+
};
316+
317+
shouldAppBeMounted = true;
318+
319+
return singleSpa.triggerAppChange().then(() => {
320+
parcel = app.mountProps.mountParcel(parcelConfig, {
321+
domElement: document.createElement("div"),
322+
});
323+
324+
shouldAppBeMounted = false;
325+
return singleSpa.triggerAppChange();
326+
});
327+
});
297328
});
298329

299330
function createParcelConfig() {

spec/parcels/mount-root-parcel.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ describe(`root parcels`, () => {
117117
});
118118
await parcel.mountPromise;
119119
});
120+
121+
// https://github.com/single-spa/single-spa/issues/656
122+
it(`can unmount a parcel that is still waiting for mount to finish`, async () => {
123+
const parcelConfig = createParcelConfig();
124+
const parcel = singleSpa.mountRootParcel(parcelConfig, {
125+
domElement: document.createElement("div"),
126+
});
127+
await parcel.unmount();
128+
});
120129
});
121130

122131
function createParcelConfig(opts = {}) {

src/parcels/mount-parcel.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,21 @@ export function mountParcel(config, customProps) {
9090
customProps,
9191
parentName: toName(owningAppOrParcel),
9292
unmountThisParcel() {
93-
if (parcel.status !== MOUNTED) {
94-
throw Error(
95-
formatErrorMessage(
96-
6,
97-
__DEV__ &&
98-
`Cannot unmount parcel '${name}' -- it is in a ${parcel.status} status`,
99-
name,
100-
parcel.status
101-
)
102-
);
103-
}
104-
105-
return toUnmountPromise(parcel, true)
93+
return mountPromise
94+
.then(() => {
95+
if (parcel.status !== MOUNTED) {
96+
throw Error(
97+
formatErrorMessage(
98+
6,
99+
__DEV__ &&
100+
`Cannot unmount parcel '${name}' -- it is in a ${parcel.status} status`,
101+
name,
102+
parcel.status
103+
)
104+
);
105+
}
106+
return toUnmountPromise(parcel, true);
107+
})
106108
.then((value) => {
107109
if (parcel.parentName) {
108110
delete owningAppOrParcel.parcels[parcel.id];

0 commit comments

Comments
 (0)