Last active
August 5, 2024 06:33
-
-
Save styk-tv/e0ff0cb7a7f1a0b12368cf7efee55acc to your computer and use it in GitHub Desktop.
xr-utils.js
This file contains 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
// xr-utils.js | |
class XrUtils { | |
constructor(storage) { | |
this.storageMock = storage; | |
} | |
eventEgress(dataSource) { | |
return (target, propertyKey, descriptor) => { | |
const originalMethod = descriptor.value; | |
descriptor.value = (...args) => { | |
const payload = originalMethod.apply(this, args); | |
if (!this.storageMock[dataSource]) { | |
this.storageMock[dataSource] = []; | |
} | |
this.storageMock[dataSource].push(payload); | |
return payload; | |
}; | |
return descriptor; | |
}; | |
} | |
async getDataById(datasource, key) { | |
const entries = this.storageMock[datasource]; | |
if (entries) { | |
for (const entry of entries) { | |
if (entry[key] !== undefined) { | |
return entry[key]; | |
} | |
} | |
} | |
return null; | |
} | |
} | |
const storage = {}; | |
const utilsInstance = new XrUtils(storage); | |
// Expose these as part of a global object | |
window.xrUtils = { | |
eventEgress: utilsInstance.eventEgress.bind(utilsInstance), | |
getDataById: utilsInstance.getDataById.bind(utilsInstance), | |
storage, // central storage | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment