Skip to content

Commit

Permalink
fix: Prevent queue being blocked by errors thrown from onOk
Browse files Browse the repository at this point in the history
  • Loading branch information
SevenOutman committed Oct 21, 2020
1 parent 045a72d commit a0052a4
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 39 deletions.
29 changes: 20 additions & 9 deletions src/InteractionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,32 @@ class InteractionManager {
new Error(`No such interaction '${method}' found.`)
);
}
return this.performInteraction(method, ...args);
}

performInteraction(method, ...args) {
const tail = this.$tail;
const newTail = this.$tail
? this.queueInteraction(this.$tail, method, ...args)
: this.performInteraction(method, ...args);

this.$tail = new Promise((resolve) => {
Promise.resolve(tail).finally(() => {
const interactionMethod = this.getInteractionMethod(method);
resolve(interactionMethod(...args));
});
newTail.finally(() => {
if (this.$tail === newTail) {
this.$tail = null;
}
});
this.$tail = newTail;

return this.$tail;
}

queueInteraction(tail, method, ...args) {
return new Promise((resolve, reject) => {
tail.finally(() => {
this.performInteraction(method, ...args).then(resolve, reject);
});
});
}

performInteraction(method, ...args) {
return this.getInteractionMethod(method)(...args);
}
}

export default new InteractionManager();
24 changes: 14 additions & 10 deletions src/alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import getContainerDOM from './getContainerDOM';
import { isFunction } from './utils';

export default function alert(message, modalConfig) {
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
ReactDOM.render(
<InteractionModal
key={Date.now()}
Expand All @@ -16,16 +16,20 @@ export default function alert(message, modalConfig) {
resolve();
return;
}
const result = modalConfig.onOk();
if (!(result instanceof Promise)) {
resolve();
return;
try {
const result = modalConfig.onOk();
if (!(result instanceof Promise)) {
resolve();
return;
}
result.then((resolved) => {
resolve();
return resolved;
}, reject);
return result;
} catch (e) {
reject(e);
}
result.then((resolved) => {
resolve();
return resolved;
});
return result;
}}
>
{message}
Expand Down
24 changes: 14 additions & 10 deletions src/confirm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import getContainerDOM from './getContainerDOM';
import { isFunction } from './utils';

export default function confirm(message, modalConfig) {
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
ReactDOM.render(
<InteractionModal
key={Date.now()}
Expand All @@ -16,16 +16,20 @@ export default function confirm(message, modalConfig) {
resolve(true);
return;
}
const result = modalConfig.onOk();
if (!(result instanceof Promise)) {
resolve(true);
return;
try {
const result = modalConfig.onOk();
if (!(result instanceof Promise)) {
resolve(true);
return;
}
result.then((resolved) => {
resolve(true);
return resolved;
}, reject);
return result;
} catch (e) {
reject(e);
}
result.then((resolved) => {
resolve(true);
return resolved;
});
return result;
}}
onCancel={(isSubmitLoading) => {
modalConfig?.onCancel?.(isSubmitLoading);
Expand Down
24 changes: 14 additions & 10 deletions src/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function PromptModal({ message, defaultResult = '', onOk, ...props }) {
}

export default function prompt(message, _default, modalConfig) {
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
ReactDOM.render(
<PromptModal
key={Date.now()}
Expand All @@ -37,16 +37,20 @@ export default function prompt(message, _default, modalConfig) {
resolve(...args);
return;
}
const result = modalConfig.onOk(...args);
if (!(result instanceof Promise)) {
resolve(...args);
return;
try {
const result = modalConfig.onOk(...args);
if (!(result instanceof Promise)) {
resolve(...args);
return;
}
result.then((resolved) => {
resolve(...args);
return resolved;
}, reject);
return result;
} catch (e) {
reject(e);
}
result.then((resolved) => {
resolve(...args);
return resolved;
});
return result;
}}
onCancel={(isSubmitLoading) => {
modalConfig?.onCancel?.(isSubmitLoading);
Expand Down

0 comments on commit a0052a4

Please sign in to comment.