Tutorial: fdc3v2.addContextListener

fdc3v2.addContextListener

Add a listener for incoming context. If an entity is part of a context group, and then sets its context listener, it will receive all of its declared contexts.

For FDC3 2.0, in order for the handler to get passed the optional ContextMetadata, the InteropBroker.invokeContextHandler must be overridden. The ContextMetadata must be added as part of the Context Object.

Example

function handleIncomingContext(contextInfo, metadata) {
    const { type, id } = contextInfo;
    switch (type) {
        case 'fdc3.instrument':
            handleInstrumentContext(contextInfo);
            break;
        case 'fdc3.country':
            handleCountryContext(contextInfo);
            break;

        default:
            break;
    }
}


function handleInstrumentContext(contextInfo) {
    const { type, id } = contextInfo;
    console.log('contextInfo for instrument', contextInfo)
}

function handleCountryContext(contextInfo) {
    const { type, id } = contextInfo;
    console.log('contextInfo for country', contextInfo)
}

fdc3.addContextListener(handleIncomingContext);