* Set the storage value along with the current timestamp. * When opt_isUpdated is true, timestamp will be the creation timestamp, * the stored value will be updated w/o updating timestamp. * @param {string} name * @param {*} value * @param {boolean=} opt_isUpdate
(name, value, opt_isUpdate)
| 235 | * @param {boolean=} opt_isUpdate |
| 236 | */ |
| 237 | set(name, value, opt_isUpdate) { |
| 238 | devAssert( |
| 239 | name != '__proto__' && name != 'prototype', |
| 240 | 'Name is not allowed: %s', |
| 241 | name |
| 242 | ); |
| 243 | // The structure is {key: {v: *, t: time}} |
| 244 | if (this.values_[name] !== undefined) { |
| 245 | const item = this.values_[name]; |
| 246 | let timestamp = Date.now(); |
| 247 | if (opt_isUpdate) { |
| 248 | // Update value w/o timestamp |
| 249 | timestamp = item['t']; |
| 250 | } |
| 251 | item['v'] = value; |
| 252 | item['t'] = timestamp; |
| 253 | } else { |
| 254 | this.values_[name] = { |
| 255 | 'v': value, |
| 256 | 't': Date.now(), |
| 257 | }; |
| 258 | } |
| 259 | |
| 260 | // Purge old values. |
| 261 | const keys = Object.keys(this.values_); |
| 262 | if (keys.length > this.maxValues_) { |
| 263 | let minTime = Infinity; |
| 264 | let minKey = null; |
| 265 | for (let i = 0; i < keys.length; i++) { |
| 266 | const item = this.values_[keys[i]]; |
| 267 | if (item['t'] < minTime) { |
| 268 | minKey = keys[i]; |
| 269 | minTime = item['t']; |
| 270 | } |
| 271 | } |
| 272 | if (minKey) { |
| 273 | delete this.values_[minKey]; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * @param {string} name |
no test coverage detected