UNPKG

4.17 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 };
9 return function (d, b) {
10 extendStatics(d, b);
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13 };
14})();
15Object.defineProperty(exports, "__esModule", { value: true });
16exports.createVolume = exports.ObjectStore = void 0;
17var volume_1 = require("./volume");
18var node_1 = require("./node");
19var ObjectStore = /** @class */ (function () {
20 function ObjectStore(obj) {
21 this.obj = obj;
22 }
23 ObjectStore.prototype.setItem = function (key, json) {
24 this.obj[key] = JSON.stringify(json);
25 };
26 ObjectStore.prototype.getItem = function (key) {
27 var data = this.obj[key];
28 if (typeof data === void 0)
29 return void 0;
30 return JSON.parse(data);
31 };
32 ObjectStore.prototype.removeItem = function (key) {
33 delete this.obj[key];
34 };
35 return ObjectStore;
36}());
37exports.ObjectStore = ObjectStore;
38function createVolume(namespace, LS) {
39 if (LS === void 0) { LS = localStorage; }
40 var store = new ObjectStore(LS);
41 var key = function (type, id) { return "memfs." + namespace + "." + type + "." + id; };
42 var NodeLocalStorage = /** @class */ (function (_super) {
43 __extends(NodeLocalStorage, _super);
44 function NodeLocalStorage() {
45 return _super !== null && _super.apply(this, arguments) || this;
46 }
47 Object.defineProperty(NodeLocalStorage.prototype, "Key", {
48 get: function () {
49 if (!this._key)
50 this._key = key('ino', this.ino);
51 return this._key;
52 },
53 enumerable: false,
54 configurable: true
55 });
56 NodeLocalStorage.prototype.sync = function () {
57 store.setItem(this.Key, this.toJSON());
58 };
59 NodeLocalStorage.prototype.touch = function () {
60 _super.prototype.touch.call(this);
61 this.sync();
62 };
63 NodeLocalStorage.prototype.del = function () {
64 _super.prototype.del.call(this);
65 store.removeItem(this.Key);
66 };
67 return NodeLocalStorage;
68 }(node_1.Node));
69 var LinkLocalStorage = /** @class */ (function (_super) {
70 __extends(LinkLocalStorage, _super);
71 function LinkLocalStorage() {
72 return _super !== null && _super.apply(this, arguments) || this;
73 }
74 Object.defineProperty(LinkLocalStorage.prototype, "Key", {
75 get: function () {
76 if (!this._key)
77 this._key = key('link', this.getPath());
78 return this._key;
79 },
80 enumerable: false,
81 configurable: true
82 });
83 LinkLocalStorage.prototype.sync = function () {
84 store.setItem(this.Key, this.toJSON());
85 };
86 return LinkLocalStorage;
87 }(node_1.Link));
88 return /** @class */ (function (_super) {
89 __extends(VolumeLocalStorage, _super);
90 function VolumeLocalStorage() {
91 return _super.call(this, {
92 Node: NodeLocalStorage,
93 Link: LinkLocalStorage,
94 }) || this;
95 }
96 VolumeLocalStorage.prototype.createLink = function (parent, name, isDirectory, perm) {
97 var link = _super.prototype.createLink.call(this, parent, name, isDirectory, perm);
98 store.setItem(key('link', link.getPath()), link.toJSON());
99 return link;
100 };
101 VolumeLocalStorage.prototype.deleteLink = function (link) {
102 store.removeItem(key('link', link.getPath()));
103 return _super.prototype.deleteLink.call(this, link);
104 };
105 return VolumeLocalStorage;
106 }(volume_1.Volume));
107}
108exports.createVolume = createVolume;