Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add RecordRefreshListener and Platform Event #65

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Loading