Skip to content

Instantly share code, notes, and snippets.

@justin-lyon
Last active June 19, 2020 20:18
Show Gist options
  • Save justin-lyon/0a2968654b4b38eff96580ea32694a4b to your computer and use it in GitHub Desktop.
Save justin-lyon/0a2968654b4b38eff96580ea32694a4b to your computer and use it in GitHub Desktop.
Trying to get the LMS demo working in a scratch
<template>
<div>
<button onclick={subscribeMC}>Subscribe</button>
</div>
<div>
<button onclick={unsubscribeMC}>Unsubscribe</button>
</div>
</template>
import { LightningElement, wire } from 'lwc'
import { APPLICATION_SCOPE, subscribe, unsubscribe, MessageContext } from 'lightning/messageService'
import SAMPLEMC from '@salesforce/messageChannel/SampleMessageChannel__c'
export default class ExpSubscriber extends LightningElement {
@wire(MessageContext)
messageContext
subscription = null
subscribeMC () {
if (this.subscription) return
console.log('begin sub to', this.messageContext)
this.subscription = subscribe(
this.messageContext,
SAMPLEMC,
this.handleMessage,
{ scope: APPLICATION_SCOPE })
console.log('subscribed', this.subscription)
}
unsubscribeMC () {
console.log('begin unsub', this.subscription)
unsubscribe(this.subscription)
this.subscription = null
console.log('unsubscribed', this.subscription)
}
handleMessage (message) {
console.log('sub message', JSON.parse(JSON.stringify(message)))
}
}
<template>
<div>
<c-exp-publisher></c-exp-publisher>
<c-exp-subscriber></c-exp-subscriber>
</div>
</template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment