-
Notifications
You must be signed in to change notification settings - Fork 9
Chore: Refactor away singleton pattern in runner #1188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Your cubic subscription is currently inactive. Please reactivate your subscription to receive AI reviews and use cubic. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Refactor runner’s singleton storage and recipeManager into injectable Runtime instances across the codebase.
- Components now use
useRuntimeinstead of a globalstorageimport for broadcasts. - CLI commands and
CharmManagerconstructors accept aRuntime, replacing direct calls to global storage, idle, and recipeManager. - Tests and the builder package are updated to initialize and use
Runtimemethods (getDoc,getImmutableCell,idle, etc.), and builder exports are adjusted (notablycreateCell).
Reviewed Changes
Copilot reviewed 71 out of 71 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/jumble/src/components/User.tsx | Switched from storage to useRuntime().storage |
| packages/jumble/src/components/NetworkInspector.tsx | Similar runtime-based storage broadcasts |
| packages/html/test/html-recipes.test.ts | Initialize and dispose Runtime, use runtime.documentMap |
| packages/cli/main.ts | Instantiate Runtime, pass into CharmManager |
| packages/cli/cast-recipe.ts | Same runtime injection for castRecipe |
| packages/charm/src/manager.ts | Refactored all storage and recipeManager usages to this.runtime |
| packages/builder/src/index.ts | Changed createCell export to a type-only export |
| packages/builder/src/built-in.ts | Removed createCell implementation in favor of declare |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking great 🥳
fef829b to
44878d2
Compare
66a9645 to
24daf8d
Compare
…EntityId, then call sync
…f hardcoding in test
2771efa to
ed8f99f
Compare
This is a significant refactoring aimed at removing singletons and introducing a centralized
Runtimeobject. This change enhances modularity, testability, and potentially concurrency by managing services like storage, scheduling, and recipe/module management through a dedicated runtime instance.Here's a detailed summary of the PR:
Core Change: Introduction of
Runtimeand Removal of SingletonsThe most fundamental change is the introduction of a
Runtimeclass. This class now encapsulates services that were previously accessed via global singletons (e.g.,storage,recipeManager,moduleMap,blobbyServerUrl,runtimefor script execution).Instantiation & Dependency Injection:
Runtimeinstance is created and often passed into constructors of classes likeCharmManager,BackgroundCharmService, or used directly where global singletons were previously called.storageUrl,blobbyServerUrl,signer,consoleHandler, anderrorHandlersare now passed into theRuntimeconstructor.Service Access:
runtime.storageinstead of the globalstorageobject.runtime.recipeManagerinstead of a globalrecipeManager.runtime.moduleRegistry.getDoc,getCell,getDocByEntityId) are often now methods onruntimeorruntime.documentMap.runtime.runSingle(for script execution) is replaced byruntime.harness.runSingle.Blobby Server URL:
setBlobbyServerUrlis removed. The URL is now configured viaRuntimeOptionswhen creating aRuntimeinstance.getBlobbyServerUrlis also removed, with the URL presumably accessed via theruntimeinstance where needed (e.g.,runtime.blobbyServerUrl).Impact on Packages:
@commontools/runner:storageobject is removed. Operations are now onruntime.storage.setBlobbyServerUrlandgetBlobbyServerUrlare removed.runtime(for script execution) is replaced byruntime.harness.getCell,getDoc,getCellFromEntityId,getCellFromLink,getImmutableCellare refactored to be methods onRuntimeorruntime.documentMap.idle,onConsole,onError,schedule,unschedule,run(for actions),addEventHandler,queueEvent) are now methods onruntime.scheduler.fetchData,llm,map,ifElse,streamData) are now registered with theruntime.moduleRegistryand receive theruntimeinstance, allowing them to use runtime services likeruntime.documentMap.getDocandruntime.idle.@commontools/charm:CharmManagerconstructor now accepts aRuntimeinstance.runtime.storage,runtime.recipeManager, andruntime.runner(forrunSynced,idle).getIframeRecipenow takescharmManagerto accessruntime.recipeManager.saveSpellalso takescharmManagerfor similar reasons.@commontools/builder:createCellfunction has been removed from direct export and is now exposed viadeclare global. The actual implementation is expected to be injected by the runtime environment (e.g.,createCellFactoryin the runner's harness).runtime.documentMap.getDocorruntime.getImmutableCell.@commontools/background-charm-service:cast-admin.tsandmain.tsinstantiate and useRuntimefor storage, blobby URL, and signer configuration.BackgroundCharmServiceconstructor and methods now take/useruntimeinstead of astorageinstance.worker.tsalso initializes and uses aRuntimeinstance for its operations, including passingconsoleHandleranderrorHandler. Error handling logic (isErrorWithContext) is slightly adjusted due to how errors might be propagated.@commontools/cli:cast-recipe.ts,charm_demo.ts, andmain.tsare updated to use the newRuntimefor setting up storage, signer, and blobby URL, and for getting cells/docs.@commontools/jumble(UI Package):RuntimeProvideranduseRuntimehook are introduced to provide theRuntimeinstance via React context.NetworkInspectorandUsernow useuseRuntimeto get the runtime ID foruseStorageBroadcast.CharmsManagerProvidernow usesuseRuntimeto pass the runtime to theCharmManagerconstructor.setupIframenow accepts aruntimeargument to use its scheduler and idle methods.main.tsx) initializes theRuntimeand wraps the application withRuntimeProvider.useSuggestionsand views likeSpellbookLaunchViewnow getcharmManager(which contains the runtime) to accessruntime.recipeManager.@commontools/toolshed(Server Package):index.ts) now initializes a globalRuntimeinstance instead of juststorage, configuring it with the signer and memory URL.runtimeinstance instead of the globalstorage. The helpergetAuthCellAndStorageis refactored togetAuthCelland uses the globalruntime.Other Notable Changes:
worker.tsfor how error context (likespace,charmId) is checked, as errors might be instances ofErrordirectly rather than a specific custom error type with those properties always present.charmIdfunction inCharmManagerto ensure the extracted ID is a string.Runtimeor use it for creating cells/docs and accessing services, reflecting the core refactoring.Summary:
This PR introduces a major architectural shift by deprecating global singleton access for core services (
storage,recipeManager, scriptruntime, etc.) and consolidating them under a newRuntimeclass. Instances ofRuntimeare created with necessary configurations (like API URLs and signers) and then injected into various managers and services (e.g.,CharmManager,BackgroundCharmService). This refactoring significantly improves the codebase's structure by promoting dependency injection, which generally leads to more modular, testable, and maintainable code. It also provides a clear context for runtime operations, potentially simplifying future extensions and concurrent execution scenarios. The changes touch nearly every package, indicating a broad and fundamental improvement to the system's architecture.