Skip to content

Commit 27c330c

Browse files
committed
In the middle of messing with Typescript
1 parent 396e809 commit 27c330c

File tree

6 files changed

+144
-40
lines changed

6 files changed

+144
-40
lines changed

packages/storage-types/index.d.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717

1818
import { FirebaseApp, FirebaseNamespace } from '@firebase/app-types';
19-
import { Observer, Unsubscribe } from '@firebase/util';
2019

2120
export interface FullMetadata extends UploadMetadata {
2221
bucket: string;
@@ -85,15 +84,25 @@ export interface UploadMetadata extends SettableMetadata {
8584
md5Hash?: string | null;
8685
}
8786

87+
export type NextFn<T> = (value: T) => void;
88+
export type ErrorFn = (error: Error | FirebaseStorageError) => void;
89+
export type CompleteFn = () => void;
90+
export type Unsubscribe = () => void;
91+
export interface StorageObserver<T> {
92+
next?: NextFn<T> | null;
93+
error?: ErrorFn | null;
94+
complete?: CompleteFn | null;
95+
}
96+
8897
export interface UploadTask {
8998
cancel(): boolean;
9099
catch(onRejected: (a: Error) => any): Promise<any>;
91100
on(
92101
event: TaskEvent,
93102
nextOrObserver?:
94-
| Partial<Observer<UploadTaskSnapshot>>
103+
| Partial<StorageObserver<UploadTaskSnapshot>>
95104
| null
96-
| ((a: UploadTaskSnapshot) => any),
105+
| ((a: UploadTaskSnapshot) => unknown),
97106
error?: ((a: Error) => any) | null,
98107
complete?: Unsubscribe | null
99108
): Function;

packages/storage/compat/reference.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
updateMetadata,
2929
deleteObject
3030
} from '../src/reference';
31+
import * as types from '@firebase/storage-types';
3132
import {
3233
validate,
3334
stringSpec,
@@ -36,12 +37,12 @@ import {
3637
uploadDataSpec
3738
} from '../src/implementation/args';
3839
import { Metadata } from '../src/metadata';
39-
import { UploadTask } from '../src/task';
4040
import { StringFormat, formatValidator } from '../src/implementation/string';
4141
import { ListResult, ListOptions } from '../src/list';
42+
import { UploadTaskCompat } from './task';
4243

43-
export class ReferenceCompat extends Reference {
44-
static fromReference(ref: Reference): ReferenceCompat {
44+
export class ReferenceCompat extends Reference implements types.Reference {
45+
static fromReference(ref: Reference): types.Reference {
4546
return new ReferenceCompat(ref.service, ref.location);
4647
}
4748
toString(): string {
@@ -53,7 +54,7 @@ export class ReferenceCompat extends Reference {
5354
* appending childPath, removing any duplicate, beginning, or trailing
5455
* slashes.
5556
*/
56-
child(childPath: string): ReferenceCompat {
57+
child(childPath: string): types.Reference {
5758
validate('child', [stringSpec()], arguments);
5859
const reference = getChild(this, childPath);
5960
return ReferenceCompat.fromReference(reference);
@@ -63,7 +64,7 @@ export class ReferenceCompat extends Reference {
6364
* @return A reference to the parent of the
6465
* current object, or null if the current object is the root.
6566
*/
66-
get parent(): ReferenceCompat | null {
67+
get parent(): types.Reference | null {
6768
validate('parent', [], arguments);
6869
const reference = getParent(this);
6970
if (reference == null) {
@@ -80,11 +81,11 @@ export class ReferenceCompat extends Reference {
8081
*/
8182
put(
8283
data: Blob | Uint8Array | ArrayBuffer,
83-
metadata: Metadata | null = null
84-
): UploadTask {
84+
metadata?: Metadata
85+
): UploadTaskCompat {
8586
validate('put', [uploadDataSpec(), metadataSpec(true)], arguments);
8687
this.throwIfRoot_('put');
87-
return uploadBytes(this, data, metadata);
88+
return uploadBytes(this, data, metadata) as UploadTaskCompat;
8889
}
8990
/**
9091
* Uploads a string to this object's location.
@@ -97,7 +98,7 @@ export class ReferenceCompat extends Reference {
9798
value: string,
9899
format: StringFormat = StringFormat.RAW,
99100
metadata?: Metadata
100-
): UploadTask {
101+
): UploadTaskCompat {
101102
validate(
102103
'putString',
103104
[stringSpec(), stringSpec(formatValidator, true), metadataSpec(true)],

packages/storage/compat/task.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { UploadTask } from '../src/task';
19+
import { UploadTaskSnapshotCompat } from './tasksnapshot';
20+
import { UploadTaskSnapshot } from '../src/tasksnapshot';
21+
import {
22+
taskStateFromInternalTaskState,
23+
TaskEvent
24+
} from '../src/implementation/taskenums';
25+
import { ReferenceCompat } from './reference';
26+
import { FbsBlob } from '../src/implementation/blob';
27+
import { Metadata } from '../src/metadata';
28+
import { ErrorFn, CompleteFn, Unsubscribe, Subscribe } from '@firebase/util';
29+
import * as types from '@firebase/storage-types';
30+
import { StorageObserver } from '../src/implementation/observer';
31+
32+
export class UploadTaskCompat extends UploadTask<UploadTaskSnapshotCompat>
33+
implements types.UploadTask {
34+
constructor(
35+
private refCompat_: ReferenceCompat,
36+
blob: FbsBlob,
37+
metadata: Metadata | null = null
38+
) {
39+
super(refCompat_, blob, metadata);
40+
}
41+
get snapshot(): UploadTaskSnapshotCompat {
42+
const externalState = taskStateFromInternalTaskState(this.state_);
43+
return new UploadTaskSnapshotCompat(
44+
this.transferred_,
45+
this.blob_.size(),
46+
externalState,
47+
this.metadata_,
48+
this,
49+
this.refCompat_
50+
);
51+
}
52+
on(
53+
type: TaskEvent,
54+
nextOrObserver?:
55+
| Partial<StorageObserver<UploadTaskSnapshotCompat>>
56+
| null
57+
| ((a: UploadTaskSnapshotCompat) => unknown),
58+
error?: ErrorFn | null,
59+
completed?: CompleteFn | null
60+
): Unsubscribe | Subscribe<UploadTaskSnapshotCompat> {
61+
const castNextOrObserver = nextOrObserver as
62+
| Partial<StorageObserver<UploadTaskSnapshot>>
63+
| null
64+
| ((a: UploadTaskSnapshot) => unknown);
65+
return super.on(type, castNextOrObserver, error, completed) as
66+
| Unsubscribe
67+
| Subscribe<types.UploadTaskSnapshot>;
68+
}
69+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @license
3+
* Copyright 2017 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
import { TaskState } from '../src/implementation/taskenums';
18+
import { Metadata } from '../src/metadata';
19+
import { ReferenceCompat } from './reference';
20+
import { UploadTaskCompat } from './task';
21+
22+
export class UploadTaskSnapshotCompat {
23+
constructor(
24+
readonly bytesTransferred: number,
25+
readonly totalBytes: number,
26+
readonly state: TaskState,
27+
readonly metadata: Metadata | null,
28+
readonly task: UploadTaskCompat,
29+
readonly ref: ReferenceCompat
30+
) {}
31+
}

packages/storage/src/reference.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ import {
3636
import * as type from './implementation/type';
3737
import { Metadata } from './metadata';
3838
import { StorageService } from './service';
39-
import { UploadTask } from './task';
4039
import { ListOptions, ListResult } from './list';
4140
import { uploadDataSpec, listOptionSpec } from './implementation/args';
41+
import { UploadTask } from './task';
4242

4343
/**
4444
* Provides methods to interact with a bucket in the Firebase Storage service.

packages/storage/src/task.ts

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,26 @@ import { Reference } from './reference';
5555
* Represents a blob being uploaded. Can be used to pause/resume/cancel the
5656
* upload and manage callbacks for various events.
5757
*/
58-
export class UploadTask {
58+
export class UploadTask<T extends UploadTaskSnapshot> {
5959
private ref_: Reference;
60-
private blob_: FbsBlob;
61-
private metadata_: Metadata | null;
60+
blob_: FbsBlob;
61+
metadata_: Metadata | null;
6262
private mappings_: fbsMetadata.Mappings;
63-
private transferred_: number = 0;
63+
transferred_: number = 0;
6464
private needToFetchStatus_: boolean = false;
6565
private needToFetchMetadata_: boolean = false;
66-
private observers_: Array<Observer<UploadTaskSnapshot>> = [];
66+
private observers_: Array<StorageObserver<T>> = [];
6767
private resumable_: boolean;
68-
private state_: InternalTaskState;
68+
state_: InternalTaskState;
6969
private error_: Error | null = null;
7070
private uploadUrl_: string | null = null;
7171
private request_: Request<unknown> | null = null;
7272
private chunkMultiplier_: number = 1;
7373
private errorHandler_: (p1: FirebaseStorageError) => void;
7474
private metadataErrorHandler_: (p1: FirebaseStorageError) => void;
75-
private resolve_: ((p1: UploadTaskSnapshot) => void) | null = null;
75+
private resolve_: ((p1: T) => void) | null = null;
7676
private reject_: ((p1: Error) => void) | null = null;
77-
private promise_: Promise<UploadTaskSnapshot>;
77+
private promise_: Promise<T>;
7878

7979
/**
8080
* @param ref The firebaseStorage.Reference object this task came
@@ -425,7 +425,7 @@ export class UploadTask {
425425
}
426426
}
427427

428-
get snapshot(): UploadTaskSnapshot {
428+
get snapshot(): T {
429429
const externalState = taskStateFromInternalTaskState(this.state_);
430430
return new UploadTaskSnapshot(
431431
this.transferred_,
@@ -434,7 +434,7 @@ export class UploadTask {
434434
this.metadata_,
435435
this,
436436
this.ref_
437-
);
437+
) as T;
438438
}
439439

440440
/**
@@ -443,13 +443,10 @@ export class UploadTask {
443443
*/
444444
on(
445445
type: TaskEvent,
446-
nextOrObserver?:
447-
| NextFn<UploadTaskSnapshot>
448-
| StorageObserver<UploadTaskSnapshot>
449-
| null,
446+
nextOrObserver?: Partial<StorageObserver<T>> | null | ((a: T) => unknown),
450447
error?: ErrorFn | null,
451448
completed?: CompleteFn | null
452-
): Unsubscribe | Subscribe<UploadTaskSnapshot> {
449+
): Unsubscribe | Subscribe<T> {
453450
function typeValidator(): void {
454451
if (type !== TaskEvent.STATE_CHANGED) {
455452
throw `Expected one of the event types: [${TaskEvent.STATE_CHANGED}].`;
@@ -490,14 +487,9 @@ export class UploadTask {
490487
validate('on', specs, arguments);
491488
const self = this;
492489

493-
function makeBinder(
494-
specs: ArgSpec[] | null
495-
): Subscribe<UploadTaskSnapshot> {
490+
function makeBinder(specs: ArgSpec[] | null): Subscribe<T> {
496491
function binder(
497-
nextOrObserver?:
498-
| NextFn<UploadTaskSnapshot>
499-
| StorageObserver<UploadTaskSnapshot>
500-
| null,
492+
nextOrObserver?: NextFn<T> | StorageObserver<T> | null,
501493
error?: ErrorFn | null,
502494
complete?: CompleteFn | null
503495
): () => void {
@@ -544,7 +536,7 @@ export class UploadTask {
544536
*/
545537
then<U>(
546538
onFulfilled?: ((value: UploadTaskSnapshot) => U | Promise<U>) | null,
547-
onRejected?: ((error: Error) => U | Promise<U>) | null
539+
onRejected?: ((error: FirebaseStorageError) => U | Promise<U>) | null
548540
): Promise<U> {
549541
// These casts are needed so that TypeScript can infer the types of the
550542
// resulting Promise.
@@ -557,22 +549,24 @@ export class UploadTask {
557549
/**
558550
* Equivalent to calling `then(null, onRejected)`.
559551
*/
560-
catch<T>(onRejected: (p1: Error) => T | Promise<T>): Promise<T> {
552+
catch<T>(
553+
onRejected: (p1: FirebaseStorageError) => T | Promise<T>
554+
): Promise<T> {
561555
return this.then(null, onRejected);
562556
}
563557

564558
/**
565559
* Adds the given observer.
566560
*/
567-
private addObserver_(observer: Observer<UploadTaskSnapshot>): void {
561+
private addObserver_(observer: Observer<T>): void {
568562
this.observers_.push(observer);
569563
this.notifyObserver_(observer);
570564
}
571565

572566
/**
573567
* Removes the given observer.
574568
*/
575-
private removeObserver_(observer: Observer<UploadTaskSnapshot>): void {
569+
private removeObserver_(observer: Observer<T>): void {
576570
const i = this.observers_.indexOf(observer);
577571
if (i !== -1) {
578572
this.observers_.splice(i, 1);
@@ -610,7 +604,7 @@ export class UploadTask {
610604
}
611605
}
612606

613-
private notifyObserver_(observer: Observer<UploadTaskSnapshot>): void {
607+
private notifyObserver_(observer: Observer<T>): void {
614608
const externalState = taskStateFromInternalTaskState(this.state_);
615609
switch (externalState) {
616610
case TaskState.RUNNING:

0 commit comments

Comments
 (0)