shelving 1.56.0 → 1.58.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/db/{Pagination.d.ts → PaginationState.d.ts} +6 -3
- package/db/{Pagination.js → PaginationState.js} +19 -14
- package/db/index.d.ts +1 -1
- package/db/index.js +1 -1
- package/package.json +6 -6
- package/react/usePagination.d.ts +2 -2
- package/react/usePagination.js +2 -2
- package/stream/LazyState.d.ts +3 -4
- package/stream/LazyState.js +6 -7
- package/stream/State.d.ts +1 -1
- package/stream/State.js +2 -2
- package/util/async.d.ts +0 -4
- package/util/async.js +0 -7
- package/util/index.d.ts +1 -0
- package/util/index.js +1 -0
- package/util/observable.d.ts +1 -1
- package/util/observable.js +2 -1
- package/util/timeout.d.ts +23 -0
- package/util/timeout.js +30 -0
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import { Results, Entry, Entries, Data } from "../util/index.js";
|
|
2
|
-
import { State } from "../stream/index.js";
|
|
2
|
+
import { BooleanState, State } from "../stream/index.js";
|
|
3
3
|
import { DatabaseQuery } from "./Database.js";
|
|
4
4
|
/**
|
|
5
5
|
* State that wraps a `Documents` reference to enable pagination.
|
|
6
6
|
* - If you pass in initial values, it will use that as the first page.
|
|
7
7
|
* - If you don't pass in initial values, it will autoload the first page.
|
|
8
8
|
*/
|
|
9
|
-
export declare class
|
|
10
|
-
|
|
9
|
+
export declare class PaginationState<T extends Data> extends State<Results<T>> implements Iterable<Entry<T>> {
|
|
10
|
+
/** Whether this pagination is currently loading results. */
|
|
11
|
+
readonly busy: BooleanState;
|
|
12
|
+
/** Reference to the query this pagination is paginating. */
|
|
11
13
|
readonly ref: DatabaseQuery<T>;
|
|
14
|
+
/** Limit set on this pagination's query. */
|
|
12
15
|
readonly limit: number;
|
|
13
16
|
constructor(ref: DatabaseQuery<T>);
|
|
14
17
|
/**
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import { getLastItem, assertNumber, yieldMerged, getMap, LOADING, assertMax } from "../util/index.js";
|
|
2
|
-
import { State } from "../stream/index.js";
|
|
2
|
+
import { BooleanState, State } from "../stream/index.js";
|
|
3
3
|
import { ConditionError } from "../index.js";
|
|
4
4
|
/**
|
|
5
5
|
* State that wraps a `Documents` reference to enable pagination.
|
|
6
6
|
* - If you pass in initial values, it will use that as the first page.
|
|
7
7
|
* - If you don't pass in initial values, it will autoload the first page.
|
|
8
8
|
*/
|
|
9
|
-
export class
|
|
9
|
+
export class PaginationState extends State {
|
|
10
10
|
constructor(ref) {
|
|
11
11
|
super();
|
|
12
|
-
this
|
|
12
|
+
/** Whether this pagination is currently loading results. */
|
|
13
|
+
this.busy = new BooleanState();
|
|
13
14
|
/**
|
|
14
15
|
* Load more results after the end.
|
|
15
16
|
* - Promise that needs to be handled.
|
|
@@ -17,25 +18,29 @@ export class Pagination extends State {
|
|
|
17
18
|
this.more = async () => {
|
|
18
19
|
if (this.closed)
|
|
19
20
|
throw new ConditionError("Pagination is closed");
|
|
20
|
-
if (!this.
|
|
21
|
-
this.
|
|
22
|
-
if (!this.
|
|
21
|
+
if (!this.busy.value) {
|
|
22
|
+
this.busy.next(true);
|
|
23
|
+
if (!this.exists) {
|
|
24
|
+
// First set of results.
|
|
25
|
+
this._value === LOADING;
|
|
26
|
+
const next = await this.ref.results;
|
|
27
|
+
this.next(next);
|
|
28
|
+
if (next.size < this.limit)
|
|
29
|
+
this.complete();
|
|
30
|
+
this.busy.next(false);
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
// Additional set of results.
|
|
23
34
|
const lastItem = getLastItem(this.value);
|
|
24
35
|
if (lastItem) {
|
|
25
36
|
const next = await this.ref.after(lastItem[0], lastItem[1]).results;
|
|
26
37
|
this.merge(next);
|
|
27
38
|
if (next.size < this.limit)
|
|
28
39
|
this.complete();
|
|
29
|
-
this.
|
|
40
|
+
this.busy.next(false);
|
|
30
41
|
return;
|
|
31
42
|
}
|
|
32
43
|
}
|
|
33
|
-
this._value === LOADING;
|
|
34
|
-
const next = await this.ref.results;
|
|
35
|
-
this.next(next);
|
|
36
|
-
if (next.size < this.limit)
|
|
37
|
-
this.complete();
|
|
38
|
-
this._pending = false;
|
|
39
44
|
}
|
|
40
45
|
};
|
|
41
46
|
this.ref = ref;
|
|
@@ -48,7 +53,7 @@ export class Pagination extends State {
|
|
|
48
53
|
* @return The change in the number of results.
|
|
49
54
|
*/
|
|
50
55
|
merge(more) {
|
|
51
|
-
this.next(getMap(this.ref.sorts.transform(yieldMerged(more, this.value))));
|
|
56
|
+
this.next(this.exists ? getMap(this.ref.sorts.transform(yieldMerged(more, this.value))) : getMap(more));
|
|
52
57
|
}
|
|
53
58
|
/** Iterate over the entries of the values currently in the pagination. */
|
|
54
59
|
[Symbol.iterator]() {
|
package/db/index.d.ts
CHANGED
package/db/index.js
CHANGED
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"state-management",
|
|
12
12
|
"query-builder"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.
|
|
14
|
+
"version": "1.58.0",
|
|
15
15
|
"repository": "https://github.com/dhoulb/shelving",
|
|
16
16
|
"author": "Dave Houlbrooke <dave@shax.com>",
|
|
17
17
|
"license": "0BSD",
|
|
@@ -62,21 +62,21 @@
|
|
|
62
62
|
"@google-cloud/firestore": "^5.0.2",
|
|
63
63
|
"@types/jest": "^27.4.1",
|
|
64
64
|
"@types/react": "^17.0.44",
|
|
65
|
-
"@types/react-dom": "^17.0.
|
|
66
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
67
|
-
"@typescript-eslint/parser": "^5.
|
|
65
|
+
"@types/react-dom": "^17.0.16",
|
|
66
|
+
"@typescript-eslint/eslint-plugin": "^5.21.0",
|
|
67
|
+
"@typescript-eslint/parser": "^5.21.0",
|
|
68
68
|
"eslint": "^8.14.0",
|
|
69
69
|
"eslint-config-prettier": "^8.5.0",
|
|
70
70
|
"eslint-plugin-import": "^2.26.0",
|
|
71
71
|
"eslint-plugin-prettier": "^4.0.0",
|
|
72
|
-
"firebase": "^9.
|
|
72
|
+
"firebase": "^9.7.0",
|
|
73
73
|
"jest": "^27.5.1",
|
|
74
74
|
"jest-ts-webcompat-resolver": "^1.0.0",
|
|
75
75
|
"prettier": "^2.6.2",
|
|
76
76
|
"react": "^17.0.2",
|
|
77
77
|
"react-dom": "^17.0.2",
|
|
78
78
|
"ts-jest": "^27.1.4",
|
|
79
|
-
"typescript": "^4.6.
|
|
79
|
+
"typescript": "^4.6.4"
|
|
80
80
|
},
|
|
81
81
|
"peerDependencies": {
|
|
82
82
|
"@google-cloud/firestore": ">=4.0.0",
|
package/react/usePagination.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { DatabaseQuery, Results,
|
|
1
|
+
import { DatabaseQuery, Results, PaginationState, Data } from "../index.js";
|
|
2
2
|
/**
|
|
3
3
|
* Use a `Pagination` for a collection.
|
|
4
4
|
* - Doesn't persist the state, so if the component or anything beneath it throws the currently paginated results will be lost.
|
|
5
5
|
*/
|
|
6
|
-
export declare function usePagination<T extends Data>(ref: DatabaseQuery<T>, initial?: Results<T>):
|
|
6
|
+
export declare function usePagination<T extends Data>(ref: DatabaseQuery<T>, initial?: Results<T>): PaginationState<T>;
|
package/react/usePagination.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PaginationState } from "../index.js";
|
|
2
2
|
import { useSubscribe } from "./useSubscribe.js";
|
|
3
3
|
import { useLazy } from "./useLazy.js";
|
|
4
4
|
/**
|
|
@@ -11,7 +11,7 @@ export function usePagination(ref, initial) {
|
|
|
11
11
|
return pagination;
|
|
12
12
|
}
|
|
13
13
|
function _createPagination(ref, initial) {
|
|
14
|
-
const pagination = new
|
|
14
|
+
const pagination = new PaginationState(ref);
|
|
15
15
|
if (initial)
|
|
16
16
|
pagination.next(initial);
|
|
17
17
|
return pagination;
|
package/stream/LazyState.d.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Observer } from "../util/index.js";
|
|
2
2
|
import { State } from "./State.js";
|
|
3
3
|
/**
|
|
4
4
|
* State that tidies up after itself by completing itself after all its subscribers unsubscribe.
|
|
5
5
|
* @param delay How long to wait (in ms) before the source subscription is stopped.
|
|
6
6
|
*/
|
|
7
7
|
export declare class LazyState<T> extends State<T> {
|
|
8
|
-
private
|
|
9
|
-
|
|
10
|
-
constructor(delay?: number);
|
|
8
|
+
private _timeout;
|
|
9
|
+
constructor(delay?: number | null);
|
|
11
10
|
_removeObserver(observer: Observer<T>): void;
|
|
12
11
|
}
|
package/stream/LazyState.js
CHANGED
|
@@ -1,24 +1,23 @@
|
|
|
1
|
+
import { Timeout } from "../util/index.js";
|
|
1
2
|
import { State } from "./State.js";
|
|
2
3
|
/**
|
|
3
4
|
* State that tidies up after itself by completing itself after all its subscribers unsubscribe.
|
|
4
5
|
* @param delay How long to wait (in ms) before the source subscription is stopped.
|
|
5
6
|
*/
|
|
6
7
|
export class LazyState extends State {
|
|
7
|
-
constructor(delay =
|
|
8
|
+
constructor(delay = null) {
|
|
8
9
|
super();
|
|
9
|
-
this.
|
|
10
|
+
this._timeout = delay ? new Timeout(delay) : null;
|
|
10
11
|
}
|
|
11
12
|
// Override to stop the source subscription when the last subscriber unsubscribes.
|
|
12
13
|
_removeObserver(observer) {
|
|
13
14
|
super._removeObserver(observer);
|
|
14
|
-
if (this.
|
|
15
|
+
if (this._timeout) {
|
|
15
16
|
// Maybe stop in a bit (if there are still no subscribers).
|
|
16
|
-
|
|
17
|
-
clearTimeout(this._timeout);
|
|
18
|
-
this._timeout = setTimeout(() => {
|
|
17
|
+
this._timeout.set(() => {
|
|
19
18
|
if (!this._observers.size && !this.closed)
|
|
20
19
|
this.complete();
|
|
21
|
-
}
|
|
20
|
+
});
|
|
22
21
|
}
|
|
23
22
|
else {
|
|
24
23
|
// Stop now.
|
package/stream/State.d.ts
CHANGED
|
@@ -32,7 +32,7 @@ export declare class State<T> extends Stream<T> {
|
|
|
32
32
|
get value(): T;
|
|
33
33
|
protected _value: T | typeof LOADING;
|
|
34
34
|
/** Is there a current value, or is it still loading. */
|
|
35
|
-
get
|
|
35
|
+
get exists(): boolean;
|
|
36
36
|
/** Apply a transformer to this state. */
|
|
37
37
|
apply(transformer: Transformer<T, T>): void;
|
|
38
38
|
error(reason: Error | unknown): void;
|
package/stream/State.js
CHANGED
|
@@ -23,8 +23,8 @@ export class State extends Stream {
|
|
|
23
23
|
return this._value;
|
|
24
24
|
}
|
|
25
25
|
/** Is there a current value, or is it still loading. */
|
|
26
|
-
get
|
|
27
|
-
return this._value
|
|
26
|
+
get exists() {
|
|
27
|
+
return this._value !== LOADING;
|
|
28
28
|
}
|
|
29
29
|
/** Apply a transformer to this state. */
|
|
30
30
|
apply(transformer) {
|
package/util/async.d.ts
CHANGED
|
@@ -59,7 +59,3 @@ export declare class Signal extends AbstractPromise<typeof DONE> {
|
|
|
59
59
|
/** Send this signal now. */
|
|
60
60
|
done(): void;
|
|
61
61
|
}
|
|
62
|
-
/** Resolve to `DONE` after a specified delay. */
|
|
63
|
-
export declare class Timeout extends AbstractPromise<typeof DONE> {
|
|
64
|
-
constructor(ms: number);
|
|
65
|
-
}
|
package/util/async.js
CHANGED
|
@@ -95,10 +95,3 @@ export class Signal extends AbstractPromise {
|
|
|
95
95
|
this._resolve(DONE);
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
|
-
/** Resolve to `DONE` after a specified delay. */
|
|
99
|
-
export class Timeout extends AbstractPromise {
|
|
100
|
-
constructor(ms) {
|
|
101
|
-
super();
|
|
102
|
-
setTimeout(this._resolve, ms, DONE);
|
|
103
|
-
}
|
|
104
|
-
}
|
package/util/index.d.ts
CHANGED
package/util/index.js
CHANGED
package/util/observable.d.ts
CHANGED
|
@@ -40,7 +40,7 @@ export interface Observer<T> {
|
|
|
40
40
|
*/
|
|
41
41
|
readonly from?: (source: Subscribable<T>) => this;
|
|
42
42
|
/** Receive the next value. */
|
|
43
|
-
readonly next
|
|
43
|
+
readonly next?: Dispatcher<[T]>;
|
|
44
44
|
/** End the subscription with an error. */
|
|
45
45
|
readonly error?: Handler;
|
|
46
46
|
/** End the subscription with success. */
|
package/util/observable.js
CHANGED
|
@@ -17,8 +17,9 @@ export function subscribe(source, target) {
|
|
|
17
17
|
}
|
|
18
18
|
/** Dispatch the next value to an observer (and if the next value errors, calls a handler). */
|
|
19
19
|
export function dispatchNext(observer, value) {
|
|
20
|
+
var _a;
|
|
20
21
|
try {
|
|
21
|
-
observer.next(value);
|
|
22
|
+
(_a = observer.next) === null || _a === void 0 ? void 0 : _a.call(observer, value);
|
|
22
23
|
}
|
|
23
24
|
catch (thrown) {
|
|
24
25
|
logError(thrown);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a new Timeout.
|
|
3
|
+
*
|
|
4
|
+
* Wrapper for `setTimeout()` that...
|
|
5
|
+
* - Keeps track of the reference returned from `setTimeout()`
|
|
6
|
+
* - Clears the any existing timeout when a new timeout is set.
|
|
7
|
+
* - Allows a default delay to be set that is applied to all new timeouts that don't have a delay set.
|
|
8
|
+
*
|
|
9
|
+
* @param ms The default delay for any created timeouts (in ms).
|
|
10
|
+
*/
|
|
11
|
+
export declare class Timeout {
|
|
12
|
+
private _ms;
|
|
13
|
+
private _timeout;
|
|
14
|
+
constructor(ms?: number);
|
|
15
|
+
/**
|
|
16
|
+
* Cancel any existing timeout and set a new one.
|
|
17
|
+
* @param callback
|
|
18
|
+
* @param ms The delay for this timeout (in ms).
|
|
19
|
+
*/
|
|
20
|
+
set(callback: () => void, ms?: number): void;
|
|
21
|
+
/** Cancel any existing timeout.. */
|
|
22
|
+
clear(): void;
|
|
23
|
+
}
|
package/util/timeout.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a new Timeout.
|
|
3
|
+
*
|
|
4
|
+
* Wrapper for `setTimeout()` that...
|
|
5
|
+
* - Keeps track of the reference returned from `setTimeout()`
|
|
6
|
+
* - Clears the any existing timeout when a new timeout is set.
|
|
7
|
+
* - Allows a default delay to be set that is applied to all new timeouts that don't have a delay set.
|
|
8
|
+
*
|
|
9
|
+
* @param ms The default delay for any created timeouts (in ms).
|
|
10
|
+
*/
|
|
11
|
+
export class Timeout {
|
|
12
|
+
constructor(ms = 0) {
|
|
13
|
+
this._timeout = undefined;
|
|
14
|
+
this._ms = ms;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Cancel any existing timeout and set a new one.
|
|
18
|
+
* @param callback
|
|
19
|
+
* @param ms The delay for this timeout (in ms).
|
|
20
|
+
*/
|
|
21
|
+
set(callback, ms = this._ms) {
|
|
22
|
+
this.clear();
|
|
23
|
+
this._timeout = setTimeout(callback, ms);
|
|
24
|
+
}
|
|
25
|
+
/** Cancel any existing timeout.. */
|
|
26
|
+
clear() {
|
|
27
|
+
if (this._timeout)
|
|
28
|
+
this._timeout = void clearTimeout(this._timeout);
|
|
29
|
+
}
|
|
30
|
+
}
|