Built-in Services
Mom ships with service interfaces and fake implementations for common browser APIs. Each service follows the same pattern: a public interface with an IID, a default browser implementation, and a fake implementation with a controller for testing.
TimeService
Wraps setInterval, clearInterval, setTimeout, clearTimeout, and Date.now().
import { TimeServiceIID } from "@asimojs/mom/services";
store.onInit(async () => { const time = await store.context.fetch(TimeServiceIID); const id = time.setInterval(() => { /* tick */ }, 1000); // time.now gives current time in ms});Fake for testing
import { createFakeTimeService } from "@asimojs/mom/services";
const timeController = createFakeTimeService();// Register on a test containerconst context = createContainer({ extends: [rootContext], services: [timeController.factory],});
// In tests:timeController.moveTime(5000); // Advance 5 seconds, fire callbackstimeController.executeNextCallback(); // Execute the next pending callbacktimeController.numberOfActiveIntervals; // Check active intervalstimeController.numberOfActiveTimeouts; // Check active timeoutstimeController.reset(); // Clean upStorageService
Wraps localStorage and sessionStorage.
import { LocalStorageIID, SessionStorageIID } from "@asimojs/mom/services";
const storage = await store.context.fetch(LocalStorageIID);storage.setItem("key", "value");const value = storage.getItem("key");storage.removeItem("key");storage.clear();Fake for testing
import { createFakeStorageService } from "@asimojs/mom/services";
const storageController = createFakeStorageService();// Provides both LocalStorageIID and SessionStorageIIDconst context = createContainer({ extends: [rootContext], services: [storageController.factory],});
// In tests:storageController.reset(); // Clear all stored dataClipboardService
Wraps navigator.clipboard.writeText() and readText().
import { ClipboardServiceIID } from "@asimojs/mom/services";
const clipboard = await store.context.fetch(ClipboardServiceIID);await clipboard.writeText("copied!");const text = await clipboard.readText();Fake for testing
import { createFakeClipboardService } from "@asimojs/mom/services";
const clipboardController = createFakeClipboardService();
// Can simulate errors:clipboardController.throwErrorAfter(1); // Next operation throwsclipboardController.reset();UrlParamsService
Wraps URL search parameter access.
import { URLParamsServiceIID } from "@asimojs/mom/services";
const urlParams = await store.context.fetch(URLParamsServiceIID);const params = urlParams.getURLParams(); // Returns parsed URL paramsurlParams.setURLParams({ page: "2" }); // Update URL paramsFake for testing
import { createFakeURLParamsService } from "@asimojs/mom/services";
const urlController = createFakeURLParamsService();urlController.getURLSearch(); // Get raw search stringurlController.reset();Creating custom services
Follow the same three-part pattern:
- Interface + IID in a types file
- Real implementation using browser APIs
- Fake implementation with a controller for testing
import { asyncIID } from "@asimojs/asimo";
export interface MyService { doSomething(): Promise<string>;}
export const MyServiceIID = asyncIID<MyService>()("myapp.MyService");import { factory } from "@asimojs/asimo";import { registerRootService } from "@asimojs/mom";import { MyServiceIID } from "./service.types";
registerRootService(factory({ provide: [MyServiceIID], load: async () => ({ async doSomething() { return fetch("/api/data").then(r => r.text()); }, }),}));import { factory } from "@asimojs/asimo";import { MyServiceIID } from "./service.types";
export function createFakeMyService() { let result = "fake-result"; return { factory: factory({ provide: [MyServiceIID], load: async () => ({ async doSomething() { return result; }, }), }), setResult(value: string) { result = value; }, reset() { result = "fake-result"; }, };}