-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add RecordRefreshListener and Platform Event (#65)
- Loading branch information
1 parent
8170263
commit 9fa1205
Showing
5 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
force-app/main/default/lwc/recordRefreshListener/recordRefreshListener.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<template></template> |
50 changes: 50 additions & 0 deletions
50
force-app/main/default/lwc/recordRefreshListener/recordRefreshListener.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
force-app/main/default/lwc/recordRefreshListener/recordRefreshListener.js-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
9 changes: 9 additions & 0 deletions
9
force-app/main/default/objects/RecordUpdated__e/RecordUpdated__e.object-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
14 changes: 14 additions & 0 deletions
14
force-app/main/default/objects/RecordUpdated__e/fields/RecordId__c.field-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |