Skip to content

Commit

Permalink
chore: test rxjs scheduler instead of worker
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Jul 16, 2024
1 parent 3299a28 commit 85d3b2c
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
"@sanity/ui@2": "$@sanity/ui",
"@typescript-eslint/eslint-plugin": "$@typescript-eslint/eslint-plugin",
"@typescript-eslint/parser": "$@typescript-eslint/parser",
"framer-motion": "11.3.0",
"framer-motion": "11.3.4",
"styled-components": "npm:[email protected]"
}
},
Expand Down
2 changes: 2 additions & 0 deletions packages/sanity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@
"rxjs": "^7.8.0",
"rxjs-exhaustmap-with-trailing": "^2.1.1",
"sanity-diff-patch": "^3.0.2",
"scheduler": "^0.23.2",
"scroll-into-view-if-needed": "^3.0.3",
"semver": "^7.3.5",
"shallow-equals": "^1.0.0",
Expand Down Expand Up @@ -285,6 +286,7 @@
"@types/refractor": "^3.0.0",
"@types/resolve-from": "^4.0.0",
"@types/rimraf": "^3.0.2",
"@types/scheduler": "^0.23.0",
"@types/semver": "^6.2.3",
"@types/tar-fs": "^2.0.1",
"@vvo/tzdb": "6.137.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,26 @@ export function PrimitiveField(props: {

// setOptimisticValue((state) => ({...state, next: inputValue}))
onChange(PatchEvent.from(hasEmptyValue ? unset() : set(inputValue)).prefixAll(member.name))
setLocalValue(hasEmptyValue ? '' : event.currentTarget.value)
// setLocalValue(hasEmptyValue ? '' : event.currentTarget.value)
},
[member.name, member.field.schemaType, onChange],
)

// Reset local value after a timeout
useEffect(() => {
if (member.field.value === undefined || localValue === undefined || member.field.focused) {
return undefined
}
const callback = requestIdleCallback(
() => {
setLocalValue(undefined)
},
{timeout: 5000},
)
return () => {
cancelIdleCallback(callback)
}
}, [localValue, member.field.focused, member.field.value])
// useEffect(() => {
// if (member.field.value === undefined || localValue === undefined || member.field.focused) {
// return undefined
// }
// const callback = requestIdleCallback(
// () => {
// setLocalValue(undefined)
// },
// {timeout: 5000},
// )
// return () => {
// cancelIdleCallback(callback)
// }
// }, [localValue, member.field.focused, member.field.value])

const validationError =
useMemo(
Expand All @@ -127,7 +127,8 @@ export function PrimitiveField(props: {
'onChange': handleNativeChange,
'value': resolveNativeNumberInputValue(
member.field.schemaType,
localValue ?? member.field.value,
member.field.value,
// localValue ?? member.field.value,
localValue,
),
'readOnly': Boolean(member.field.readOnly),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import {
import {getPairListener, type ListenerEvent} from '../getPairListener'
import {type IdPair, type PendingMutationsEvent, type ReconnectEvent} from '../types'

// import {
// unstable_NormalPriority as NormalPriority,
// unstable_scheduleCallback as scheduleCallback,
// } from 'scheduler'

const isMutationEventForDocId =
(id: string) =>
(
Expand Down Expand Up @@ -188,6 +193,8 @@ export function checkoutPair(

const listenerEventsConnector = new Subject<ListenerEvent>()
const listenerEvents$ = getPairListener(client, idPair).pipe(
// @TODO figure out how to schedule updates the same way react does for input change: scheduleCallback(NormalPriority, () => schedule)
// observeOn(asapScheduler, 10000),
share({connector: () => listenerEventsConnector}),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {publishReplay, refCount} from 'rxjs/operators'

import {type IdPair} from '../types'
import {memoize} from '../utils/createMemoizer'
import {checkoutPair, type Pair} from './checkoutPairWithWorker'
// Test the worker implementation
// import {checkoutPair, type Pair} from './checkoutPairWithWorker'
import {checkoutPair, type Pair} from './checkoutPair'
import {memoizeKeyGen} from './memoizeKeyGen'

export const memoizedPair: (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {type SanityClient} from '@sanity/client'
import {type SanityDocument} from '@sanity/types'
import {type Observable} from 'rxjs'
import {filter, map, publishReplay, refCount} from 'rxjs/operators'
import {asapScheduler, type Observable} from 'rxjs'
import {filter, map, observeOn, publishReplay, refCount} from 'rxjs/operators'

import {type BufferedDocumentEvent, type MutationPayload, type SnapshotEvent} from '../buffered-doc'
import {type IdPair, type PendingMutationsEvent, type ReconnectEvent} from '../types'
Expand All @@ -20,6 +20,7 @@ function isSnapshotEvent(event: BufferedDocumentEvent | ReconnectEvent): event i
function withSnapshots(pair: DocumentVersion): DocumentVersionSnapshots {
return {
snapshots$: pair.events.pipe(
observeOn(asapScheduler),
filter(isSnapshotEvent),
map((event) => event.document),
publishReplay(1),
Expand Down Expand Up @@ -67,7 +68,6 @@ export const snapshotPair = memoize(
typeName: string,
serverActionsEnabled: Observable<boolean>,
): Observable<SnapshotPair> => {
// @TODO test worker implementation here
return memoizedPair(client, idPair, typeName, serverActionsEnabled).pipe(
map(({published, draft, transactionsPendingEvents$}): SnapshotPair => {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import {
checkoutPair,
type DocumentVersionEvent,
type Pair,
} from './document-pair/checkoutPairWithWorker'
// Test the worker implementation
// } from './document-pair/checkoutPairWithWorker'
} from './document-pair/checkoutPair'
import {consistencyStatus} from './document-pair/consistencyStatus'
import {documentEvents} from './document-pair/documentEvents'
import {editOperations} from './document-pair/editOperations'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import {memo, useCallback, useMemo, useState} from 'react'
import {
type DocumentActionComponent,
type DocumentActionDescription,
shouldArrayDialogOpen,
useSource,
useTimelineSelector,
} from 'sanity'

import {shouldArrayDialogOpen} from '../../../../core/form/studio/tree-editing/utils/shouldArrayDialogOpen'
import {Button, Tooltip} from '../../../../ui-components'
import {RenderActionCollectionState} from '../../../components'
import {HistoryRestoreAction} from '../../../documentActions'
Expand Down
2 changes: 2 additions & 0 deletions packages/sanity/web-workers/checkoutPair.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10572,6 +10572,8 @@ function xE(r, e, n, s) {
}
function SE(r, e, n) {
const { publishedId: s, draftId: u } = e, a = new Je(), f = bE(r, e).pipe(
// @TODO figure out how to schedule updates the same way react does for input change: scheduleCallback(NormalPriority, () => schedule)
// observeOn(asapScheduler, 10000),
Vu({ connector: () => a })
), l = f.pipe(
Xe((b) => b.type === "reconnect")
Expand Down
26 changes: 18 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 85d3b2c

Please sign in to comment.