Skip to content

Typescript API

Programs access most of Membrane’s functionality by importing the "membrane" module. This module is specific to each program and it’s regenerated every time you change its schema or its connections.

import { state, nodes, root } from "membrane"
// ↑ ↑ ↑
// | | |
// | | Gref to the program's root node
// | |
// | Connected nodes
// |
// Store persistent data

You can explore it by cmd+clicking on "membrane" to open the generated membrane.d.ts. You’ll see the types documented on this page, plus any additional types specific to that program’s schema.

Exported objects

state

Use this object to store data. Durable state is a core concept of Membrane. Learn more about effectively using state by reading the concepts section and the Managing State guide.

Example usage:

import {
const state: State

An object that can hold any data which transparently persists across program updates. To give this object a type, export a type named State from your program's index.ts. For example:

export type State = {
birthdays: Record<string, Date>;
}

In Membrane you can use state to efficiently store any object that persist across invocations and updates.

Data stored in state doesn't necessarily have to be serializable, any JavaScript value can be kept in it, including functions, promises, dates, etc.

state
} from "membrane"
// Initialize state at the top-level of the file
const state: State

An object that can hold any data which transparently persists across program updates. To give this object a type, export a type named State from your program's index.ts. For example:

export type State = {
birthdays: Record<string, Date>;
}

In Membrane you can use state to efficiently store any object that persist across invocations and updates.

Data stored in state doesn't necessarily have to be serializable, any JavaScript value can be kept in it, including functions, promises, dates, etc.

state
.
count: number
count
??= 0
export function
function increment(): void
increment
() {
const state: State

An object that can hold any data which transparently persists across program updates. To give this object a type, export a type named State from your program's index.ts. For example:

export type State = {
birthdays: Record<string, Date>;
}

In Membrane you can use state to efficiently store any object that persist across invocations and updates.

Data stored in state doesn't necessarily have to be serializable, any JavaScript value can be kept in it, including functions, promises, dates, etc.

state
.
count: number
count
++
}

nodes

Every connection can be accessed via the nodes object.

Values in nodes have accessors that match the node’s schema so autocomplete works as expected.

From a node, you can access other reachable nodes, actions and events.

Example usage:

import {
const nodes: {
readonly user: github.User;
readonly clock: Clock;
readonly process: Process;
}

Contains the graph references (grefs) that this program has been given access to.

nodes
} from "membrane"
async function
function run(): Promise<void>
run
() {
// This program has a connection named `user` to the GitHub driver
// so we can query it, invoke actions, subscribe to events, etc.
const
const bio: string
bio
= await
const nodes: {
readonly user: github.User;
readonly clock: Clock;
readonly process: Process;
}

Contains the graph references (grefs) that this program has been given access to.

nodes
.
user: github.handles.User
user
.
github.handles.User.bio: Scalar<string> & (() => Scalar<string>)

The user's bio

bio
// Autocomplete works as expected
const nodes: {
readonly user: github.User;
readonly clock: Clock;
readonly process: Process;
}

Contains the graph references (grefs) that this program has been given access to.

nodes
.
user: github.handles.User
user
.re
any
repos
}

Learn more about Connections and Queries in their respective sections.

root

This is a reference to the program’s own root node. This can be used to:

Example usage:

