Skip to content

Commit

Permalink
reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
ammarahm-ed committed Apr 30, 2021
1 parent 3a273b7 commit 4248f55
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 223 deletions.
21 changes: 10 additions & 11 deletions docs/usemmkvstorage.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,32 @@ Now whenever you update value of `"user"` in storage, your `App` component will
```jsx
setUser("andrew");
//or you cal call setUser without a value to remove the value
setUser() //removes the value from storage.

setUser(); //removes the value from storage.

// or you can do this too anywhere in your app:
MMKV.setString("user", "andrew");
```


Simple right? now refresh the app or restart it. When it loads, it will always show andrew as the user until you update it.
The ideal way which I would recommend for better development experience would to wrap `useMMKVStorage` hook in a custom hook as follows:

```jsx
const MMKV = new MMKVStorage.Loader().initialize();

export const useStorage = (key) => {
const [value,setValue] = useMMKVStorage(key,MMKV);
return [value,setValue];
}
const [value, setValue] = useMMKVStorage(key, MMKV);
return [value, setValue];
};
```

Now you don't have to import `MMKV` everywhere in your app but only once. If you use TypeScript you can do something like below to get nice intellisense in your editor.

```tsx
const MMKV:MMKVStorage.API = new MMKVStorage.Loader().initialize();
const MMKV: MMKVStorage.API = new MMKVStorage.Loader().initialize();
type LiteralUnion<T extends U, U = string> = T | (U & {});

export const useStorage = (key:LiteralUnion<"user" | "password">) => {
const [value,setValue] = useMMKVStorage(key,MMKV);
return [value,setValue];
}
export const useStorage = (key: LiteralUnion<"user" | "password">) => {
const [value, setValue] = useMMKVStorage(key, MMKV);
return [value, setValue];
};
```
22 changes: 11 additions & 11 deletions src/encryption.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import {initialize, currentInstancesStatus} from './initializer';
import generatePassword from './keygen';
import {stringToHex} from './utils';
import {handleActionAsync} from './handlers';
import IDStore from './mmkv/IDStore';
import { initialize, currentInstancesStatus } from "./initializer";
import generatePassword from "./keygen";
import { stringToHex } from "./utils";
import { handleActionAsync } from "./handlers";
import IDStore from "./mmkv/IDStore";

function encryptStorage(
options,
key,
secureKeyStorage = true,
alias,
accessibleMode,
callback,
callback
) {
if (secureKeyStorage) {
global.setSecureKey(alias, key, accessibleMode);
Expand Down Expand Up @@ -71,7 +71,7 @@ export default class encryption {
reject(e);
}
resolve(r);
},
}
);
} else {
initialize(this.options, (e) => {
Expand All @@ -90,7 +90,7 @@ export default class encryption {
reject(e);
}
resolve(r);
},
}
);
});
}
Expand All @@ -108,7 +108,7 @@ export default class encryption {
key,
secureKeyStorage = true,
alias,
accessibleMode,
accessibleMode
) {
if (accessibleMode) {
this.accessibleMode = accessibleMode;
Expand Down Expand Up @@ -142,7 +142,7 @@ export default class encryption {
reject(e);
}
resolve(r);
},
}
);
} else {
initialize(this.options, (e) => {
Expand All @@ -161,7 +161,7 @@ export default class encryption {
reject(e);
}
resolve(r);
},
}
);
});
}
Expand Down
86 changes: 42 additions & 44 deletions src/eventmanager.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,44 @@

class EventManager {
constructor() {
this._registry = {};
}

unsubscribeAll() {
this._registry = {};
}

subscribeMulti(names, handler) {
names.forEach((name) => {
this.subscribe(name, handler);
});
}

subscribe(name, handler) {
if (!name || !handler) throw new Error("name and handler are required.");
if (!this._registry[name]) this._registry[name] = [];
this._registry[name].push(handler);
}

unsubscribe(name, handler) {
if (!this._registry[name]) return;
const index = this._registry[name].indexOf(handler);
if (index <= -1) return;
this._registry[name].splice(index, 1);
}

publish(name, ...args) {
if (!this._registry[name]) return;
const handlers = this._registry[name];
handlers.forEach((handler) => {
handler(...args);
});
}

async publishWithResult(name, ...args) {
if (!this._registry[name]) return true;
const handlers = this._registry[name];
if (handlers.length <= 0) return true;
return await Promise.all(handlers.map((handler) => handler(...args)));
}
constructor() {
this._registry = {};
}

unsubscribeAll() {
this._registry = {};
}

subscribeMulti(names, handler) {
names.forEach((name) => {
this.subscribe(name, handler);
});
}

subscribe(name, handler) {
if (!name || !handler) throw new Error("name and handler are required.");
if (!this._registry[name]) this._registry[name] = [];
this._registry[name].push(handler);
}

unsubscribe(name, handler) {
if (!this._registry[name]) return;
const index = this._registry[name].indexOf(handler);
if (index <= -1) return;
this._registry[name].splice(index, 1);
}

publish(name, ...args) {
if (!this._registry[name]) return;
const handlers = this._registry[name];
handlers.forEach((handler) => {
handler(...args);
});
}

async publishWithResult(name, ...args) {
if (!this._registry[name]) return true;
const handlers = this._registry[name];
if (handlers.length <= 0) return true;
return await Promise.all(handlers.map((handler) => handler(...args)));
}
export default EventManager;
}
export default EventManager;
7 changes: 3 additions & 4 deletions src/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
* @param {...any} args Arguments for the native function
*/

import { options } from 'react-native-mmkv-storage/src/utils';
import { currentInstancesStatus, initialize } from './initializer';
import { options } from "react-native-mmkv-storage/src/utils";
import { currentInstancesStatus, initialize } from "./initializer";

export function handleAction(action, ...args) {
let id = args[args.length - 1];
Expand All @@ -28,12 +28,11 @@ export function handleAction(action, ...args) {
if (!ready) return undefined;
if (!action) return;
return action(...args);

}

export async function handleActionAsync(action, ...args) {
let id = args[args.length - 1];
return new Promise(async (resolve,reject) => {
return new Promise(async (resolve, reject) => {
if (currentInstancesStatus[id]) {
if (!action) {
resolve(undefined);
Expand Down
Loading

0 comments on commit 4248f55

Please sign in to comment.