Skip to content

Commit

Permalink
Cancel takes a behavior again
Browse files Browse the repository at this point in the history
  • Loading branch information
sajmoni committed Nov 28, 2022
1 parent 993c857 commit 48f9bfd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,14 @@ export default function createInstance(): Instance {
/**
* Cancel a behavior
*/
const cancel = (behaviorId: string): void => {
const behaviorObject = get(behaviorId)
const cancel = (behavior: string | Behavior): void => {
const behaviorObject =
typeof behavior === 'string' ? get(behavior) : behavior

if (behaviorObject) {
behaviorsToRemove.push(behaviorObject)
} else {
console.warn(
`level1: Tried to cancel non-existent behavior: ${behaviorId}`,
)
console.warn(`level1: Tried to cancel non-existent behavior: ${behavior}`)
}
}

Expand Down
19 changes: 19 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,22 @@ test('cancel - id', (t) => {
t.is(get(id), undefined)
t.deepEqual(getByLabel(label), [])
})

test('cancel - object', (t) => {
const { delay, update, get, cancel } = createInstance()

const id = 'An id'

delay(10, { id })

update(deltaTime)

t.is(get(id)?.id, id)

update(deltaTime)
cancel(get(id)!)

update(deltaTime)

t.is(get(id), undefined)
})

0 comments on commit 48f9bfd

Please sign in to comment.