@mackenly/zaraz-tools
Version:
Unofficial 3rd party toolkit for Zaraz
38 lines • 1.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.actionAlreadyFired = actionAlreadyFired;
/**
* actionAlreadyFired
* Checks if an action has already been fired based on a unique action key and a specified duration.
* @param client The client object to get/set the timestamp. Not private because it is stored in the client.
* @param actionKey The unique action key.
* @param scope The duration to remember the action (default: 'session').
* @param expiry The expiry date for the action (default: null).
* @param dontStore If true, the timestamp will not be stored in the client (default: false). Useful for checking without setting.
* @returns: The Date of when the action was fired, or false if not.
* @throws: Error if the operation fails.
* @since 2024-09-19
*/
function actionAlreadyFired(client, actionKey, scope = 'session', expiry = null, dontStore = false) {
try {
const storageKey = `action-fired-${actionKey}`;
const timestamp = client.get(storageKey);
if (timestamp) {
return new Date(timestamp);
}
const now = new Date().toISOString();
let options = { scope };
if (expiry) {
options = Object.assign(Object.assign({}, options), { expiry });
}
if (!dontStore) {
client.set(storageKey, now, options);
}
return false;
}
catch (error) {
console.error(error);
throw error;
}
}
//# sourceMappingURL=actionAlreadyFired.js.map