Skip to content

Commit

Permalink
optimization code
Browse files Browse the repository at this point in the history
  • Loading branch information
52cik committed May 30, 2018
1 parent e1497ca commit 0b36484
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
9 changes: 8 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
interface MapLike<K, V> {
get(key: K): Promise<any>;
set(key: K, value: V): Promise<boolean>;
delete(key: K): Promise<boolean>;
clear(): Promise<void>;
}

interface iOptions {
/** Namespace for the current instance. */
prefix?: string;
Expand All @@ -6,7 +13,7 @@ interface iOptions {
/** A custom deserialization function. */
deserialize?: (data: string) => any;
/** The storage adapter instance to be used by MaybeStore. */
store?: any;
store?: MapLike<string, any>;
/** Default TTL. Can be overridden by specififying a TTL on `.set()`. */
ttl?: number;
}
Expand Down
6 changes: 1 addition & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ const defaultOption = {
}),
};

function pTry(cb) {
return new Promise((resolve) => {
resolve(cb());
});
}
const pTry = cb => new Promise(resolve => resolve(cb()));

class MaybeStore {
constructor(opts = {}) {
Expand Down
11 changes: 11 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,14 @@ test('MaybeStore throw Error', async (t) => {
const error = await t.throws(mStore.set('foo', 'bar'));
t.is(error.message, 'serialize error');
});

test('MaybeStore throw Error store.set', async (t) => {
const store = {
set() {
throw Error('store.set error');
},
};
const mStore = new MaybeStore({ store });
const error = await t.throws(mStore.set('foo', 'bar'));
t.is(error.message, 'store.set error');
});

0 comments on commit 0b36484

Please sign in to comment.