Skip to content

Runtime and Tasks ​

Import the APIs on this page from @makoojs/core. The examples use Panel.vue from Component Injection.

createMakoo() ​

Creates a Makoo runtime.

Type ​

ts
function createMakoo(options?: CreateMakooOptions): MakooRuntime;

Parameters ​

options uses CreateMakooOptions:

FieldTypeDefaultDescription
defaultsPartial<MakooDefaults>{}Sets the default injection options
adaptersResolvableMountAdapter[][]Registers adapters that can mount artifacts
hooksLifecycleHookMapundefinedRegisters global lifecycle hooks
loggerILoggernew Logger()Sets the logger implementation
observerObserverHubcreateObserverHub(logger)Sets the lifecycle event hub

Details ​

The final default values of defaults are:

json
{
  "alive": false,
  "scope": "local",
  "timeout": 5000
}

Returns ​

Returns MakooRuntime:

MethodReturnsDescription
start(tasks)StartedTasksRegisters and starts a batch of MakooTaskDeclaration; an empty array throws TaskError
reset(taskId)voidResets a task and releases its current resources
destroy(taskId)voidDestroys and removes a task
resetAll()voidResets every task in the runtime
destroyAll()voidDestroys every task in the runtime
enableAlive(taskId)voidEnables alive observation for a component task
disableAlive(taskId)voidDisables alive observation for a component task
on(event, hook)() => voidSubscribes to an event and returns an unsubscribe function
onTask(taskId, event, hook)() => voidSubscribes to an event for one task and returns an unsubscribe function
onAny(hook)() => voidSubscribes to all events and returns an unsubscribe function
off(event, hook?)voidRemoves one or all hooks for an event
offTask(taskId, event?, hook?)voidRemoves task hooks
offAny(hook)voidRemoves a hook registered with onAny()
getLogger()ILoggerReturns the current logger

Duplicate task IDs are skipped and are not included in the StartedTasks.tasks returned by that start() call.

Example ​

ts
import { createMakoo, inject } from '@makoojs/core';
import { createVueAdapter } from '@makoojs/vue';
import Panel from './Panel.vue';

const makoo = createMakoo({ adapters: [createVueAdapter()] });
makoo.start([
  inject({ id: 'panel', injectAt: 'body', artifact: Panel })
]);

inject() ​

Creates a component task declaration. inject() does not register or start the task; pass its result to MakooRuntime.start().

Type ​

ts
function inject<TArtifact>(
	input: MakooInjectionInput<TArtifact>
): MakooInjectionDeclaration<TArtifact>;

Parameters ​

ts
type MakooInjectionInput<TArtifact = unknown> = {
	id?: string;
	injectAt: string;
	artifact: TArtifact;
	options?: ArtifactOptions;
};

When id is omitted, the runtime derives a task ID from the artifact and injectAt.

ArtifactOptions ​

FieldTypeDescription
alivebooleanWaits for the same selector and mounts again after the host target is removed
scope'local' | 'global'Observation scope used by alive mode
timeoutnumberMilliseconds to wait for the target element
onMakooListenerDeclarationEvent listener registered with the component
hooksLifecycleHookMapLifecycle hooks for this task

Returns ​

Returns MakooInjectionDeclaration<TArtifact>.

Example ​

ts
import { inject } from '@makoojs/core';
import Panel from './Panel.vue';

const declaration = inject({
	id: 'settings-panel',
	injectAt: '#settings',
	artifact: Panel,
	options: {
		alive: true,
		scope: 'global'
	}
});

listen() ​

Creates a listener task declaration. listen() does not register or start the task. Pass its result to MakooRuntime.start() or use it as ArtifactOptions.on.

Type ​

ts
function listen(input: MakooListenerInput): MakooListenerDeclaration;

Parameters ​

ts
type MakooListenerInput = {
	id?: string;
	listenAt: string;
	type: string;
	callback: EventListener;
	capture?: boolean;
	activitySignal?: () => ActivitySignalSource<boolean>;
};

When id is omitted, the runtime uses listener-${listenAt}-${type} as the task ID. capture defaults to false. Set it to true to run the listener during the DOM capture phase.

Returns ​

Returns MakooListenerDeclaration. It can be started as a standalone task or assigned to ArtifactOptions.on.

Example ​

ts
import { listen } from '@makoojs/core';

const onEscape: EventListener = (event) => {
  if (event instanceof KeyboardEvent && event.key === 'Escape') {
    console.log('Escape pressed');
  }
};

const declaration = listen({
	id: 'escape-close',
	listenAt: 'body',
	type: 'keydown',
	capture: true,
	callback: onEscape
});

Task types ​

ts
type MakooDefaults = {
	alive: boolean;
	scope: 'local' | 'global';
	timeout: number;
};

type MakooInjectionDeclaration<TArtifact = unknown> = {
	kind: 'component';
	id?: string;
	injectAt: string;
	artifact: TArtifact;
	options?: ArtifactOptions;
};

type MakooListenerDeclaration = {
	kind: 'listener';
	id?: string;
	listenAt: string;
	event: string;
	type: string;
	callback: EventListener;
	capture?: boolean;
	activitySignal?: () => ActivitySignalSource<boolean>;
};

type MakooTaskDeclaration<TArtifact = unknown> =
	| MakooInjectionDeclaration<TArtifact>
	| MakooListenerDeclaration;

StartedTasks ​

ts
type StartedComponentTask = {
	kind: 'component';
	taskId: string;
	enableAlive(): void;
	disableAlive(): void;
	reset(): void;
	destroy(): void;
};

type StartedListenerTask = {
	kind: 'listener';
	taskId: string;
	open(): boolean;
	close(): boolean;
	destroy(): void;
};

type StartedTask = StartedComponentTask | StartedListenerTask;

type StartedTasks = {
	tasks: StartedTask[];
	get(taskId: string): StartedTask | undefined;
	resetAll(): void;
	destroyAll(): void;
};

StartedTasks.resetAll() and StartedTasks.destroyAll() only affect the task batch returned by that start() call.

Listener handles return whether open() / close() successfully changed listening state. They return false if already in that state or the operation cannot complete. See Event Listeners.

InjectionConfig ​

ts
type InjectionConfig = MakooDefaults & {
	logger: ILogger;
	observer?: ObserverHub;
	hooks?: LifecycleHookMap;
};