2018-11-14 00:03:35 +08:00
|
|
|
import { queueJob, queuePostEffect, nextTick } from '../src/index'
|
2018-09-20 03:36:56 +08:00
|
|
|
|
2018-09-20 05:45:19 +08:00
|
|
|
describe('scheduler', () => {
|
2018-09-20 23:43:14 +08:00
|
|
|
it('queueJob', async () => {
|
2018-09-20 05:45:19 +08:00
|
|
|
const calls: any = []
|
|
|
|
const job1 = () => {
|
|
|
|
calls.push('job1')
|
|
|
|
}
|
|
|
|
const job2 = () => {
|
|
|
|
calls.push('job2')
|
|
|
|
}
|
|
|
|
queueJob(job1)
|
2018-09-20 03:36:56 +08:00
|
|
|
queueJob(job2)
|
2018-09-20 05:45:19 +08:00
|
|
|
expect(calls).toEqual([])
|
|
|
|
await nextTick()
|
2018-09-20 03:36:56 +08:00
|
|
|
expect(calls).toEqual(['job1', 'job2'])
|
2018-09-20 05:45:19 +08:00
|
|
|
})
|
2018-09-20 03:36:56 +08:00
|
|
|
|
2018-09-20 23:43:14 +08:00
|
|
|
it('queueJob while already flushing', async () => {
|
2018-09-20 05:45:19 +08:00
|
|
|
const calls: any = []
|
|
|
|
const job1 = () => {
|
|
|
|
calls.push('job1')
|
|
|
|
// job1 queues job2
|
|
|
|
queueJob(job2)
|
|
|
|
}
|
|
|
|
const job2 = () => {
|
|
|
|
calls.push('job2')
|
|
|
|
}
|
|
|
|
queueJob(job1)
|
|
|
|
expect(calls).toEqual([])
|
|
|
|
await nextTick()
|
|
|
|
expect(calls).toEqual(['job1', 'job2'])
|
|
|
|
})
|
2018-09-20 03:36:56 +08:00
|
|
|
|
2018-11-09 09:08:28 +08:00
|
|
|
it('queueJob w/ postCommitCb', async () => {
|
2018-09-20 05:45:19 +08:00
|
|
|
const calls: any = []
|
|
|
|
const job1 = () => {
|
|
|
|
calls.push('job1')
|
2018-11-14 00:03:35 +08:00
|
|
|
queuePostEffect(cb1)
|
2018-09-20 05:45:19 +08:00
|
|
|
}
|
|
|
|
const job2 = () => {
|
|
|
|
calls.push('job2')
|
2018-11-14 00:03:35 +08:00
|
|
|
queuePostEffect(cb2)
|
2018-09-20 05:45:19 +08:00
|
|
|
}
|
|
|
|
const cb1 = () => {
|
|
|
|
calls.push('cb1')
|
|
|
|
}
|
|
|
|
const cb2 = () => {
|
|
|
|
calls.push('cb2')
|
|
|
|
}
|
2018-11-09 09:08:28 +08:00
|
|
|
queueJob(job1)
|
|
|
|
queueJob(job2)
|
2018-09-20 05:45:19 +08:00
|
|
|
await nextTick()
|
2018-11-09 09:08:28 +08:00
|
|
|
// post commit cbs are called in reverse!
|
|
|
|
expect(calls).toEqual(['job1', 'job2', 'cb2', 'cb1'])
|
2018-09-20 05:45:19 +08:00
|
|
|
})
|
|
|
|
|
2018-09-20 23:43:14 +08:00
|
|
|
it('queueJob w/ postFlushCb while flushing', async () => {
|
2018-09-20 05:45:19 +08:00
|
|
|
const calls: any = []
|
|
|
|
const job1 = () => {
|
|
|
|
calls.push('job1')
|
2018-11-14 00:03:35 +08:00
|
|
|
queuePostEffect(cb1)
|
2018-09-20 05:45:19 +08:00
|
|
|
// job1 queues job2
|
2018-11-09 09:08:28 +08:00
|
|
|
queueJob(job2)
|
2018-09-20 05:45:19 +08:00
|
|
|
}
|
|
|
|
const job2 = () => {
|
|
|
|
calls.push('job2')
|
2018-11-14 00:03:35 +08:00
|
|
|
queuePostEffect(cb2)
|
2018-09-20 05:45:19 +08:00
|
|
|
}
|
|
|
|
const cb1 = () => {
|
|
|
|
calls.push('cb1')
|
|
|
|
}
|
|
|
|
const cb2 = () => {
|
|
|
|
calls.push('cb2')
|
|
|
|
}
|
2018-11-09 09:08:28 +08:00
|
|
|
queueJob(job1)
|
2018-09-20 05:45:19 +08:00
|
|
|
expect(calls).toEqual([])
|
|
|
|
await nextTick()
|
2018-11-09 09:08:28 +08:00
|
|
|
expect(calls).toEqual(['job1', 'job2', 'cb2', 'cb1'])
|
2018-09-20 05:45:19 +08:00
|
|
|
})
|
2018-09-20 09:38:27 +08:00
|
|
|
|
2018-09-20 23:43:14 +08:00
|
|
|
it('should dedupe queued tasks', async () => {
|
2018-09-20 09:38:27 +08:00
|
|
|
const calls: any = []
|
|
|
|
const job1 = () => {
|
|
|
|
calls.push('job1')
|
|
|
|
}
|
|
|
|
const job2 = () => {
|
|
|
|
calls.push('job2')
|
|
|
|
}
|
|
|
|
queueJob(job1)
|
|
|
|
queueJob(job2)
|
|
|
|
queueJob(job1)
|
|
|
|
queueJob(job2)
|
|
|
|
expect(calls).toEqual([])
|
|
|
|
await nextTick()
|
|
|
|
expect(calls).toEqual(['job1', 'job2'])
|
|
|
|
})
|
2018-10-29 00:08:58 +08:00
|
|
|
|
2018-11-14 00:03:35 +08:00
|
|
|
it('queueJob inside postEffect', async () => {
|
2018-10-29 00:08:58 +08:00
|
|
|
const calls: any = []
|
|
|
|
const job1 = () => {
|
|
|
|
calls.push('job1')
|
2018-11-14 00:03:35 +08:00
|
|
|
queuePostEffect(cb1)
|
2018-10-29 00:08:58 +08:00
|
|
|
}
|
|
|
|
const cb1 = () => {
|
|
|
|
// queue another job in postFlushCb
|
|
|
|
calls.push('cb1')
|
2018-11-09 09:08:28 +08:00
|
|
|
queueJob(job2)
|
2018-10-29 00:08:58 +08:00
|
|
|
}
|
|
|
|
const job2 = () => {
|
|
|
|
calls.push('job2')
|
2018-11-14 00:03:35 +08:00
|
|
|
queuePostEffect(cb2)
|
2018-10-29 00:08:58 +08:00
|
|
|
}
|
|
|
|
const cb2 = () => {
|
|
|
|
calls.push('cb2')
|
|
|
|
}
|
|
|
|
|
2018-11-09 09:08:28 +08:00
|
|
|
queueJob(job1)
|
|
|
|
queueJob(job2)
|
2018-10-29 00:08:58 +08:00
|
|
|
await nextTick()
|
2018-11-09 09:08:28 +08:00
|
|
|
expect(calls).toEqual(['job1', 'job2', 'cb2', 'cb1', 'job2', 'cb2'])
|
2018-10-29 00:08:58 +08:00
|
|
|
})
|
2018-09-20 03:36:56 +08:00
|
|
|
})
|