feat: add destroy method for backward compatibility

This commit is contained in:
leeching 2018-07-03 10:07:25 +08:00
parent 834e5939d8
commit 1ffe2b985e
1 changed files with 22 additions and 1 deletions

View File

@ -55,7 +55,23 @@ function getLong(buffer, offset) {
class IP2Region {
static create(dbPath) {
return new IP2Region({dbPath});
const oldInstance = IP2Region._instances.get(dbPath);
if (oldInstance) {
return oldInstance;
} else {
const instance = new IP2Region({ dbPath });
IP2Region._instances.set(dbPath, instance);
return instance;
}
}
/**
* For backward compatibility
*/
static destroy() {
IP2Region._instances.forEach(([key, instance]) => {
instance.destroy();
});
}
constructor(options = {}) {
@ -72,6 +88,8 @@ class IP2Region {
);
}
IP2Region._instances.set((this.dbPath = dbPath), this);
this.totalBlocks = this.firstIndexPtr = this.lastIndexPtr = 0;
this.calcTotalBlocks();
@ -87,6 +105,7 @@ class IP2Region {
*/
destroy() {
fs.closeSync(ip2rObj.dbFd);
IP2Region._instances.delete(this.dbPath);
}
/**
@ -279,4 +298,6 @@ class IP2Region {
}
}
IP2Region._instances = new Map();
module.exports = IP2Region;