Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to specify prefix for value #281

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export interface Settings {
readonly retryDelay: number;
readonly retryJitter: number;
readonly automaticExtensionThreshold: number;
readonly valuePrefix: string;
}

// Define default settings.
Expand All @@ -109,6 +110,7 @@ const defaultSettings: Readonly<Settings> = {
retryDelay: 200,
retryJitter: 100,
automaticExtensionThreshold: 500,
valuePrefix: '',
};

// Modifyng this object is forbidden.
Expand Down Expand Up @@ -232,6 +234,10 @@ export default class Redlock extends EventEmitter {
typeof settings.automaticExtensionThreshold === "number"
? settings.automaticExtensionThreshold
: defaultSettings.automaticExtensionThreshold,
valuePrefix:
typeof settings.valuePrefix === "string"
? settings.valuePrefix
: defaultSettings.valuePrefix,
};

// Use custom scripts and script modifiers.
Expand Down Expand Up @@ -303,7 +309,7 @@ export default class Redlock extends EventEmitter {
throw new Error("Duration must be an integer value in milliseconds.");
}

const value = this._random();
const value = (settings?.valuePrefix ?? this.settings.valuePrefix) + this._random();

try {
const { attempts, start } = await this._execute(
Expand Down