svgedit/packages/svgcanvas/dataStorage.js

29 lines
949 B
JavaScript
Raw Normal View History

2021-12-27 03:14:18 +00:00
/** A storage solution aimed at replacing jQuerys data function.
* Implementation Note: Elements are stored in a (WeakMap)[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap].
* This makes sure the data is garbage collected when the node is removed.
*/
const dataStorage = {
_storage: new WeakMap(),
put: function (element, key, obj) {
if (!this._storage.has(element)) {
this._storage.set(element, new Map())
2021-12-27 03:14:18 +00:00
}
this._storage.get(element).set(key, obj)
2021-12-27 03:14:18 +00:00
},
get: function (element, key) {
return this._storage.get(element)?.get(key)
2021-12-27 03:14:18 +00:00
},
has: function (element, key) {
return this._storage.has(element) && this._storage.get(element).has(key)
2021-12-27 03:14:18 +00:00
},
remove: function (element, key) {
const ret = this._storage.get(element).delete(key)
if (this._storage.get(element).size === 0) {
this._storage.delete(element)
2021-12-27 03:14:18 +00:00
}
return ret
2021-12-27 03:14:18 +00:00
}
}
2021-12-27 03:14:18 +00:00
export default dataStorage