@ngutil/data 0.0.11 → 0.0.13

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,11 +1,11 @@
1
1
  import { flattenDepth, intersection, flattenDeep, isEqual, sortBy as sortBy$1, groupBy as groupBy$1, merge } from 'lodash';
2
2
  import * as i1 from '@ngutil/common';
3
3
  import { isPlainObject, deepClone, toSorted, deepFreeze } from '@ngutil/common';
4
+ import { BehaviorSubject, map, combineLatest, take, of, shareReplay, ReplaySubject, switchMap, distinctUntilChanged, Subject, tap, debounceTime, finalize, Observable, takeUntil, share, Subscription, throwError } from 'rxjs';
4
5
  import { DataSource as DataSource$1 } from '@angular/cdk/collections';
5
- import { BehaviorSubject, map, combineLatest, take, of, shareReplay, ReplaySubject, distinctUntilChanged, Subject, tap, switchMap, debounceTime, finalize, Observable, takeUntil, share, Subscription, throwError } from 'rxjs';
6
6
  import * as i0 from '@angular/core';
7
7
  import { Directive, Optional, Input } from '@angular/core';
8
- import { toSignal } from '@angular/core/rxjs-interop';
8
+ import { takeUntilDestroyed, toSignal } from '@angular/core/rxjs-interop';
9
9
 
10
10
  function pathGetterCompile(path) {
11
11
  if (!path || path.length === 0) {
@@ -538,6 +538,13 @@ function sorterMerge(...sorters) {
538
538
  }
539
539
  return result;
540
540
  }
541
+ function sorterFind(sorters, name) {
542
+ const sorter = sorters.find(v => v[name] != null);
543
+ if (sorter != null) {
544
+ return sorter[name];
545
+ }
546
+ return undefined;
547
+ }
541
548
 
542
549
  function slimBy(slimer) { }
543
550
  function slimerNormalize(slimer) { }
@@ -935,8 +942,34 @@ class SorterCombined extends PropertyCombined {
935
942
  super(...arguments);
936
943
  this.normal = new SorterProperty(undefined);
937
944
  this.forced = new SorterProperty(undefined);
945
+ // TODO: normalized sorter
938
946
  this.merged$ = mergedProperty(sorterMerge, this.normal, this.forced);
939
947
  }
948
+ of(name) {
949
+ return this.merged$.pipe(map((sorters) => sorterFind(sorters, name)));
950
+ }
951
+ directionOf(name) {
952
+ return this.of(name).pipe(map(value => {
953
+ if (value == null) {
954
+ return undefined;
955
+ }
956
+ else if (typeof value === "string") {
957
+ return value;
958
+ }
959
+ else {
960
+ return value.dir;
961
+ }
962
+ }));
963
+ }
964
+ isSet(name) {
965
+ return this.directionOf(name).pipe(map(v => v != null));
966
+ }
967
+ isAsc(name) {
968
+ return this.directionOf(name).pipe(map(v => v === "asc" /* SortDirection.Asc */));
969
+ }
970
+ isDesc(name) {
971
+ return this.directionOf(name).pipe(map(v => v === "desc" /* SortDirection.Desc */));
972
+ }
940
973
  }
941
974
 
942
975
  class SlimerProperty extends Property {
@@ -988,29 +1021,27 @@ class DataSource extends DataSource$1 {
988
1021
  grouper: this.grouper.merged$
989
1022
  }).pipe(shareReplay(1));
990
1023
  this.#slice = new ReplaySubject(1);
991
- this.slice$ = this.#slice.pipe(distinctUntilChanged(isEqual), map(slice => deepFreeze(deepClone(slice))), shareReplay(1));
1024
+ this.slice$ = this.#slice.pipe(switchMap(slice => this.provider.clampSlice(slice)), distinctUntilChanged(isEqual), map(slice => deepFreeze(deepClone(slice))), shareReplay(1));
992
1025
  this.#reload = new Subject();
