Skip to content

Commit 1c2d4b6

Browse files
committed
Cleanup start
1 parent 2d209ee commit 1c2d4b6

File tree

4 files changed

+49
-30
lines changed

4 files changed

+49
-30
lines changed

example/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'react-app-polyfill/ie11';
22
import * as React from 'react';
33
import * as ReactDOM from 'react-dom';
4-
import { createSharedFetch, syncState, useAsync } from '../.';
4+
import { createSharedFetch, syncState, useAsync } from '../src/index';
55
import { map } from 'rxjs/operators';
66

77
interface TodoInterface {

src/exhaustMapWithTrailing.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {
2+
from,
3+
Observable,
4+
ObservableInput,
5+
OperatorFunction,
6+
Subject,
7+
} from 'rxjs';
8+
import { exhaustMap, finalize, throttle } from 'rxjs/operators';
9+
10+
/**
11+
* Credit to: https://github.com/ReactiveX/rxjs/issues/5004
12+
* @param project
13+
*/
14+
export function exhaustMapWithTrailing<T, R>(
15+
project: (value: T, index: number) => ObservableInput<R>
16+
): OperatorFunction<T, R> {
17+
return (source): Observable<R> => {
18+
const release = new Subject();
19+
20+
return source.pipe(
21+
throttle(() => release, {
22+
leading: true,
23+
trailing: true,
24+
}),
25+
// TODO: Upgrade to TypeScript 3.6.3 when available and remove the cast
26+
// https://github.com/microsoft/TypeScript/issues/33131
27+
exhaustMap((value, index) =>
28+
from(project(value, index)).pipe(
29+
finalize(() => {
30+
release.next();
31+
})
32+
)
33+
) as OperatorFunction<T, R>
34+
);
35+
};
36+
}

src/index.tsx

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
catchError,
33
concatMap,
4-
exhaustMap,
54
filter,
65
finalize,
76
map,
@@ -12,48 +11,21 @@ import {
1211
switchMap,
1312
take,
1413
tap,
15-
throttle,
1614
} from 'rxjs/operators';
1715
import {
1816
BehaviorSubject,
1917
combineLatest,
2018
ConnectableObservable,
2119
from,
2220
Observable,
23-
ObservableInput,
2421
of,
2522
OperatorFunction,
2623
Subject,
2724
Subscription,
2825
} from 'rxjs';
2926
import { fromFetch as defaultFromFetch } from 'rxjs/fetch';
3027
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
31-
32-
/**
33-
* Credit to: https://github.com/ReactiveX/rxjs/issues/5004
34-
* @param project
35-
*/
36-
function exhaustMapWithTrailing<T, R>(
37-
project: (value: T, index: number) => ObservableInput<R>
38-
): OperatorFunction<T, R> {
39-
return (source): Observable<R> => {
40-
const release = new Subject();
41-
42-
return source.pipe(
43-
throttle(() => release, {
44-
leading: true,
45-
trailing: true,
46-
}),
47-
exhaustMap((value, index) =>
48-
from(project(value, index)).pipe(
49-
finalize(() => {
50-
release.next();
51-
})
52-
)
53-
)
54-
);
55-
};
56-
}
28+
import { exhaustMapWithTrailing } from './exhaustMapWithTrailing';
5729

5830
export type AsyncResult<T, ERR> = {
5931
pending: boolean;

src/types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Observable } from 'rxjs';
2+
3+
export type AsyncResult<T, ERR> = {
4+
pending: boolean;
5+
result?: T;
6+
error?: ERR;
7+
};
8+
9+
export type ObserveValue = <T>(input: Observable<T>) => Promise<T>;
10+
11+
export type AsyncFactory<T> = (observe: ObserveValue) => Promise<T>;

0 commit comments

Comments
 (0)