()
| 54 | * localStorage. Throw an error if Store is malformed. |
| 55 | */ |
| 56 | export function initStoreFromUrlOrLocalStorage(): Store { |
| 57 | const encodedSourceFromUrl = location.hash.slice(1); |
| 58 | const encodedSourceFromLocal = localStorage.getItem('playgroundStore'); |
| 59 | const encodedSource = encodedSourceFromUrl || encodedSourceFromLocal; |
| 60 | |
| 61 | /** |
| 62 | * No data in the URL and no data in the localStorage to fallback to. |
| 63 | * Initialize with the default store. |
| 64 | */ |
| 65 | if (!encodedSource) return defaultStore; |
| 66 | |
| 67 | const raw: any = decodeStore(encodedSource); |
| 68 | |
| 69 | invariant(isValidStore(raw), 'Invalid Store'); |
| 70 | |
| 71 | // Make sure all properties are populated |
| 72 | return { |
| 73 | source: raw.source, |
| 74 | config: 'config' in raw && raw['config'] ? raw.config : defaultConfig, |
| 75 | showInternals: 'showInternals' in raw ? raw.showInternals : false, |
| 76 | }; |
| 77 | } |
no test coverage detected