UNPKG

@silver-zepp/easy-storage

Version:

The Easy Storage library offers a suite of storage solutions designed for ZeppOS applications, providing a range of data persistence strategies to accommodate different data management needs.

3 lines (2 loc) 2.82 kB
/** @about Easy Storage @min_zeppos 2.0 @author: Silver, Zepp Health. @license: MIT */ import{debugLog,readFile}from"./core/core";import{AsyncStorage,nd_tokenDecode}from"./async-storage";export class EasyStorageAsync{#filename;#content_obj={};#autosave=true;#is_ready=false;constructor(filename="easy_storage.json",callback){this.#filename=filename;AsyncStorage.ReadJson(filename,(err,data)=>{if(!this.#is_ready){if(!err&&data)this.#content_obj=data;this.#is_ready=true}if(callback)callback(err,this)})}isReady(){return this.#is_ready}synchronize(){if(this.#is_ready)return true;try{const raw=readFile(this.#filename);if(raw)this.#content_obj=this.#parseMultiSync(raw)}catch(e){debugLog(1,"[ASYNC] synchronize() read error:",e)}this.#is_ready=true;return this.#content_obj!==null}setKey(key,value,callback){debugLog(2,`[ASYNC] Saving key: ${key} with value: ${value}`);this.#content_obj[key]=value;if(this.#autosave){this.saveAll(callback)}else if(callback){callback(null,true)}}getKey(key,default_value=""){debugLog(3,`[ASYNC] Retrieving key: ${key}`);if(Object.prototype.hasOwnProperty.call(this.#content_obj,key)){const value=this.#content_obj[key];debugLog(2,`[ASYNC] Found value for key '${key}': ${value}`);return value}debugLog(3,`[ASYNC] Key '${key}' not found, returning default: ${default_value}`);return default_value===""?undefined:default_value}hasKey(key){return Object.prototype.hasOwnProperty.call(this.#content_obj,key)}removeKey(key,callback){delete this.#content_obj[key];if(this.#autosave){this.saveAll(callback)}else if(callback){callback(null,true)}}saveAll(callback){AsyncStorage.WriteJson(this.#filename,this.#content_obj,callback||function(){})}deleteAll(callback){this.#content_obj={};this.saveAll(callback)}getStorageSnapshot(stringify=false){return stringify?JSON.stringify(this.#content_obj):this.#content_obj}saveAndQuit(){return AsyncStorage.SaveAndQuit()}SetAutosaveEnable(bool){this.#autosave=bool}SetStorageFilename(filename){this.#filename=filename}GetStorageFilename(){return this.#filename}printContents(){console.log("Storage contents: "+JSON.stringify(this.#content_obj))}isBusy(){return AsyncStorage.IsBusy()}#parseMultiSync(raw){const lines=raw.trim().split("\n");if(!lines.length)return{};let first;try{first=nd_tokenDecode(JSON.parse(lines[0]))}catch(_){return JSON.parse(raw)}if(!first||first.type!=="meta"){return JSON.parse(raw)}const res={};const arrays={};for(let i=0;i<lines.length;i++){let obj;try{obj=nd_tokenDecode(JSON.parse(lines[i]))}catch(_){continue}if(obj.type==="meta"){const arr_fields=obj.__arrays||[];Object.keys(obj).forEach(k=>{if(k==="type"||k==="__arrays")return;if(arr_fields.includes(k)){arrays[k]=[];res[k]=arrays[k]}else{res[k]=obj[k]}})}else{const k=obj.type;if(!arrays[k]){arrays[k]=[];res[k]=arrays[k]}arrays[k].push(obj.data)}}return res}}