Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add onError and onRender callbacks #1636

Merged
merged 6 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion apis/nucleus/src/components/Cell.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import InstanceContext from '../contexts/InstanceContext';
import useObjectSelections from '../hooks/useObjectSelections';
import eventmixin from '../selections/event-mixin';
import useStyling from '../hooks/useStyling';
import RenderError from '../utils/render-error';

/**
* @interface
Expand Down Expand Up @@ -295,7 +296,7 @@ const loadType = async ({
},
});
} else {
dispatch({ type: 'ERROR', error: { title: err.message } });
dispatch({ type: 'ERROR', error: { title: err.message, errorObject: err } });
}
onMount();
}
Expand All @@ -313,6 +314,7 @@ const Cell = forwardRef(
currentId,
emitter,
navigation,
onError,
},
ref
) => {
Expand Down Expand Up @@ -517,6 +519,10 @@ const Cell = forwardRef(
if (state.loading && !state.longRunningQuery) {
Content = <LoadingSn />;
} else if (state.error) {
if (onError) {
const e = state.error.errorObject ? state.error.errorObject : new RenderError(state.error.title);
onError(e);
}
Content = <CError {...state.error} />;
} else if (state.loaded) {
Content = (
Expand Down
2 changes: 2 additions & 0 deletions apis/nucleus/src/components/glue.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default function glue({
emitter,
initialError,
navigation,
onError,
}) {
const { root } = halo;
const cellRef = React.createRef();
Expand All @@ -29,6 +30,7 @@ export default function glue({
onMount={onMount}
emitter={emitter}
navigation={navigation}
onError={onError}
/>,
element,
currentId
Expand Down
6 changes: 4 additions & 2 deletions apis/nucleus/src/object/create-session-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import init from './initiate';
* @description Configuration for rendering a visualisation, either creating or fetching an existing object.
* @property {HTMLElement} element Target html element to render in to
* @property {object=} options Options passed into the visualisation
* @property {function=} onRender Callback function called after rendering successfully
* @property {function(RenderError)=} onError Callback function called if an error occurs
* @property {Plugin[]} [plugins] plugins passed into the visualisation
* @property {string=} id For existing objects: Engine identifier of object to render
* @property {string=} type For creating objects: Type of visualisation to render
Expand Down Expand Up @@ -39,7 +41,7 @@ import init from './initiate';
* nebbie.render(createConfig);
*/
export default async function createSessionObject(
{ type, version, fields, properties, options, plugins, element, extendProperties, navigation },
{ type, version, fields, properties, options, plugins, element, extendProperties, navigation, onRender, onError },
halo,
store
) {
Expand Down Expand Up @@ -90,5 +92,5 @@ export default async function createSessionObject(
await halo.app.destroySessionObject(model.id);
unsubscribe();
};
return init(model, { options, plugins, element }, halo, navigation, error, onDestroy);
return init(model, { options, plugins, element, onRender, onError }, halo, navigation, error, onDestroy);
}
8 changes: 6 additions & 2 deletions apis/nucleus/src/object/get-generic-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import init from './initiate';
import initSheet from './initiate-sheet';
import createNavigationApi from './navigation/navigation';

