Skip to content

Commit 1765b5d

Browse files
committed
πŸ‘¨β€πŸ’» v5 CommonJS build
1 parent 11fc920 commit 1765b5d

File tree

1 file changed

+47
-28
lines changed

1 file changed

+47
-28
lines changed

β€Žindex.cjsβ€Ž

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -728,11 +728,14 @@ class JoSk {
728728
* Cancel (abort) current interval timer.
729729
* Must be called in a separate event loop from `.setInterval()`
730730
* @name clearInterval
731-
* @param {string} timerId - Unique function (task) identification as a string, returned from `.setInterval()`
731+
* @param {string|Promise<string>} timerId - Unique function (task) identification as a string, returned from `.setInterval()`
732732
* @param {function} [callback] - optional callback
733733
* @returns {Promise<boolean>} - `true` if task cleared, `false` if task doesn't exist
734734
*/
735735
async clearInterval(timerId) {
736+
if (typeof timerId === 'object' && timerId instanceof Promise) {
737+
return await this.__remove(await timerId);
738+
}
736739
return await this.__remove(timerId);
737740
}
738741

@@ -742,11 +745,14 @@ class JoSk {
742745
* Cancel (abort) current timeout timer.
743746
* Must be called in a separate event loop from `.setTimeout()`
744747
* @name clearTimeout
745-
* @param {string} timerId - Unique function (task) identification as a string, returned from `.setTimeout()`
748+
* @param {string|Promise<string>} timerId - Unique function (task) identification as a string, returned from `.setTimeout()`
746749
* @param {function} [callback] - optional callback
747750
* @returns {Promise<boolean>} - `true` if task cleared, `false` if task doesn't exist
748751
*/
749752
async clearTimeout(timerId) {
753+
if (typeof timerId === 'object' && timerId instanceof Promise) {
754+
return await this.__remove(await timerId);
755+
}
750756
return await this.__remove(timerId);
751757
}
752758

@@ -841,6 +847,7 @@ class JoSk {
841847
}
842848
throw error;
843849
}
850+
844851
const date = new Date();
845852
const timestamp = +date;
846853

@@ -870,6 +877,7 @@ class JoSk {
870877
return true;
871878
};
872879

880+
let hasError;
873881
let returnedPromise;
874882
try {
875883
if (task.isInterval === false) {
@@ -880,6 +888,7 @@ class JoSk {
880888
} catch (removeError) {
881889
this._debug(`[${task.uid}] [__execute] [__remove] has thrown an exception; Check connection with StorageAdapter; removeError:`, removeError);
882890
}
891+
883892
if (isRemoved === true) {
884893
returnedPromise = originalTask(ready);
885894
}
@@ -889,41 +898,51 @@ class JoSk {
889898

890899
if (returnedPromise && returnedPromise instanceof Promise) {
891900
await returnedPromise;
901+
} else {
902+
return;
892903
}
893904
} catch (taskExecError) {
905+
hasError = true;
894906
this.__errorHandler(taskExecError, 'Exception during task execution', 'An exception was thrown during task execution', task.uid);
895907
}
896908

897-
await ready();
898-
} else {
899-
await this.adapter.update(task, new Date(Date.now() + this.zombieTime));
900-
this.tasks[task.uid] = function () { };
901-
this.tasks[task.uid].isMissing = true;
902-
903-
if (this.autoClear) {
909+
if ((returnedPromise && returnedPromise instanceof Promise) || (executionsQty === 0 && hasError)) {
904910
try {
905-
await this.__remove(task.uid);
906-
this._debug(`[FYI] [${task.uid}] task was auto-cleared`);
907-
} catch (removeError) {
908-
this._debug(`[${task.uid}] [__execute] [this.autoClear] [__remove] has thrown an exception; removeError:`, removeError);
911+
await ready();
912+
} catch (readyErr) {
913+
this._debug(`[${task.uid}] [__execute] [ready] has thrown an exception; readyErr:`, readyErr);
909914
}
910-
} else if (this.onError) {
911-
this.onError('One of your tasks is missing', {
912-
description: `Something went wrong with one of your tasks - is missing.
913-
Try to use different instances.
914-
It's safe to ignore this message.
915-
If this task is obsolete - simply remove it with \`JoSk#clearTimeout('${task.uid}')\`,
916-
or enable autoClear with \`new JoSk({autoClear: true})\``,
917-
error: null,
918-
uid: task.uid
919-
});
920-
} else {
921-
this._debug(`[__execute] [${task.uid}] Something went wrong with one of your tasks is missing.
915+
}
916+
return;
917+
}
918+
919+
await this.adapter.update(task, new Date(Date.now() + this.zombieTime));
920+
this.tasks[task.uid] = function () { };
921+
this.tasks[task.uid].isMissing = true;
922+
923+
if (this.autoClear) {
924+
try {
925+
await this.__remove(task.uid);
926+
this._debug(`[FYI] [${task.uid}] task was auto-cleared`);
927+
} catch (removeError) {
928+
this._debug(`[${task.uid}] [__execute] [this.autoClear] [__remove] has thrown an exception; removeError:`, removeError);
929+
}
930+
} else if (this.onError) {
931+
this.onError('One of your tasks is missing', {
932+
description: `Something went wrong with one of your tasks - is missing.
922933
Try to use different instances.
923934
It's safe to ignore this message.
924-
If this task is obsolete - simply remove it with \`JoSk#clearTimeout(\'${task.uid}\')\`,
925-
or enable autoClear with \`new JoSk({autoClear: true})\``);
926-
}
935+
If this task is obsolete - simply remove it with \`JoSk#clearTimeout('${task.uid}')\`,
936+
or enable autoClear with \`new JoSk({autoClear: true})\``,
937+
error: null,
938+
uid: task.uid
939+
});
940+
} else {
941+
this._debug(`[__execute] [${task.uid}] Something went wrong with one of your tasks is missing.
942+
Try to use different instances.
943+
It's safe to ignore this message.
944+
If this task is obsolete - simply remove it with \`JoSk#clearTimeout(\'${task.uid}\')\`,
945+
or enable autoClear with \`new JoSk({autoClear: true})\``);
927946
}
928947
}
929948

0 commit comments

Comments
Β (0)