Events
Any Membrane program can emit events and subscribe to events.
For example, if you have a program that handles GitHub webhooks, your program could emit a ghWebhookReceived
event, and another program that forwards GitHub notifications to Slack could subscribe to that event.
Emitting an event
import type { const root: Root
A gref to the root of this program.
root } from "membrane";
export const const endpoint: (req: {
method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS";
path: string;
body?: string;
query?: string;
headers: string;
}) => string
endpoint: resolvers.interface resolvers.Root
Root["endpoint"] = (req: {
method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS";
path: string;
body?: string;
query?: string;
headers: string;
}
req) => {
switch (`${req: {
method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS";
path: string;
body?: string;
query?: string;
headers: string;
}
req.method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS"
method} ${req: {
method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS";
path: string;
body?: string;
query?: string;
headers: string;
}
req.path: string
path}`) {
case "POST /gh-webhook-path":
const root: Root
A gref to the root of this program.
root.ghWebhookReceived.$emit();
/* ... */
}
};
Subscribing to an event
import type { import nodes
nodes } from "membrane";
export async function function sendToSlack(_: any, { event }: {
event: any;
}): Promise<void>
sendToSlack(_: any
_, { event: any
event }) {
await import nodes
nodes.slack.channel(/* ... */).sendMessage(/* ... */);
}
export async function function configure(): Promise<void>
configure() {
import nodes
nodes.ghWebhookReceived.$subscribe(function sendToSlack(_: any, { event }: {
event: any;
}): Promise<void>
sendToSlack);
}