Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
test('saga cancellation: nested forked task cancellation', () => {
let actual = []
const middleware = sagaMiddleware()
createStore(() => ({}), {}, applyMiddleware(middleware))
let start = deferred(),
stop = deferred(),
subtaskDefs = arrayOfDeferred(2),
nestedTaskDefs = arrayOfDeferred(2)
Promise.resolve(1)
.then(() => start.resolve('start'))
.then(() => subtaskDefs[0].resolve('subtask_1'))
.then(() => nestedTaskDefs[0].resolve('nested_task_1'))
.then(() => stop.resolve('stop')) //
.then(() => nestedTaskDefs[1].resolve('nested_task_2'))
.then(() => subtaskDefs[1].resolve('subtask_2'))
function* nestedTask() {
try {
actual.push(yield nestedTaskDefs[0].promise)
actual.push(yield nestedTaskDefs[1].promise)
} finally {
if (yield io.cancelled()) actual.push(yield 'nested task cancelled')
}
}
test('saga cancellation: race effect', () => {
let actual = []
const middleware = sagaMiddleware()
createStore(() => ({}), {}, applyMiddleware(middleware))
let startDef = deferred()
let cancelDef = deferred()
let subroutineDefs = arrayOfDeferred(2)
Promise.resolve(1)
.then(() => startDef.resolve('start'))
.then(() => cancelDef.resolve('cancel'))
.then(() => subroutineDefs[0].resolve('subroutine 1'))
.then(() => subroutineDefs[1].resolve('subroutine 2'))
function* main() {
actual.push(yield startDef.promise)
try {
actual.push(
yield io.race({
subroutine1: io.call(subroutine1),
subroutine2: io.call(subroutine2),
}),
)
test('saga cancellation: nested task cancellation', () => {
let actual = []
const middleware = sagaMiddleware()
createStore(() => ({}), {}, applyMiddleware(middleware))
let start = deferred(),
stop = deferred(),
subtaskDefs = arrayOfDeferred(2),
nestedTask1Defs = arrayOfDeferred(2),
nestedTask2Defs = arrayOfDeferred(2)
Promise.resolve(1)
.then(() => start.resolve('start'))
.then(() => subtaskDefs[0].resolve('subtask_1'))
.then(() => nestedTask1Defs[0].resolve('nested_task_1_1'))
.then(() => nestedTask2Defs[0].resolve('nested_task_2_1'))
.then(() => stop.resolve('stop'))
.then(() => nestedTask1Defs[1].resolve('nested_task_1_2'))
.then(() => nestedTask2Defs[1].resolve('nested_task_2_2'))
.then(() => subtaskDefs[1].resolve('subtask_2'))
function* nestedTask1() {
try {
actual.push(yield nestedTask1Defs[0].promise)
actual.push(yield nestedTask1Defs[1].promise)
test('saga cancellation: nested task cancellation', () => {
let actual = []
const middleware = sagaMiddleware()
createStore(() => ({}), {}, applyMiddleware(middleware))
let start = deferred(),
stop = deferred(),
subtaskDefs = arrayOfDeferred(2),
nestedTask1Defs = arrayOfDeferred(2),
nestedTask2Defs = arrayOfDeferred(2)
Promise.resolve(1)
.then(() => start.resolve('start'))
.then(() => subtaskDefs[0].resolve('subtask_1'))
.then(() => nestedTask1Defs[0].resolve('nested_task_1_1'))
.then(() => nestedTask2Defs[0].resolve('nested_task_2_1'))
.then(() => stop.resolve('stop'))
.then(() => nestedTask1Defs[1].resolve('nested_task_1_2'))
.then(() => nestedTask2Defs[1].resolve('nested_task_2_2'))
.then(() => subtaskDefs[1].resolve('subtask_2'))
function* nestedTask1() {
try {
actual.push(yield nestedTask1Defs[0].promise)
actual.push(yield nestedTask1Defs[1].promise)
} finally {
test('saga cancellation: automatic race competitor cancellation', () => {
let actual = []
const middleware = sagaMiddleware()
createStore(() => ({}), {}, applyMiddleware(middleware))
let winnerSubtaskDefs = arrayOfDeferred(2),
loserSubtaskDefs = arrayOfDeferred(2),
parallelSubtaskDefs = arrayOfDeferred(2)
Promise.resolve(1)
.then(() => winnerSubtaskDefs[0].resolve('winner_1'))
.then(() => loserSubtaskDefs[0].resolve('loser_1'))
.then(() => parallelSubtaskDefs[0].resolve('parallel_1'))
.then(() => winnerSubtaskDefs[1].resolve('winner_2'))
.then(() => loserSubtaskDefs[1].resolve('loser_2'))
.then(() => parallelSubtaskDefs[1].resolve('parallel_2'))
function* winnerSubtask() {
try {
actual.push(yield winnerSubtaskDefs[0].promise)
actual.push(yield winnerSubtaskDefs[1].promise)
} finally {
if (yield io.cancelled()) actual.push(yield 'winner subtask cancelled')
test('saga cancellation: automatic parallel effect cancellation', () => {
let actual = []
const middleware = sagaMiddleware()
createStore(() => ({}), {}, applyMiddleware(middleware))
let subtask1Defs = arrayOfDeferred(2),
subtask2Defs = arrayOfDeferred(2)
Promise.resolve(1)
.then(() => subtask1Defs[0].resolve('subtask_1'))
.then(() => subtask2Defs[0].resolve('subtask_2'))
.then(() => subtask1Defs[1].reject('subtask_1 rejection'))
.then(() => subtask2Defs[1].resolve('subtask_2_2'))
function* subtask1() {
actual.push(yield subtask1Defs[0].promise)
actual.push(yield subtask1Defs[1].promise)
}
function* subtask2() {
try {
actual.push(yield subtask2Defs[0].promise)
actual.push(yield subtask2Defs[1].promise)
test('saga cancellation: forked children', () => {
const actual = []
const middleware = sagaMiddleware()
createStore(() => ({}), {}, applyMiddleware(middleware))
let cancelDef = deferred()
const rootDef = deferred()
const childAdef = deferred()
const childBdef = deferred()
const neverDef = deferred()
const defs = arrayOfDeferred(4)
Promise.resolve()
.then(() => childAdef.resolve('childA resolve'))
.then(() => rootDef.resolve('root resolve'))
.then(() => defs[0].resolve('leaf 0 resolve'))
.then(() => childBdef.resolve('childB resolve')) //
.then(() => cancelDef.resolve('cancel'))
.then(() => defs[3].resolve('leaf 3 resolve'))
.then(() => defs[2].resolve('leaf 2 resolve'))
.then(() => defs[1].resolve('leaf 1 resolve'))
function* main() {
try {
yield io.fork(childA)
actual.push(yield rootDef.promise)
yield io.fork(childB)
yield neverDef.promise
test('saga cancellation: automatic race competitor cancellation', () => {
let actual = []
const middleware = sagaMiddleware()
createStore(() => ({}), {}, applyMiddleware(middleware))
let winnerSubtaskDefs = arrayOfDeferred(2),
loserSubtaskDefs = arrayOfDeferred(2),
parallelSubtaskDefs = arrayOfDeferred(2)
Promise.resolve(1)
.then(() => winnerSubtaskDefs[0].resolve('winner_1'))
.then(() => loserSubtaskDefs[0].resolve('loser_1'))
.then(() => parallelSubtaskDefs[0].resolve('parallel_1'))
.then(() => winnerSubtaskDefs[1].resolve('winner_2'))
.then(() => loserSubtaskDefs[1].resolve('loser_2'))
.then(() => parallelSubtaskDefs[1].resolve('parallel_2'))
function* winnerSubtask() {
try {
actual.push(yield winnerSubtaskDefs[0].promise)
actual.push(yield winnerSubtaskDefs[1].promise)
} finally {
if (yield io.cancelled()) actual.push(yield 'winner subtask cancelled')
}
test('cancel should be able to cancel multiple tasks', () => {
const defs = arrayOfDeferred(3)
let actual = []
const middleware = sagaMiddleware()
createStore(() => ({}), {}, applyMiddleware(middleware))
function* worker(i) {
try {
yield defs[i].promise
} finally {
if (yield io.cancelled()) {
actual.push(i)
}
}
}
function* genFn() {
const t1 = yield io.fork(worker, 0)
test('saga cancellation: nested task cancellation', () => {
let actual = []
const middleware = sagaMiddleware()
createStore(() => ({}), {}, applyMiddleware(middleware))
let start = deferred(),
stop = deferred(),
subtaskDefs = arrayOfDeferred(2),
nestedTask1Defs = arrayOfDeferred(2),
nestedTask2Defs = arrayOfDeferred(2)
Promise.resolve(1)
.then(() => start.resolve('start'))
.then(() => subtaskDefs[0].resolve('subtask_1'))
.then(() => nestedTask1Defs[0].resolve('nested_task_1_1'))
.then(() => nestedTask2Defs[0].resolve('nested_task_2_1'))
.then(() => stop.resolve('stop'))
.then(() => nestedTask1Defs[1].resolve('nested_task_1_2'))
.then(() => nestedTask2Defs[1].resolve('nested_task_2_2'))
.then(() => subtaskDefs[1].resolve('subtask_2'))
function* nestedTask1() {
try {
actual.push(yield nestedTask1Defs[0].promise)
actual.push(yield nestedTask1Defs[1].promise)
} finally {
if (yield io.cancelled()) actual.push(yield 'nested task 1 cancelled')