Skip to content

Commit

Permalink
add RecordRefreshListener and Platform Event (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-lyon authored Dec 8, 2023
1 parent 8170263 commit 9fa1205
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<template></template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { LightningElement, api } from 'lwc';
import {
subscribe as subToPlatformEvent,
unsubscribe as unsubFromPlatformEvent,
isEmpEnabled
} from 'lightning/empApi';
import { RefreshEvent } from 'lightning/refresh';

export default class RecordRefreshListener extends LightningElement {
@api recordId;
channelName = '/event/RecordUpdated__e';
subscription = {};

async connectedCallback() {
if (!isEmpEnabled) {
console.warn('emp is not enabled in this context', this.recordId);
return;
}

await this.subscribe().catch((error) => {
console.error('error', error);
});
}

async disconnectedCallback() {
await this.unsubscribe();
}

subscribe() {
const callback = (response) => {
const recordId = response.data.payload.RecordId__c;
if (this.recordId === recordId) {
this.dispatchEvent(new RefreshEvent());
}
};

return subToPlatformEvent(this.channelName, -1, callback).then(
(response) => {
this.subscription = response;
}
);
}

unsubscribe() {
return unsubFromPlatformEvent(this.subscription, (response) => {
console.log('unsubscribed', JSON.stringify(response));
this.subscription = null;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8" ?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>58.0</apiVersion>
<isExposed>true</isExposed>
<masterLabel>Record Refresh Listener</masterLabel>
<description>Refresh when a Refresh Platform Event is fired.</description>
<targets>
<target>lightning__RecordPage</target>
</targets>
<targetConfigs>
<targetConfig targets="lightning__RecordPage">
<property
name="recordId"
type="String"
label="Record Id"
description="Automatically bind the page's record id to the component variable"
/>
<objects>
<object>Account</object>
<object>Contact</object>
<object>Lead</object>
<object>Opportunity</object>
</objects>
</targetConfig>
</targetConfigs>
</LightningComponentBundle>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
<deploymentStatus>Deployed</deploymentStatus>
<description>Used to signal that a record has been updated.</description>
<eventType>HighVolume</eventType>
<label>Record Updated</label>
<pluralLabel>Record Updated</pluralLabel>
<publishBehavior>PublishAfterCommit</publishBehavior>
</CustomObject>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>RecordId__c</fullName>
<description>The Id of the updated record.</description>
<externalId>false</externalId>
<isFilteringDisabled>false</isFilteringDisabled>
<isNameField>false</isNameField>
<isSortingDisabled>false</isSortingDisabled>
<label>Record Id</label>
<length>18</length>
<required>true</required>
<type>Text</type>
<unique>false</unique>
</CustomField>

0 comments on commit 9fa1205

Please sign in to comment.