@atlaskit/editor-common
Version:
A package that contains common classes and components for editor and renderer
37 lines (36 loc) • 1.02 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import { EXPERIENCE_FAILURE_REASON } from './consts';
const DEFAULT_FAILURE_RESULT = {
status: 'failure',
reason: EXPERIENCE_FAILURE_REASON.TIMEOUT
};
/**
* Check for the completion of an experience based on a timeout
*
* By default, will result in failure with reason 'timeout' after the specified duration.
*
* Can be customized for different results on timeout via the onTimeout callback.
*/
export class ExperienceCheckTimeout {
constructor({
durationMs,
onTimeout = () => DEFAULT_FAILURE_RESULT
}) {
_defineProperty(this, "durationMs", 0);
this.durationMs = durationMs;
this.onTimeout = onTimeout;
}
start(callback) {
this.stop();
this.timeoutId = setTimeout(() => {
const result = this.onTimeout() || DEFAULT_FAILURE_RESULT;
callback(result);
}, this.durationMs);
}
stop() {
if (this.timeoutId) {
clearTimeout(this.timeoutId);
this.timeoutId = undefined;
}
}
}