-
-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathstate.js
1 lines (1 loc) · 1.44 KB
/
state.js
1
const fs=require("uxp").storage.localFileSystem,STATE_FILE="state.json";class LocalState{constructor(t){this.serverUrl=t}async load(){let t=null,e={};try{const a=await fs.getDataFolder();console.log("Data folder: "+a);try{t=await a.getEntry(STATE_FILE)}catch(t){console.log("State file does NOT exist ? ")}if(t){const a=await t.read();e=JSON.parse(a)}else t=await a.createFile(STATE_FILE,{overwrite:!0}),await t.write("{}"),console.log("No data settings file. Created one.")}catch(t){console.log("Failed to get state"),console.log(t)}this.data=e}getPrefImageQuality(){return this.data.imageQuality}setPrefImageQuality(t){this.data||(this.data={}),this.data.imageQuality=t}getHash(t){for(var e=0,a=0;a<t.length;a++){e=(e<<5)-e+t.charCodeAt(a),e&=e}return e}addLastSynced(t,e){this.data[t]={hash:t,last_synced:Date.now(),project_id:e}}getLastSynced(t){return this.data[t]}async saveData(){try{const t=await fs.getDataFolder(),e=await t.createFile(STATE_FILE,{overwrite:!0});await e.write(JSON.stringify(this.data,null,4))}catch(t){console.log("Failed to overwrite the state"),console.log(t)}}setCurrentProject(t){this.currentProject=t}getCurrentProject(){return this.currentProject}writeStats(t,e,a){let r=this.serverUrl+"/api/analytics/event/V2";fetch(r,{method:"POST",headers:{"Content-Type":"application/json"},credentials:"include",body:JSON.stringify({account_id:"xd_plugin:5.0.2",campaign_id:"xd_plugin",key:t,page:e,value:a})})}}module.exports={LocalState:LocalState};