local_store.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * DevExtreme (data/local_store.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. var eventsEngine = require("../events/core/events_engine");
  11. var domAdapter = require("../core/dom_adapter");
  12. var windowUtils = require("../core/utils/window");
  13. var window = windowUtils.getWindow();
  14. var Class = require("../core/class");
  15. var abstract = Class.abstract;
  16. var errors = require("./errors").errors;
  17. var ArrayStore = require("./array_store");
  18. var LocalStoreBackend = Class.inherit({
  19. ctor: function(store, storeOptions) {
  20. this._store = store;
  21. this._dirty = !!storeOptions.data;
  22. this.save();
  23. var immediate = this._immediate = storeOptions.immediate;
  24. var flushInterval = Math.max(100, storeOptions.flushInterval || 1e4);
  25. if (!immediate) {
  26. var saveProxy = this.save.bind(this);
  27. setInterval(saveProxy, flushInterval);
  28. eventsEngine.on(window, "beforeunload", saveProxy);
  29. if (window.cordova) {
  30. domAdapter.listen(domAdapter.getDocument(), "pause", saveProxy, false)
  31. }
  32. }
  33. },
  34. notifyChanged: function() {
  35. this._dirty = true;
  36. if (this._immediate) {
  37. this.save()
  38. }
  39. },
  40. load: function() {
  41. this._store._array = this._loadImpl();
  42. this._dirty = false
  43. },
  44. save: function() {
  45. if (!this._dirty) {
  46. return
  47. }
  48. this._saveImpl(this._store._array);
  49. this._dirty = false
  50. },
  51. _loadImpl: abstract,
  52. _saveImpl: abstract
  53. });
  54. var DomLocalStoreBackend = LocalStoreBackend.inherit({
  55. ctor: function(store, storeOptions) {
  56. var name = storeOptions.name;
  57. if (!name) {
  58. throw errors.Error("E4013")
  59. }
  60. this._key = "dx-data-localStore-" + name;
  61. this.callBase(store, storeOptions)
  62. },
  63. _loadImpl: function() {
  64. var raw = window.localStorage.getItem(this._key);
  65. if (raw) {
  66. return JSON.parse(raw)
  67. }
  68. return []
  69. },
  70. _saveImpl: function(array) {
  71. if (!array.length) {
  72. window.localStorage.removeItem(this._key)
  73. } else {
  74. window.localStorage.setItem(this._key, JSON.stringify(array))
  75. }
  76. }
  77. });
  78. var localStoreBackends = {
  79. dom: DomLocalStoreBackend
  80. };
  81. var LocalStore = ArrayStore.inherit({
  82. ctor: function(options) {
  83. if ("string" === typeof options) {
  84. options = {
  85. name: options
  86. }
  87. } else {
  88. options = options || {}
  89. }
  90. this.callBase(options);
  91. this._backend = new localStoreBackends[options.backend || "dom"](this, options);
  92. this._backend.load()
  93. },
  94. clear: function() {
  95. this.callBase();
  96. this._backend.notifyChanged()
  97. },
  98. _insertImpl: function(values) {
  99. var b = this._backend;
  100. return this.callBase(values).done(b.notifyChanged.bind(b))
  101. },
  102. _updateImpl: function(key, values) {
  103. var b = this._backend;
  104. return this.callBase(key, values).done(b.notifyChanged.bind(b))
  105. },
  106. _removeImpl: function(key) {
  107. var b = this._backend;
  108. return this.callBase(key).done(b.notifyChanged.bind(b))
  109. }
  110. }, "local");
  111. module.exports = LocalStore;
  112. module.exports.default = module.exports;