Migration Guide - v0.8.0
Migration Guide - v0.8.0
Section titled “Migration Guide - v0.8.0”This guide will help you migrate from v0.7.x to v0.8.0. Good news: there are no breaking changes! All old APIs continue to work but are now deprecated in favor of more semantically accurate names.
Overview of Changes
Section titled “Overview of Changes”v0.8.0 introduces:
- Better API naming - “items” instead of “containers” for better clarity
- Synchronous methods - New
getSync()andgetItemsSync()for SSR support - New events - Item-focused events with clearer naming
- React hook improvements - Better SSR support and clearer naming
Core Library (iti) Migration
Section titled “Core Library (iti) Migration”Method Renaming
Section titled “Method Renaming”The following methods have been renamed. Old names still work but are deprecated:
import { createContainer } from "iti"
const container = createContainer().add({ serviceA: () => new ServiceA(), serviceB: () => new ServiceB(),})
// Get multiple itemsconst items = await container.getContainerSet(["serviceA", "serviceB"])
// Subscribe to changescontainer.subscribeToContainer("serviceA", (err, service) => { console.log("Service A updated")})
container.subscribeToContainerSet(["serviceA", "serviceB"], (err, items) => { console.log("Services updated")})import { createContainer } from "iti"
const container = createContainer().add({ serviceA: () => new ServiceA(), serviceB: () => new ServiceB(),})
// Get multiple items (new name)const items = await container.getItems(["serviceA", "serviceB"])
// Subscribe to changes (new names)container.subscribeToItem("serviceA", (err, service) => { console.log("Service A updated")})
container.subscribeToItems(["serviceA", "serviceB"], (err, items) => { console.log("Services updated")})Quick Reference
Section titled “Quick Reference”| Old API (deprecated) | New API (recommended) |
|---|---|
getContainerSet() | getItems() |
subscribeToContainerSet() | subscribeToItems() |
subscribeToContainer() | subscribeToItem() |
New Synchronous Methods ✨
Section titled “New Synchronous Methods ✨”v0.8.0 introduces synchronous methods for better SSR support:
// Get a single item synchronously (returns undefined if not cached)const service = container.getSync("myService")
// Get multiple items synchronouslyconst items = container.getItemsSync(["serviceA", "serviceB"])// Returns items object if all are cached, otherwise returns a PromiseWhen to use:
- Server-Side Rendering (SSR)
- React Server Components
- When you know items are already cached
- When you need synchronous access to avoid hydration mismatches
Event Naming Updates
Section titled “Event Naming Updates”New item-focused events provide clearer semantics:
container.on("containerCreated", ({ key, newItem }) => {})container.on("containerUpdated", ({ key, newItem }) => {})container.on("containerUpserted", ({ key, newItem }) => {})container.on("containerDeleted", ({ key }) => {})container.on("containerDisposed", ({ key }) => {})// New item-focused events (recommended)container.on("itemCreated", ({ key, newItem }) => {})container.on("itemUpdated", ({ key, newItem }) => {})container.on("itemUpserted", ({ key, newItem }) => {})container.on("itemDeleted", ({ key }) => {})container.on("itemDisposed", ({ key }) => {})
// Old events still work but are deprecatedReact Integration (iti-react) Migration
Section titled “React Integration (iti-react) Migration”Hook Generator Naming
Section titled “Hook Generator Naming”The main hook generator function remains the same, but the returned hook names have changed:
import { getContainerSetHooks } from "iti-react"
const MyContext = React.createContext<MyContainer>({})
const { useContainer, useContainerSet } = getContainerSetHooks(MyContext)import { getContainerHooks } from "iti-react"
const MyContext = React.createContext<MyContainer>({})
const { useItem, useItems } = getContainerHooks(MyContext)Quick Reference
Section titled “Quick Reference”| Old API (deprecated) | New API (recommended) |
|---|---|
getContainerSetHooks() | getContainerHooks() |
useContainer() | useItem() |
useContainerSet() | useItems() |
generateEnsureContainerSet() | generateEnsureItems() |
Hook Usage in Components
Section titled “Hook Usage in Components”import { useContainer, useContainerSet } from "./_containers/hooks"
function MyComponent() { // Single item const [auth, authErr] = useContainer().auth
// Multiple items const [items, itemsErr] = useContainerSet(["auth", "config"])
if (!items || itemsErr) return <div>Loading...</div>
return <div>Hello {items.auth.user.name}</div>}import { useItem, useItems } from "./_containers/hooks"
function MyComponent() { // Single item const [auth, authErr] = useItem().auth
// Multiple items (now with SSR support!) const [items, itemsErr] = useItems(["auth", "config"])
if (!items || itemsErr) return <div>Loading...</div>
return <div>Hello {items.auth.user.name}</div>}Ensure Wrapper Updates
Section titled “Ensure Wrapper Updates”import { generateEnsureContainerSet } from "iti-react"import { useContainerSet } from "./hooks"
const { EnsureWrapper, contextHook } = generateEnsureContainerSet(() => useContainerSet(["auth", "config"]))
export const EnsureAuth = EnsureWrapperexport const useAuthContext = contextHookimport { generateEnsureItems } from "iti-react"import { useItems } from "./hooks"
const { EnsureWrapper, contextHook } = generateEnsureItems(() => useItems(["auth", "config"]))
export const EnsureAuth = EnsureWrapperexport const useAuthContext = contextHookComplete Migration Example
Section titled “Complete Migration Example”Here’s a complete example showing all the changes together:
Core Container Setup
Section titled “Core Container Setup”import { createContainer } from "iti"
export const createAppContainer = () => createContainer().add({ config: () => ({ apiUrl: process.env.API_URL, }), authService: async () => { const res = await fetch("/api/auth") return res.json() }, apiClient: (ctx) => new ApiClient(ctx.config.apiUrl), })
export type AppContainer = ReturnType<typeof createAppContainer>React Hooks Setup
Section titled “React Hooks Setup”import React from "react"import { getContainerHooks } from "iti-react" // ✨ Updatedimport { AppContainer } from "./app-container"
export const AppContext = React.createContext<AppContainer | null>(null)
const hooks = getContainerHooks(AppContext)export const useItem = hooks.useItem // ✨ Updatedexport const useItems = hooks.useItems // ✨ UpdatedComponent Usage
Section titled “Component Usage”import { useItem, useItems } from "../_containers/hooks"
export function Profile() { // Single item const [auth, authErr] = useItem().authService
// Multiple items const [deps, depsErr] = useItems(["authService", "config"])
if (authErr || depsErr) return <div>Error loading</div> if (!auth || !deps) return <div>Loading...</div>
return <div>Hello {auth.user.name}!</div>}Automated Migration
Section titled “Automated Migration”You can use these find-and-replace commands to help with migration:
# Core libraryfind . -type f -name "*.ts" -o -name "*.tsx" | xargs sed -i '' 's/getContainerSet(/getItems(/g'find . -type f -name "*.ts" -o -name "*.tsx" | xargs sed -i '' 's/subscribeToContainer(/subscribeToItem(/g'find . -type f -name "*.ts" -o -name "*.tsx" | xargs sed -i '' 's/subscribeToContainerSet(/subscribeToItems(/g'
# React hooksfind . -type f -name "*.ts" -o -name "*.tsx" | xargs sed -i '' 's/getContainerSetHooks/getContainerHooks/g'find . -type f -name "*.ts" -o -name "*.tsx" | xargs sed -i '' 's/generateEnsureContainerSet/generateEnsureItems/g'find . -type f -name "*.ts" -o -name "*.tsx" | xargs sed -i '' 's/useContainerSet/useItems/g'find . -type f -name "*.ts" -o -name "*.tsx" | xargs sed -i '' 's/useContainer/useItem/g'
# Eventsfind . -type f -name "*.ts" -o -name "*.tsx" | xargs sed -i '' 's/"containerCreated"/"itemCreated"/g'find . -type f -name "*.ts" -o -name "*.tsx" | xargs sed -i '' 's/"containerUpdated"/"itemUpdated"/g'find . -type f -name "*.ts" -o -name "*.tsx" | xargs sed -i '' 's/"containerUpserted"/"itemUpserted"/g'find . -type f -name "*.ts" -o -name "*.tsx" | xargs sed -i '' 's/"containerDeleted"/"itemDeleted"/g'find . -type f -name "*.ts" -o -name "*.tsx" | xargs sed -i '' 's/"containerDisposed"/"itemDisposed"/g'Deprecation Timeline
Section titled “Deprecation Timeline”- v0.8.0 (Current) - Old API deprecated but fully functional
- v0.9.0 (Future) - Deprecation warnings may become more prominent
- v1.0.0 (Future) - Old API may be removed (with advance notice)