Skip to content
Moku
@moku-labs/system pages

API reference

Every export of @moku-labs/system and its nine capability subpaths, with signatures.

On this page

Entries

ImportFor
@moku-labs/systemcreateApp, createPlugin, ok, err and the types. No plugin instance.
@moku-labs/system/storestorePlugin
@moku-labs/system/traytrayPlugin
@moku-labs/system/notifynotifyPlugin
@moku-labs/system/clipboardclipboardPlugin
@moku-labs/system/deep-linkdeepLinkPlugin
@moku-labs/system/lifecyclelifecyclePlugin
@moku-labs/system/backbackPlugin
@moku-labs/system/hapticshapticsPlugin
@moku-labs/system/keep-awakekeepAwakePlugin

Signatures below are simplified for reading. The installed dist/index.d.mts is the source.

createApp

function createApp<const ExtraPlugins extends readonly AnyPluginInstance[] = readonly []>(options?: {
  plugins?: ExtraPlugins;
  pluginConfigs?: { [pluginName: string]: Partial<PluginConfig> };
  onReady?: (ctx: AppCallbackContext) => void;
  onError?: (error: Error, ctx: AppCallbackContext) => void;
  onStart?: (ctx: AppCallbackContext) => void | Promise<void>;
  onStop?: (ctx: AppCallbackContext) => void | Promise<void>;
}): App;

Creates an app from the capabilities you list. The log, env and runtime core plugins are always there. Each plugin mounts its API at app.<name>: app.store, app.deepLink, app.keepAwake.

MemberWhat it is
start()Picks every capability's provider. Call it once.
stop()Releases native resources. Waits at most 5 seconds for a provider that is still loading.
log, env, runtimeThe core plugin APIs.
<name>The API of every plugin in plugins.

createPlugin

function createPlugin<Name extends string, Config, State, Api, Deps, Events, Hooks>(
  name: Name,
  spec: {
    config?: Config;
    depends?: Deps;
    createState?: (ctx: { global; config }) => State;
    events?: (register) => Events;
    api?: (ctx: PluginContext) => Api;
    hooks?: (ctx: PluginContext) => Hooks;
    onInit?: (ctx: PluginContext) => void;
    onStart?: (ctx: PluginContext) => void | Promise<void>;
    onStop?: (ctx: { global; config; state }) => void | Promise<void>;
  }
): PluginInstance<Name, Config, State, Api, Events>;

Writes a plugin bound to this framework. Its ctx carries ctx.runtime, ctx.log and ctx.env.

src/plugins/shell-name.ts
import { createApp, createPlugin } from "@moku-labs/system";

const shellName = createPlugin("shellName", {
  api: ctx => ({ kind: () => ctx.runtime.kind })
});

const app = createApp({ plugins: [shellName] });
console.log(app.shellName.kind()); // "web" in a browser

ok and err

function ok<T>(value: T, provider: RuntimeKind): SystemOk<T>;
function err(provider: RuntimeKind, reason: SystemErrorReason, message?: string): SystemErr;

Build a SystemResult by hand, for your own capability plugin or a test double.

src/result.ts
import { err, ok } from "@moku-labs/system";

const saved = ok(42, "web"); // { ok: true, value: 42, provider: "web" }
const refused = err("tauri", "denied", "no permission"); // { ok: false, provider: "tauri", reason: "denied", message: "no permission" }
console.log(saved.value, refused.reason);

Plugins

ExportMounted atConfig keyMethods
storePluginapp.storestore: { name }get, set, delete, keys, clear
trayPluginapp.traytray: { id, icon? }setMenu, setTooltip, setIcon, destroy
notifyPluginapp.notifynoneshow, requestPermission, isPermissionGranted
clipboardPluginapp.clipboardnonereadText, writeText
deepLinkPluginapp.deepLinkdeepLink: { schemes }getCurrent, onOpen
lifecyclePluginapp.lifecyclenoneonPause, onResume
backPluginapp.backnoneonPress, exit
hapticsPluginapp.hapticsnoneimpact, notify, selection
keepAwakePluginapp.keepAwakenoneset

Each has its own page under Plugins.

Types

TypeWhat it is
SystemResult<T>, SystemOk<T>, SystemErrThe outcome of every capability method.
SystemErrorReason"unsupported" | "denied" | "unavailable" | "error".
RuntimeKind"tauri" | "web".
RuntimePlatform"macos" | "windows" | "linux" | "ios" | "android" | "unknown".
JsonValue, JsonPrimitiveWhat store accepts.
Store, Tray, Notify, Clipboard, DeepLink, Lifecycle, Back, Haptics, KeepAwakeOne type namespace per capability: Store.StoreApi, Tray.TrayMenuItem, Notify.NotifyOptions.