Wire Service Sync
Usage
Extend you component with mixed class generated on the fly
export default class Mappings extends Mixin(LightningElement)
.with(AccountObjectWireProvider, CachedDataWireProvider).addWireSync(){}
- Pass a base class to extend from to a Mixin function (
LightningElementin this case). - Call
.withand pass provider you want to make available for the component (AccountObjectWireProvider,CachedDataWireProviderin this case). - Call
addWireSyncto addwireSyncResolver()method to your component. - In the place where you need to ensure @wire data from providers is available subscribe to the result promise from
this.wireSyncResolver()call.
Full Example
export default class Mappings extends Mixin(LightningElement).with(AccountObjectWireProvider, CachedDataWireProvider).addWireSync() {
connectedCallback() {
this.wireSyncResolver().then(this.init.bind(this));
}
async init() {
// Some code that required data from
// AccountObjectWireProvider and CachedDataWireProvider to be available
}
}
How to Add a custom @wire service provider
The provider is a function that returns a class that has to have the following things:
- a variable initialized with
new Resolver()(accountObjectWireProviderResolverin this case) - static variable called
WIRE_RESOLVER_NAMEthat is initialized with the name of the variable from the previous point (accountObjectWireProviderResolver); - @wire service callback that calls method
.resolve()of the variable from the point 1 when the data is available
The value of WIRE_RESOLVER_NAME has to be unique within the list of the providers for a specific component
For example .with(AccountObjectWireProvider, CachedDataWireProvider) uniqueness is required only across this call not across all project
but it's better for the value to be unique across the whole project to prevent bugs.
export const AccountObjectWireProvider = baseClass => {
return class extends baseClass {
static WIRE_RESOLVER_NAME = 'accountObjectWireProviderResolver';
accountObjectWireProviderResolver = new Resolver();
@wire(getObjectInfo, { objectApiName: ACCOUNT })
accountObjectInfoCallback({ data }) {
if (data) {
this.accObjectData = data;
this.accountObjectWireProviderResolver.resolve();
}
}
accObjectData = null;
};
};
How to include in an already existing project
This feature is actually an extension to an already existing Mixin framework from AqLib so you just have to tweak your Mixin class
a little to leverage this feature.