import {
const nodes: {
readonly user: github.User;
readonly clock: Clock;
readonly process: Process;
}

Contains the graph references (grefs) that this program has been given access to.

nodes
,
const root: Root

A gref to the root of this program.

root
} from "membrane"
async function
function setup(): Promise<void>
setup
() {
// Gref to a repo
const
const repo: github.handles.Repository
repo
=
const nodes: {
readonly user: github.User;
readonly clock: Clock;
readonly process: Process;
}

Contains the graph references (grefs) that this program has been given access to.

nodes
.
user: github.handles.User
user
.
github.handles.User.repos: github.handles.RepositoryCollection & (() => github.handles.RepositoryCollection)

Collection of Github repositories owned by the user

repos
.
github.handles.RepositoryCollection.one: (args: {
name: string;
}) => github.handles.Repository

Retrieve a single repository by name

one
({
name: string
name
: "docs" })
// Subscribe to an event
const repo: github.handles.Repository
repo
.
github.handles.Repository.issueOpened: NodeEvent<github.values.IssueEvent> & (() => NodeEvent<github.values.IssueEvent>)

Event indicating the opening of an issue within the repository

issueOpened
.
NodeEvent<github.values.IssueEvent>.$subscribe: (handler: Action<any>) => Promise<Subscription>
$subscribe
(
const root: Root

A gref to the root of this program.

root
.
handles.Root.handleIssueOpened: Action<void> & (() => Action<void>)
handleIssueOpened
)
}
// Handle the event
export async function
function handleIssueOpened(_: any, { event }: {
event: any;
}): Promise<void>
handleIssueOpened
(
_: any
_
, {
event: any
event
}) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without calling require('console').

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(`New issue opened: ${await
event: any
event
.
any
issue
.
any
title
}`)
}

Exported types

Action<T>

All actions implement the following interface. Where T is the type of the action’s return value.

// membrane.d.ts
interface Action<T> extends Gref {
$invoke(): Promise<T>;
$invokeAt(time: number | Date): void;
$invokeIn(seconds: number): void;
$cron(spec: string): void;
}

$invoke

Invoke an action immediately.

Signature:

$invoke(): void;

Example usage:

import {
const nodes: {
readonly email: email.Root;
readonly clock: Clock;
readonly process: Process;
}

Contains the graph references (grefs) that this program has been given access to.

nodes
,
const root: Root

A gref to the root of this program.

root
} from "membrane"
export async function
function run(): Promise<void>
run
() {
// Reference to the action
const
const action: Action<void>
action
=
const nodes: {
readonly email: email.Root;
readonly clock: Clock;
readonly process: Process;
}

Contains the graph references (grefs) that this program has been given access to.

nodes
.
email: email.handles.Root
email
.
email.handles.Root.send: (args: {
subject: string;
body: string;
}) => Action<void>

Sends an email to the configured address

Note: remember to await this action invocation. Or, chain another Action method: .$invoke(), .$invokeAt(), .$invokeIn(), .$cron()

See: https://docs.membrane.io/reference/membrane-module#action

send
({
subject: string
subject
: "I love",
body: string
body
: "tacos!" })
// Invoke it
await
const action: Action<void>
action
.
Action<void>.$invoke(): Promise<void>

Invoke this action

$invoke
()
// For convenience, you can just await the action to invoke it
await
const nodes: {
readonly email: email.Root;
readonly clock: Clock;
readonly process: Process;
}

Contains the graph references (grefs) that this program has been given access to.

nodes
.
email: email.handles.Root
email
.
email.handles.Root.send: (args: {
subject: string;
body: string;
}) => Action<void>

Sends an email to the configured address

Note: remember to await this action invocation. Or, chain another Action method: .$invoke(), .$invokeAt(), .$invokeIn(), .$cron()

See: https://docs.membrane.io/reference/membrane-module#action

send
({
subject: string
subject
: "Burritos",
body: string
body
: "are also okay" })
}

$invokeAt

Schedules the action to be invoked at a specific time. The invocation time can be either a Date object or a UNIX timestamp (seconds since January 1st, 1970).

See also: $invokeIn

Signature:

$invokeAt(time: Date | number): void;

Example usage:

