Skip to content

refactor: migrate ClientSessionSyncProcessor’s materializeEvent() to Effect #376

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

Open
wants to merge 3 commits into
base: igor/refactor-client-session-sync-processor-push-to-effect
Choose a base branch
from
Open
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
26 changes: 8 additions & 18 deletions packages/@livestore/common/src/sync/ClientSessionSyncProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
Stream,
Subscribable,
} from '@livestore/utils/effect'
import * as otel from '@opentelemetry/api'
import type * as otel from '@opentelemetry/api'

import { type ClientSession, SyncError, type UnexpectedError } from '../adapter-types.ts'
import * as EventSequenceNumber from '../schema/EventSequenceNumber.ts'
Expand Down Expand Up @@ -52,12 +52,12 @@ export const makeClientSessionSyncProcessor = ({
runtime: Runtime.Runtime<Scope.Scope>
materializeEvent: (
eventDecoded: LiveStoreEvent.AnyDecoded,
options: { otelContext: otel.Context; withChangeset: boolean; materializerHashLeader: Option.Option<number> },
) => {
options: { withChangeset: boolean; materializerHashLeader: Option.Option<number> },
) => Effect.Effect<{
writeTables: Set<string>
sessionChangeset: { _tag: 'sessionChangeset'; data: Uint8Array; debug: any } | { _tag: 'no-op' } | { _tag: 'unset' }
materializerHash: Option.Option<number>
}
}>
rollback: (changeset: Uint8Array) => void
refreshTables: (tables: Set<string>) => void
span: otel.Span
Expand Down Expand Up @@ -96,10 +96,7 @@ export const makeClientSessionSyncProcessor = ({
/** We're queuing push requests to reduce the number of messages sent to the leader by batching them */
const leaderPushQueue = BucketQueue.make<LiveStoreEvent.EncodedWithMeta>().pipe(Effect.runSync)

const push: ClientSessionSyncProcessor['push'] = Effect.fn('client-session-sync-processor:push')(function* (
batch,
{ otelContext },
) {
const push: ClientSessionSyncProcessor['push'] = Effect.fn('client-session-sync-processor:push')(function* (batch) {
// TODO validate batch

let baseEventSequenceNumber = syncStateRef.current.localHead
Expand Down Expand Up @@ -151,8 +148,7 @@ export const makeClientSessionSyncProcessor = ({
writeTables: newWriteTables,
sessionChangeset,
materializerHash,
} = materializeEvent(decodedEventDef, {
otelContext,
} = yield* materializeEvent(decodedEventDef, {
withChangeset: true,
materializerHashLeader: Option.none(),
})
Expand All @@ -176,8 +172,6 @@ export const makeClientSessionSyncProcessor = ({
rejectCount: 0,
}

const otelContext = otel.trace.setSpan(otel.context.active(), span)

const boot: ClientSessionSyncProcessor['boot'] = Effect.gen(function* () {
if (confirmUnsavedChanges && typeof window !== 'undefined' && typeof window.addEventListener === 'function') {
const onBeforeUnload = (event: BeforeUnloadEvent) => {
Expand Down Expand Up @@ -301,8 +295,7 @@ export const makeClientSessionSyncProcessor = ({
writeTables: newWriteTables,
sessionChangeset,
materializerHash,
} = materializeEvent(decodedEventDef, {
otelContext,
} = yield* materializeEvent(decodedEventDef, {
withChangeset: true,
materializerHashLeader: event.meta.materializerHashLeader,
})
Expand Down Expand Up @@ -359,10 +352,7 @@ export const makeClientSessionSyncProcessor = ({
}

export interface ClientSessionSyncProcessor {
push: (
batch: ReadonlyArray<LiveStoreEvent.PartialAnyDecoded>,
options: { otelContext: otel.Context },
) => Effect.Effect<
push: (batch: ReadonlyArray<LiveStoreEvent.PartialAnyDecoded>) => Effect.Effect<
{
writeTables: Set<string>
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ exports[`otel > QueryBuilder subscription - basic functionality 1`] = `
"attributes": {
"batchSize": 1,
},
"children": [
{
"_name": "client-session-sync-processor:materialize-event",
"children": [
{
"_name": "livestore.in-memory-db:execute",
"attributes": {
"sql.query": "INSERT INTO 'todos' (id, text, completed) VALUES (?, ?, ?)",
},
},
],
},
],
},
{
"_name": "@livestore/common:LeaderSyncProcessor:push",
Expand Down Expand Up @@ -55,14 +68,6 @@ exports[`otel > QueryBuilder subscription - basic functionality 1`] = `
],
"livestore.eventsCount": 1,
},
"children": [
{
"_name": "livestore.in-memory-db:execute",
"attributes": {
"sql.query": "INSERT INTO 'todos' (id, text, completed) VALUES (?, ?, ?)",
},
},
],
},
],
},
Expand Down Expand Up @@ -144,6 +149,19 @@ exports[`otel > QueryBuilder subscription - direct table subscription 1`] = `
"attributes": {
"batchSize": 1,
},
"children": [
{
"_name": "client-session-sync-processor:materialize-event",
"children": [
{
"_name": "livestore.in-memory-db:execute",
"attributes": {
"sql.query": "INSERT INTO 'todos' (id, text, completed) VALUES (?, ?, ?)",
},
},
],
},
],
},
{
"_name": "@livestore/common:LeaderSyncProcessor:push",
Expand Down Expand Up @@ -174,14 +192,6 @@ exports[`otel > QueryBuilder subscription - direct table subscription 1`] = `
],
"livestore.eventsCount": 1,
},
"children": [
{
"_name": "livestore.in-memory-db:execute",
"attributes": {
"sql.query": "INSERT INTO 'todos' (id, text, completed) VALUES (?, ?, ?)",
},
},
],
},
],
},
Expand Down Expand Up @@ -263,12 +273,38 @@ exports[`otel > QueryBuilder subscription - unsubscribe functionality 1`] = `
"attributes": {
"batchSize": 1,
},
"children": [
{
"_name": "client-session-sync-processor:materialize-event",
"children": [
{
"_name": "livestore.in-memory-db:execute",
"attributes": {
"sql.query": "INSERT INTO 'todos' (id, text, completed) VALUES (?, ?, ?)",
},
},
],
},
],
},
{
"_name": "client-session-sync-processor:push",
"attributes": {
"batchSize": 1,
},
"children": [
{
"_name": "client-session-sync-processor:materialize-event",
"children": [
{
"_name": "livestore.in-memory-db:execute",
"attributes": {
"sql.query": "INSERT INTO 'todos' (id, text, completed) VALUES (?, ?, ?)",
},
},
],
},
],
},
{
"_name": "@livestore/common:LeaderSyncProcessor:push",
Expand Down Expand Up @@ -306,14 +342,6 @@ exports[`otel > QueryBuilder subscription - unsubscribe functionality 1`] = `
],
"livestore.eventsCount": 1,
},
"children": [
{
"_name": "livestore.in-memory-db:execute",
"attributes": {
"sql.query": "INSERT INTO 'todos' (id, text, completed) VALUES (?, ?, ?)",
},
},
],
},
{
"_name": "LiveStore:commit",
Expand All @@ -323,14 +351,6 @@ exports[`otel > QueryBuilder subscription - unsubscribe functionality 1`] = `
],
"livestore.eventsCount": 1,
},
"children": [
{
"_name": "livestore.in-memory-db:execute",
"attributes": {
"sql.query": "INSERT INTO 'todos' (id, text, completed) VALUES (?, ?, ?)",
},
},
],
},
],
},
Expand Down Expand Up @@ -438,6 +458,19 @@ exports[`otel > otel 3`] = `
"attributes": {
"batchSize": 1,
},
"children": [
{
"_name": "client-session-sync-processor:materialize-event",
"children": [
{
"_name": "livestore.in-memory-db:execute",
"attributes": {
"sql.query": "INSERT INTO 'todos' (id, text, completed) VALUES (?, ?, ?)",
},
},
],
},
],
},
{
"_name": "@livestore/common:LeaderSyncProcessor:push",
Expand Down Expand Up @@ -468,14 +501,6 @@ exports[`otel > otel 3`] = `
],
"livestore.eventsCount": 1,
},
"children": [
{
"_name": "livestore.in-memory-db:execute",
"attributes": {
"sql.query": "INSERT INTO 'todos' (id, text, completed) VALUES (?, ?, ?)",
},
},
],
},
],
},
Expand Down Expand Up @@ -719,6 +744,19 @@ exports[`otel > with thunks 7`] = `
"attributes": {
"batchSize": 1,
},
"children": [
{
"_name": "client-session-sync-processor:materialize-event",
"children": [
{
"_name": "livestore.in-memory-db:execute",
"attributes": {
"sql.query": "INSERT INTO 'todos' (id, text, completed) VALUES (?, ?, ?)",
},
},
],
},
],
},
{
"_name": "@livestore/common:LeaderSyncProcessor:push",
Expand Down Expand Up @@ -749,14 +787,6 @@ exports[`otel > with thunks 7`] = `
],
"livestore.eventsCount": 1,
},
"children": [
{
"_name": "livestore.in-memory-db:execute",
"attributes": {
"sql.query": "INSERT INTO 'todos' (id, text, completed) VALUES (?, ?, ?)",
},
},
],
},
],
},
Expand Down Expand Up @@ -834,6 +864,19 @@ exports[`otel > with thunks with query builder and without labels 3`] = `
"attributes": {
"batchSize": 1,
},
"children": [
{
"_name": "client-session-sync-processor:materialize-event",
"children": [
{
"_name": "livestore.in-memory-db:execute",
"attributes": {
"sql.query": "INSERT INTO 'todos' (id, text, completed) VALUES (?, ?, ?)",
},
},
],
},
],
},
{
"_name": "@livestore/common:LeaderSyncProcessor:push",
Expand Down Expand Up @@ -864,14 +907,6 @@ exports[`otel > with thunks with query builder and without labels 3`] = `
],
"livestore.eventsCount": 1,
},
"children": [
{
"_name": "livestore.in-memory-db:execute",
"attributes": {
"sql.query": "INSERT INTO 'todos' (id, text, completed) VALUES (?, ?, ?)",
},
},
],
},
],
},
Expand Down
Loading
Loading