Skip to content

feat: rework useTransition #750

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

Closed
wants to merge 13 commits into from
82 changes: 0 additions & 82 deletions packages/core/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,88 +74,6 @@ export function interpolateTo<T extends ReservedProps>(
return out
}

type ItemKey = number | string

interface Item {
key: ItemKey
originalKey: ItemKey
phase: string
item: any
props: object
destroyed?: boolean
}

interface DeletedItem extends Item {
left?: ItemKey
right?: ItemKey
}

/**
* This tries to put deleted items back into the given `out` list in correct
* order. Deleted items must have a `left` and `right` property with key of
* their sibling which is used to find the correct placement.
*/
export function reconcileDeleted(
deleted: DeletedItem[],
current: Item[]
): any[] {
// Copy as we will be mutating the arrays
deleted = [...deleted]
current = [...current]

// Used to detect deadlock (when a pass finds 0 siblings)
let failedTries = 0

// Track where the current pass start/ends
let passIndex = 0
let nextPassIndex = deleted.length

// Insert all deleted items into `current`
for (let i = 0; i < deleted.length; i++) {
if (i === nextPassIndex) {
// Sanity test: Push to end if somehow no siblings were found
if (passIndex + failedTries === nextPassIndex) {
for (let j = i; j < deleted.length; j++) {
const { left, right, ...deletedItem } = deleted[j]
current.push(deletedItem)
}
break
}
// Update local state at the end of each pass
passIndex = nextPassIndex
nextPassIndex = deleted.length
failedTries = 0
}

// The index of the deleted item in `current`
let index = -1

// Look for the left or right sibling in `current`
const { left, right, ...deletedItem } = deleted[i]
for (let j = current.length; --j >= 0; ) {
const { originalKey: key } = current[j]
if (key === right) {
index = j
break
}
if (key === left) {
index = j + 1
break
}
}

// Items with no index are revisited in the next pass
if (index < 0) {
failedTries++
deleted.push(deleted[i])
} else {
current.splice(index, 0, deletedItem)
}
}

return current
}

export function freeze<T extends object>(obj: T): T {
if (
typeof process !== 'undefined' &&
Expand Down
13 changes: 2 additions & 11 deletions packages/core/src/legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@ export function Trail({ items, children, ...props }) {
})
}

export function Transition({ items, keys = null, children, ...props }) {
const transitions = useTransition(items, keys, props)
return transitions.map(({ item, key, props, phase }, index) => {
const result = children(item, phase, index)
const element = is.fun(result) ? result(props) : result
return element && element.type ? (
<element.type {...element.props} key={key} ref={element.ref} />
) : (
element
)
})
export function Transition({ items, children, ...props }) {
return useTransition(items, props)(children)
}
124 changes: 0 additions & 124 deletions packages/core/src/useTransition.d.ts

This file was deleted.

Loading