import {
const nodes: {
readonly email: email.Root;
readonly clock: Clock;
readonly process: Process;
}

Contains the graph references (grefs) that this program has been given access to.

nodes
} from "membrane"
export async function
function run(): Promise<void>
run
() {
// New Year's day in eastern time
const
const time: Date
time
= new
var Date: DateConstructor
new (value: number | string | Date) => Date (+3 overloads)
Date
("2026-01-01T00:00:00.000-04:00")
// Schedule an email to be sent
await
const nodes: {
readonly email: email.Root;
readonly clock: Clock;
readonly process: Process;
}

Contains the graph references (grefs) that this program has been given access to.

nodes
.
email: email.handles.Root
email
.
email.handles.Root.send: (args: {
subject: string;
body: string;
}) => Action<void>

Sends an email to the configured address

Note: remember to await this action invocation. Or, chain another Action method: .$invoke(), .$invokeAt(), .$invokeIn(), .$cron()

See: https://docs.membrane.io/reference/membrane-module#action

send
({
subject: string
subject
: "Happy",
body: string
body
: "New Year!" })
.
Action<void>.$invokeAt(time: number | Date): void

Schedules a timer to invoke the action at a specific time

@paramtime The time to invoke the action, either a UNIX timestamp or a Date object

$invokeAt
(
const time: Date
time
)
}

$invokeIn

Schedules the action to be invoked after a specified delay in seconds.

See also: $invokeAt

Signature:

$invokeIn(seconds: number): void;

Example usage:

import {
const nodes: {
readonly email: email.Root;
readonly clock: Clock;
readonly process: Process;
}

Contains the graph references (grefs) that this program has been given access to.

nodes
} from "membrane"
export async function
function run(): Promise<void>
run
() {
// Schedule an email to be sent in 10 minutes
await
const nodes: {
readonly email: email.Root;
readonly clock: Clock;
readonly process: Process;
}

Contains the graph references (grefs) that this program has been given access to.

nodes
.
email: email.handles.Root
email
.
email.handles.Root.send: (args: {
subject: string;
body: string;
}) => Action<void>

Sends an email to the configured address

Note: remember to await this action invocation. Or, chain another Action method: .$invoke(), .$invokeAt(), .$invokeIn(), .$cron()

See: https://docs.membrane.io/reference/membrane-module#action

send
({
subject: string
subject
: "It's been",
body: string
body
: "10 minutes!" })
.
Action<void>.$invokeIn(seconds: number): void

Schedules a timer to invoke the action after a specified delay

@paramseconds The number of seconds to wait before invoking the action

$invokeIn
(60 * 10)
}

$cron

Schedules a cron-like timer that invokes an action at configured times. Unlike traditional cronjob expressions, Membrane has a resolution of seconds instead of the traditional minutes, so expression has an extra slot to specify at which second the action should be invoked.

Signature:

$cron(expression: string): void;

Cron expression syntax:

// At second zero
// | At minute zero
// | | At 5pm
// | | | Every day of month
// | | | | Every month
// | | | | | Only on Fridays
// | | | | | |
const everyFridayAt5pm = "0 0 17 * * Fri"

Valid values for each slot are:

SlotValid values
second0-59
minute0-59
hour0-23
day of month1-31
month1-12 or Jan, Feb, Mar, Apr, etc.
day of week0-7 or Sun, Mon, Tue, Wed, etc.
(both 0 and 7 represent Sunday)

Each slot can also accept * to mean “every possible value”.

Note that the time zone is always UTC.

Example usage:

import {
const nodes: {
readonly email: email.Root;
readonly clock: Clock;
readonly process: Process;
}

Contains the graph references (grefs) that this program has been given access to.

nodes
,
const root: Root

A gref to the root of this program.

root
} from "membrane"
export async function
function run(): Promise<void>
run
() {
// Every Friday at 5pm UTC
const root: Root

A gref to the root of this program.

root
.
handles.Root.everyFriday: any
everyFriday
.
any
$cron
("0 0 17 * * Fri")
}
export async function
function everyFriday(): Promise<void>
everyFriday
() {
await
const nodes: {
readonly email: email.Root;
readonly clock: Clock;
readonly process: Process;
}

Contains the graph references (grefs) that this program has been given access to.

nodes
.
email: email.handles.Root
email
.
email.handles.Root.send: any

Sends an email to the configured address

Note: remember to await this action invocation. Or, chain another Action method: .$invoke(), .$invokeAt(), .$invokeIn(), .$cron()

See: https://docs.membrane.io/reference/membrane-module#action

send
({
subject: string
subject
: "It's Friday!",
body: string
body
: "🎉" })
}

