Skip to content

Commit

Permalink
Add new function for subapps: waitForSubApp
Browse files Browse the repository at this point in the history
  • Loading branch information
christianlent committed Jan 28, 2020
1 parent 264878c commit 15b94ce
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
27 changes: 18 additions & 9 deletions packages/subapp-pbundle/src/redux-bundler.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,24 @@ setStoreContainer(typeof window === "undefined" ? global : window);
export function reduxRenderStart(options) {
const store = options._store || options.reduxCreateStore(options.initialState);
const { Component } = options;
let component = undefined;

render(
<Provider store={store}>
<Component />
</Provider>,
options.element
);
if (options.element) {
render(
<Provider store={store}>
<Component />
</Provider>,
options.element
);
} else {
component = (
<Provider store={store}>
<Component />
</Provider>
);
}

return store;
return {store, component};
}

//
Expand All @@ -33,7 +42,7 @@ export function reduxBundlerLoadSubApp(info) {
const reduxCreateStore = instance.reduxCreateStore || this.info.reduxCreateStore;
const Component = this.info.StartComponent || this.info.Component;

const store = reduxRenderStart({
const {store, component} = reduxRenderStart({
_store: instance._store,
initialState,
reduxCreateStore,
Expand All @@ -43,7 +52,7 @@ export function reduxBundlerLoadSubApp(info) {
});

instance._store = store;
return store;
return component;
};

// allow subApp to specify redux bundles as reduxBundles or bundles
Expand Down
14 changes: 12 additions & 2 deletions packages/subapp-web/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,23 @@ export function getSubAppComponent({ name, timeout = 15000, onReady, onError, fa
//
}

export function waitForSubApp(name) {
return new Promise((resolve, reject) => {
dynamicLoadSubApp({
name,
onLoad: () => resolve(),
onError: () => reject()
});
});
}

export function dynamicLoadSubApp({ name, id, timeout = 15000, onLoad, onError, fallback }) {
// TODO: timeout and callback
const lname = name.toLowerCase();

const renderToDomId = subApp => {
if (!id) {
return onLoad();
return onLoad && onLoad();
} else {
const element = document.getElementById(id);
if (element && subApp.start) {
Expand All @@ -133,7 +143,7 @@ export function dynamicLoadSubApp({ name, id, timeout = 15000, onLoad, onError,
const renderInline = () => {
const subApp = xarc.getSubApp(name);
if (subApp) {
return "Render inline";
return subApp.start({});
}
return false;
};
Expand Down

0 comments on commit 15b94ce

Please sign in to comment.