Skip to content

Commit 13e4400

Browse files
fix(overmind): fix async detection of operators
1 parent cd55e72 commit 13e4400

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

packages/node_modules/overmind/src/operator.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,7 @@ export function createOperator<ThisConfig extends IConfiguration>(
128128
): any {
129129
const operator = (err, context, next, final) => {
130130
operatorStarted(type, name, context)
131-
let asyncTimeout = setTimeout(() => {
132-
context.execution.emit(EventType.OPERATOR_ASYNC, {
133-
...context.execution,
134-
isAsync: true,
135-
})
136-
})
131+
let nextIsCalled = false
137132
try {
138133
cb(
139134
err,
@@ -170,12 +165,12 @@ export function createOperator<ThisConfig extends IConfiguration>(
170165
.then((promiseValue) => run(err, promiseValue))
171166
.catch((promiseError) => run(promiseError, promiseError))
172167
} else {
173-
clearTimeout(asyncTimeout)
168+
nextIsCalled = true
174169
run(err, value)
175170
}
176171
},
177172
(err, value) => {
178-
clearTimeout(asyncTimeout)
173+
nextIsCalled = true
179174
operatorStopped(context, err || value, {
180175
isSkipped: Boolean(err),
181176
isIntercepted: !err,
@@ -184,12 +179,19 @@ export function createOperator<ThisConfig extends IConfiguration>(
184179
}
185180
)
186181
} catch (error) {
187-
clearTimeout(asyncTimeout)
182+
nextIsCalled = true
188183
operatorStopped(context, context.value, {
189184
error,
190185
})
191186
next(error, createContext(context, context.value))
192187
}
188+
189+
if (!nextIsCalled) {
190+
context.execution.emit(EventType.OPERATOR_ASYNC, {
191+
...context.execution,
192+
isAsync: true,
193+
})
194+
}
193195
}
194196

195197
operator[IS_OPERATOR] = true
@@ -226,12 +228,7 @@ export function createMutationOperator<ThisConfig extends IConfiguration>(
226228
})
227229
})
228230
}
229-
let asyncTimeout = setTimeout(() => {
230-
context.execution.emit(EventType.OPERATOR_ASYNC, {
231-
...context.execution,
232-
isAsync: true,
233-
})
234-
})
231+
let nextIsCalled = false
235232
try {
236233
cb(
237234
err,
@@ -256,12 +253,12 @@ export function createMutationOperator<ThisConfig extends IConfiguration>(
256253
.then((promiseValue) => run(err, promiseValue))
257254
.catch((promiseError) => run(promiseError, promiseError))
258255
} else {
259-
clearTimeout(asyncTimeout)
256+
nextIsCalled = true
260257
run(err, value)
261258
}
262259
},
263260
(err, value) => {
264-
clearTimeout(asyncTimeout)
261+
nextIsCalled = true
265262
operatorStopped(context, err || value, {
266263
isSkipped: Boolean(err),
267264
isIntercepted: !err,
@@ -293,12 +290,19 @@ export function createMutationOperator<ThisConfig extends IConfiguration>(
293290
})
294291
}
295292
} catch (error) {
296-
clearTimeout(asyncTimeout)
293+
nextIsCalled = true
297294
operatorStopped(context, context.value, {
298295
error,
299296
})
300297
next(error, createContext(context, context.value))
301298
}
299+
300+
if (!nextIsCalled) {
301+
context.execution.emit(EventType.OPERATOR_ASYNC, {
302+
...context.execution,
303+
isAsync: true,
304+
})
305+
}
302306
}
303307

304308
operator[IS_OPERATOR] = true

0 commit comments

Comments
 (0)