Field<T> & ListField<T[]>

All nodes except for scalars implement this interface. This includes connections available in the nodes object, as well as nodes in the program itself (e.g. root, root.status, etc).

InterfaceImplemented by
Field<T>fields that return an object type
ListField<T[]>fields that return a list types

Where T is the result(s) of querying that node (a type with all of its inner fields).

// membrane.d.ts
interface Field<T> extends Gref {
$query(query: string): Promise<T>;
}
interface ListField<T> extends Gref {
$query: (q: string) => Promise<T[]>;
}

$query

Queries a node using GraphQL.

Signature:

$query(query: string): Promise<T>;

Example usage:

import {
const nodes: {
readonly user: github.User;
readonly clock: Clock;
readonly process: Process;
}

Contains the graph references (grefs) that this program has been given access to.

nodes
} from "membrane"
async function
function run(): Promise<void>
run
() {
const {
const name: string | undefined
name
,
const bio: string | undefined
bio
} = await
const nodes: {
readonly user: github.User;
readonly clock: Clock;
readonly process: Process;
}

Contains the graph references (grefs) that this program has been given access to.

nodes
.
user: github.handles.User
user
.
Field<github.values.User>.$query(query: string): Promise<github.values.User>

Queries the graph starting at node referenced by this g-ref.

@paramquery The query to perform. Must be valid GraphQL query

@returnsThe result of the query. An object with the same shape as the query

@example

This example queries the status and body of the membrane.io website:

const { status, body } = await nodes.http.get({ url: "https://example.com" }).$query("{ status body }")

$query
("{ name bio }")
}

Learn more in the Queries section.

Scalar<T>

All Scalar nodes (String, Number, Boolean, etc) implement this interface.

interface Scalar<T> extends Gref {
$get: () => Promise<T>;
}

$get

Gets the value of a scalar node. Note that you can also omit the $get() when querying a scalar (see example below).

import {
const nodes: {
readonly user: github.User;
readonly clock: Clock;
readonly process: Process;
}

Contains the graph references (grefs) that this program has been given access to.

nodes
} from "membrane"
async function
function run(): Promise<void>
run
() {
// Using $get() to query a string
const
const bio: string
bio
= await
const nodes: {
readonly user: github.User;
readonly clock: Clock;
readonly process: Process;
}

Contains the graph references (grefs) that this program has been given access to.

nodes
.
user: github.handles.User
user
.
github.handles.User.bio: Scalar<string> & (() => Scalar<string>)

The user's bio

bio
.
Scalar<string>.$get: () => Promise<string>

Gets the value of this field. This is equivalent to calling $query without arguments

$get
()
// For convenience, you can omit the $get() when querying a scalar
const
const bio: string
bio
= await
const nodes: {
readonly user: github.User;
readonly clock: Clock;
readonly process: Process;
}

Contains the graph references (grefs) that this program has been given access to.

nodes
.
user: github.handles.User
user
.
github.handles.User.bio: Scalar<string> & (() => Scalar<string>)

The user's bio

bio
}

Gref

The base interface implemented by all fields, actions, and events.

interface Gref {
$args(): Record<string, any>
$argsAt(pattern: Field<unknown>): Record<string, any>
$pop(): Field<any>
}

$args

Returns the arguments from the last part of the gref. Only the last element of the gref is considered. Consider using $argsAt if you need arguments from the middle of the path.

Returns an empty object if there are no arguments.

Signature:

$args(): Record<string, any>;

Example usage:

