Last active
August 1, 2024 03:19
-
-
Save dav1app/61684d3f9396b95768fac228562189c7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MemoryManager { | |
constructor() { | |
this.currentSignature = []; | |
this.previousSignature = []; | |
this.currentData = []; | |
this.previousData = []; | |
} | |
add(signature, data) { | |
if (signature === undefined) { | |
throw new Error('Signature bool is required'); | |
} | |
this.currentSignature.push(signature); | |
this.currentData.push(data); | |
} | |
swap() { | |
this.previousSignature = [...this.currentSignature]; | |
this.currentSignature = []; | |
this.previousData = [...this.currentData]; | |
this.currentData = []; | |
} | |
check() { | |
if (!this.previousSignature.length) return; // First run | |
const currentSignatureJSON = JSON.stringify(this.currentSignature); | |
const previousSignatureJSON = JSON.stringify(this.previousSignature); | |
const currentDataJSON = JSON.stringify(this.currentData); | |
const previousDataJSON = JSON.stringify(this.previousData); | |
if (!this.arraysEqual(this.currentSignature, this.previousSignature)) { | |
this.reset(); | |
throw new Error(`Invalid! \n${currentSignatureJSON}\n${previousSignatureJSON}\n${currentDataJSON}\n${previousDataJSON}`); | |
} | |
// console.log(`Valid! \n${currentSignatureJSON}\n${previousSignatureJSON}\n${currentDataJSON}\n${previousDataJSON}`); | |
} | |
arraysEqual(arr1, arr2) { | |
if (arr1.length !== arr2.length) return false; | |
for (let i = 0; i < arr1.length; i++) { | |
if (arr1[i] !== arr2[i]) return false; | |
} | |
return true; | |
} | |
reset() { | |
this.currentSignature = []; | |
this.previousSignature = []; | |
this.currentData = []; | |
this.previousData = []; | |
} | |
} | |
const i = new MemoryManager() | |
module.exports = i; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment