Skip to content

Instantly share code, notes, and snippets.

@andreastt
Last active December 10, 2019 13:30
Show Gist options
  • Save andreastt/0d081f4767d7d8ffdaea8c08386bfc9c to your computer and use it in GitHub Desktop.
Save andreastt/0d081f4767d7d8ffdaea8c08386bfc9c to your computer and use it in GitHub Desktop.
"use strict";
var EXPORTED_SYMBOLS = ["StreamHandleCache"];
var singleton;
class StreamHandleCache extends Map {
constructor() {
super();
Services.obs.addObserver(this, "xpcom-will-shutdown");
}
destructor() {
for (const [handle, stream] of this.entries()) {
// do whatever is necessary to destroy stream
}
}
observe(subject, topic, data) {
this.destructor();
}
static get() {
if (!singleton) {
singleton = new StreamHandleCache();
}
return singleton;
}
}
// In bc test:
const { StreamHandleCache } = ChromeUtils.import(…);
const cache = StreamHandleCache.get();
add_task(function() {
cache.add(handle, new Stream());
cache.destructor();
is(cache.size, 0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment