forked from aws-amplify/amplify-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
upgrade core and api (aws-amplify#4498)
Co-authored-by: manueliglesias <[email protected]> Co-authored-by: elorzafe <[email protected]>
- Loading branch information
1 parent
b40a1c2
commit ec06c62
Showing
7 changed files
with
274 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/*! | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2016 Christian Speckner <[email protected]> | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
import { Mutex } from '../src/Util'; | ||
|
||
describe('Mutex', function() { | ||
let mutex: Mutex; | ||
|
||
beforeEach(() => (mutex = new Mutex())); | ||
|
||
test('ownership is exclusive', function() { | ||
let flag = false; | ||
|
||
mutex.acquire().then(release => | ||
setTimeout(() => { | ||
flag = true; | ||
release(); | ||
}, 50) | ||
); | ||
|
||
return mutex.acquire().then(release => { | ||
release(); | ||
|
||
expect(flag).toBe(true); | ||
}); | ||
}); | ||
|
||
test('runExclusive passes result (immediate)', function() { | ||
return mutex | ||
.runExclusive<number>(() => 10) | ||
.then(value => expect(value).toBe(10)); | ||
}); | ||
|
||
test('runExclusive passes result (promise)', function() { | ||
return mutex | ||
.runExclusive<number>(() => Promise.resolve(10)) | ||
.then(value => expect(value).toBe(10)); | ||
}); | ||
|
||
test('runExclusive passes rejection', function() { | ||
return mutex | ||
.runExclusive<number>(() => Promise.reject('foo')) | ||
.then( | ||
() => Promise.reject('should have been rejected'), | ||
value => expect(value).toBe('foo') | ||
); | ||
}); | ||
|
||
test('runExclusive passes exception', function() { | ||
return mutex | ||
.runExclusive<number>(() => { | ||
throw 'foo'; | ||
}) | ||
.then( | ||
() => Promise.reject('should have been rejected'), | ||
value => expect(value).toBe('foo') | ||
); | ||
}); | ||
|
||
test('runExclusive is exclusive', function() { | ||
let flag = false; | ||
|
||
mutex.runExclusive( | ||
() => | ||
new Promise(resolve => | ||
setTimeout(() => { | ||
flag = true; | ||
resolve(); | ||
}, 50) | ||
) | ||
); | ||
|
||
return mutex.runExclusive(() => expect(flag).toBe(true)); | ||
}); | ||
|
||
test('exceptions during runExclusive do not leave mutex locked', function() { | ||
let flag = false; | ||
|
||
mutex | ||
.runExclusive<number>(() => { | ||
flag = true; | ||
throw new Error(); | ||
}) | ||
.then( | ||
() => undefined, | ||
() => undefined | ||
); | ||
|
||
return mutex.runExclusive(() => expect(flag).toBe(true)); | ||
}); | ||
|
||
test('new mutex is unlocked', function() { | ||
expect(!mutex.isLocked()).toBe(true); | ||
}); | ||
|
||
test('isLocked reflects the mutex state', async function() { | ||
const lock1 = mutex.acquire(), | ||
lock2 = mutex.acquire(); | ||
|
||
expect(mutex.isLocked()).toBe(true); | ||
|
||
const releaser1 = await lock1; | ||
|
||
expect(mutex.isLocked()).toBe(true); | ||
|
||
releaser1(); | ||
|
||
expect(mutex.isLocked()).toBe(true); | ||
|
||
const releaser2 = await lock2; | ||
|
||
expect(mutex.isLocked()).toBe(true); | ||
|
||
releaser2(); | ||
|
||
expect(!mutex.isLocked()).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/*! | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2016 Christian Speckner <[email protected]> | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
interface MutexInterface { | ||
acquire(): Promise<MutexInterface.Releaser>; | ||
|
||
runExclusive<T>(callback: MutexInterface.Worker<T>): Promise<T>; | ||
|
||
isLocked(): boolean; | ||
} | ||
|
||
namespace MutexInterface { | ||
export interface Releaser { | ||
(): void; | ||
} | ||
|
||
export interface Worker<T> { | ||
(): Promise<T> | T; | ||
} | ||
} | ||
|
||
class Mutex implements MutexInterface { | ||
isLocked(): boolean { | ||
return this._pending; | ||
} | ||
|
||
acquire(): Promise<MutexInterface.Releaser> { | ||
const ticket = new Promise<MutexInterface.Releaser>(resolve => | ||
this._queue.push(resolve) | ||
); | ||
|
||
if (!this._pending) { | ||
this._dispatchNext(); | ||
} | ||
|
||
return ticket; | ||
} | ||
|
||
runExclusive<T>(callback: MutexInterface.Worker<T>): Promise<T> { | ||
return this.acquire().then(release => { | ||
let result: T | Promise<T>; | ||
|
||
try { | ||
result = callback(); | ||
} catch (e) { | ||
release(); | ||
throw e; | ||
} | ||
|
||
return Promise.resolve(result).then( | ||
(x: T) => (release(), x), | ||
e => { | ||
release(); | ||
throw e; | ||
} | ||
); | ||
}); | ||
} | ||
|
||
private _dispatchNext(): void { | ||
if (this._queue.length > 0) { | ||
this._pending = true; | ||
this._queue.shift()!(this._dispatchNext.bind(this)); | ||
} else { | ||
this._pending = false; | ||
} | ||
} | ||
|
||
private _queue: Array<(release: MutexInterface.Releaser) => void> = []; | ||
private _pending = false; | ||
} | ||
|
||
export default Mutex; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import * as Observable from 'zen-observable'; | ||
|
||
type NetworkStatus = { | ||
online: boolean; | ||
}; | ||
|
||
export default class ReachabilityNavigator implements Reachability { | ||
networkMonitor(): Observable<NetworkStatus> { | ||
return new Observable(observer => { | ||
observer.next({ online: window.navigator.onLine }); | ||
|
||
const notifyOnline = () => observer.next({ online: true }); | ||
const notifyOffline = () => observer.next({ online: false }); | ||
|
||
window.addEventListener('online', notifyOnline); | ||
window.addEventListener('offline', notifyOffline); | ||
|
||
return () => { | ||
window.removeEventListener('online', notifyOnline); | ||
window.removeEventListener('offline', notifyOffline); | ||
}; | ||
}); | ||
} | ||
} | ||
|
||
interface Reachability { | ||
networkMonitor(): Observable<NetworkStatus>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './Retry'; | ||
export { default as Mutex } from './Mutex'; | ||
export { default as Reachability } from './Reachability'; |