Merge pull request #71 from Leeds-eBooks/patch/flow-errors-2

Fixed several flow errors
This commit is contained in:
Michael Diarmid 2017-04-28 12:03:17 +01:00 committed by GitHub
commit 8a545439a4
5 changed files with 76 additions and 28 deletions

View File

@ -83,7 +83,12 @@ export default class Database extends Base {
* @param origCB
* @returns {*}
*/
off(refId: number, listeners: Array<DatabaseListener>, remainingListenersCount: number) {
off(
refId: number,
// $FlowFixMe
listeners: Array<DatabaseListener>,
remainingListenersCount: number
) {
this.log.debug('off() : ', refId, listeners);
// Delete the reference if there are no more listeners

View File

@ -151,23 +151,25 @@ export default class Reference extends ReferenceBase {
*/
off(eventName?: string = '', origCB?: () => any) {
this.log.debug('ref.off(): ', this.refId, eventName);
// $FlowFixMe
const listeners: Array<DatabaseListener> = Object.values(this.listeners);
let listenersToRemove;
if (eventName && origCB) {
listenersToRemove = Object.values(this.listeners).filter((listener) => {
listenersToRemove = listeners.filter((listener) => {
return listener.eventName === eventName && listener.successCallback === origCB;
});
// Only remove a single listener as per the web spec
if (listenersToRemove.length > 1) listenersToRemove = [listenersToRemove[0]];
} else if (eventName) {
listenersToRemove = Object.values(this.listeners).filter((listener) => {
listenersToRemove = listeners.filter((listener) => {
return listener.eventName === eventName;
});
} else if (origCB) {
listenersToRemove = Object.values(this.listeners).filter((listener) => {
listenersToRemove = listeners.filter((listener) => {
return listener.successCallback === origCB;
});
} else {
listenersToRemove = Object.values(this.listeners);
listenersToRemove = listeners;
}
// Remove the listeners from the reference to prevent memory leaks
listenersToRemove.forEach((listener) => {
@ -183,7 +185,7 @@ export default class Reference extends ReferenceBase {
* @param onComplete
* @param applyLocally
*/
transaction(transactionUpdate: Function, onComplete, applyLocally: boolean = false) {
transaction(transactionUpdate: Function, onComplete: (?Error, boolean, ?Snapshot) => *, applyLocally: boolean = false) {
if (!isFunction(transactionUpdate)) return Promise.reject(new Error('Missing transactionUpdate function argument.'));
return new Promise((resolve, reject) => {

View File

@ -80,7 +80,7 @@ export default class StorageReference extends ReferenceBase {
* Alias to putFile
* @returns {StorageReference.putFile}
*/
get put() {
get put(): Function {
return this.putFile;
}

View File

@ -11,11 +11,13 @@ declare type UploadTaskSnapshotType = {
downloadURL: string|null,
metadata: Object, // TODO flow type def for https://firebase.google.com/docs/reference/js/firebase.storage.FullMetadata.html
ref: StorageReference,
state: StorageStatics.TaskState.RUNNING
|StorageStatics.TaskState.PAUSED
|StorageStatics.TaskState.SUCCESS
|StorageStatics.TaskState.CANCELLED
|StorageStatics.TaskState.ERROR,
state: (
typeof StorageStatics.TaskState.RUNNING
| typeof StorageStatics.TaskState.PAUSED
| typeof StorageStatics.TaskState.SUCCESS
| typeof StorageStatics.TaskState.CANCELLED
| typeof StorageStatics.TaskState.ERROR
),
task: StorageTask,
totalBytes: number,
};
@ -24,15 +26,26 @@ declare type FuncSnapshotType = null|(snapshot: UploadTaskSnapshotType) => any;
declare type FuncErrorType = null|(error: Error) => any;
declare type NextOrObserverType = null
|{ next?: FuncSnapshotType, error?: FuncErrorType, complete?:FuncSnapshotType }
|FuncSnapshotType;
declare type NextOrObserverType = null |
{
next?: FuncSnapshotType,
error?: FuncErrorType,
complete?:FuncSnapshotType
} |
FuncSnapshotType;
/**
* @url https://firebase.google.com/docs/reference/js/firebase.storage.UploadTask
*/
export default class StorageTask {
constructor(type: UPLOAD_TASK|DOWNLOAD_TASK, promise: Promise, storageRef: StorageReference) {
type: typeof UPLOAD_TASK | typeof DOWNLOAD_TASK
ref: StorageReference
storage: StorageReference.storage
path: StorageReference.path
then: Promise<*>
catch: () => Promise<*>
constructor(type: typeof UPLOAD_TASK | typeof DOWNLOAD_TASK, promise: Promise<*>, storageRef: StorageReference) {
this.type = type;
this.ref = storageRef;
this.storage = storageRef.storage;
@ -49,13 +62,13 @@ export default class StorageTask {
* @returns {Promise.<T>}
* @private
*/
_interceptSnapshotEvent(f: Function|null|undefined): null|() => any {
_interceptSnapshotEvent(f: ?Function): null | () => * {
if (!isFunction(f)) return null;
return (snapshot) => {
const _snapshot = Object.assign({}, snapshot);
_snapshot.task = this;
_snapshot.ref = this.ref;
return f(_snapshot);
return f && f(_snapshot);
};
}
@ -65,12 +78,13 @@ export default class StorageTask {
* @returns {*}
* @private
*/
_interceptErrorEvent(f: Function|null|undefined): null|() => any {
_interceptErrorEvent(f: ?Function): null | (Error) => * {
if (!isFunction(f)) return null;
return (error) => {
const _error = new Error(error.message);
// $FlowFixMe
_error.code = error.code;
return f(_error);
return f && f(_error);
};
}
@ -83,15 +97,41 @@ export default class StorageTask {
* @private
*/
_subscribe(nextOrObserver: NextOrObserverType, error: FuncErrorType, complete: FuncSnapshotType): Function {
const observer = isObject(nextOrObserver);
let _error;
let _next;
let _complete;
const _error = this._interceptErrorEvent(observer ? nextOrObserver.error : error);
const _next = this._interceptSnapshotEvent(observer ? nextOrObserver.next : nextOrObserver);
const _complete = this._interceptSnapshotEvent(observer ? nextOrObserver.complete : complete);
if (typeof nextOrObserver === 'function') {
_error = this._interceptErrorEvent(error);
_next = this._interceptSnapshotEvent(nextOrObserver);
_complete = this._interceptSnapshotEvent(complete);
} else if (nextOrObserver) {
_error = this._interceptErrorEvent(nextOrObserver.error);
_next = this._interceptSnapshotEvent(nextOrObserver.next);
_complete = this._interceptSnapshotEvent(nextOrObserver.complete);
}
if (_next) this.storage._addListener(this.path, StorageStatics.TaskEvent.STATE_CHANGED, _next);
if (_error) this.storage._addListener(this.path, `${this.type}_failure`, _error);
if (_complete) this.storage._addListener(this.path, `${this.type}_success`, _complete);
if (_next) {
this.storage._addListener(
this.path,
StorageStatics.TaskEvent.STATE_CHANGED,
_next
);
}
if (_error) {
this.storage._addListener(
this.path,
`${this.type}_failure`,
_error
);
}
if (_complete) {
this.storage._addListener(
this.path,
`${this.type}_success`,
_complete
);
}
return () => {
if (_next) this.storage._removeListener(this.path, StorageStatics.TaskEvent.STATE_CHANGED, _next);

View File

@ -25,7 +25,7 @@ class CoreContainer extends React.Component {
StatusBar.setBackgroundColor('#0279ba');
}
if (Platform.OS === 'ios') {
StatusBar.setBarStyle('light-content')
StatusBar.setBarStyle('light-content');
}
AppState.addEventListener('change', this.handleAppStateChange);
NetInfo.isConnected.fetch().then((isConnected) => {
@ -44,6 +44,7 @@ class CoreContainer extends React.Component {
}
props: Props;
_isConnected: boolean;
/**
* Handle app state changes