JSFiddle - React, Tailwind, and code Playground
by Nidhi Patel
JavaScript
function decryptEncrypt(key, payload, callback) {
const newKey = someProcessToGenerateNewKey(key);
const message = someProcessToDecrypt(payload, key);
callback({ key: newKey, message });
}
class Decryption {
constructor(initialKey) {
this.key = initialKey;
this.callbackQueue = [];
}
decrypt(payload, messageCallback) {
this.callbackQueue.push(messageCallback);
decryptEncrypt(this.key, payload, ({ key: newKey, message }) => {
this.key = newKey;
this.processQueue(message);
});
}
processQueue(message) {
while (this.callbackQueue.length > 0) {
const callback = this.callbackQueue.shift();
callback(message);
}
}
}
const mgr = new Decryption('initialKey');
function decryptAndLog(encryptedPayload) {
mgr.decrypt(encryptedPayload, (decryptedMessage) => {
console.log('Decrypted message:', decryptedMessage);
});
}
function someProcessToGenerateNewKey(oldKey) {
return oldKey + "new";
}
function someProcessToDecrypt(payload, key) {
return "decrypted" + payload;
}
decryptAndLog('payload')