shelving 1.57.1 → 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.
@@ -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 Pagination<T extends Data> extends State<Results<T>> implements Iterable<Entry<T>> {
10
- protected _pending: boolean;
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 Pagination extends State {
9
+ export class PaginationState extends State {
10
10
  constructor(ref) {
11
11
  super();
12
- this._pending = false; // Prevents double-loading.
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._pending) {
21
- this._pending = true;
22
- if (!this.loading) {
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._pending = false;
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
@@ -1,4 +1,4 @@
1
1
  export * from "./Database.js";
2
2
  export * from "./Operation.js";
3
- export * from "./Pagination.js";
3
+ export * from "./PaginationState.js";
4
4
  export * from "./errors.js";
package/db/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  export * from "./Database.js";
2
2
  export * from "./Operation.js";
3
- export * from "./Pagination.js";
3
+ export * from "./PaginationState.js";
4
4
  export * from "./errors.js";
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "state-management",
12
12
  "query-builder"
13
13
  ],
14
- "version": "1.57.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.15",
66
- "@typescript-eslint/eslint-plugin": "^5.20.0",
67
- "@typescript-eslint/parser": "^5.20.0",
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.6.11",
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.3"
79
+ "typescript": "^4.6.4"
80
80
  },
81
81
  "peerDependencies": {
82
82
  "@google-cloud/firestore": ">=4.0.0",
@@ -1,6 +1,6 @@
1
- import { DatabaseQuery, Results, Pagination, Data } from "../index.js";
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>): Pagination<T>;
6
+ export declare function usePagination<T extends Data>(ref: DatabaseQuery<T>, initial?: Results<T>): PaginationState<T>;
@@ -1,4 +1,4 @@
1
- import { Pagination } from "../index.js";
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 Pagination(ref);
14
+ const pagination = new PaginationState(ref);
15
15
  if (initial)
16
16
  pagination.next(initial);
17
17
  return pagination;
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 loading(): boolean;
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 loading() {
27
- return this._value === LOADING;
26
+ get exists() {
27
+ return this._value !== LOADING;
28
28
  }
29
29
  /** Apply a transformer to this state. */
30
30
  apply(transformer) {