mirror of
				https://github.com/livebook-dev/livebook.git
				synced 2025-10-26 21:36:02 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			34 lines
		
	
	
	
		
			675 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			34 lines
		
	
	
	
		
			675 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| const PREFIX = "livebook:";
 | |
| 
 | |
| /**
 | |
|  * Loads value from local storage.
 | |
|  */
 | |
| export function load(key) {
 | |
|   try {
 | |
|     const json = localStorage.getItem(PREFIX + key);
 | |
| 
 | |
|     if (json) {
 | |
|       return JSON.parse(json);
 | |
|     }
 | |
|   } catch (error) {
 | |
|     console.error(
 | |
|       `Failed to load from local storage, reason: ${error.message}`
 | |
|     );
 | |
|   }
 | |
| 
 | |
|   return undefined;
 | |
| }
 | |
| 
 | |
| /**
 | |
|  * Stores value in local storage.
 | |
|  *
 | |
|  * The value is serialized as JSON.
 | |
|  */
 | |
| export function store(key, value) {
 | |
|   try {
 | |
|     const json = JSON.stringify(value);
 | |
|     localStorage.setItem(PREFIX + key, json);
 | |
|   } catch (error) {
 | |
|     console.error(`Failed to write to local storage, reason: ${error.message}`);
 | |
|   }
 | |
| }
 |