Skip to content

Commit

Permalink
Updated storage class to make it work with non-browser storage system
Browse files Browse the repository at this point in the history
  • Loading branch information
baptistegreve committed Apr 26, 2023
1 parent e304992 commit 8d0f9d7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@orbisclub/orbis-sdk",
"version": "0.4.50",
"version": "0.4.51",
"description": "Official package to implement quickly an Orbis powered decentralized social layer within your application.",
"author": "Baptiste Grève",
"license": "ISC",
Expand Down
44 changes: 23 additions & 21 deletions store.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,33 @@
export class Store {
/** Type of storage we want to use: 'localStorage' or 'AsyncStorage' */
type;
storeAsync = false;

/** Initialize storage with one of the supported options */
constructor(options) {
/** Save storage type */
if(options && options.type) {
if(options.type == "localStorage" || options.type == "AsyncStorage") {
this.type = options.type;
} else {
console.log("Type not supported, defaulting to localStorage.");
this.type = "localStorage";
}
this.type = options.type;
} else {
this.type = "localStorage";
this.type = localStorage;
}

/** Save storage async settings */
if(options && options.storeAsync) {
this.storeAsync = options.storeAsync;
}
}

/** Function to set an item storage */
async setItem(key, value) {
switch (this.type) {
switch (this.storeAsync) {
/** Browser storage */
case "localStorage":
localStorage.setItem(key, value);
case false:
this.type.setItem(key, value);
return true;

/** Async storage */
default:
case true:
await this.type.setItem(key, value);
return true;
}
Expand All @@ -36,31 +38,31 @@ export class Store {
async getItem(key) {

let res;
switch (this.type) {
switch (this.storeAsync) {
/** Browser storage */
case "localStorage":
res = localStorage.getItem(key);
case false:
res = this.type.getItem(key);
break;

/** Async storage */
case "AsyncStorage":
res = await AsyncStorage.getItem(key);
case true:
res = await this.type.getItem(key);
break;
}
return res;
}

/** Function to remove an item from the local storage */
async removeItem(key) {
switch (this.type) {
switch (this.storeAsync) {
/** Browser storage */
case "localStorage":
localStorage.removeItem(key);
case false:
this.type.removeItem(key);
return true;

/** Async storage */
case "AsyncStorage":
await AsyncStorage.removeItem(key);
case true:
await this.type.removeItem(key);
return true;
}
}
Expand Down

0 comments on commit 8d0f9d7

Please sign in to comment.