Skip to main content

Wire Service Sync

Nikolay Tishakin
Nikolay Tishakin

Usage​

Extend you component with mixed class generated on the fly


export default class Mappings extends Mixin(LightningElement)
.with(AccountObjectWireProvider, CachedDataWireProvider).addWireSync(){}
  1. Pass a base class to extend from to a Mixin function (LightningElement in this case).
  2. Call .with and pass provider you want to make available for the component (AccountObjectWireProvider, CachedDataWireProvider in this case).
  3. Call addWireSync to add wireSyncResolver() method to your component.
  4. 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:

  1. a variable initialized with new Resolver() (accountObjectWireProviderResolver in this case)
  2. static variable called WIRE_RESOLVER_NAME that is initialized with the name of the variable from the previous point (accountObjectWireProviderResolver);
  3. @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.