export default async function getObject({ id, options, plugins, element, navigation: inputNavigation }, halo, store) {
export default async function getObject(
{ id, options, plugins, element, onRender, onError, navigation: inputNavigation },
halo,
store
) {
const { modelStore, rpcRequestModelStore } = store;
const key = `${id}`;
let rpc = rpcRequestModelStore.get(key);
Expand All @@ -18,5 +22,5 @@ export default async function getObject({ id, options, plugins, element, navigat
return initSheet(model, { options, plugins, element }, halo, navigation);
}

return init(model, { options, plugins, element }, halo, navigation);
return init(model, { options, plugins, element, onRender, onError }, halo, navigation);
}
3 changes: 3 additions & 0 deletions apis/nucleus/src/object/initiate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
import vizualizationAPI from '../viz';

export default async function init(model, optional, halo, navigation, initialError, onDestroy = async () => {}) {
const { onRender, onError } = optional;
const api = vizualizationAPI({
model,
halo,
navigation,
initialError,
onDestroy,
onRender,
onError,
});
if (optional.options) {
api.__DO_NOT_USE__.options(optional.options);
Expand Down
4 changes: 3 additions & 1 deletion apis/nucleus/src/sn/load.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import RenderError from '../utils/render-error';

const LOADED = {};

/**
Expand Down Expand Up @@ -32,7 +34,7 @@ export async function load(name, version, { config }, loader) {
if (__NEBULA_DEV__) {
console.warn(e); // eslint-disable-line no-console
}
throw new Error(`Failed to load visualization: '${sKey}'`);
throw new RenderError(`Failed to load visualization: '${sKey}'`, e);
});
}

Expand Down
15 changes: 15 additions & 0 deletions apis/nucleus/src/utils/render-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @class RenderError
* @extends Error
* @param {string} message
* @param {Error} originalError
* @property {Error} originalError
*/

export default class RenderError extends Error {
constructor(message, originalError) {
super(message);
this.originalError = originalError;
this.name = 'RenderError';
}
}
20 changes: 15 additions & 5 deletions apis/nucleus/src/viz.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,33 @@ import saveSoftProperties from './utils/save-soft-properties';

const noopi = () => {};

export default function viz({ model, halo, navigation, initialError, onDestroy = async () => {} } = {}) {
export default function viz({
model,
halo,
navigation,
initialError,
onDestroy = async () => {},
onRender = () => {},
onError = () => {},
} = {}) {
let unmountCell = noopi;
let cellRef = null;
let mountedReference = null;
let onMount = null;
let onRender = null;
let onRenderResolve = null;
let viewDataObjectId;
const mounted = new Promise((resolve) => {
onMount = resolve;
});

const rendered = new Promise((resolve) => {
onRender = resolve;
onRenderResolve = resolve;
});

const createOnInitialRender = (override) => () => {
override && override();
onRender();
override?.(); // from options.onInitialRender
onRenderResolve(); // internal promise in viz to wait for render
onRender(); // from RenderConfig
};

let initialSnOptions = {};
Expand Down Expand Up @@ -248,6 +257,7 @@ export default function viz({ model, halo, navigation, initialError, onDestroy =
onMount,
emitter,
navigation,
onError,
});
return mounted;
},
Expand Down
42 changes: 42 additions & 0 deletions apis/stardust/api-spec/spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -1757,6 +1757,22 @@
"optional": true,
"type": "object"
},
"onRender": {
"description": "Callback function called after rendering successfully",
"optional": true,
"kind": "function",
"params": []
},
"onError": {
"description": "Callback function called if an error occurs",
"optional": true,
"kind": "function",
"params": [
{
"type": "#/definitions/RenderError"
}
]
},
"plugins": {
"description": "plugins passed into the visualisation",
"optional": true,
Expand Down Expand Up @@ -1902,6 +1918,32 @@
}
}
},
"RenderError": {
"extends": [
{
"type": "Error"
}
],
"kind": "class",
"constructor": {
"kind": "function",
"params": [
{
"name": "message",
"type": "string"
},
{
"name": "originalError",
"type": "Error"
}
]
},
"entries": {
"originalError": {
"type": "Error"
}
}
},
"Navigation": {
"stability": "experimental",
"availability": {
Expand Down
16 changes: 16 additions & 0 deletions apis/stardust/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,15 @@ declare namespace stardust {
interface RenderConfig {
element: HTMLElement;
options?: object;
/**
* Callback function called after rendering successfully
*/
onRender?(): void;
/**
* Callback function called if an error occurs
* @param $
*/
onError?($: stardust.RenderError): void;
plugins?: stardust.Plugin[];
id?: string;
type?: string;
Expand Down Expand Up @@ -602,6 +611,13 @@ declare namespace stardust {
meta?: object;
}

class RenderError extends Error {
constructor(message: string, originalError: Error);

originalError: Error;

}

class Navigation implements stardust.Emitter {
constructor();

Expand Down
1 change: 1 addition & 0 deletions test/mashup/visualize/life.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<div class="wrapper">
<div class="viz"></div>
<div class="actions"></div>
<div class="errors" data-tid="error-external"></div>
</div>
</body>
</html>
4 changes: 4 additions & 0 deletions test/mashup/visualize/life.int.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ describe('object lifecycle', () => {
'[data-tid="error-title"]',
"Could not find a version of 'voooz' that supports current object version. Did you forget to register voooz?"
);
await waitForTextStatus(
'[data-tid="error-external"]',
"Could not find a version of 'voooz' that supports current object version. Did you forget to register voooz?"
);
});

it('should show spinner and requirements for known type', async () => {
Expand Down
10 changes: 9 additions & 1 deletion test/mashup/visualize/scenarios.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,15 @@ async function render() {
const scenario = getScenario();
const app = await EnigmaMocker.fromGenericObjects([scenario.genericObject], scenario.options);
const element = document.querySelector('.viz');
const viz = await configuration(app).render({ element, id: 'bb8' });
const viz = await configuration(app).render({
element,
id: 'bb8',
onError: (e) => {
console.log(e);
const errorElement = document.querySelector('.errors');
errorElement.textContent = e.message;
},
});

if (typeof scenario.post === 'function') {
scenario.post(viz);
Expand Down