Skip to content

Commit

Permalink
fix: typescript promise type (#160)
Browse files Browse the repository at this point in the history
* fix: change Tweenable to implement Promise<unknown>

Co-authored-by: Jeremy Kahn <[email protected]>
  • Loading branch information
jspears and jeremyckahn committed Oct 6, 2022
1 parent a6ea330 commit f0c71d0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/tweenable.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,13 @@ const remove = ((previousTween, nextTween) => tween => {
})()

const defaultPromiseCtor = typeof Promise === 'function' ? Promise : null

/**
* @class
* @implements {Promise<unknown>}
*/
export class Tweenable {
//required for Promise implementation
[Symbol.toStringTag] = 'Promise'
/**
* @method Tweenable.now
* @static
Expand Down Expand Up @@ -555,9 +560,9 @@ export class Tweenable {
/**
* Overrides any `finish` function passed via a {@link shifty.tweenConfig}.
* @method Tweenable#then
* @param {function} onFulfilled Receives {@link shifty.promisedData} as the
* @param {function=} onFulfilled Receives {@link shifty.promisedData} as the
* first parameter.
* @param {function} onRejected Receives {@link shifty.promisedData} as the
* @param {function=} onRejected Receives {@link shifty.promisedData} as the
* first parameter.
* @return {Promise<Object>}
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then
Expand All @@ -582,6 +587,15 @@ export class Tweenable {
catch(onRejected) {
return this.then().catch(onRejected)
}
/**
* @method Tweenable#finally
* @param {function} onFinally
* @return {Promise<undefined>}
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/finally
*/
finally(onFinally) {
return this.then().finally(onFinally)
}

/**
* @method Tweenable#get
Expand Down
23 changes: 23 additions & 0 deletions src/tweenable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,29 @@ describe('#tween', () => {
expect(testState).toEqual({ x: 10 })
})
})
describe('finally', () => {
test('runs finally with no parameters', done => {
Tweenable.now = () => 0
tweenable = new Tweenable()

const tween = tweenable.tween({
from: { x: 0 },
to: { x: 10 },
duration: 500,
})
tween
.catch(() => {})
.finally(state => {
expect(state).toEqual(undefined)
done()
})

Tweenable.now = () => 250
processTweens()

tween.cancel()
})
})

describe('rejection', () => {
let testState
Expand Down

0 comments on commit f0c71d0

Please sign in to comment.