update对ListNode结果的变化影响

This commit is contained in:
Xu Chang 2025-01-03 16:12:57 +08:00
parent 769951ee54
commit 1fdeff00f4
3 changed files with 140 additions and 2 deletions

View File

@ -1,5 +1,5 @@
import { assert } from 'oak-domain/lib/utils/assert';
import { cloneDeep, unset, uniq } from "oak-domain/lib/utils/lodash";
import { cloneDeep, difference, unset, uniq, intersection } from "oak-domain/lib/utils/lodash";
import { combineFilters, getRelevantIds } from "oak-domain/lib/store/filter";
import { createOperationsFromModies } from 'oak-domain/lib/store/modi';
import { judgeRelation } from "oak-domain/lib/store/relation";
@ -507,6 +507,50 @@ class ListNode extends EntityNode {
}
case 'u':
default: {
// 更新可能会引起本行不再满足条件
const { e, d, f } = record;
if (e === this.entity) {
const { id } = f;
const ids = typeof id === 'string' ? [id] : id.$in;
const currentIds = Object.keys(this.sr);
const intersected = intersection(ids, currentIds);
const diffed = difference(ids, currentIds);
/**
* 以及原本不在的现在是不是满足条件了
* 后一种情况也可能原本就满足但不在当前的ids中这时是判断不出来的total+1可能不对但是应该是较少的case*/
const filter = this.constructFilters(true, true, true);
if (intersected.length) {
const rows = this.cache.get(this.entity, {
data: {
id: 1,
},
filter: combineFilters(this.entity, this.schema, [
filter,
{
id: {
$in: intersected,
}
}
])
});
const rowIds = rows.map(ele => ele.id);
const missed = difference(intersected, rowIds);
missed.forEach((id) => {
unset(this.sr, id);
if (this.pagination.total) {
this.pagination.total--;
}
});
}
if (diffed.length) {
diffed.forEach((ele) => {
this.sr[ele] = {};
if (this.pagination.total) {
this.pagination.total++;
}
});
}
}
// hasUpdated = true;
break;
}

View File

@ -510,6 +510,50 @@ class ListNode extends EntityNode {
}
case 'u':
default: {
// 更新可能会引起本行不再满足条件
const { e, d, f } = record;
if (e === this.entity) {
const { id } = f;
const ids = typeof id === 'string' ? [id] : id.$in;
const currentIds = Object.keys(this.sr);
const intersected = (0, lodash_1.intersection)(ids, currentIds);
const diffed = (0, lodash_1.difference)(ids, currentIds);
/**
* 以及原本不在的现在是不是满足条件了
* 后一种情况也可能原本就满足但不在当前的ids中这时是判断不出来的total+1可能不对但是应该是较少的case*/
const filter = this.constructFilters(true, true, true);
if (intersected.length) {
const rows = this.cache.get(this.entity, {
data: {
id: 1,
},
filter: (0, filter_1.combineFilters)(this.entity, this.schema, [
filter,
{
id: {
$in: intersected,
}
}
])
});
const rowIds = rows.map(ele => ele.id);
const missed = (0, lodash_1.difference)(intersected, rowIds);
missed.forEach((id) => {
(0, lodash_1.unset)(this.sr, id);
if (this.pagination.total) {
this.pagination.total--;
}
});
}
if (diffed.length) {
diffed.forEach((ele) => {
this.sr[ele] = {};
if (this.pagination.total) {
this.pagination.total++;
}
});
}
}
// hasUpdated = true;
break;
}

View File

@ -1,5 +1,5 @@
import { assert } from 'oak-domain/lib/utils/assert';
import { cloneDeep, difference, unset, merge, uniq, omit } from "oak-domain/lib/utils/lodash";
import { cloneDeep, difference, unset, merge, uniq, omit, intersection } from "oak-domain/lib/utils/lodash";
import { combineFilters, getRelevantIds } from "oak-domain/lib/store/filter";
import { createOperationsFromModies } from 'oak-domain/lib/store/modi';
import { judgeRelation } from "oak-domain/lib/store/relation";
@ -628,6 +628,56 @@ class ListNode<
}
case 'u':
default: {
// 更新可能会引起本行不再满足条件
const { e, d, f } = record as UpdateOpResult<ED, keyof ED>;
if (e === this.entity) {
const { id } = f!;
const ids: string[] = typeof id === 'string' ? [id] : id.$in;
const currentIds = Object.keys(this.sr);
const intersected = intersection(ids, currentIds);
const diffed = difference(ids, currentIds);
/**
*
* ids中total+1case*/
const filter = this.constructFilters(true, true, true);
if (intersected.length) {
const rows = this.cache.get(this.entity, {
data: {
id: 1,
},
filter: combineFilters(this.entity, this.schema, [
filter,
{
id: {
$in: intersected,
}
}
])
});
const rowIds = rows.map(ele => ele.id!);
const missed = difference(intersected, rowIds);
missed.forEach(
(id) => {
unset(this.sr, id);
if (this.pagination.total) {
this.pagination.total --;
}
}
);
}
if (diffed.length) {
diffed.forEach(
(ele) => {
this.sr[ele] = {};
if (this.pagination.total) {
this.pagination.total ++;
}
}
);
}
}
// hasUpdated = true;
break;
}