fetch, filesystem, or application preferences. Install only the services the surface needs. Use matching versions of the Swift host and npm adapter. The TS entry point is effect-appkit/services; import effect-appkit/runtime first in the bundle entry.import AppKit import EffectAppKit @MainActor final class NotesWindow { let navigation: AppKitNavigation let connectivity = AppKitConnectivity() let surface: EffectAppKitView init(script: String, restoredPath: String = "/") throws { navigation = AppKitNavigation(initialPath: restoredPath, scheme: "notes", host: "app") surface = try EffectAppKitView(script: script, services: [ .http(allowedOrigins: [URL(string: "https://notes.example.org")!]), .keyValueStore(namespace: "notes"), .visibility(), connectivity.service(), navigation.service() ]) } }
surface as the window's content view. Call surface.close() on permanent removal. SwiftUI accepts the same services: argument; retain the registration objects, since changing their identities remounts the surface. The host still owns its sandbox entitlements (including outgoing network connections), URL scheme registration, window selection, and menus.application(_:open:) or SwiftUI .onOpenURL, forward each URL to its window's navigation.open(url). Only the configured scheme and authority are accepted. notes://app/note/7?tab=body becomes /note/7?tab=body; an open received before mounting sets the initial entry. Unknown schemes are not launched for you.target to navigation and their action to #selector(AppKitNavigation.goBack(_:)) / #selector(AppKitNavigation.goForward(_:)). Use canGoBack and canGoForward for enablement. These actions traverse the same native history as upstream Router.back and Router.forward, including opaque Router state. navigation.currentURL can be saved with your own window restoration data; recreating with initialPath: restores a location, not an entire history stack. No app-global menu or delegate is replaced.load: callback if a route is allowed to leave your Router. Validate its argument before handing it to another app. Without one, full-load requests fail rather than opening arbitrary URLs.mount, not only to an individual query. A foreground edge (active and not hidden) or a satisfied NWPathMonitor edge lets upstream Query refetch stale watched entries. connectivity.networkEnabled = false represents an application offline preference. These signals do not promise server reachability or block a request: return a useful typed failure from your query.queries and their service layers for your app. QueryPersistence.layerKvs handles the upstream format, expiry, and flush on close. Mark eligible queries persist: true and include the signed-in account in prefix. The native KVS stores strings or encoded bytes in a bounded namespace, not an unrestricted settings API; use it for modest cached values, not secrets or large blobs.import "effect-appkit/runtime" import { QueryPersistence, Route, Router, UiSchema } from "@effect/ui" import { Effect, Exit, Layer, Schema, Scope } from "effect" import { type Bridge, component, mount } from "effect-appkit" import { connectivityLayer, keyValueStoreLayer, routerLayer, visibilityLayer } from "effect-appkit/services" const routes = Route.union({ Home: Route.make("/"), Note: Route.make("/note/:id", { params: { id: Schema.Number } }) }) const Page = Schema.Struct({ route: routes }) const Message = UiSchema.TaggedEnum({ Navigated: { route: routes } }) const Notes = component({ name: "Notes", props: Page, model: Page, message: Message }).make({ init: ({ props }) => props, update: { Navigated: (_, { route }) => ({ route }) }, subscriptions: ({ model }) => [Router.subscription(routes, model.route, route => Message.Navigated({ route }))], view: (h, model) => h.label.prop("value", routes.print(model.route)) }) const services = Layer.mergeAll(visibilityLayer, connectivityLayer, routerLayer(routes), QueryPersistence.layerKvs({ prefix: "notes/account-42", maxAge: "7 days" }).pipe(Layer.provide(keyValueStoreLayer))) const scope = Scope.makeUnsafe() const bridge = globalThis as typeof globalThis & Bridge & { __akUnmount: () => void } bridge.__akUnmount = () => Effect.runSync(Scope.close(scope, Exit.void)) Effect.runSync(Effect.gen(function*() { const context = yield* Layer.buildWithScope(services, scope) yield* Effect.gen(function*() { yield* mount(bridge, Notes, yield* Router.props(routes)) }).pipe(Effect.provideContext(context)) }).pipe(Scope.provide(scope)))
HTTPTransport.request is a cold Effect stream over an ephemeral URLSession. Each run emits the HTTP status and response headers, then binary data as it arrives. Use Stream.take, interrupt the fiber, or close the surface to cancel. The bounded 64-event queue fails on overflow rather than silently dropping bytes. Non-2xx statuses are HTTP responses, not transport failures. Cookies, URL credentials, persistent caching, and automatic redirect following are off. Only exact granted http(s) origins (scheme, host, effective port) are permitted; an origin grant covers all paths there. This is not a browser fetch polyfill.12345678910import { Effect, Stream } from "effect" import { HTTPTransport, httpLayer } from "effect-appkit/services" const loadSummary = Effect.gen(function*() { const http = yield* HTTPTransport return yield* http.request({ url: "https://notes.example.org/summary" }).pipe( Stream.mapEffect(event => event.kind === "data" ? Effect.succeed(event.bytes) : event.status === 200 ? Effect.succeed(new Uint8Array(0)) : Effect.fail(`HTTP ${event.status}`)), Stream.decodeText(), Stream.mkString) }).pipe(Effect.provide(httpLayer))
NativeAppService rather than exporting a JSContext or arbitrary native objects. install runs once per surface; call returns a JSON value or throws. listen emits .value, .end, or .failure and returns a cancellation closure. All run on the main actor. Cancel work before releasing resources in close. The bridge detaches callbacks before cancellation; late events cannot re-enter a closed VM. Interrupted streams and a failed script initialization also clean up.1234567891011import EffectAppKit import Foundation @MainActor func localeService() -> NativeAppService { NativeAppService("locale") { .init(call: { method, _ in guard method == "identifier" else { throw NativeServiceError("Unknown locale operation") } return Locale.current.identifier }) } }
1234import { Effect, Schema } from "effect" import { nativeCall } from "effect-appkit/services" const locale = nativeCall("locale", "identifier").pipe(Effect.flatMap(Schema.decodeUnknownEffect(Schema.String)))
nativeStream(name, method, input) returns a cold stream of unknown JSON values. Validate them with a Schema, as for nativeCall. The runnable src/services-fixture.ts and Tests/EffectAppKitTests/ServiceTests.swift connect a persisted query to an actual loopback HTTP server, restore a cached success in a fresh process, and drive a button, native URL delivery and Back/Forward menu actions in a window. Run bun run bundle:services && swift test --filter ServiceTests in a logged-in macOS session.