import {
const nodes: {
readonly user: github.User;
readonly clock: Clock;
readonly process: Process;
}

Contains the graph references (grefs) that this program has been given access to.

nodes
} from "membrane"
async function
function run(): Promise<void>
run
() {
const
const repo: github.handles.Repository
repo
= await
const nodes: {
readonly user: github.User;
readonly clock: Clock;
readonly process: Process;
}

Contains the graph references (grefs) that this program has been given access to.

nodes
.
user: github.handles.User
user
.
github.handles.User.repos: github.handles.RepositoryCollection & (() => github.handles.RepositoryCollection)

Collection of Github repositories owned by the user

repos
.
github.handles.RepositoryCollection.one: (args: {
name: string;
}) => github.handles.Repository

Retrieve a single repository by name

one
({
name: string
name
: "docs" })
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without calling require('console').

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.assert(value: any, message?: string, ...optionalParams: any[]): void (+1 overload)

console.assert() writes a message if value is falsy or omitted. It only writes a message and does not otherwise affect execution. The output always starts with "Assertion failed". If provided, message is formatted using util.format().

If value is truthy, nothing happens.

console.assert(true, 'does nothing');
console.assert(false, 'Whoops %s work', 'didn\'t');
// Assertion failed: Whoops didn't work
console.assert();
// Assertion failed

@sincev0.1.101

@paramvalue The value tested for being truthy.

@parammessage All arguments besides value are used as error message.

assert
(
const repo: github.handles.Repository
repo
.
Gref.$args(): Record<string, any>

Gets the arguments of the last path element of this g-ref.

Only the last element of the g-ref path is considered. Consider using $argsAt if you need arguments from the middle of the path.

@example

const gref = root.a({ x: 1 }).b({ y: 2 });
const two = gref.$args().y;
console.assert(two === 2);

$args
().
any
name
=== "docs")
}

$argsAt

Returns the arguments from an arbitrary part of the gref. The provided pattern is also a gref and determines which part to get the arguments from.

Returns an empty object if there are no arguments or if the pattern does not match the gref.

This similar to using a regular expression on a URL to extract parts of it.

This is useful when implementing API drivers to extract the arguments for an API call.

$argsAt(pattern: Field<unknown>): Record<string, any>;

Example usage:

const gref = root.a({ x: 1 }).b({ y: 2 })
const { x } = gref.$argsAt(root.a)
const { y } = gref.$argsAt(root.a.b)

$pop

Returns a gref with the last part of the path removed. This does nothing if the path is empty.

$pop(): Field<any>;
const gref = root.a.b
const parent = gref.$pop()
console.assert(parent === root.a)

NodeEvent

All events implement this interface. This is not called Event to avoid conflicts with other Event types in node.js and browser environments. To unsubscribe from events, use the global unsubscribe function.

Learn more about Events.

// membrane.d.ts
interface NodeEvent<T> extends Gref {
$subscribe: (handler: Action<any>) => Promise<Subscription>;
$emit(event: Partial<T>): void;
}

$subscribe

Subscribes to the event so that when it fires, the handler action gets invoked.

See also: unsubscribe.

Signature:

$subscribe: (handler: Action<any>) => Promise<Subscription>;

Example usage:

import {
const nodes: {
readonly user: github.User;
readonly clock: Clock;
readonly process: Process;
}

Contains the graph references (grefs) that this program has been given access to.

nodes
,
const root: Root

A gref to the root of this program.

root
} from "membrane"
async function
function setup(): Promise<void>
setup
() {
// Gref to a repo
const
const repo: github.handles.Repository
repo
=
const nodes: {
readonly user: github.User;
readonly clock: Clock;
readonly process: Process;
}

Contains the graph references (grefs) that this program has been given access to.

nodes
.
user: github.handles.User
user
.
github.handles.User.repos: github.handles.RepositoryCollection & (() => github.handles.RepositoryCollection)

Collection of Github repositories owned by the user

repos
.
github.handles.RepositoryCollection.one: (args: {
name: string;
}) => github.handles.Repository

Retrieve a single repository by name

one
({
name: string
name
: "docs" })
// Subscribe to an event
const repo: github.handles.Repository
repo
.
github.handles.Repository.issueOpened: NodeEvent<github.values.IssueEvent> & (() => NodeEvent<github.values.IssueEvent>)

Event indicating the opening of an issue within the repository

issueOpened
.
NodeEvent<github.values.IssueEvent>.$subscribe: (handler: Action<any>) => Promise<Subscription>
$subscribe
(
const root: Root

A gref to the root of this program.

root
.
handles.Root.handleIssueOpened: Action<void> & (() => Action<void>)
handleIssueOpened
)
}
// Event handler (must be an action)
export async function
function handleIssueOpened(_: any, { event }: {
event: any;
}): Promise<void>
handleIssueOpened
(
_: any
_
, {
event: any
event
}) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without calling require('console').

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(`New issue opened: ${await
event: any
event
.
any
issue
.
any
title
}`)
}

