Skip to content

Latest commit

 

History

History

angular

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

README.md

@coexist/angular

Angular bindings for Coexist: an environment provider and inject* helpers that expose a Coexist app (or a worker-hosted app) as Angular signals.

Installation

pnpm add @coexist/angular @coexist/core

Ships as ESM only: your project needs "type": "module" (or .mjs/.mts) and Node.js >=22.12.0 or a modern bundler.

@coexist/core is a peer dependency: this package shares the app runtime you install, rather than bundling a second copy of it.

Peer dependency: @angular/core >=17 <23.

Quick start

Register the app with provideCoexist(app) during bootstrap, then inject the module and signals inside components.

import { Component } from "@angular/core";
import { bootstrapApplication } from "@angular/platform-browser";
import { injectModule, injectSignal, provideCoexist } from "@coexist/angular";

@Component({
  selector: "counter-view",
  template: `<button (click)="counter.increase()">{{ count() }}</button>`,
})
class CounterView {
  readonly counter = injectModule(Counter);
  readonly count = injectSignal(Counter, (module) => module.count);
}

bootstrapApplication(CounterView, {
  providers: [provideCoexist(app)],
});

injectSignal returns a read-only Angular Signal<T> — call it (count()) in the template.

API

Function Returns Description
provideCoexist(app) EnvironmentProviders Register the app for DI.
injectCoexistApp() App Inject the app.
injectModule(token) T The bound module facade. No subscription.
injectSignal(fn, opts?) Signal<T> Signal for fn(app).
injectSignal(token, fn, opts?) Signal<TValue> Signal for fn(module, app).

injectModule resolves the module and nothing else — it creates no signal, and a facade read is not tracked by Angular's reactivity. Reading state off it in a template (injectModule(Counter).count) renders correctly once and then goes stale, silently. Call actions through the facade; render state through injectSignal.

injectSignal accepts { equals } (defaults to Object.is) and unsubscribes automatically through DestroyRef. It must run in an injection context.

Worker-hosted state

injectWorkerSignal reads the client snapshot synchronously, so it throws until the host's first snapshot arrives. Await client.ready before bootstrapping components that use it; injectWorkerModule is safe at any time.

import { Component } from "@angular/core";
import { bootstrapApplication } from "@angular/platform-browser";
import { injectWorkerModule, injectWorkerSignal, provideWorkerClient } from "@coexist/angular";

type CounterState = { readonly counter: { readonly count: number } };

@Component({
  selector: "counter-view",
  template: `<button (click)="counter.increase()">{{ count() }}</button>`,
})
class WorkerCounterView {
  readonly counter = injectWorkerModule<Counter>("counter");
  readonly count = injectWorkerSignal((state) => (state as CounterState).counter.count);
}

bootstrapApplication(WorkerCounterView, {
  providers: [provideWorkerClient(client)],
});
  • provideWorkerClient(client) → EnvironmentProviders.
  • injectWorkerClient() → the WorkerClient.
  • injectWorkerModule<T>(name) → an AsyncMethodProxy<T>.
  • injectWorkerSignal(fn, opts?) → a Signal<T> of worker state.

Exports

provideCoexist, provideWorkerClient, the COEXIST_APP / COEXIST_WORKER_CLIENT injection tokens, injectCoexistApp, injectModule, injectSignal, injectWorkerClient, injectWorkerModule, injectWorkerSignal, and the InjectSignalOptions, AppSelector, ModuleSelector types.

License

MIT © Coaction