Skip to content
Closed
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
1 change: 1 addition & 0 deletions apps/desktop/src/app/DesktopConnectionCatalogStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ const migrateSavedEnvironmentRecords = Effect.fn(
return {
schemaVersion: 1,
targets,
disabledEnvironmentIds: [],
profiles,
credentials,
remoteDpopTokens: [],
Expand Down
2 changes: 2 additions & 0 deletions apps/mobile/src/connection/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ function migrateConnection(
label: connection.environmentLabel,
}),
}),
{ enabled: true },
);
}

Expand Down Expand Up @@ -84,6 +85,7 @@ function migrateConnection(
token: connection.bearerToken,
}),
}),
{ enabled: true },
);
}

Expand Down
38 changes: 35 additions & 3 deletions apps/mobile/src/connection/storage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
ConnectionPersistenceError,
ConnectionActivationStore,
ConnectionRegistrationStore,
ConnectionTargetStore,
registerConnectionInCatalog,
Expand All @@ -20,7 +21,13 @@ import * as Option from "effect/Option";
import * as CatalogStore from "./catalog-store";

function targetPersistenceError(
operation: "list-targets" | "register-connection" | "remove-connection",
operation:
| "list-targets"
| "register-connection"
| "remove-connection"
| "list-activation"
| "set-activation"
| "reconcile-activation",
error: ConnectionTransientError,
) {
return new ConnectionPersistenceError({
Expand All @@ -40,15 +47,39 @@ export const connectionStorageLayer = Layer.effectContext(
),
});
const registrationStore = ConnectionRegistrationStore.of({
register: (registration) =>
register: (registration, options) =>
catalog
.update((document) => registerConnectionInCatalog(document, registration))
.update((document) => registerConnectionInCatalog(document, registration, options))
.pipe(Effect.mapError((error) => targetPersistenceError("register-connection", error))),
remove: (target) =>
catalog
.update((document) => removeConnectionFromCatalog(document, target))
.pipe(Effect.mapError((error) => targetPersistenceError("remove-connection", error))),
});
const activationStore = ConnectionActivationStore.of({
listDisabled: catalog.read.pipe(
Effect.map((document) => new Set(document.disabledEnvironmentIds)),
Effect.mapError((error) => targetPersistenceError("list-activation", error)),
),
setEnabled: (environmentId, enabled) =>
catalog
.update((document) => ({
...document,
disabledEnvironmentIds: enabled
? document.disabledEnvironmentIds.filter((candidate) => candidate !== environmentId)
: [...new Set([...document.disabledEnvironmentIds, environmentId])],
}))
.pipe(Effect.mapError((error) => targetPersistenceError("set-activation", error))),
reconcile: (environmentIds) =>
catalog
.update((document) => ({
...document,
disabledEnvironmentIds: document.disabledEnvironmentIds.filter((environmentId) =>
environmentIds.has(environmentId),
),
}))
.pipe(Effect.mapError((error) => targetPersistenceError("reconcile-activation", error))),
});
const profileStore = ProfileStore.make({
get: (connectionId) =>
catalog.read.pipe(
Expand Down Expand Up @@ -130,6 +161,7 @@ export const connectionStorageLayer = Layer.effectContext(
});
return Context.make(ConnectionTargetStore, targetStore).pipe(
Context.add(ConnectionRegistrationStore, registrationStore),
Context.add(ConnectionActivationStore, activationStore),
Context.add(ProfileStore.ConnectionProfileStore, profileStore),
Context.add(CredentialStore.ConnectionCredentialStore, credentialStore),
Context.add(TokenStore.RemoteDpopAccessTokenStore, remoteTokenStore),
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/connection/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { makeCatalogBackend, makeCatalogStore } from "./storage";
const emptyCatalog = {
schemaVersion: 1,
targets: [],
disabledEnvironmentIds: [],
profiles: [],
credentials: [],
remoteDpopTokens: [],
Expand Down
33 changes: 31 additions & 2 deletions apps/web/src/connection/storage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
ConnectionCatalogDocument,
type ConnectionCatalogDocument as ConnectionCatalogDocumentType,
ConnectionActivationStore,
ConnectionPersistenceError,
ConnectionRegistrationStore,
ConnectionTargetStore,
Expand Down Expand Up @@ -99,6 +100,9 @@ function persistenceError(
| "list-targets"
| "register-connection"
| "remove-connection"
| "list-activation"
| "set-activation"
| "reconcile-activation"
| "load-shell"
| "save-shell"
| "load-thread"
Expand Down Expand Up @@ -370,16 +374,40 @@ export const connectionStorageLayer = Layer.effectContext(
);
const catalog = yield* makeCatalogStore(makeCatalogBackend(database));

const activationStore = ConnectionActivationStore.of({
listDisabled: catalog.read.pipe(
Effect.map((document) => new Set(document.disabledEnvironmentIds)),
Effect.mapError((cause) => persistenceError("list-activation", cause)),
),
setEnabled: (environmentId, enabled) =>
catalog
.update((document) => ({
...document,
disabledEnvironmentIds: enabled
? document.disabledEnvironmentIds.filter((candidate) => candidate !== environmentId)
: [...new Set([...document.disabledEnvironmentIds, environmentId])],
}))
.pipe(Effect.mapError((cause) => persistenceError("set-activation", cause))),
reconcile: (environmentIds) =>
catalog
.update((document) => ({
...document,
disabledEnvironmentIds: document.disabledEnvironmentIds.filter((environmentId) =>
environmentIds.has(environmentId),
),
}))
.pipe(Effect.mapError((cause) => persistenceError("reconcile-activation", cause))),
});
const targetStore = ConnectionTargetStore.of({
list: catalog.read.pipe(
Effect.map((document) => document.targets),
Effect.mapError((cause) => persistenceError("list-targets", cause)),
),
});
const registrationStore = ConnectionRegistrationStore.of({
register: (registration) =>
register: (registration, options) =>
catalog
.update((document) => registerConnectionInCatalog(document, registration))
.update((document) => registerConnectionInCatalog(document, registration, options))
.pipe(Effect.mapError((cause) => persistenceError("register-connection", cause))),
remove: (target) =>
catalog
Expand Down Expand Up @@ -664,6 +692,7 @@ export const connectionStorageLayer = Layer.effectContext(

return Context.make(ConnectionTargetStore, targetStore).pipe(
Context.add(ConnectionRegistrationStore, registrationStore),
Context.add(ConnectionActivationStore, activationStore),
Context.add(ProfileStore.ConnectionProfileStore, profileStore),
Context.add(CredentialStore.ConnectionCredentialStore, credentialStore),
Context.add(TokenStore.RemoteDpopAccessTokenStore, remoteTokenStore),
Expand Down
32 changes: 25 additions & 7 deletions packages/client-runtime/src/connection/layer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Schedule from "effect/Schedule";
import * as Stream from "effect/Stream";

import * as ConnectionResolver from "./resolver.ts";
Expand All @@ -19,9 +20,13 @@ const driverLayer = ConnectionDriver.layer.pipe(
Layer.provide(Layer.mergeAll(resolverLayer, RpcSession.layer)),
);

const registryLayer = EnvironmentRegistry.layer.pipe(Layer.provide(driverLayer));
const registryLayer = EnvironmentRegistry.layer.pipe(
Layer.provide(driverLayer),
);

const onboardingLayer = ConnectionOnboarding.layer.pipe(Layer.provide(registryLayer));
const onboardingLayer = ConnectionOnboarding.layer.pipe(
Layer.provide(registryLayer),
);

const connectionServicesLayer = Layer.mergeAll(
registryLayer,
Expand All @@ -32,13 +37,26 @@ const connectionServicesLayer = Layer.mergeAll(
const connectionStartupLayer = Layer.effectDiscard(
Effect.gen(function* () {
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
const platformSource = yield* PlatformConnectionSource.PlatformConnectionSource;
yield* registry.start;
yield* platformSource.registrations.pipe(
Stream.runForEach(registry.reconcilePlatform),
const platformSource =
yield* PlatformConnectionSource.PlatformConnectionSource;
yield* registry.start.pipe(
Effect.tapError((error) =>
Effect.logWarning(
"Could not initialize environment connections; retrying.",
{ error },
),
),
Effect.retry(Schedule.exponential("1 second")),
Effect.andThen(
platformSource.registrations.pipe(
Stream.runForEach(registry.reconcilePlatform),
),
),
Effect.forkScoped,
);
}).pipe(Effect.withSpan("clientRuntime.connection.application.start")),
);

export const layer = connectionStartupLayer.pipe(Layer.provideMerge(connectionServicesLayer));
export const layer = connectionStartupLayer.pipe(
Layer.provideMerge(connectionServicesLayer),
);
Loading
Loading