Skip to content

Commit a452023

Browse files
committed
Incorporated review changes
1 parent 310f181 commit a452023

File tree

2 files changed

+33
-60
lines changed

2 files changed

+33
-60
lines changed

packages/datafile-manager/__test__/reactNativeAsyncStorageCache.spec.ts

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,57 +23,41 @@ describe('reactNativeAsyncStorageCache', () => {
2323
cacheInstance = new ReactNativeAsyncStorageCache()
2424
})
2525

26-
describe('get', function() {
27-
it('should return correct object when item is found in cache', function(done) {
28-
cacheInstance.get('keyThatExists')
29-
.then(v => {
30-
expect(v).toEqual({ name: "Awesome Object" })
31-
done()
32-
})
26+
describe('get', function() {
27+
it('should return correct object when item is found in cache', function() {
28+
return cacheInstance.get('keyThatExists')
29+
.then(v => expect(v).toEqual({ name: "Awesome Object" }))
3330
})
3431

35-
it('should return null if item is not found in cache', function(done) {
36-
cacheInstance.get('keyThatDoesNotExist')
37-
.then(v => {
38-
expect(v).toBeNull()
39-
done()
40-
})
32+
it('should return null if item is not found in cache', function() {
33+
return cacheInstance.get('keyThatDoesNotExist').then(v => expect(v).toBeNull())
4134
})
4235

43-
it('should reject promise error if string has an incorrect JSON format', function(done) {
44-
cacheInstance.get('keyWithInvalidJsonObject')
45-
.catch(e => {
46-
done()
47-
})
36+
it('should reject promise error if string has an incorrect JSON format', function() {
37+
return cacheInstance.get('keyWithInvalidJsonObject').catch(() => {})
4838
})
4939
})
5040

5141
describe('set', function() {
52-
it('should resolve promise if item was successfully set in the cache', function(done) {
42+
it('should resolve promise if item was successfully set in the cache', function() {
5343
const testObj = { name: "Awesome Object" }
54-
cacheInstance.set('testKey', testObj).then(() => done())
44+
return cacheInstance.set('testKey', testObj)
5545
})
5646

57-
it('should reject promise if item was not set in the cache because of json stringifying error', function(done) {
47+
it('should reject promise if item was not set in the cache because of json stringifying error', function() {
5848
const testObj: any = { name: "Awesome Object" }
5949
testObj.myOwnReference = testObj
60-
cacheInstance.set('testKey', testObj).catch(() => done())
50+
return cacheInstance.set('testKey', testObj).catch(() => {})
6151
})
6252
})
6353

64-
describe('contains', function() {
65-
it('should return true if object with key exists', function(done) {
66-
cacheInstance.contains('keyThatExists').then(v => {
67-
expect(v).toBeTruthy()
68-
done()
69-
})
54+
describe('contains', function() {
55+
it('should return true if object with key exists', function() {
56+
return cacheInstance.contains('keyThatExists').then(v => expect(v).toBeTruthy())
7057
})
7158

72-
it('should return false if object with key does not exist', function(done) {
73-
cacheInstance.contains('keyThatDoesNotExist').then(v => {
74-
expect(v).toBeFalsy()
75-
done()
76-
})
59+
it('should return false if object with key does not exist', function() {
60+
return cacheInstance.contains('keyThatDoesNotExist').then(v => expect(v).toBeFalsy())
7761
})
7862
})
7963
})

packages/datafile-manager/src/reactNativeAsyncStorageCache.ts

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,26 @@ const logger = getLogger('DatafileManager')
2424
export default class ReactNativeAsyncStorageCache implements PersistentKeyValueCache {
2525

2626
get(key: string): Promise<Object | null> {
27-
return new Promise((resolve, reject) => {
28-
AsyncStorage.getItem(key).then((val: string | null) => {
29-
if (val) {
30-
try {
31-
resolve(JSON.parse(val))
32-
} catch (ex) {
33-
logger.error('Error Parsing Object from cache - %s', ex)
34-
reject(ex)
35-
}
36-
} else {
37-
resolve(null)
38-
}
39-
}).catch(reject)
40-
})
27+
try {
28+
return AsyncStorage.getItem(key)
29+
.then((val: string | null) => val ? JSON.parse(val) : null)
30+
} catch (ex) {
31+
logger.error('Error Parsing Object from cache - %s', ex)
32+
return Promise.resolve(ex);
33+
}
4134
}
42-
43-
set(key: string, val: Object): Promise<void> {
44-
return new Promise((resolve, reject) => {
45-
try {
46-
AsyncStorage.setItem(key, JSON.stringify(val)).then(resolve).catch(reject)
47-
} catch (e) {
48-
logger.error('Error stringifying Object to Json - %s', e)
49-
reject(e)
50-
}
51-
})
35+
36+
set(key: string, val: Object): Promise<void> {
37+
try {
38+
return AsyncStorage.setItem(key, JSON.stringify(val))
39+
} catch (e) {
40+
logger.error('Error stringifying Object to Json - %s', e)
41+
return Promise.reject(e)
42+
}
5243
}
5344

5445
contains(key: string): Promise<Boolean> {
55-
return new Promise((resolve, reject) =>
56-
AsyncStorage.getItem(key).then((val: string | null) => resolve(val !== null)).catch(reject)
57-
)
46+
return AsyncStorage.getItem(key).then((val: string | null) => (val !== null))
5847
}
5948

6049
remove(key: string): Promise<void> {

0 commit comments

Comments
 (0)