Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilianoSanchez committed Nov 30, 2023
1 parent e4aaadc commit 58ca122
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions src/storages/inRedis/RedisAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,29 +73,14 @@ export class RedisAdapter extends ioredis {
_setTimeoutWrappers() {
const instance: Record<string, any> = this;

const wrapWithQueueOrExecute = (method: string, commandWrapper: Function) => {
if (instance._notReadyCommandsQueue) {
return new Promise((resolve, reject) => {
instance._notReadyCommandsQueue.unshift({
resolve,
reject,
command: commandWrapper,
name: method.toUpperCase()
});
});
} else {
return commandWrapper();
}
};

const wrapCommand = (originalMethod: Function, methodName: string) => {
// The value of "this" in this function should be the instance actually executing the method. It might be the instance referred (the base one)
// or it can be the instance of a Pipeline object.
return function (this: RedisAdapter | Pipeline) {
const params = arguments;
const caller: (Pipeline | RedisAdapter) = this;
const caller = this;

const commandWrapper = () => {
function commandWrapper() {
instance.log.debug(`${LOG_PREFIX}Executing ${methodName}.`);
const result = originalMethod.apply(caller, params);

Expand All @@ -117,9 +102,20 @@ export class RedisAdapter extends ioredis {
}

return result;
};
}

return wrapWithQueueOrExecute(methodName, commandWrapper);
if (instance._notReadyCommandsQueue) {
return new Promise((resolve, reject) => {
instance._notReadyCommandsQueue.unshift({
resolve,
reject,
command: commandWrapper,
name: methodName.toUpperCase()
});
});
} else {
return commandWrapper();
}
};
};

Expand All @@ -134,8 +130,6 @@ export class RedisAdapter extends ioredis {
const originalFn = instance[methodName];
// "First level wrapper" to handle the sync execution and wrap async, queueing later if applicable.
instance[methodName] = function () {
instance.log.debug(`${LOG_PREFIX} Creating ${methodName}.`);

const res = originalFn.apply(instance, arguments);
const originalExec = res.exec;

Expand Down

0 comments on commit 58ca122

Please sign in to comment.