persistent-typescript
Version:
An easy way to have persistent data on the browser and on nodejs
136 lines (135 loc) • 5.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var Utils_1 = require("./utils/Utils");
var Map_1 = require("./utils/Map");
var fs = require("fs");
var process = require("process");
var path = require("path");
var Storage = /** @class */ (function () {
function Storage() {
var This = this;
this.persistentObjects = new Map_1.Map();
this.persistentObjectsMetadata = new Map_1.Map();
if (Utils_1.Utils.isBrowser()) {
window.addEventListener("unload", function (event) {
This.save();
});
}
else {
process.on('SIGINT', function () {
This.save();
process.exit();
});
process.on('SIGHUP', function () {
This.save();
process.exit();
});
process.on('SIGQUIT', function () {
This.save();
process.exit();
});
process.on('SIGTERM', function () {
This.save();
process.exit();
});
process.on('uncaughtException', function () {
This.save();
process.exit();
});
process.on('exit', function () {
This.save();
process.exit();
});
}
}
/**
* Loads a persistent file to retrieve the classes information stored in it
* @param options Persistent option, with theses options you can choose your saver, loader and the path to the file
* @param force Force to reload the file
*/
Storage.prototype.loadPersistentFile = function (options, force) {
try {
var filePath = path.resolve(options.path);
var data = void 0;
if (this.persistentObjects.containsKey(filePath) == true && !force)
return;
this.persistentObjects.put(filePath, options.plugin.init());
this.persistentObjectsMetadata.put(filePath, options);
if (Utils_1.Utils.isBrowser())
data = localStorage.getItem(filePath);
else
data = fs.readFileSync(filePath, "utf8");
if (data == null)
return;
this.persistentObjects.put(filePath, options.plugin.deserialize(data));
this.persistentObjectsMetadata.put(filePath, options);
}
catch (err) {
if (options.debug) {
console.log("Failed to load the persistent file '" + options.path + "'");
console.log("Error: " + err);
}
}
};
/**
* Stores the class instance to the storage
* @param classInstance class instance to store
* @param options Persistent option, with theses options you can choose your saver, loader and the path to the file
*/
Storage.prototype.store = function (classInstance, options) {
this.loadPersistentFile(options, false);
var className = Utils_1.Utils.getClassName(classInstance);
var filePath = path.resolve(options.path);
if (className == null)
return;
var savedClass = options.plugin.get(this.persistentObjects.getValue(filePath), className);
if (savedClass != null) {
for (var field in savedClass) {
classInstance[field] = savedClass[field];
}
}
options.plugin.put(this.persistentObjects.getValue(filePath), className, classInstance);
};
/**
* Retrieves a class instance by its name and its options
* @param className class name
* @param options Persistent option, with theses options you can choose your saver, loader and the path to the file
*/
Storage.prototype.getInstanceOfClass = function (className, options) {
var savedClass = options.plugin.get(this.persistentObjects.getValue(path.resolve(options.path)), className);
return savedClass;
};
/**
* Saves all classes stored in the storage using the plugin specified
*/
Storage.prototype.save = function () {
var option;
var instance;
for (var _i = 0, _a = this.persistentObjects.keys(); _i < _a.length; _i++) {
var key = _a[_i];
option = this.persistentObjectsMetadata.getValue(key);
instance = this.persistentObjects.getValue(key);
if (option.debug)
console.log("Trying to save the instance of the object '" + Utils_1.Utils.getClassName(instance) + "' to the path '" + key + "'");
if (Utils_1.Utils.isBrowser())
localStorage.setItem(key, option.plugin.serialize(instance));
else
fs.writeFileSync(key, option.plugin.serialize(instance));
if (option.debug)
console.log("The instance of the object '" + Utils_1.Utils.getClassName(instance) + "' has been saved to the path '" + key + "'");
}
};
/**
* Returns the number of class saved
*/
Storage.prototype.getSize = function () {
return this.persistentObjects.size();
};
return Storage;
}());
exports.Storage = Storage;
;
//@ts-ignore
if (exports.storage == undefined) {
exports.storage = new Storage();
}