Skip to content

Instantly share code, notes, and snippets.

@BruJu
Last active July 9, 2020 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BruJu/245847a131a8e13cb0629d979e62bd27 to your computer and use it in GitHub Desktop.
Save BruJu/245847a131a8e13cb0629d979e62bd27 to your computer and use it in GitHub Desktop.
Destructor
// Not yet tested because WeakRef isn't supported on my browsers
// Actually that's bad because Destructors will exist :
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry
class Destructor {
constructor(object, destructor, time) {
this.object = new WeakRef(object);
this.destructor = destructor;
this.requestFutureCheckIfDead(time);
}
requestFutureCheckIfDead(time) {
let self = this;
setInterval(function () {
if (self.object.deref() === undefined) {
self.destructor();
} else {
self.requestFutureCheckIfDead(time);
}
}, time);
}
}
function makeDestructorForWasmTreeDataset(ds, time) {
let tree = ds.tree;
return new Destructor(ds, () => tree.free(), time);
}
function makeDestructorForSophiaWasmDataset(ds, time) {
let ptr = ds.ptr;
let freeFunction = ds.prototype.free;
return new Destructor(ds, () => freeFunction.call({ ptr: ptr }), time);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment