Messenger is a simple message handler for Figma.
Note
This hasn't been packaged yet and is mainly a learning experience for me.
In both the UI and main code you'll need to create an instance of Msgr.
const msgr = new Msgr();
Define your event handlers.
const msgr = new Msgr({ handlers });
const handlers = {
greet(value: any) {
console.log(value);
},
};
Calling emit invokes the event handler for the matching event name in either UI or main context.
msgr.emit("greet", "Hello world!");
After emitting a message you can await a response from the other context.
let res = await msgr.emit("greet");