Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions packages/runner/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,7 @@ import { Runner } from "./runner.ts";
import { registerBuiltins } from "./builtins/index.ts";
import { StaticCache } from "@commontools/static";

export type {
IExtendedStorageTransaction,
IStorageManager,
IStorageProvider,
MemorySpace,
};
export type { IExtendedStorageTransaction, IStorageProvider, MemorySpace };

export type ErrorWithContext = Error & {
action: Action;
Expand Down Expand Up @@ -89,6 +84,13 @@ export interface RuntimeOptions {
navigateCallback?: NavigateCallback;
staticAssetServerUrl?: URL;
debug?: boolean;
/**
* When true, uses the StorageManager's native transaction API instead of the
* transaction shim. This allows for better integration with the underlying
* storage system's transaction capabilities.
* @default false
*/
useStorageManagerTransactions?: boolean;
}

export interface IRuntime {
Expand All @@ -104,6 +106,8 @@ export interface IRuntime {
readonly navigateCallback?: NavigateCallback;
readonly cfc: ContextualFlowControl;
readonly staticCache: StaticCache;
readonly useStorageManagerTransactions?: boolean;
readonly storageManager: IStorageManager;

idle(): Promise<void>;
dispose(): Promise<void>;
Expand Down Expand Up @@ -324,6 +328,8 @@ export class Runtime implements IRuntime {
readonly navigateCallback?: NavigateCallback;
readonly cfc: ContextualFlowControl;
readonly staticCache: StaticCache;
readonly storageManager: IStorageManager;
readonly useStorageManagerTransactions?: boolean;

constructor(options: RuntimeOptions) {
this.staticCache = options.staticAssetServerUrl
Expand All @@ -334,6 +340,8 @@ export class Runtime implements IRuntime {
// Create harness first (no dependencies on other services)
this.harness = new Engine(this);
this.id = options.storageManager.id;
this.useStorageManagerTransactions =
options.useStorageManagerTransactions ?? false;

// Create core services with dependencies injected
this.scheduler = new Scheduler(
Expand All @@ -346,6 +354,7 @@ export class Runtime implements IRuntime {
throw new Error("blobbyServerUrl is required");
}

this.storageManager = options.storageManager;
this.storage = new Storage(this, options.storageManager);

this.documentMap = new DocumentMap(this);
Expand Down Expand Up @@ -384,6 +393,7 @@ export class Runtime implements IRuntime {
documentMap: !!this.documentMap,
harness: !!this.harness,
runner: !!this.runner,
useStorageManagerTransactions: this.useStorageManagerTransactions,
});
}
}
Expand Down Expand Up @@ -424,8 +434,13 @@ export class Runtime implements IRuntime {
* multiple spaces but writing only to one space.
*/
edit(): IExtendedStorageTransaction {
// TODO(seefeld): Make this a flag to use the new transaction system instead
return new ExtendedStorageTransaction(new StorageTransaction(this));
// Use transaction API from storage manager if enabled, otherwise
// use a shim.
const transaction = this.useStorageManagerTransactions
? this.storageManager.edit()
: new StorageTransaction(this);

return new ExtendedStorageTransaction(transaction);
}

// Cell factory methods
Expand Down
6 changes: 1 addition & 5 deletions packages/runner/src/storage/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import type {
Claim,
IRemoteStorageProviderSettings,
IStorageManager,
IStorageManagerV2,
IStorageProvider,
IStorageProviderWithReplica,
IStorageSubscription,
Expand Down Expand Up @@ -1588,10 +1587,7 @@ export interface Options {
}

export class StorageManager
implements
IStorageManager,
IStorageManagerV2,
IStorageSubscriptionCapability {
implements IStorageManager, IStorageSubscriptionCapability {
address: URL;
as: Signer;
id: string;
Expand Down
20 changes: 10 additions & 10 deletions packages/runner/src/storage/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,16 @@ export interface StorageValue<T = any> {

export interface IStorageManager {
id: string;
/**
* @deprecated
*/
open(space: MemorySpace): IStorageProviderWithReplica;
/**
* Creates a storage transaction that can be used to read / write data into
* locally replicated memory spaces. Transaction allows reading from many
* multiple spaces but writing only to one space.
*/
edit(): IStorageTransaction;
}

export interface IRemoteStorageProviderSettings {
Expand Down Expand Up @@ -177,17 +186,8 @@ export interface IStorageProviderWithReplica extends IStorageProvider {
replica: ISpaceReplica;
}

export interface IStorageManagerV2 {
/**
* Creates a storage transaction that can be used to read / write data into
* locally replicated memory spaces. Transaction allows reading from many
* multiple spaces but writing only to one space.
*/
edit(): IStorageTransaction;
}

/**
* Extension of {@link IStorageManagerV2} which is supposed to merge into
* Extension of {@link IStorageManager} which is supposed to merge into
* {@link IStorageManager} in the future. It provides capability to subscribe
* to the storage notifications.
*/
Expand Down
6 changes: 5 additions & 1 deletion packages/runner/test/push-conflict.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as Memory from "@commontools/memory";
import * as Consumer from "@commontools/memory/consumer";
import { Provider } from "../src/storage/cache.ts";
import * as Subscription from "../src/storage/subscription.ts";
import { IStorageManager } from "../src/storage/interface.ts";
const signer = await Identity.fromPassphrase("test operator");

// In the transition to TX we had to remove the current push retry logic
Expand All @@ -18,14 +19,17 @@ describe.skip("Push conflict", () => {
let storage: IStorage;
let provider: Memory.Provider.Provider<Memory.Protocol>;
let consumer: Consumer.MemoryConsumer<Consumer.MemorySpace>;
const storageManager = {
const storageManager: IStorageManager = {
id: "some id",
open: (space: Consumer.MemorySpace) =>
Provider.open({
space,
subscription: Subscription.create(),
session: consumer,
}),
edit() {
throw new Error("Not implemented");
},
};

beforeEach(() => {
Expand Down