$emit

Emits the event.

Signature:

$emit(event: Partial<T>): void;

Example usage:

import {
const state: State

An object that can hold any data which transparently persists across program updates. To give this object a type, export a type named State from your program's index.ts. For example:

export type State = {
birthdays: Record<string, Date>;
}

In Membrane you can use state to efficiently store any object that persist across invocations and updates.

Data stored in state doesn't necessarily have to be serializable, any JavaScript value can be kept in it, including functions, promises, dates, etc.

state
,
const root: Root

A gref to the root of this program.

root
} from "membrane"
const state: State

An object that can hold any data which transparently persists across program updates. To give this object a type, export a type named State from your program's index.ts. For example:

export type State = {
birthdays: Record<string, Date>;
}

In Membrane you can use state to efficiently store any object that persist across invocations and updates.

Data stored in state doesn't necessarily have to be serializable, any JavaScript value can be kept in it, including functions, promises, dates, etc.

state
.
count: number
count
??= 0
export function
function status(): string
status
() {
return `Count ${
const state: State

An object that can hold any data which transparently persists across program updates. To give this object a type, export a type named State from your program's index.ts. For example:

export type State = {
birthdays: Record<string, Date>;
}

In Membrane you can use state to efficiently store any object that persist across invocations and updates.

Data stored in state doesn't necessarily have to be serializable, any JavaScript value can be kept in it, including functions, promises, dates, etc.

state
.
count: number
count
}`
}
export function
function increment(): void
increment
() {
const state: State

An object that can hold any data which transparently persists across program updates. To give this object a type, export a type named State from your program's index.ts. For example:

export type State = {
birthdays: Record<string, Date>;
}

In Membrane you can use state to efficiently store any object that persist across invocations and updates.

Data stored in state doesn't necessarily have to be serializable, any JavaScript value can be kept in it, including functions, promises, dates, etc.

state
.
count: number
count
++
const root: Root

A gref to the root of this program.

root
.
handles.Root.statusChanged: NodeEvent<void> & (() => NodeEvent<void>)
statusChanged
.
NodeEvent<void>.$emit(event: void): void
$emit
()
}

Globals

These are function available in the global scope.

sleep

Returns a promise that resolves after a given number of seconds. You can await this promise to temporarily pause the execution of a function. Thanks to Membrane’s durable runtime, you can pause for an arbitrarily long time.

Signature:

function sleep(seconds: number): Promise<void>;

Example usage:

export async function
function run(): Promise<void>
run
() {
// Print the time every 10 seconds
for (let
let i: number
i
= 0;
let i: number
i
< 3;
let i: number
i
++) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without calling require('console').

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
("The time is", new
var Date: DateConstructor
new () => Date (+3 overloads)
Date
().
Date.toLocaleTimeString(locales?: Intl.LocalesArgument, options?: Intl.DateTimeFormatOptions): string (+2 overloads)

Converts a time to a string by using the current or specified locale.

@paramlocales A locale string, array of locale strings, Intl.Locale object, or array of Intl.Locale objects that contain one or more language or locale tags. If you include more than one locale string, list them in descending order of priority so that the first entry is the preferred locale. If you omit this parameter, the default locale of the JavaScript runtime is used.

@paramoptions An object that contains one or more properties that specify comparison options.

toLocaleTimeString
())
await
var sleep: (seconds: number) => Promise<any>

Returns a promise that resolves after the provided number of seconds

@paramseconds the number of seconds to sleep for

@returnsa promise that resolves once the timer has been set

sleep
(10)
}
}

