Skip to content

Commit

Permalink
Merge pull request #7254 from mokibit/rename-parent-variable
Browse files Browse the repository at this point in the history
www: Rename confusing variables
  • Loading branch information
p12tic committed Dec 5, 2023
2 parents 4c55b1c + 339c4de commit e4ccba9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
4 changes: 2 additions & 2 deletions www/react-data-module/src/data/BasicDataMultiCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ export class BasicDataMultiCollection<ParentDataType extends BaseClass,

@observable byParentId = observable.map<string, Collection>();
@observable sortedParentIds = observable.array<string>();
callback: (child: ParentDataType) => Collection;
callback: (parent: ParentDataType) => Collection;
private disposer: IReactionDisposer;

constructor(accessor: IDataAccessor,
parentArray: IObservableArray<ParentDataType> | null,
parentArrayMap: ObservableMap<string, DataCollection<ParentDataType>> | null,
parentFilteredIds: IObservableArray<string> | null,
callback: (child: ParentDataType) => Collection) {
callback: (parent: ParentDataType) => Collection) {
makeObservable(this);

this.accessor = accessor;
Expand Down
2 changes: 1 addition & 1 deletion www/react-data-module/src/data/DataCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class DataCollection<DataType extends BaseClass> implements IDataCollecti
}

getRelated<ChildDataType extends BaseClass>(
callback: (child: DataType) => DataCollection<ChildDataType>) {
callback: (parent: DataType) => DataCollection<ChildDataType>) {
return new DataMultiCollection<DataType, ChildDataType>(this.accessor, this.array, null, null,
callback);
}
Expand Down
37 changes: 22 additions & 15 deletions www/react-data-module/src/data/DataMultiCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,55 @@ export class DataMultiCollection<ParentDataType extends BaseClass,

// Acquires nth element across all collections tracked by this multi collection. The iteration
// order is in ascending order of parent IDs.
//
// Note that when there are many key-value pairs waiting for replies from the server side, nth
// element might appear in an unexpected position. First requests replies can take longer than
// replies to the following values, so they get their values first. In this case, nth element
// will actually be later than expected, because preceding values have not arrived yet.
// The nth element is always the same when there is only one key-value pair or when all the
// information has already been acquired from the server.
getNthOrNull(index: number): DataType | null {
for (const parentId of this.sortedParentIds) {
const parent = this.byParentId.get(parentId);
if (parent === undefined) {
const childCollection = this.byParentId.get(parentId);
if (childCollection === undefined) {
continue;
}
if (index < parent.array.length) {
return parent.array[index];
if (index < childCollection.array.length) {
return childCollection.array[index];
}
index -= parent.array.length;
index -= childCollection.array.length;
}
return null;
}

getAll(): DataType[] {
const all: DataType[] = [];
for (const parentId of this.sortedParentIds) {
const parent = this.byParentId.get(parentId);
if (parent === undefined) {
const childCollection = this.byParentId.get(parentId);
if (childCollection === undefined) {
continue;
}
all.push(...parent.array);
all.push(...childCollection.array);
}
return all;
}

getNthOfParentOrNull(parentId: string, index: number): DataType | null {
const collection = this.byParentId.get(parentId);
if (collection === undefined) {
const childCollection = this.byParentId.get(parentId);
if (childCollection === undefined) {
return null;
}
if (index >= collection.array.length) {
if (index >= childCollection.array.length) {
return null;
}
return collection.array[index];
return childCollection.array[index];
}

getParentCollectionOrEmpty(parentId: string): DataCollection<DataType> {
const collection = this.byParentId.get(parentId);
if (collection === undefined) {
const childCollection = this.byParentId.get(parentId);
if (childCollection === undefined) {
return new DataCollection<DataType>();
}
return collection;
return childCollection;
}
}

0 comments on commit e4ccba9

Please sign in to comment.