993
1026
  this.reset$ = merge(this.#queryBase, this.#reload).pipe(tap(() => this.#setBusy(true)), switchMap(() => this.store.clear()), shareReplay(1));
994
1027
  this.query$ = combineLatest({ base: this.#queryBase, slice: this.slice$ }).pipe(map(({ base, slice }) => {
995
1028
  return { ...base, slice };
996
1029
  }), shareReplay(1));
997
- this.items$ = combineLatest({ query: this.query$, reset: this.reset$ }).pipe(tap(() => this.#setBusy(true)), debounceTime(DEBOUNCE_TIME), switchMap(({ query }) =>
998
- // TODO: maybe hasSlice + getSlice
999
- this.store.getSlice(query.slice).pipe(switchMap(items => {
1000
- // TODO: endReached
1001
- const hasMissing = items.some(v => v == null);
1002
- if (hasMissing) {
1003
- return this.provider.queryList(query).pipe(switchMap(result => {
1004
- if (result.total != null) {
1005
- this.total$.next(result.total);
1006
- }
1007
- return this.store.insertSlice(query.slice, result.items);
1008
- }));
1009
- }
1010
- else {
1011
- return of(items);
1012
- }
1013
- }))), finalize(() => this.#setBusy(false)), shareReplay(1));
1030
+ this.items$ = combineLatest({ query: this.query$, reset: this.reset$ }).pipe(tap(() => this.#setBusy(true)), debounceTime(DEBOUNCE_TIME), switchMap(({ query }) => {
1031
+ return this.store.hasSlice(query.slice).pipe(switchMap(hasSlice => {
1032
+ if (hasSlice) {
1033
+ return this.store.getSlice(query.slice);
1034
+ }
1035
+ else {
1036
+ return this.provider.queryList(query).pipe(switchMap(result => {
1037
+ if (result.total != null) {
1038
+ this.total$.next(result.total);
1039
+ }
1040
+ return this.store.insertSlice(query.slice, result.items);
1041
+ }), take(1));
1042
+ }
1043
+ }));
1044
+ }), finalize(() => this.#setBusy(false)), shareReplay(1));
1014
1045
  this.#cvSubs = new Map();
1015
1046
  if (store == null) {
1016
1047
  store = new MemoryStore();
@@ -1021,6 +1052,9 @@ class DataSource extends DataSource$1 {
1021
1052
  this.#slice.next(slice);
1022
1053
  return this;
1023
1054
  }
1055
+ all() {
1056
+ return this.setSlice({ start: 0, end: Infinity });
1057
+ }
1024
1058
  realod() {
1025
1059
  this.#reload.next();
1026
1060
  }
@@ -1100,7 +1134,7 @@ class DataSource extends DataSource$1 {
1100
1134
  * ```
1101
1135
  */
1102
1136
  class DataSourceProxy extends DataSource$1 {
1103
- set source(value) {
1137
+ set value(value) {
1104
1138
  this.#valueSub?.unsubscribe();
1105
1139
  this.#valueSub = coerceDataSource(value)
1106
1140
  .pipe(tap(v => {
@@ -1108,14 +1142,18 @@ class DataSourceProxy extends DataSource$1 {
1108
1142
  throw new Error("Missing DataSource");
1109
1143
  }
1110
1144
  }))
1111
- .subscribe(this.source$);
1145
+ .subscribe(this.#value);
1146
+ }
1147
+ get value() {
1148
+ return this.#valueSig();
1112
1149
  }
1113
1150
  #valueSub;
1151
+ #value;
1152
+ #valueSig;
1114
1153
  set filter(value) {
1115
1154
  this.#filter.next(value);
1116
1155
  }
1117
1156
  #filter;
1118
- // TODO: maybe mergedFilter$ = this.value$.pipe(switchMap(value => value.filter.merged$))
1119
1157
  set sorter(value) {
1120
1158
  this.#sorter.next(value);
1121
1159
  }
@@ -1131,9 +1169,11 @@ class DataSourceProxy extends DataSource$1 {
1131
1169
  #subs;
1132
1170
  constructor(busy) {
1133
1171
  super();
1134
- this.source$ = new ReplaySubject(1);
1135
- this.items$ = this.source$.pipe(switchMap(value => value.items$), share());
1136
- this.busy$ = this.source$.pipe(switchMap(value => value.busy$), share());
1172
+ this.#value = new ReplaySubject(1);
1173
+ this.value$ = this.#value.pipe(takeUntilDestroyed());
1174
+ this.#valueSig = toSignal(this.value$);
1175
+ this.items$ = this.value$.pipe(switchMap(value => value.items$), share());
1176
+ this.busy$ = this.value$.pipe(switchMap(value => value.busy$), share());
1137
1177
  this.isBusy = toSignal(this.busy$, { rejectErrors: true, initialValue: false });
1138
1178
  this.#filter = new ReplaySubject(1);
1139
1179
  this.#sorter = new ReplaySubject(1);
@@ -1144,16 +1184,16 @@ class DataSourceProxy extends DataSource$1 {
1144
1184
  if (busy != null) {
1145
1185
  this.#subs.add(busy.connect(this.busy$).subscribe());
1146
1186
  }
1147
- this.#subs.add(combineLatest({ src: this.source$, filter: this.#filter }).subscribe(({ src, filter }) => {
1187
+ this.#subs.add(combineLatest({ src: this.value$, filter: this.#filter }).subscribe(({ src, filter }) => {
1148
1188
  src.filter.forced.set(filter);
1149
1189
  }));
1150
- this.#subs.add(combineLatest({ src: this.source$, sorter: this.#sorter }).subscribe(({ src, sorter }) => {
1190
+ this.#subs.add(combineLatest({ src: this.value$, sorter: this.#sorter }).subscribe(({ src, sorter }) => {
1151
1191
  src.sorter.forced.set(sorter);
1152
1192
  }));
1153
- this.#subs.add(combineLatest({ src: this.source$, grouper: this.#grouper }).subscribe(({ src, grouper }) => {
1193
+ this.#subs.add(combineLatest({ src: this.value$, grouper: this.#grouper }).subscribe(({ src, grouper }) => {
1154
1194
  src.grouper.forced.set(grouper);
1155
1195
  }));
1156
- this.#subs.add(combineLatest({ src: this.source$, slimer: this.#slimer }).subscribe(({ src, slimer }) => {
1196
+ this.#subs.add(combineLatest({ src: this.value$, slimer: this.#slimer }).subscribe(({ src, slimer }) => {
1157
1197
  src.slimer.forced.set(slimer);
1158
1198
  }));
1159
1199
  }
@@ -1162,7 +1202,7 @@ class DataSourceProxy extends DataSource$1 {
1162
1202
  const until = new Subject();
1163
1203
  this.#cvSubs.get(collectionViewer)?.next();
1164
1204
  this.#cvSubs.set(collectionViewer, until);
1165
- return this.source$.pipe(switchMap(value => value.connect(collectionViewer)), takeUntil(until), finalize(() => this.#cvSubs.delete(collectionViewer)));
1205
+ return this.value$.pipe(switchMap(value => value.connect(collectionViewer)), takeUntil(until), finalize(() => this.#cvSubs.delete(collectionViewer)));
1166
1206
  }
1167
1207
  disconnect(collectionViewer) {
1168
1208
  this.#cvSubs.get(collectionViewer)?.next();
@@ -1173,10 +1213,10 @@ class DataSourceProxy extends DataSource$1 {
1173
1213
  this.#valueSub = undefined;
1174
1214
  this.#subs.unsubscribe();
1175
1215
  }
1176
- static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.5", ngImport: i0, type: DataSourceProxy, deps: [{ token: i1.Busy, optional: true }], target: i0.ɵɵFactoryTarget.Directive }); }
1177
- static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "17.3.5", type: DataSourceProxy, isStandalone: true, selector: "[nuDataSource]", inputs: { source: ["nuDataSource", "source"], filter: "filter", sorter: "sorter", grouper: "grouper", slimer: "slimer" }, exportAs: ["nuDataSource"], usesInheritance: true, ngImport: i0 }); }
1216
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.6", ngImport: i0, type: DataSourceProxy, deps: [{ token: i1.Busy, optional: true }], target: i0.ɵɵFactoryTarget.Directive }); }
1217
+ static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "17.3.6", type: DataSourceProxy, isStandalone: true, selector: "[nuDataSource]", inputs: { value: ["nuDataSource", "value"], filter: "filter", sorter: "sorter", grouper: "grouper", slimer: "slimer" }, exportAs: ["nuDataSource"], usesInheritance: true, ngImport: i0 }); }
1178
1218
  }
1179
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.5", ngImport: i0, type: DataSourceProxy, decorators: [{
1219
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.6", ngImport: i0, type: DataSourceProxy, decorators: [{
1180
1220
  type: Directive,
1181
1221
  args: [{
1182
1222
  standalone: true,
@@ -1185,7 +1225,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.5", ngImpor
1185
1225
  }]
1186
1226
  }], ctorParameters: () => [{ type: i1.Busy, decorators: [{
1187
1227
  type: Optional
1188
- }] }], propDecorators: { source: [{
1228
+ }] }], propDecorators: { value: [{
1189
1229
  type: Input,
1190
1230
  args: [{ required: true, alias: "nuDataSource" }]
1191
1231
  }], filter: [{
@@ -1199,7 +1239,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.5", ngImpor
1199
1239
  }] } });
1200
1240
  function coerceDataSource(value) {
1201
1241
  if (value instanceof DataSourceProxy) {
1202
- return value.source$;
1242
+ return value.value$;
1203
1243
  }
1204
1244
  else if (value instanceof DataSource) {
1205
1245
  return of(value);
@@ -1213,6 +1253,12 @@ function coerceDataSource(value) {
1213
1253
  }
1214
1254
 
1215
1255
  class DataProvider {
1256
+ /**
1257
+ * Froce Slice boundaries, useful in array, or obeservable providers
1258
+ */
1259
+ clampSlice(slice) {
1260
+ return of(slice);
1261
+ }
1216
1262
  /**
1217
1263
  * @returns New data source instance
1218
1264
  */
@@ -1241,6 +1287,9 @@ class LocalProvider extends DataProvider {
1241
1287
  queryPosition(ref, request) {
1242
1288
  return this.queryList(request).pipe(map(list => list.items.findIndex(ref.toFilter())));
1243
1289
  }
1290
+ clampSlice(slice) {
1291
+ return this.items$.pipe(take(1), map(items => sliceClamp(slice, { start: 0, end: items.length })));
1292
+ }
1244
1293
  }
1245
1294
 
1246
1295
  class ArrayProvider extends LocalProvider {
@@ -1261,5 +1310,5 @@ class ObservableProvider extends LocalProvider {
1261
1310
  * Generated bundle index. Do not edit.
1262
1311
  */
1263
1312
 
1264
- export { ArrayProvider, CollectionStore, DataProvider, DataSource, DataSourceProxy, LocalProvider, MemoryStore, ModelMeta, ModelRefByIndex, ModelRefByKey, ModelRefNorm, ObservableProvider, UnknownMeta, filterBy, filterMerge, groupBy, grouperMerge, pathGetterCompile, queryExecutor, sliceApply, sliceClamp, sliceEq, sliceInsert, sliceMerge, sliceToPages, slimBy, slimerMerge, sortBy, sorterMerge };
1313
+ export { ArrayProvider, CollectionStore, DataProvider, DataSource, DataSourceProxy, LocalProvider, MemoryStore, ModelMeta, ModelRefByIndex, ModelRefByKey, ModelRefNorm, ObservableProvider, UnknownMeta, filterBy, filterMerge, groupBy, grouperMerge, pathGetterCompile, queryExecutor, sliceApply, sliceClamp, sliceEq, sliceInsert, sliceMerge, sliceToPages, slimBy, slimerMerge, sortBy, sorterFind, sorterMerge };
1265
1314
  //# sourceMappingURL=ngutil-data.mjs.map