Skip to content

Commit

Permalink
[#128] Don't reuse variables
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyckahn committed Dec 15, 2020
1 parent af71d52 commit db7c646
Showing 1 changed file with 31 additions and 46 deletions.
77 changes: 31 additions & 46 deletions src/tweenable.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,7 @@ const formulas = { ...easingFunctions }
* @returns {Object}
* @private
*/
export const tweenProps = ((
easedPosition,
easingFn,
easingObjectProp,
hasOneEase,
key,
normalizedPosition,
start
) => (
export const tweenProps = (
forPosition,
currentState,
originalState,
Expand All @@ -68,18 +60,22 @@ export const tweenProps = ((
timestamp,
easing
) => {
normalizedPosition =
let easedPosition
let easingObjectProp
let start

const normalizedPosition =
forPosition < timestamp ? 0 : (forPosition - timestamp) / duration

easingFn = null
hasOneEase = false
let easingFn = null
let hasOneEase = false

if (easing && easing.call) {
hasOneEase = true
easedPosition = easing(normalizedPosition)
}

for (key in currentState) {
for (const key in currentState) {
if (!hasOneEase) {
easingObjectProp = easing[key]
easingFn = easingObjectProp.call
Expand All @@ -95,36 +91,25 @@ export const tweenProps = ((
}

return currentState
})()
}

const processTween = ((
duration,
timestamp,
endTime,
timeToCompute,
hasEnded,
hasFilters,
offset,
currentState,
targetState,
delay
) => (tween, currentTime) => {
timestamp = tween._timestamp
currentState = tween._currentState
delay = tween._delay
const processTween = (tween, currentTime) => {
let timestamp = tween._timestamp
const currentState = tween._currentState
const delay = tween._delay

if (currentTime < timestamp + delay) {
return
}

duration = tween._duration
targetState = tween._targetState
let duration = tween._duration
const targetState = tween._targetState

endTime = timestamp + delay + duration
timeToCompute = currentTime > endTime ? endTime : currentTime
hasEnded = timeToCompute >= endTime
offset = duration - (endTime - timeToCompute)
hasFilters = tween._filters.length > 0
const endTime = timestamp + delay + duration
let timeToCompute = currentTime > endTime ? endTime : currentTime
const hasEnded = timeToCompute >= endTime
const offset = duration - (endTime - timeToCompute)
const hasFilters = tween._filters.length > 0

if (hasEnded) {
tween._render(targetState, tween._data, offset)
Expand Down Expand Up @@ -159,9 +144,8 @@ const processTween = ((
}

tween._render(currentState, tween._data, offset)
})()
}

export const processTweens = (currentTime, currentTween, nextTweenToProcess) =>
/**
* Process all tweens currently managed by Shifty for the current tick. This
* does not perform any timing or update scheduling; it is the logic that is
Expand All @@ -180,16 +164,18 @@ export const processTweens = (currentTime, currentTween, nextTweenToProcess) =>
* @method shifty.processTweens
* @see https://github.com/jeremyckahn/shifty/issues/109
*/
(() => {
currentTime = Tweenable.now()
currentTween = listHead
export const processTweens = () => {
let nextTweenToProcess

const currentTime = Tweenable.now()
let currentTween = listHead

while (currentTween) {
nextTweenToProcess = currentTween._next
processTween(currentTween, currentTime)
currentTween = nextTweenToProcess
}
})()
}

const getCurrentTime = Date.now || (() => +new Date())
let now
Expand Down Expand Up @@ -412,15 +398,14 @@ export class Tweenable {

const { from, to = {} } = config
const { _currentState, _originalState, _targetState } = this
let key

for (key in from) {
for (const key in from) {
_currentState[key] = from[key]
}

let anyPropsAreStrings = false

for (key in _currentState) {
for (const key in _currentState) {
const currentProp = _currentState[key]

if (!anyPropsAreStrings && typeof currentProp === TYPE_STRING) {
Expand All @@ -442,7 +427,7 @@ export class Tweenable {
this._filters.length = 0

if (anyPropsAreStrings) {
for (key in Tweenable.filters) {
for (const key in Tweenable.filters) {
if (Tweenable.filters[key].doesApply(this)) {
this._filters.push(Tweenable.filters[key])
}
Expand Down

0 comments on commit db7c646

Please sign in to comment.