Note that, even though you can sleep for an arbitrarily long time, once a function is executing, you can’t change it. When building long-running workflows, it’s best to use $invokeIn or $invokeAt to schedule workflow steps as separate actions.

unsubscribe

Unsubscribes from a previously subscribed event.

See also: $subscribe

Signature:

unsubscribe(subscription: Subscription): Promise<any>;

Example usage:

import {
const nodes: {
readonly user: github.User;
readonly clock: Clock;
readonly process: Process;
}

Contains the graph references (grefs) that this program has been given access to.

nodes
,
const root: Root

A gref to the root of this program.

root
,
const state: State

An object that can hold any data which transparently persists across program updates. To give this object a type, export a type named State from your program's index.ts. For example:

export type State = {
birthdays: Record<string, Date>;
}

In Membrane you can use state to efficiently store any object that persist across invocations and updates.

Data stored in state doesn't necessarily have to be serializable, any JavaScript value can be kept in it, including functions, promises, dates, etc.

state
} from "membrane"
async function
function setup(): Promise<void>
setup
() {
// Gref to a repo
const
const repo: github.handles.Repository
repo
=
const nodes: {
readonly user: github.User;
readonly clock: Clock;
readonly process: Process;
}

Contains the graph references (grefs) that this program has been given access to.

nodes
.
user: github.handles.User
user
.
github.handles.User.repos: github.handles.RepositoryCollection & (() => github.handles.RepositoryCollection)

Collection of Github repositories owned by the user

repos
.
github.handles.RepositoryCollection.one: (args: {
name: string;
}) => github.handles.Repository

Retrieve a single repository by name

one
({
name: string
name
: "docs" })
// Subscribe to an event
const state: State

An object that can hold any data which transparently persists across program updates. To give this object a type, export a type named State from your program's index.ts. For example:

export type State = {
birthdays: Record<string, Date>;
}

In Membrane you can use state to efficiently store any object that persist across invocations and updates.

Data stored in state doesn't necessarily have to be serializable, any JavaScript value can be kept in it, including functions, promises, dates, etc.

state
.
subscription: number
subscription
= await
const repo: github.handles.Repository
repo
.
github.handles.Repository.issueOpened: NodeEvent<github.values.IssueEvent> & (() => NodeEvent<github.values.IssueEvent>)

Event indicating the opening of an issue within the repository

issueOpened
.
NodeEvent<github.values.IssueEvent>.$subscribe: (handler: Action<any>) => Promise<Subscription>
$subscribe
(
const root: Root

A gref to the root of this program.

root
.
handles.Root.handleIssueOpened: Action<void> & (() => Action<void>)
handleIssueOpened
)
}
async function
function stop(): Promise<void>
stop
() {
// Unsubscribe from the event
var unsubscribe: (subscription: Subscription) => Promise<any>

Unsubscribes from a previously subscribed event

@paramsubscription the subscription to unsubscribe from. The result of a previous call to $subscribe

unsubscribe
(
const state: State

An object that can hold any data which transparently persists across program updates. To give this object a type, export a type named State from your program's index.ts. For example:

export type State = {
birthdays: Record<string, Date>;
}

In Membrane you can use state to efficiently store any object that persist across invocations and updates.

Data stored in state doesn't necessarily have to be serializable, any JavaScript value can be kept in it, including functions, promises, dates, etc.

state
.
subscription: number
subscription
)
}
// Handle the event
export async function
function handleIssueOpened(_: any, { event }: {
event: any;
}): Promise<void>
handleIssueOpened
(
_: any
_
, {
event: any
event
}) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without calling require('console').

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(`New issue opened: ${await
event: any
event
.
any
issue
.
any
title
}`)
}