Skip to content

Instantly share code, notes, and snippets.

@feliperyan
Created October 23, 2019 15:53
Show Gist options
  • Save feliperyan/84aaa20096ee509955a7d9253d1a58cf to your computer and use it in GitHub Desktop.
Save feliperyan/84aaa20096ee509955a7d9253d1a58cf to your computer and use it in GitHub Desktop.
platform events lightning web component variable scope shenanigans
import { LightningElement, track } from 'lwc';
import { subscribe, unsubscribe, onError, setDebugFlag, isEmpEnabled } from 'lightning/empApi';
export default class Geofenceevent extends LightningElement {
@track channelName = '/event/GeoFenceTrip__e';
@track isSubscribeDisabled = false;
@track isUnsubscribeDisabled = !this.isSubscribeDisabled;
subscription = {};
@track latestMessageReceived = "dummy for now."; // Fryan
handleChannelName(event) {
this.channelName = event.target.value;
}
handleTest() {
console.log(this.latestMessageReceived);
// Dummy for now - no matter if we get the events. It never gets set.
}
handleSubscribe() {
const messageCallback = function(response) {
console.log(this.latestMessageReceived); // Undefined
this.latestMessageReceived = JSON.stringify(response);
console.log('FRYAN last message: ', this.latestMessageReceived);
// Works so much be getting set locally to this "scope"?
};
subscribe(this.channelName, -1, messageCallback).then(response => {
console.log('Successfully subscribed to : ', JSON.stringify(response.channel));
this.subscription = response;
this.toggleSubscribeButton(true);
});
}
handleUnsubscribe() {
this.toggleSubscribeButton(false);
unsubscribe(this.subscription, response => {
console.log('unsubscribe() response: ', JSON.stringify(response));
});
}
toggleSubscribeButton(enableSubscribe) {
this.isSubscribeDisabled = enableSubscribe;
this.isUnsubscribeDisabled = !enableSubscribe;
}
registerErrorListener() {
// Invoke onError empApi method
onError(error => {
console.log('Received error from server: ', JSON.stringify(error));
// Error contains the server-side error
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment