@streamscloud/kit 0.19.0 → 0.19.1

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.
@@ -8,11 +8,13 @@ export declare class CursorDataLoaderWithSearch<T> implements IDataLoader<T> {
8
8
  private pending;
9
9
  private generation;
10
10
  private searchStringMinLength;
11
+ private _loading;
11
12
  constructor(init: {
12
13
  loadPage: (continuationToken: ContinuationToken, searchString: string) => Promise<CursorResult<T> | null>;
13
14
  searchStringMinLength?: number;
14
15
  });
15
16
  get searchString(): string;
17
+ get loading(): boolean;
16
18
  loadMore: () => Promise<T[]>;
17
19
  reset(): Promise<void>;
18
20
  updateSearchString: (searchString: string | null) => void;
@@ -8,6 +8,7 @@ export class CursorDataLoaderWithSearch {
8
8
  pending = null;
9
9
  generation = 0;
10
10
  searchStringMinLength = 1;
11
+ _loading = $state(false);
11
12
  constructor(init) {
12
13
  this.loadPage = init.loadPage;
13
14
  if (init.searchStringMinLength !== undefined) {
@@ -18,6 +19,9 @@ export class CursorDataLoaderWithSearch {
18
19
  get searchString() {
19
20
  return this._searchString;
20
21
  }
22
+ get loading() {
23
+ return this._loading;
24
+ }
21
25
  loadMore = async () => {
22
26
  if (this.pending) {
23
27
  return this.pending;
@@ -25,6 +29,7 @@ export class CursorDataLoaderWithSearch {
25
29
  if (!this.continuationToken.canLoadMore) {
26
30
  return [];
27
31
  }
32
+ this._loading = true;
28
33
  const pending = this.runLoad();
29
34
  this.pending = pending;
30
35
  try {
@@ -34,12 +39,14 @@ export class CursorDataLoaderWithSearch {
34
39
  // a concurrent reset() may have installed a newer load — leave that one alone
35
40
  if (this.pending === pending) {
36
41
  this.pending = null;
42
+ this._loading = false;
37
43
  }
38
44
  }
39
45
  };
40
46
  async reset() {
41
47
  this.generation++;
42
48
  this.pending = null;
49
+ this._loading = false;
43
50
  this.items = [];
44
51
  this.continuationToken = ContinuationToken.init();
45
52
  await this.loadMore();
@@ -6,9 +6,11 @@ export declare class CursorDataLoader<T> implements IDataLoader<T> {
6
6
  private loadPage;
7
7
  private pending;
8
8
  private generation;
9
+ private _loading;
9
10
  constructor(init: {
10
11
  loadPage: (continuationToken: ContinuationToken) => Promise<CursorResult<T> | null>;
11
12
  });
13
+ get loading(): boolean;
12
14
  loadMore: () => Promise<T[]>;
13
15
  reset(): Promise<void>;
14
16
  private runLoad;
@@ -5,9 +5,13 @@ export class CursorDataLoader {
5
5
  loadPage;
6
6
  pending = null;
7
7
  generation = 0;
8
+ _loading = $state(false);
8
9
  constructor(init) {
9
10
  this.loadPage = init.loadPage;
10
11
  }
12
+ get loading() {
13
+ return this._loading;
14
+ }
11
15
  loadMore = async () => {
12
16
  if (this.pending) {
13
17
  return this.pending;
@@ -15,6 +19,7 @@ export class CursorDataLoader {
15
19
  if (!this.continuationToken.canLoadMore) {
16
20
  return [];
17
21
  }
22
+ this._loading = true;
18
23
  const pending = this.runLoad();
19
24
  this.pending = pending;
20
25
  try {
@@ -24,12 +29,14 @@ export class CursorDataLoader {
24
29
  // a concurrent reset() may have installed a newer load — leave that one alone
25
30
  if (this.pending === pending) {
26
31
  this.pending = null;
32
+ this._loading = false;
27
33
  }
28
34
  }
29
35
  };
30
36
  async reset() {
31
37
  this.generation++;
32
38
  this.pending = null;
39
+ this._loading = false;
33
40
  this.items = [];
34
41
  this.continuationToken = ContinuationToken.init();
35
42
  await this.loadMore();
@@ -1,4 +1,5 @@
1
1
  export interface IDataLoader<T> {
2
2
  items: T[];
3
+ loading: boolean;
3
4
  loadMore: () => Promise<T[]>;
4
5
  }
@@ -1,15 +1,18 @@
1
1
  import type { IDataLoader } from './data-loader';
2
2
  export declare class PageDataLoader<T> implements IDataLoader<T> {
3
3
  private _items;
4
+ private _loading;
4
5
  private canLoadMore;
5
6
  private perPage;
6
7
  private loadPage;
7
8
  private pageNumber;
9
+ private generation;
8
10
  constructor(init: {
9
11
  loadPage: (page: number, perPage: number) => Promise<T[]>;
10
12
  perPage?: number;
11
13
  });
12
14
  get items(): T[];
15
+ get loading(): boolean;
13
16
  loadMore: () => Promise<T[]>;
14
17
  reset: () => Promise<void>;
15
18
  }
@@ -1,9 +1,11 @@
1
1
  export class PageDataLoader {
2
2
  _items = $state.raw([]);
3
+ _loading = $state(false);
3
4
  canLoadMore = $state(true);
4
5
  perPage = 20;
5
6
  loadPage;
6
7
  pageNumber = 1;
8
+ generation = 0;
7
9
  constructor(init) {
8
10
  this.loadPage = init.loadPage;
9
11
  if (init.perPage) {
@@ -13,12 +15,22 @@ export class PageDataLoader {
13
15
  get items() {
14
16
  return this._items;
15
17
  }
18
+ get loading() {
19
+ return this._loading;
20
+ }
16
21
  loadMore = async () => {
17
- if (!this.canLoadMore) {
22
+ if (this._loading || !this.canLoadMore) {
18
23
  return [];
19
24
  }
25
+ const generation = this.generation;
26
+ this._loading = true;
20
27
  try {
21
- const items = await this.loadPage(this.pageNumber++, this.perPage);
28
+ const items = await this.loadPage(this.pageNumber, this.perPage);
29
+ // a reset() mid-load bumps the generation — drop the stale page instead of appending it to the cleared list
30
+ if (generation !== this.generation) {
31
+ return [];
32
+ }
33
+ this.pageNumber++;
22
34
  this.canLoadMore = items.length === this.perPage;
23
35
  this._items = [...this._items, ...items];
24
36
  return items;
@@ -27,8 +39,15 @@ export class PageDataLoader {
27
39
  console.error('PageDataLoader: failed to load page', error);
28
40
  return [];
29
41
  }
42
+ finally {
43
+ if (generation === this.generation) {
44
+ this._loading = false;
45
+ }
46
+ }
30
47
  };
31
48
  reset = async () => {
49
+ this.generation++;
50
+ this._loading = false;
32
51
  this.pageNumber = 1;
33
52
  this._items = [];
34
53
  this.canLoadMore = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@streamscloud/kit",
3
- "version": "0.19.0",
3
+ "version": "0.19.1",
4
4
  "author": "StreamsCloud",
5
5
  "repository": {
6
6
  "type": "git",