MCPcopy
hub / github.com/sunnylqm/react-native-storage / _saveToMap

Method _saveToMap

storage.js:77–112  ·  view source on GitHub ↗
(params)

Source from the content-addressed store, hash-verified

75 return key + '_' + id;
76 }
77 _saveToMap(params) {
78 let { key, id, data } = params,
79 newId = this._getId(key, id),
80 m = this._m;
81 if(m[newId] !== undefined) {
82 //update existed data
83 if(this.enableCache) this.cache[newId] = JSON.parse(data);
84 return this.setItem('map_' + m[newId], data);
85 }
86 if(m[m.index] !== undefined){
87 //loop over, delete old data
88 let oldId = m[m.index];
89 let splitOldId = oldId.split('_')
90 delete m[oldId];
91 this._removeIdInKey(splitOldId[0], splitOldId[1])
92 if(this.enableCache) {
93 delete this.cache[oldId];
94 }
95 }
96 m[newId] = m.index;
97 m[m.index] = newId;
98
99 m.__keys__[key] = m.__keys__[key] || [];
100 m.__keys__[key].push(id);
101
102 if(this.enableCache) {
103 const cacheData = JSON.parse(data);
104 this.cache[newId] = cacheData;
105 }
106 let currentIndex = m.index;
107 if(++m.index === this._SIZE) {
108 m.index = 0;
109 }
110 this.setItem('map_' + currentIndex, data);
111 this.setItem('map', JSON.stringify(m));
112 }
113 save(params) {
114 const { key, id, data, rawData, expires = this.defaultExpires } = params;
115 if (key.toString().indexOf('_') !== -1) {

Callers 1

saveMethod · 0.95

Calls 3

_getIdMethod · 0.95
setItemMethod · 0.95
_removeIdInKeyMethod · 0.95

Tested by

no test coverage detected