Skip to content

Commit

Permalink
refactor: remove debug code and previous implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Jul 16, 2024
1 parent d472374 commit 8b72911
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 271 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"check:deps": "pnpm --recursive --parallel exec depcheck",
"check:format": "prettier . --check",
"check:lint": "turbo run lint --continue -- --quiet",
"check:react-compiler": "eslint --no-inline-config --no-eslintrc --ext .cjs,.mjs,.js,.jsx,.ts,.tsx --parser @typescript-eslint/parser --plugin react-compiler --rule 'react-compiler/react-compiler: [warn]' --ignore-path .eslintignore.react-compiler --max-warnings 30 .",
"check:react-compiler": "eslint --no-inline-config --no-eslintrc --ext .cjs,.mjs,.js,.jsx,.ts,.tsx --parser @typescript-eslint/parser --plugin react-compiler --rule 'react-compiler/react-compiler: [warn]' --ignore-path .eslintignore.react-compiler --max-warnings 29 .",
"check:test": "run-s test -- --silent",
"check:types": "tsc && turbo run check:types --filter='./packages/*' --filter='./packages/@sanity/*'",
"chore:format:fix": "prettier --cache --write .",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable no-console */
import {debounce} from 'lodash'
import {useEffect, useLayoutEffect, useMemo, useReducer, useRef, useState} from 'react'
import {useLayoutEffect, useMemo, useReducer, useRef, useState} from 'react'

import {type IsEqualFunction} from './types'

Expand All @@ -12,6 +11,14 @@ export interface TrackerContextStore<Value> {
}

function createStore<Value>(reportedValues: Map<string, Value>, publish: () => void) {
/**
* This implementation is over 4 years old, and is part of tackling a hard problem:
* tracking the position of DOM nodes efficiently, so that Presence Sticky Overlays can render correctly and respond to scroll,
* and so that Change Indicator connectors can draw paths that traces a document change to its form input field no matter how they layout shifts.
* 4 years ago we didn't have a lot of options when solving this problem.
* But today we have great success with using `@floating-ui/react` in `@sanity/ui` with a very similar problem: positioning tooltips and popovers correctly no matter how the page scrolls or the layout shifts.
* We should consider migrating to `@floating-ui/react` for this problem as well.
*/
function add(id: string, value: Value) {
if (reportedValues.has(id)) {
// eslint-disable-next-line no-console
Expand Down Expand Up @@ -64,10 +71,6 @@ export function useTrackerStore<Value>(): {
[debouncedUpdateSnapshot, reportedValues],
)

useEffect(() => {
console.log('useTrackerStore.useEffect')
}, [])

return {store, snapshot}
}

Expand All @@ -86,20 +89,13 @@ export function useTrackerStoreReporter<Value>(
* Setup and teardown, only runs if `id`, `store` or the `value` getter changes
*/
if (id === null || store === null) {
console.log('useTrackerStoreReporter.add', 'id is null')
return undefined
}
console.groupCollapsed(`useTrackerStoreReporter.add(${id})`)
console.count(id)
const nextValue = value()
console.log({current: nextValue})
console.log('previous.current', previousRef.current)
store.add(id, nextValue)
idRef.current = id
previousRef.current = nextValue
console.groupEnd()
return () => {
console.count(`useTrackerStoreReporter.remove(${id})`)
store.remove(id)
idRef.current = null
previousRef.current = null
Expand All @@ -113,38 +109,16 @@ export function useTrackerStoreReporter<Value>(
* @TODO This is a bit expensive, and we should migrate to using a library like `@floating-ui/react` instead of rolling our own solution.
*/
if (id === null || idRef.current === null || store === null || id !== idRef.current) {
console.count(
`useTrackerStoreReporter.update(${idRef.current || 'null'}, ${id || 'null'}): skipped`,
)
return undefined
}
const nextValue = value()
if (isEqual(previousRef.current, nextValue)) {
console.count(
`useTrackerStoreReporter.update(${idRef.current || 'null'}, ${id || 'null'}): skipped, equal state`,
)
return undefined
}

console.group(`useTrackerStoreReporter.update(${id})`)
store.update(id, nextValue)
console.count(`update(id: ${id}, current: ${nextValue})`)
console.log({'previous.current': previousRef.current, 'current': nextValue})
console.groupEnd()

previousRef.current = nextValue

return undefined
})
}

/** @internal */
export function useTrackerStoreReportedValues<Value>(
snapshot: TrackerContextGetSnapshot<Value> | null,
): [string, Value][] {
useEffect(() => {
console.log('useTrackerStoreReportedValues.useEffect', {snapshot})
}, [snapshot])

return snapshot || []
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './hooks'
export * from './types'
export * from './v1'
export * from './v2'
205 changes: 0 additions & 205 deletions packages/sanity/src/core/components/react-track-elements/v1.tsx

This file was deleted.

31 changes: 3 additions & 28 deletions packages/sanity/src/core/presence/overlay/tracker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {memo, useContext} from 'react'
import {PresenceTrackerContextGetSnapshot, PresenceTrackerContextStore} from 'sanity/_singletons'

import {
createTrackerScope,
type Reported,
type ReporterHook,
type TrackerContextGetSnapshot,
Expand All @@ -13,14 +12,6 @@ import {type FieldPresenceData} from '../types'

export type ReportedPresenceData = Reported<FieldPresenceData>

const variant: '1' | '2' = '2'

const {
Tracker: PresenceTrackerV1,
useReporter: useReporterV1,
useReportedValues: useReportedValuesV1,
} = createTrackerScope<FieldPresenceData>()

function PresenceTrackerComponent(props: {children: React.ReactNode}) {
const {children} = props
const {store, snapshot} = useTrackerStore<FieldPresenceData>()
Expand All @@ -37,14 +28,14 @@ function PresenceTrackerComponent(props: {children: React.ReactNode}) {
/**
* @internal
*/
export const PresenceTrackerV2 = memo(PresenceTrackerComponent)
export const PresenceTracker = memo(PresenceTrackerComponent)

const EMPTY_ARRAY: Reported<FieldPresenceData>[] = []

/**
* @internal
*/
export function usePresenceReportedValuesV2(): TrackerContextGetSnapshot<FieldPresenceData> {
export function usePresenceReportedValues(): TrackerContextGetSnapshot<FieldPresenceData> {
const snapshot = useContext(PresenceTrackerContextGetSnapshot)

if (snapshot === null) {
Expand All @@ -63,7 +54,7 @@ export function usePresenceReportedValuesV2(): TrackerContextGetSnapshot<FieldPr
/**
* @internal
*/
export const usePresenceReporterV2: ReporterHook<FieldPresenceData> = (id, value, isEqual?) => {
export const usePresenceReporter: ReporterHook<FieldPresenceData> = (id, value, isEqual?) => {
const store = useContext(PresenceTrackerContextStore)

if (store === null) {
Expand All @@ -77,19 +68,3 @@ export const usePresenceReporterV2: ReporterHook<FieldPresenceData> = (id, value

useTrackerStoreReporter<FieldPresenceData>(store, id, value, isEqual)
}

/**
* @internal
*/
export const PresenceTracker = variant === '1' ? PresenceTrackerV1 : PresenceTrackerV2

/**
* @internal
*/
export const usePresenceReportedValues =
variant === '1' ? useReportedValuesV1 : usePresenceReportedValuesV2

/**
* @internal
*/
export const usePresenceReporter = variant === '1' ? useReporterV1 : usePresenceReporterV2

0 comments on commit 8b72911

Please sign in to comment.