Skip to content

Commit

Permalink
Merge pull request #34 from agentlab/refactor-base-table
Browse files Browse the repository at this point in the history
[ADD] Stable loadMore, between filter for ints
  • Loading branch information
amivanoff authored Dec 20, 2021
2 parents faa50e0 + 1eafce6 commit fa4eb6f
Show file tree
Hide file tree
Showing 5 changed files with 807 additions and 719 deletions.
44 changes: 22 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@agentlab/sparql-jsld-client",
"version": "5.0.3",
"version": "5.0.4",
"description": "SPARQL JSON Schema Linked Data Client",
"license": "GPL-3.0",
"author": "Alexey Ivanov <[email protected]>",
Expand Down Expand Up @@ -58,13 +58,13 @@
"is-url": ">=1.2.4",
"jsonld": ">=5.2.0",
"lodash-es": ">=4.17.21",
"mobx": ">=6.3.7",
"mobx-state-tree": ">=5.0.5",
"mobx": ">=6.3.9",
"mobx-state-tree": ">=5.1.0",
"moment": ">=2.29.1",
"rdf-literal": ">=1.3.0",
"sparqljs": ">=3.5.1",
"utility-types": ">=3.10.0",
"uuid62": ">=1.0.1"
"uuid62": ">=1.0.2"
},
"dependencies": {
"@types/json-schema": "^7.0.9",
Expand All @@ -76,49 +76,49 @@
"is-url": "^1.2.4",
"jsonld": "^5.2.0",
"lodash-es": "^4.17.21",
"mobx": "^6.3.7",
"mobx-state-tree": "^5.0.5",
"mobx": "^6.3.9",
"mobx-state-tree": "^5.1.0",
"moment": "^2.29.1",
"rdf-literal": "^1.3.0",
"sparqljs": "^3.5.1",
"utility-types": "^3.10.0",
"uuid62": "^1.0.1"
"uuid62": "^1.0.2"
},
"devDependencies": {
"@babel/core": "^7.16.0",
"@babel/core": "^7.16.5",
"@rollup/plugin-babel": "^5.3.0",
"@rollup/plugin-commonjs": "^21.0.1",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.0.6",
"@rollup/plugin-node-resolve": "^13.1.1",
"@rollup/plugin-typescript": "^8.3.0",
"@types/is-url": "^1.2.30",
"@types/jest": "^27.0.2",
"@types/node": "^16.11.7",
"@typescript-eslint/eslint-plugin": "^5.4.0",
"@typescript-eslint/parser": "^5.4.0",
"@types/jest": "^27.0.3",
"@types/node": "^17.0.1",
"@typescript-eslint/eslint-plugin": "^5.8.0",
"@typescript-eslint/parser": "^5.8.0",
"custom-env": "^2.0.1",
"babel-loader": "^8.2.3",
"cross-env": "^7.0.3",
"eslint": "^8.2.0",
"eslint": "^8.5.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-flowtype": "^8.0.3",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-jest": "^25.2.4",
"eslint-plugin-jest": "^25.3.0",
"eslint-plugin-prettier": "^4.0.0",
"husky": "^7.0.4",
"jest": "^27.3.1",
"jest-extended": "^1.1.0",
"jest": "^27.4.5",
"jest-extended": "^1.2.0",
"jest-watch-typeahead": "^1.0.0",
"lint-staged": "^12.0.2",
"prettier": "^2.4.1",
"lint-staged": "^12.1.3",
"prettier": "^2.5.1",
"rimraf": "^3.0.2",
"rollup": "^2.60.0",
"rollup": "^2.61.1",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-peer-deps-external": "^2.2.4",
"rollup-plugin-terser": "^7.0.2",
"ts-jest": "^27.0.7",
"ts-jest": "^27.1.2",
"tslib": "^2.3.1",
"typescript": "4.4.4"
"typescript": "4.5.4"
},
"lint-staged": {
"{src,test}/**/*.{js,jsx,ts,tsx,json}": [
Expand Down
42 changes: 42 additions & 0 deletions src/SparqlGen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,48 @@ export function getExtendedFilter(
literal(`${filterProperty.value[0]}`, getFullIriNamedNode('xsd:integer', entConstr.prefixes)),
];
break;
case 'between':
filter.operator = '&&';
filter.args = [
{
args: [
variable,
literal(`${filterProperty.value[0]}`, getFullIriNamedNode('xsd:integer', entConstr.prefixes)),
],
operator: '>=',
type: 'operation',
},
{
args: [
variable,
literal(`${filterProperty.value[1]}`, getFullIriNamedNode('xsd:integer', entConstr.prefixes)),
],
operator: '<',
type: 'operation',
},
];
break;
case 'between-incl-both':
filter.operator = '&&';
filter.args = [
{
args: [
variable,
literal(`${filterProperty.value[0]}`, getFullIriNamedNode('xsd:integer', entConstr.prefixes)),
],
operator: '>=',
type: 'operation',
},
{
args: [
variable,
literal(`${filterProperty.value[1]}`, getFullIriNamedNode('xsd:integer', entConstr.prefixes)),
],
operator: '<=',
type: 'operation',
},
];
break;
case 'any':
buildEnumFilter(filter, variable, entConstr.prefixes, filterProperty.value, 'integer');
break;
Expand Down
43 changes: 28 additions & 15 deletions src/models/MstColl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* SPDX-License-Identifier: GPL-3.0-only
********************************************************************************/
import moment from 'moment';
import { isEqual } from 'lodash-es';
import { reaction } from 'mobx';
import {
types,
Expand Down Expand Up @@ -134,7 +135,7 @@ export const MstColl = types
*/
isLoading: types.optional(types.boolean, false),

pageSize: types.optional(types.number, 10),
pageSize: types.optional(types.number, 100),
/**
* Add colls to the repository for the discovered in dataIntrnl CollConstrs.
*/
Expand Down Expand Up @@ -201,11 +202,19 @@ export const MstColl = types
return collConstrJs;
},
(newVal: any, oldVal: any) => {
//console.log('MstColl.collConstr changed, reload data', { newVal, oldVal });
setImmediate(() => {
//@ts-ignore
self.loadColl();
});
// ignore if its internal loadMore()
if (
!self.isLoading &&
(!isEqual(newVal.entConstrs, oldVal.entConstrs) ||
!isEqual(newVal.orderBy, oldVal.orderBy) ||
(newVal.limit !== oldVal.limit && self.dataIntrnl.length !== newVal.limit))
) {
//console.log('MstColl.collConstr changed, reload data', { newVal, oldVal });
setImmediate(() => {
//@ts-ignore
self.loadColl();
});
}
},
{ fireImmediately: false, name: 'MstColl-Attach' },
);
Expand All @@ -225,6 +234,7 @@ export const MstColl = types
if (dispose) dispose();
},
loadColl: flow(function* loadColl() {
if (self.isLoading) return; // do not mess with other loading process in this coll
self.isLoading = true;
//console.log('loadColl START');
if (self.collConstr) {
Expand Down Expand Up @@ -279,7 +289,8 @@ export const MstColl = types
}),

loadMore: flow(function* loadMore() {
if (!self.isLoading) self.isLoading = true;
if (self.isLoading) return; // do not mess with other loading process in this coll
self.isLoading = true;
if (self.collConstr) {
const collConstr = {
...getSnapshot<ICollConstrSnapshotOut>(self.collConstr),
Expand All @@ -295,14 +306,16 @@ export const MstColl = types
rep.ns.currentJs,
client,
);
self.dataIntrnl.push(...objects);
//const objectsToAdd: any[] = [];
//objects.forEach((o) => {
//
//});
//self.dataIntrnl.push(...objectsToAdd);
if (self.collConstr.limit) {
self.collConstr.setLimit(self.dataIntrnl.length);
// it seems, we could have some duplicates in loadMore series in case with concurrent updates
const objectsToAdd: any[] = objects.filter(
(o: any) => !self.dataIntrnl.some((d: any) => d.get('@id') === o['@id']), // d is a MapType object
);
if (objectsToAdd.length > 0) {
self.dataIntrnl.push(...objectsToAdd);
if (self.collConstr.limit) {
// triggers reaction from afterAttach but we filter it out there in that reaction
self.collConstr.setLimit(self.dataIntrnl.length);
}
}
self.isLoading = false;
}
Expand Down
72 changes: 50 additions & 22 deletions test/SimpleRetrieve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,40 +433,68 @@ describe('RetrieveWithParent', () => {
});
});

/*describe('LoadMore', () => {
describe('LoadMore', () => {
it('should sync load incrementally additional data into Coll', async () => {
const coll = repository.addColl({
entConstrs: [
{
schema: 'rm:ArtifactShape',
},
],
limit: 10,
});
const objects = [{ '@id': 'same' }, { '@id': 'other1' }];
const dataIntrnl = [{ '@id': 'same' }, { '@id': 'other2' }];

/*const objectsToAdd: any[] = objects.filter((o: any) => {
const ret1 = dataIntrnl.some((d: any) => {
const ret2 = d['@id'] === o['@id'];
return ret2;
});
return !ret1;
});*/

const coll = repository.addColl(
{
entConstrs: [
{
schema: 'rm:ArtifactShape',
},
],
limit: 2,
},
{ pageSize: 10 },
);
expect(coll).not.toBeUndefined();
await coll.loadColl();
const data: any = coll && coll.data !== undefined ? getSnapshot(coll.data) : [];
expect(data.length).toBe(10);
expect(data.length).toBe(2);

// load another page=10
await coll.loadMore();
const data2: any = coll && coll.data !== undefined ? getSnapshot(coll.data) : [];
expect(data2.length).toBe(15);
repository.removeColl(coll);
expect(data2.length).toBe(12);
data.forEach((el: any, i: any) => expect(data2[i]).toEqual(el));
data2.slice(data.length).forEach((el: any) => expect(data).not.toContainEqual(el));

// load another 5 (the rest of it)
await coll.loadMore();
const data3: any = coll && coll.data !== undefined ? getSnapshot(coll.data) : [];
expect(data3.length).toBe(15);

// check empty load
await coll.loadMore();
const data4: any = coll && coll.data !== undefined ? getSnapshot(coll.data) : [];
expect(data4.length).toBe(15);

repository.removeColl(coll);
});

it('should async load incrementally additional data into Coll', () => {
return new Promise<void>((done) => {
const coll = repository.addColl({
entConstrs: [
{
schema: 'rm:ArtifactShape',
},
],
limit: 10,
});
const coll = repository.addColl(
{
entConstrs: [
{
schema: 'rm:ArtifactShape',
},
],
limit: 10,
},
{ pageSize: 10 },
);
expect(coll).not.toBeUndefined();

const disp1 = when(
Expand All @@ -487,4 +515,4 @@ describe('RetrieveWithParent', () => {
coll.loadColl();
});
});
});*/
});
Loading

0 comments on commit fa4eb6f

Please sign in to comment.