Skip to content

messageService

James Hou edited this page Jan 24, 2021 · 1 revision

Designed to ease the friction of inter-component (and technology) communication. This component is both the publisher and the subscriber to reduce implementation friction.

With that design, it's important to leverage the boundary property to properly scope your event publisher/listeners.

Some components in this library, like soqlDatatable, will generate a UUID to help scope payloads to its own child components (custom data type cells, dialogs etc).

This component uses Lightning Message Service on OpenChannel__c to message payloads in a key / value format as defined in OpenChannel__c like this:

const payload = {
    key: 'coolevent',
    value: {
        hello: 'world',
        foo: 'bar'
    }
}
this._messageService.publish(payload);

// or, my preferred method, this way

const payload = {
    accountId: '12345'
}
this._messageService.publish({key: 'supercoolevent', value: payload});

And handled like this, composed on the template:

<c-message-service
    boundary="sample_app_lwc"
    oncoolevent={handleCoolEvent}
    onsupercoolevent={handleSuperCoolEvent}
></c-message-service>
handleCoolEvent(event) {
    console.log(event.detail.value.hello) // world
    console.log(event.detail.value.foo) // bar
}

handleSuperCoolEvent(event) {
    const payload = event.detail.value
    console.log(payload.accountId) // 12345
}

This component doesn't need to subscribe to an event, it can be used for publish only:

<!-- No listeners, but has a boundary set for any publish() calls -->
<c-message-service boundary={recordId}></c-message-service>

... or

<!-- No listeners, no boundary set for any publish() calls -->
<c-message-service></c-message-service>

This component also provides public methods to Aura only APIs like overlayLibrary.

For example, using .dialogService() ultimately routes to DialogService.cmp:

const dialogServicePayload = {
    method: 'bodyModalLarge',
    config: {
        auraId: 'soql-datatable-example',
        headerLabel: 'Dynamically Created SOQL Datatable',
        component: 'c:soqlDatatable',
        componentParams: {
            isRecordBind: false,
            recordId: this.recordId,
            queryString: query
        }
    }
};
this._messageService.dialogService(dialogServicePayload);

Specification

Attributes

name type access required default description
boundary string public no Filter subscription messages like a namespace.

e.g. recordId if you only want same components on same record flexipage to handle the publish.

e.g. sample_app_lwc as reference among various components that share the same functionality.

Enablement of APPLICATION_SCOPE like in this diagram is not currently enabled.

Public Methods

name arguments description
dialogService (payload) payload is in the shape required by MessageServiceHandler. Examples:
flowWizardLauncherExample
lwcContactDatatable
soqlDatatableLauncherExample
notifyClose Uses publishOpen to fire a closedialog LMS Event which will close any dialog opened by DialogService
publish (payload) Leverages LMS's publish functionality.
Defaults to no boundary.
If boundary is set, all subscribers will require the same boundary.
publishOpen (payload) Leverage LMS's publish functionality without boundary. Any subscriber can react to this event.
// TODO Useful for closedialog unless this critical update is enabled.
// TODO When a user can simultaneously open multiple dialogs in service console, it's better to set a boundary.
forceRefreshView Uses eval("$A.get('e.force:refreshView').fire();"); directly.
notifySuccess (title, message = null) Convenience function for ShowToastEvent
notifyInfo (title, message = null) Convenience function for ShowToastEvent
notifySingleError (title, error = '') Convenience function for ShowToastEvent.
error object can be passed directly in, it will be reduced/parsed by c-utils.reduceError.
Clone this wiki locally