Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge committed Oct 6, 2024
1 parent fbd0193 commit b4e706f
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 251 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,3 @@ yalc.lock

## Documentation Report
scripts/docs-report.md

scripts/sdx1652-repro.ts
2 changes: 1 addition & 1 deletion dev/test-studio/sanity.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ const defaultWorkspace = {
icon: SanityMonogram,
// eslint-disable-next-line camelcase
__internal_serverDocumentActions: {
//enabled: true,
enabled: true,
},
scheduledPublishing: {
enabled: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,6 @@ export const createObservableBufferedDocument = (listenerEvent$: Observable<List
}

const currentBufferedDocument$ = listenerEvent$.pipe(
// filter((e, i) => {
// const drop = i < 10 || Math.random() < 0.9
// if (drop) {
// console.log('DROP EVENT!!!', e)
// }
// return drop
// }),
scan((bufferedDocument: BufferedDocument | null, listenerEvent) => {
// consider renaming 'snapshot' to initial/welcome
if (listenerEvent.type === 'snapshot') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ interface Snapshots {
export interface InitialSnapshotEvent {
type: 'snapshot'
documentId: string
initialRevision: string | undefined
document: SanityDocument | null
}

Expand Down Expand Up @@ -205,6 +204,5 @@ function createSnapshotEvent(
type: 'snapshot',
documentId,
document,
initialRevision: document?._rev,
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,38 +1,8 @@
import {describe, expect, it} from '@jest/globals'

import {discardChainTo, partitionChainableAndOrphaned, toOrderedChains} from '../eventChainUtils'
import {discardChainTo, toOrderedChains} from '../eventChainUtils'
import {mutationEvent} from './test-utils'

describe(partitionChainableAndOrphaned.name, () => {
it('returns an array of chainable and orphaned events', () => {
const events = [
mutationEvent({previousRev: 'a', resultRev: 'b', mutations: []}), // <-- orphaned
mutationEvent({previousRev: 'b', resultRev: 'c', mutations: []}),
mutationEvent({previousRev: 'c', resultRev: 'd', mutations: []}),
mutationEvent({previousRev: 'd', resultRev: 'e', mutations: []}),
mutationEvent({previousRev: 'e', resultRev: 'f', mutations: []}),
]
const [chainable, orphaned] = partitionChainableAndOrphaned(events)
expect(chainable.map((ev) => ev.resultRev)).toEqual(['c', 'd', 'e', 'f'])
expect(orphaned.map((ev) => ev.resultRev)).toEqual(['b'])
})

it('is not order sensitive', () => {
const events = [
mutationEvent({previousRev: 'e', resultRev: 'f', mutations: []}),
mutationEvent({previousRev: 'b', resultRev: 'c', mutations: []}),
mutationEvent({previousRev: 'x', resultRev: 'y', mutations: []}), // <-- orphaned
mutationEvent({previousRev: 'c', resultRev: 'd', mutations: []}),
mutationEvent({previousRev: 'a', resultRev: 'b', mutations: []}), // <-- orphaned
mutationEvent({previousRev: 'd', resultRev: 'e', mutations: []}),
]
const [chainable, orphaned] = partitionChainableAndOrphaned(events)
// Note, it's still in the order received
expect(chainable.map((ev) => ev.resultRev)).toEqual(['f', 'c', 'd', 'e'])
expect(orphaned.map((ev) => ev.resultRev)).toEqual(['y', 'b'])
})
})

describe(toOrderedChains.name, () => {
it('returns a list of chains', () => {
const events = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ test("it accumulates events that doesn't apply in a chain starting at the curren
{
type: 'snapshot',
documentId: 'test',
initialRevision: 'one',
document: {
_rev: 'one',
_id: 'test',
Expand Down Expand Up @@ -84,7 +83,7 @@ test("it accumulates events that doesn't apply in a chain starting at the curren
event.type === 'mutation'
? event.previousRev
: event.type === 'snapshot'
? event.initialRevision
? event.document?._rev
: null,
]
}),
Expand All @@ -102,7 +101,6 @@ test('it ignores events already applied to the current head revision', async ()
{
type: 'snapshot',
documentId: 'test',
initialRevision: 'one',
document: {
_rev: 'one',
_id: 'test',
Expand Down Expand Up @@ -144,7 +142,6 @@ test('it throws an MaxBufferExceededError if the buffer exceeds `maxBuffer`', as
{
type: 'snapshot',
documentId: 'test',
initialRevision: 'one',
document: {
_rev: 'one',
_id: 'test',
Expand Down Expand Up @@ -196,7 +193,6 @@ test('it throws an OutOfSyncError if the buffer exceeds `maxBuffer`', async () =
{
type: 'snapshot',
documentId: 'test',
initialRevision: 'one',
document: {
_rev: 'one',
_id: 'test',
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {partition} from 'lodash'

import {type MutationEvent} from '../types'

export function discardChainTo(chain: MutationEvent[], revision: string | undefined) {
Expand Down Expand Up @@ -41,11 +39,3 @@ export function toOrderedChains<T extends {previousRev: string; resultRev: strin
return sortedList
})
}

export function partitionChainableAndOrphaned(
events: MutationEvent[],
): [chainable: MutationEvent[], orphaned: MutationEvent[]] {
return partition(events, (event) => {
return events.some((other) => event.previousRev === other.resultRev)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import {discardChainTo, toOrderedChains} from './eventChainUtils'

interface ListenerSequenceState {
/**
* Base revision will be undefined if document doesn't exist
* base is `undefined` until the snapshot event is received
* Tracks the latest revision from the server that can be applied locally
* Once we receive a mutation event that has a `previousRev` that equals `base.revision`
* we will move `base.revision` to the event's `resultRev`
* `base.revision` will be undefined if document doesn't exist.
* `base` is `undefined` until the snapshot event is received
*/
base: {revision: string | undefined} | undefined
/**
Expand All @@ -24,31 +27,6 @@ interface ListenerSequenceState {
buffer: MutationEvent[]
}

export class OutOfSyncError extends Error {
/**
* Attach state to the error for debugging/reporting
*/
state: ListenerSequenceState
constructor(message: string, state: ListenerSequenceState) {
super(message)
this.name = 'OutOfSyncError'
this.state = state
}
}

export class DeadlineExceededError extends OutOfSyncError {
constructor(message: string, state: ListenerSequenceState) {
super(message, state)
this.name = 'DeadlineExceededError'
}
}
export class MaxBufferExceededError extends OutOfSyncError {
constructor(message: string, state: ListenerSequenceState) {
super(message, state)
this.name = 'MaxBufferExceededError'
}
}

const DEFAULT_MAX_BUFFER_SIZE = 20
const DEFAULT_DEADLINE_MS = 30000

Expand Down Expand Up @@ -81,7 +59,7 @@ export function sequentializeListenerEvents(options?: {
if (event.type === 'snapshot') {
// When receiving a new snapshot, we can safely discard the current orphaned and chainable buffers
return {
base: {revision: event.initialRevision},
base: {revision: event.document?._rev},
buffer: EMPTY_ARRAY,
emitEvents: [event],
}
Expand Down Expand Up @@ -175,3 +153,28 @@ export function sequentializeListenerEvents(options?: {
)
}
}

export class OutOfSyncError extends Error {
/**
* Attach state to the error for debugging/reporting
*/
state: ListenerSequenceState
constructor(message: string, state: ListenerSequenceState) {
super(message)
this.name = 'OutOfSyncError'
this.state = state
}
}

export class DeadlineExceededError extends OutOfSyncError {
constructor(message: string, state: ListenerSequenceState) {
super(message, state)
this.name = 'DeadlineExceededError'
}
}
export class MaxBufferExceededError extends OutOfSyncError {
constructor(message: string, state: ListenerSequenceState) {
super(message, state)
this.name = 'MaxBufferExceededError'
}
}

0 comments on commit b4e706f

Please sign in to comment.