@exceptionless/fetchclient
Advanced tools
Comparing version 0.29.0 to 0.30.0
@@ -0,1 +1,2 @@ | ||
import { ObjectEvent } from "./ObjectEvent.js"; | ||
/** | ||
@@ -6,11 +7,4 @@ * Represents a counter that can be incremented and decremented. | ||
#count = 0; | ||
#onChange; | ||
#onChange = new ObjectEvent(); | ||
/** | ||
* Creates a new instance of the Counter class. | ||
* @param onChange - Optional callback function that will be called whenever the count changes. | ||
*/ | ||
constructor(onChange) { | ||
this.#onChange = onChange; | ||
} | ||
/** | ||
* Gets the current count. | ||
@@ -22,9 +16,14 @@ */ | ||
/** | ||
* Gets an event that is triggered when the count changes. | ||
*/ | ||
get changed() { | ||
return this.#onChange.expose(); | ||
} | ||
/** | ||
* Increments the count by 1. | ||
*/ | ||
increment() { | ||
const previous = this.#count; | ||
this.#count++; | ||
if (this.#onChange) { | ||
this.#onChange(this.#count); | ||
} | ||
this.#onChange.trigger({ previous, value: this.#count }); | ||
} | ||
@@ -35,7 +34,6 @@ /** | ||
decrement() { | ||
const previous = this.#count; | ||
this.#count--; | ||
if (this.#onChange) { | ||
this.#onChange(this.#count); | ||
} | ||
this.#onChange.trigger({ previous, value: this.#count }); | ||
} | ||
} |
@@ -6,2 +6,3 @@ import { Counter } from "./Counter.js"; | ||
import { getCurrentProvider } from "./DefaultHelpers.js"; | ||
import { ObjectEvent } from "./ObjectEvent.js"; | ||
/** | ||
@@ -15,2 +16,3 @@ * Represents a client for making HTTP requests using the Fetch API. | ||
#middleware = []; | ||
#onLoading = new ObjectEvent(); | ||
/** | ||
@@ -33,2 +35,13 @@ * Represents a FetchClient that handles HTTP requests using the Fetch API. | ||
} | ||
this.#counter.changed.on((e) => { | ||
if (!e) { | ||
throw new Error("Event data is required."); | ||
} | ||
if (e.value > 0 && e.previous == 0) { | ||
this.#onLoading.trigger(true); | ||
} | ||
else if (e.value == 0 && e.previous > 0) { | ||
this.#onLoading.trigger(false); | ||
} | ||
}); | ||
} | ||
@@ -69,6 +82,12 @@ /** | ||
*/ | ||
get loading() { | ||
get isLoading() { | ||
return this.requestCount > 0; | ||
} | ||
/** | ||
* Gets an event that is triggered when the loading state changes. | ||
*/ | ||
get loading() { | ||
return this.#onLoading.expose(); | ||
} | ||
/** | ||
* Adds one or more middleware functions to the FetchClient's middleware pipeline. | ||
@@ -75,0 +94,0 @@ * Middleware functions are executed in the order they are added. |
import { FetchClient } from "./FetchClient.js"; | ||
import { Counter } from "./Counter.js"; | ||
import { FetchClientCache } from "./FetchClientCache.js"; | ||
import { ObjectEvent } from "./ObjectEvent.js"; | ||
/** | ||
@@ -12,2 +13,3 @@ * Represents a provider for creating instances of the FetchClient class with shared default options and cache. | ||
#counter = new Counter(); | ||
#onLoading = new ObjectEvent(); | ||
/** | ||
@@ -27,2 +29,13 @@ * Creates a new instance of FetchClientProvider. | ||
}; | ||
this.#counter.changed.on((e) => { | ||
if (!e) { | ||
throw new Error("Event data is required."); | ||
} | ||
if (e.value > 0 && e.previous == 0) { | ||
this.#onLoading.trigger(true); | ||
} | ||
else if (e.value == 0 && e.previous > 0) { | ||
this.#onLoading.trigger(false); | ||
} | ||
}); | ||
} | ||
@@ -48,2 +61,8 @@ /** | ||
/** | ||
* Gets an event that is triggered when the loading state changes. | ||
*/ | ||
get loading() { | ||
return this.#onLoading.expose(); | ||
} | ||
/** | ||
* Gets the number of ongoing requests. | ||
@@ -50,0 +69,0 @@ */ |
{ | ||
"name": "@exceptionless/fetchclient", | ||
"version": "0.29.0", | ||
"version": "0.30.0", | ||
"description": "A simple fetch client with middleware support for Deno and the browser.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -137,4 +137,30 @@ <!-- deno-fmt-ignore-file --> | ||
## Contributing | ||
Run tests: | ||
```shell | ||
deno test --allow-net | ||
``` | ||
Lint code: | ||
```shell | ||
deno lint | ||
``` | ||
Format code: | ||
```shell | ||
deno fmt | ||
``` | ||
Type check code: | ||
```shell | ||
deno check scripts/*.ts *.ts src/*.ts | ||
``` | ||
## License | ||
MIT © [Exceptionless](https://exceptionless.com) |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Counter = void 0; | ||
const ObjectEvent_js_1 = require("./ObjectEvent.js"); | ||
/** | ||
@@ -9,11 +10,4 @@ * Represents a counter that can be incremented and decremented. | ||
#count = 0; | ||
#onChange; | ||
#onChange = new ObjectEvent_js_1.ObjectEvent(); | ||
/** | ||
* Creates a new instance of the Counter class. | ||
* @param onChange - Optional callback function that will be called whenever the count changes. | ||
*/ | ||
constructor(onChange) { | ||
this.#onChange = onChange; | ||
} | ||
/** | ||
* Gets the current count. | ||
@@ -25,9 +19,14 @@ */ | ||
/** | ||
* Gets an event that is triggered when the count changes. | ||
*/ | ||
get changed() { | ||
return this.#onChange.expose(); | ||
} | ||
/** | ||
* Increments the count by 1. | ||
*/ | ||
increment() { | ||
const previous = this.#count; | ||
this.#count++; | ||
if (this.#onChange) { | ||
this.#onChange(this.#count); | ||
} | ||
this.#onChange.trigger({ previous, value: this.#count }); | ||
} | ||
@@ -38,8 +37,7 @@ /** | ||
decrement() { | ||
const previous = this.#count; | ||
this.#count--; | ||
if (this.#onChange) { | ||
this.#onChange(this.#count); | ||
} | ||
this.#onChange.trigger({ previous, value: this.#count }); | ||
} | ||
} | ||
exports.Counter = Counter; |
@@ -9,2 +9,3 @@ "use strict"; | ||
const DefaultHelpers_js_1 = require("./DefaultHelpers.js"); | ||
const ObjectEvent_js_1 = require("./ObjectEvent.js"); | ||
/** | ||
@@ -18,2 +19,3 @@ * Represents a client for making HTTP requests using the Fetch API. | ||
#middleware = []; | ||
#onLoading = new ObjectEvent_js_1.ObjectEvent(); | ||
/** | ||
@@ -36,2 +38,13 @@ * Represents a FetchClient that handles HTTP requests using the Fetch API. | ||
} | ||
this.#counter.changed.on((e) => { | ||
if (!e) { | ||
throw new Error("Event data is required."); | ||
} | ||
if (e.value > 0 && e.previous == 0) { | ||
this.#onLoading.trigger(true); | ||
} | ||
else if (e.value == 0 && e.previous > 0) { | ||
this.#onLoading.trigger(false); | ||
} | ||
}); | ||
} | ||
@@ -72,6 +85,12 @@ /** | ||
*/ | ||
get loading() { | ||
get isLoading() { | ||
return this.requestCount > 0; | ||
} | ||
/** | ||
* Gets an event that is triggered when the loading state changes. | ||
*/ | ||
get loading() { | ||
return this.#onLoading.expose(); | ||
} | ||
/** | ||
* Adds one or more middleware functions to the FetchClient's middleware pipeline. | ||
@@ -78,0 +97,0 @@ * Middleware functions are executed in the order they are added. |
@@ -7,2 +7,3 @@ "use strict"; | ||
const FetchClientCache_js_1 = require("./FetchClientCache.js"); | ||
const ObjectEvent_js_1 = require("./ObjectEvent.js"); | ||
/** | ||
@@ -16,2 +17,3 @@ * Represents a provider for creating instances of the FetchClient class with shared default options and cache. | ||
#counter = new Counter_js_1.Counter(); | ||
#onLoading = new ObjectEvent_js_1.ObjectEvent(); | ||
/** | ||
@@ -31,2 +33,13 @@ * Creates a new instance of FetchClientProvider. | ||
}; | ||
this.#counter.changed.on((e) => { | ||
if (!e) { | ||
throw new Error("Event data is required."); | ||
} | ||
if (e.value > 0 && e.previous == 0) { | ||
this.#onLoading.trigger(true); | ||
} | ||
else if (e.value == 0 && e.previous > 0) { | ||
this.#onLoading.trigger(false); | ||
} | ||
}); | ||
} | ||
@@ -52,2 +65,8 @@ /** | ||
/** | ||
* Gets an event that is triggered when the loading state changes. | ||
*/ | ||
get loading() { | ||
return this.#onLoading.expose(); | ||
} | ||
/** | ||
* Gets the number of ongoing requests. | ||
@@ -54,0 +73,0 @@ */ |
@@ -7,7 +7,2 @@ /** | ||
/** | ||
* Creates a new instance of the Counter class. | ||
* @param onChange - Optional callback function that will be called whenever the count changes. | ||
*/ | ||
constructor(onChange?: (count: number) => void); | ||
/** | ||
* Gets the current count. | ||
@@ -17,2 +12,9 @@ */ | ||
/** | ||
* Gets an event that is triggered when the count changes. | ||
*/ | ||
get changed(): import("./ObjectEvent.js").IObjectEvent<{ | ||
previous: number; | ||
value: number; | ||
}>; | ||
/** | ||
* Increments the count by 1. | ||
@@ -19,0 +21,0 @@ */ |
@@ -42,4 +42,8 @@ import type { GetRequestOptions, RequestOptions } from "./RequestOptions.js"; | ||
*/ | ||
get loading(): boolean; | ||
get isLoading(): boolean; | ||
/** | ||
* Gets an event that is triggered when the loading state changes. | ||
*/ | ||
get loading(): import("./ObjectEvent.js").IObjectEvent<boolean>; | ||
/** | ||
* Adds one or more middleware functions to the FetchClient's middleware pipeline. | ||
@@ -46,0 +50,0 @@ * Middleware functions are executed in the order they are added. |
@@ -31,2 +31,6 @@ import { FetchClient } from "./FetchClient.js"; | ||
/** | ||
* Gets an event that is triggered when the loading state changes. | ||
*/ | ||
get loading(): import("./ObjectEvent.js").IObjectEvent<boolean>; | ||
/** | ||
* Gets the number of ongoing requests. | ||
@@ -33,0 +37,0 @@ */ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
189472
112
2577
166