volume-localstorage.js 4.3 KB

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