| Jeremy Mordkoff | 03156e3 | 2017-09-30 21:42:44 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2017 RIFT.IO Inc |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | */ |
| 18 | import appConfiguration from '../utils/appConfiguration' |
| 19 | |
| 20 | let versionKeyPrefix = null; |
| 21 | |
| 22 | const localCache = new class { |
| 23 | get(key) { |
| 24 | let valueAsString = localStorage.getItem(key); |
| 25 | return valueAsString ? JSON.parse(valueAsString) : undefined; |
| 26 | } |
| 27 | set(key, val) { |
| 28 | localStorage.setItem(key, typeof val === 'string' ? val : JSON.stringify(val)); |
| 29 | } |
| 30 | }(); |
| 31 | |
| 32 | let objCache = new Map(); |
| 33 | |
| 34 | const storeCache = new class { |
| 35 | get(key) { |
| 36 | if (objCache[key]) { |
| 37 | objCache[key].timerId && clearTimeout(objCache[key].timerId) |
| 38 | objCache[key].timerId = setTimeout((key) => delete objCache[key], 2000) |
| 39 | return objCache[key].value; |
| 40 | } |
| 41 | const obj = localCache.get(key); |
| 42 | if (obj) { |
| 43 | objCache[key] = { |
| 44 | value: obj, |
| 45 | timerId: setTimeout((key) => delete objCache[key], 2000) |
| 46 | } |
| 47 | return obj; |
| 48 | } |
| 49 | } |
| 50 | set(key, obj) { |
| 51 | setTimeout(localCache.set, 100, key, obj); |
| 52 | objCache[key] = { |
| 53 | value: obj, |
| 54 | timerId: setTimeout((key) => delete objCache[key], 2000) |
| 55 | } |
| 56 | } |
| 57 | init(version) { |
| 58 | versionKeyPrefix = 's-v-' + version; |
| 59 | const currentStoreVersion = localStorage.getItem('store-version'); |
| 60 | if (currentStoreVersion !== version) { |
| 61 | let removeItems = []; |
| 62 | for (let i = 0; i < localStorage.length; ++i) { |
| 63 | let key = localStorage.key(i); |
| 64 | if (key.startsWith('s-v-')) { |
| 65 | removeItems.push(key); |
| 66 | } |
| 67 | } |
| 68 | removeItems.forEach((key) => localStorage.removeItem(key)); |
| 69 | localStorage.setItem('store-version', version); |
| 70 | } |
| 71 | } |
| 72 | }(); |
| 73 | |
| 74 | class StoreCache { |
| 75 | constructor(name) { |
| 76 | this.name = 's-v-' + name; |
| 77 | } |
| 78 | get(key) { |
| 79 | return storeCache.get(this.name + key); |
| 80 | } |
| 81 | set(key, obj) { |
| 82 | storeCache.set(this.name + key, obj); |
| 83 | } |
| 84 | init() { |
| 85 | return versionKeyPrefix ? Promise.resolve() : new Promise( |
| 86 | (resolve, reject) => { |
| 87 | appConfiguration.get().then((config) => { |
| 88 | storeCache.init(config.version); |
| 89 | resolve(); |
| 90 | }) |
| 91 | } |
| 92 | ) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | module.exports = StoreCache; |