Skip to content

Commit 31ccc35

Browse files
authored
Merge pull request #81 from Lemoncode/feature/manually-endpoints
Implement manually endpoints
2 parents 66fdf45 + 9dbc65b commit 31ccc35

File tree

4 files changed

+1330
-13
lines changed

4 files changed

+1330
-13
lines changed

src/index.d.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,29 @@ import * as React from "react";
1111
*/
1212
export function trackPromise<T>(promise: Promise<T>, area?: string): Promise<T>;
1313

14+
/**
15+
* Perform a reset for the area counter (default-area by default).
16+
*/
17+
export function manuallyResetPromiseCounter(area?: string): void;
18+
19+
/**
20+
* Decrement the area counter (default-area by default).
21+
*/
22+
export function manuallyDecrementPromiseCounter(area?: string): void;
23+
24+
/**
25+
* Increment the area counter (default-area by default).
26+
*/
27+
export function manuallyIncrementPromiseCounter(area?: string): void;
28+
1429
/**
1530
* Configuration contract: user can setup areas (display more than one spinner) or delay when
1631
* the spinner is shown (this is useful when a user has a fast connection, to avoid unneccessary flickering)
1732
*/
1833

1934
interface Config {
20-
area?: string;
21-
delay?: number;
35+
area?: string;
36+
delay?: number;
2237
}
2338

2439
/**
@@ -37,12 +52,16 @@ export interface TrackerHocProps {
3752
config?: Config;
3853
}
3954

40-
export function promiseTrackerHoc<P>(component: React.ComponentType<P & ComponentToWrapProps>): React.ComponentType<P & TrackerHocProps>;
55+
export function promiseTrackerHoc<P>(
56+
component: React.ComponentType<P & ComponentToWrapProps>
57+
): React.ComponentType<P & TrackerHocProps>;
4158

4259
/**
4360
* React Promise Tracker custom hook, this hook will expose a promiseInProgress boolean flag.
4461
*
4562
* @param configuration (optional can be null).
4663
* @returns promiseInProgressFlag.
4764
*/
48-
export function usePromiseTracker(outerConfig? : Config) : { promiseInProgress : boolean };
65+
export function usePromiseTracker(
66+
outerConfig?: Config
67+
): { promiseInProgress: boolean };

src/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1-
export { trackPromise } from './trackPromise';
2-
export { promiseTrackerHoc } from './trackerHoc';
3-
export { usePromiseTracker } from './trackerHook';
1+
export {
2+
trackPromise,
3+
manuallyResetPromiseCounter,
4+
manuallyDecrementPromiseCounter,
5+
manuallyIncrementPromiseCounter
6+
} from "./trackPromise";
7+
export { promiseTrackerHoc } from "./trackerHoc";
8+
export { usePromiseTracker } from "./trackerHook";

src/trackPromise.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,20 @@ export const getCounter = area => counter[area];
1212

1313
export const trackPromise = (promise, area) => {
1414
area = area || defaultArea;
15-
incrementCounter(area);
16-
17-
const promiseInProgress = anyPromiseInProgress(area);
18-
emitter.emit(promiseCounterUpdateEventId, promiseInProgress, area);
15+
incrementPromiseCounter(area);
1916

2017
const onResolveHandler = () => decrementPromiseCounter(area);
2118
promise.then(onResolveHandler, onResolveHandler);
2219

2320
return promise;
2421
};
2522

23+
const incrementPromiseCounter = area => {
24+
incrementCounter(area);
25+
const promiseInProgress = anyPromiseInProgress(area);
26+
emitter.emit(promiseCounterUpdateEventId, promiseInProgress, area);
27+
};
28+
2629
const incrementCounter = area => {
2730
if (Boolean(counter[area])) {
2831
counter[area]++;
@@ -34,7 +37,7 @@ const incrementCounter = area => {
3437
const anyPromiseInProgress = area => counter[area] > 0;
3538

3639
const decrementPromiseCounter = area => {
37-
decrementCounter(area);
40+
counter[area] > 0 && decrementCounter(area);
3841
const promiseInProgress = anyPromiseInProgress(area);
3942
emitter.emit(promiseCounterUpdateEventId, promiseInProgress, area);
4043
};
@@ -43,5 +46,20 @@ const decrementCounter = area => {
4346
counter[area]--;
4447
};
4548

49+
export const manuallyResetPromiseCounter = area => {
50+
area = area || defaultArea;
51+
counter[area] = 0;
52+
emitter.emit(promiseCounterUpdateEventId, false, area);
53+
};
54+
55+
export const manuallyDecrementPromiseCounter = area => {
56+
area = area || defaultArea;
57+
decrementPromiseCounter(area);
58+
};
59+
60+
export const manuallyIncrementPromiseCounter = area => {
61+
area = area || defaultArea;
62+
incrementPromiseCounter(area);
63+
};
4664
// TODO: Enhancement we could catch here errors and throw an Event in case there's an HTTP Error
4765
// then the consumer of this event can be listening and decide what to to in case of error

0 commit comments

Comments
 (0)