Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set hasEmitted for initialData #370

Merged
merged 3 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions docs/reference/interfaces/useobservable.observablestatus.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,44 +27,66 @@

• **data**: T

Defined in: [src/useObservable.ts:36](https://github.com/FirebaseExtended/reactfire/blob/main/src/useObservable.ts#L36)
The most recent value.

If `initialData` is passed in, the first value of `data` will be the valuea provided in `initialData` **UNLESS** the underlying observable is ready, in which case it will skip `initialData`.

Defined in: [src/useObservable.ts:55](https://github.com/FirebaseExtended/reactfire/blob/main/src/useObservable.ts#L55)

___

### error

• **error**: *undefined* \| Error

Defined in: [src/useObservable.ts:37](https://github.com/FirebaseExtended/reactfire/blob/main/src/useObservable.ts#L37)
Any error that may have occurred in the underlying observable

Defined in: [src/useObservable.ts:59](https://github.com/FirebaseExtended/reactfire/blob/main/src/useObservable.ts#L59)

___

### firstValuePromise

• **firstValuePromise**: *Promise*<void\>

Defined in: [src/useObservable.ts:38](https://github.com/FirebaseExtended/reactfire/blob/main/src/useObservable.ts#L38)
Promise that resolves after first emit from observable

Defined in: [src/useObservable.ts:63](https://github.com/FirebaseExtended/reactfire/blob/main/src/useObservable.ts#L63)

___

### hasEmitted

• **hasEmitted**: *boolean*

Defined in: [src/useObservable.ts:34](https://github.com/FirebaseExtended/reactfire/blob/main/src/useObservable.ts#L34)
Indicates whether the hook has emitted a value at some point

If `initialData` is passed in, this will be `true`.

Defined in: [src/useObservable.ts:45](https://github.com/FirebaseExtended/reactfire/blob/main/src/useObservable.ts#L45)

___

### isComplete

• **isComplete**: *boolean*

Defined in: [src/useObservable.ts:35](https://github.com/FirebaseExtended/reactfire/blob/main/src/useObservable.ts#L35)
If this is `true`, the hook will be emitting no further items.

Defined in: [src/useObservable.ts:49](https://github.com/FirebaseExtended/reactfire/blob/main/src/useObservable.ts#L49)

___

### status

• **status**: ``"loading"`` \| ``"error"`` \| ``"success"``

Defined in: [src/useObservable.ts:30](https://github.com/FirebaseExtended/reactfire/blob/main/src/useObservable.ts#L30)
The loading status.

- `loading`: Waiting for the first value from an observable
- `error`: Something went wrong. Check `ObservableStatus.error` for more details
- `success`: The hook has emitted at least one value

If `initialData` is passed in, this will skip `loading` and go straight to `success`.

Defined in: [src/useObservable.ts:39](https://github.com/FirebaseExtended/reactfire/blob/main/src/useObservable.ts#L39)
2 changes: 1 addition & 1 deletion docs/reference/modules/useobservable.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ ___

**Returns:** [*ObservableStatus*](../interfaces/useobservable.observablestatus.md)<T\>

Defined in: [src/useObservable.ts:41](https://github.com/FirebaseExtended/reactfire/blob/main/src/useObservable.ts#L41)
Defined in: [src/useObservable.ts:66](https://github.com/FirebaseExtended/reactfire/blob/main/src/useObservable.ts#L66)
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"test:firestore": "firebase emulators:exec --only firestore --project rxfire-525a3 \"tsdx test firestore --verbose\"",
"test:database": "firebase emulators:exec --only database --project rxfire-525a3 \"tsdx test database --verbose\"",
"test:auth": "firebase emulators:exec --only auth --project rxfire-525a3 \"tsdx test auth --verbose\"",
"test:useObservable": "tsdx test useObservable --verbose",
"lint": "tsdx lint src test",
"size": "size-limit",
"analyze": "size-limit --why",
Expand Down
41 changes: 33 additions & 8 deletions src/useObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,40 @@ export function preloadObservable<T>(source: Observable<T>, id: string) {
}

export interface ObservableStatus<T> {
status:
| 'loading' // waiting for first value from observable
| 'error'
| 'success'; // has received at least one value
/**
* The loading status.
*
* - `loading`: Waiting for the first value from an observable
* - `error`: Something went wrong. Check `ObservableStatus.error` for more details
* - `success`: The hook has emitted at least one value
*
* If `initialData` is passed in, this will skip `loading` and go straight to `success`.
*/
status: 'loading' | 'error' | 'success';
/**
* Indicates whether the hook has emitted a value at some point
*
* If `initialData` is passed in, this will be `true`.
*/
hasEmitted: boolean; // has received at least one value
isComplete: boolean; // observable has triggered onComplete event
data: T; // latest data from observable
/**
* If this is `true`, the hook will be emitting no further items.
*/
isComplete: boolean;
/**
* The most recent value.
*
* If `initialData` is passed in, the first value of `data` will be the valuea provided in `initialData` **UNLESS** the underlying observable is ready, in which case it will skip `initialData`.
*/
data: T;
/**
* Any error that may have occurred in the underlying observable
*/
error: Error | undefined;
firstValuePromise: Promise<void>; // promise that resolves after first emit from observable
/**
* Promise that resolves after first emit from observable
*/
firstValuePromise: Promise<void>;
}

export function useObservable<T>(observableId: string, source: Observable<T | any>, config: ReactFireOptions = {}): ObservableStatus<T> {
Expand Down Expand Up @@ -79,7 +104,7 @@ export function useObservable<T>(observableId: string, source: Observable<T | an

return {
status,
hasEmitted: observable.hasValue,
hasEmitted: observable.hasValue || hasInitialData,
isComplete: observable.isStopped,
data: latest,
error: observable.ourError,
Expand Down
24 changes: 24 additions & 0 deletions test/useObservable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ describe('useObservable', () => {
expect(result.current.isComplete).toEqual(false);
expect(result.current.status).toEqual('success');
});

it('Sets status correctly when passed initialData', () => {
const observable$: Subject<any> = new Subject();

const initialData = 1;
const asyncData = 2;

const { result } = renderHook(() => useObservable('non-suspense test with initialData', observable$, { suspense: false, initialData }));

expect(result.current.data).toEqual(initialData);
expect(result.current.error).toBeUndefined();
expect(result.current.firstValuePromise).toBeInstanceOf(Promise);
expect(result.current.hasEmitted).toEqual(true); // set `hasEmitted` to true since there is data
expect(result.current.isComplete).toEqual(false);
expect(result.current.status).toEqual('success'); // skip 'loading'

actOnHook(() => observable$.next(asyncData));

expect(result.current.data).toEqual(asyncData);
expect(result.current.error).toBeUndefined();
expect(result.current.hasEmitted).toEqual(true);
expect(result.current.isComplete).toEqual(false);
expect(result.current.status).toEqual('success');
});
});

describe('Suspense Mode', () => {
Expand Down