Skip to content

Instantly share code, notes, and snippets.

@styk-tv
Last active August 5, 2024 06:33
Show Gist options
  • Save styk-tv/e0ff0cb7a7f1a0b12368cf7efee55acc to your computer and use it in GitHub Desktop.
Save styk-tv/e0ff0cb7a7f1a0b12368cf7efee55acc to your computer and use it in GitHub Desktop.
xr-utils.js
// 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