04 · Core Kernel
Providers & Refs
Reach for provide() whenever a dependency has one clear default implementation that callers are allowed to import directly—like a config helper, an MVC repository, or an application service. It returns a Ref<T> that knows how to build itself on demand and caches its result inside the scope that owns it.
1let created = 02export const configRef = provide(() => ({ id: ++created }))34export const cachingApp = bootstrapApp(5 ({ inject }) => {6 const first = inject(configRef)7 const second = inject(configRef)8 console.log(first === second, created) // true 19 },10 { providers: [configRef] },11)1213await cachingApp.start()
Self-Installing Refs vs Root Registration
In a lot of DI libraries, forgetting to list a class in a central container throws a runtime error. A Ref<T> from provide() skips that boilerplate: if the active scope (and its parents) haven't bound configRef yet, the first inject(configRef) installs its default factory right there, runs it once, and caches the instance (first === second is true, and created stays 1).
That convenience comes with one important nuance: a Ref is scoped, not a global singleton.
- Two separate
bootstrapApp()calls never share instances. - If two sibling
{ providers }subtrees each inject an unregisteredReffor the first time, each subtree self-installs and caches its own separate copy.
When a Ref holds shared state or a scarce resource—like a database connection pool, an in-memory cache, or configRef above—list it explicitly in bootstrapApp(..., { providers: [configRef] }). Installing it at the root scope still keeps creation lazy (the factory won't run until something calls inject()), while guaranteeing the entire app shares that single instance.
Checking Handles with isProvideRef()
If you're writing thin framework glue or a test helper that inspects unknown values at runtime, use isProvideRef(value) to narrow a value to Ref<unknown>.
isProvideRef() returns true only for a real Ref created by provide(). It returns false for token() keys, provideFor() bindings, bundleProviders() bundles, plain values, and shallow clones like { ...sampleRef }.
1export const sampleRef = provide(() => 'ok')2export const sampleToken = token<string>('Sample')3export const sampleBinding = provideFor(sampleToken, () => 'ok')4export const sampleBundle = bundleProviders(sampleRef, sampleBinding)56console.log(isProvideRef(sampleRef)) // true7console.log(isProvideRef(sampleToken)) // false8console.log(isProvideRef(sampleBinding)) // false9console.log(isProvideRef(sampleBundle)) // false10console.log(isProvideRef({ ...sampleRef })) // false (copied object is rejected)
Installation Rules & Edge Cases
Whenever the kernel installs a providers list—whether at bootstrapApp() or inside a nested { providers } option—four predictable rules apply:
- Snapshotting on creation: Arrays and bundles passed to
provide(),provideFor(),bundleProviders(), orbootstrapApp()are shallow-copied immediately. Mutating your original array later won't alter an already-created ref, binding, or bundle. - Source-order flattening: Nested arrays and
ProviderBundles are flattened left-to-right in exact source order. - Last-write-wins per scope: Installing the same key twice in one scope silently replaces the earlier binding—including any nested
{ providers }attached to that earlier binding. That's what lets integration tests list[infraProviders, provideFor(repoToken, fakeRepo)]and cleanly override one adapter. - Validation timing: Root
providerspassed tobootstrapApp()are validated up front; a bogus value throwsInvalid provider input receivedbefore anApphandle even exists. Nested{ providers }on a ref or binding are validated lazily the first time that subtree is resolved, throwingInvalid provider input receivedfrom thatinject()call.