Skip to content

Commit

Permalink
[fix] Multiple schemas loading, isLoading
Browse files Browse the repository at this point in the history
  • Loading branch information
amivanoff committed Oct 19, 2021
1 parent d35a3c2 commit a175db0
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@agentlab/sparql-jsld-client",
"version": "5.0.0-rc.18",
"version": "5.0.0-rc.19",
"description": "SPARQL JSON Schema Linked Data Client",
"license": "GPL-3.0",
"author": "Alexey Ivanov <[email protected]>",
Expand Down
2 changes: 0 additions & 2 deletions src/models/MstColl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ export const MstColl = types
//console.log('MstColl afterAttach, @id=', self['@id']);
// first-time load
if (self.lazy === false) {
self.isLoading = true;
setImmediate(() => {
//@ts-ignore
self.loadColl();
Expand All @@ -201,7 +200,6 @@ export const MstColl = types
},
(newVal: any, oldVal: any) => {
//console.log('MstColl.collConstr changed, reload data', { newVal, oldVal });
self.isLoading = true;
setImmediate(() => {
//@ts-ignore
self.loadColl();
Expand Down
16 changes: 8 additions & 8 deletions src/models/MstRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,17 +275,17 @@ export const MstRepository = types
* @param by
*/
editConn(connections: any[], data: any) {
//console.log('editConn START with data=', data);
//console.log('editConn START with data=', { connections, data });
connections.forEach((conn: any) => {
const toId = conn.toObj;
let value = conn.fromProp === undefined || data === undefined ? data : data[conn.fromProp];
let value = conn.fromProp === undefined || data === undefined || data === null ? data : data[conn.fromProp];
//console.log('editConn conn with id=', toId);
const node: any = values(self.colls).find((coll: any) => {
//console.log('editConn coll=', getSnapshot(coll));
//console.log('editConn collConstr=', getSnapshot(coll.collConstr));
//console.log('editConn collConstr @id=', coll.collConstr['@id']);
if (coll.collConstr['@id'] === toId) {
//console.log('editConn found coll.collConstr');
console.log('editConn found coll.collConstr, ignore it');
return true;
}
return values(coll.collConstr.entConstrs).find((entConstr: any) => {
Expand All @@ -312,13 +312,13 @@ export const MstRepository = types
...entConstrJs,
};
if (value !== undefined) {
//console.log('editConn set key=', conn.toProp);
entConstrJs[conn.toProp] = value;
console.log('editConn set key=', conn.toProp);
//entConstrJs[conn.toProp] = value;
} else {
//console.log('editConn delete key=', conn.toProp);
delete entConstrJs[conn.toProp];
}
console.log('editConn set new entConstr=', entConstrJs);
console.log('editConn set new entConstr by conn', { entConstrJs, conn });
applySnapshot(entConstr, entConstrJs);
//console.log('editConn applied entConstr=', entConstrJs);
}
Expand Down Expand Up @@ -357,7 +357,7 @@ export const MstRepository = types
//console.log('editConn delete key=', conn.toProp);
delete condition[conn.toProp];
}
console.log('editConn set new condition=', condition);
console.log('editConn set new condition by conn', { condition, conn });
applySnapshot(node, condition);
//console.log('editConn applied condition=', condition);
return true;
Expand All @@ -376,7 +376,7 @@ export const MstRepository = types
// applySnapshot(node, condition);
// console.log('editConn applied condition=', condition);
//}
console.log('editConn END');
//console.log('editConn END');
});
},

Expand Down
77 changes: 62 additions & 15 deletions src/models/MstSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,16 @@ export const MstSchemas = types
} else {
// TODO: this is ugly, but workaround the idea that views should be side effect free.
// We need a more elegant solution.
//@ts-ignore
setImmediate(() => self.loadSchemaByClassIri(iri));
setImmediate(() => {
if (iri && !self.class2schema.has(iri)) {
try {
//@ts-ignore
self.loadSchemaByClassIri(iri);
} catch (err) {
console.log(err);
}
}
});
return null;
}
},
Expand All @@ -233,21 +241,23 @@ export const MstSchemas = types
getOrLoadSchemaByIri(iri: string | undefined) {
if (!iri || iri.length === 0) return null;
iri = abbreviateIri(iri, repository.ns.currentJs);
if (!self.json.has(iri)) {
// TODO: this is ugly, but workaround the idea that views should be side effect free.
// We need a more elegant solution.
setImmediate(async () => {
if (self.json.has(iri)) {
//console.log('getOrLoadSchemaByIri: return schema', iri);
return self.json.get(iri);
}
// TODO: this is ugly, but workaround the idea that views should be side effect free.
// We need a more elegant solution.
setImmediate(async () => {
if (iri && !self.json.has(iri)) {
try {
//@ts-ignore
await self.loadSchemaByIri(iri);
} catch (err) {
console.log(err);
}
});
return null;
} else {
return self.json.get(iri);
}
}
});
return null;
},
};
})
Expand Down Expand Up @@ -292,6 +302,7 @@ export const MstSchemas = types
for (let i = 0; i < schemaAllOf.length; i++) {
const iri = schemaAllOf[i].$ref;
if (!self.json.has(iri)) {
//console.log('getDirectSuperSchemas: load schema', iri);
//@ts-ignore
yield self.loadSchemaByIri(iri);
}
Expand Down Expand Up @@ -324,6 +335,9 @@ export const MstSchemas = types
return schema;
};

const loadingByClass: any = {};
const loadingByIri: any = {};

return {
addSchema(schema: JSONSchema6forRdf): void {
const iri: string = abbreviateIri(schema['@id'], repository.ns.currentJs);
Expand All @@ -346,8 +360,26 @@ export const MstSchemas = types
loadSchemaByClassIri: flow(function* loadSchemaByClassIri(iri: string | undefined) {
//console.log('loadSchemaByClassIri', iri);
if (!iri || iri.length === 0) return;
const schemaObs = yield loadSchemaInternal({ targetClass: iri });
//console.log('END loadSchemaByClassIri', iri);
// check loaded repo
let schemaObs: any = self.class2schema.get(iri);
if (schemaObs !== undefined) {
schemaObs = self.json.get(schemaObs);
if (schemaObs !== undefined) return schemaObs;
}
// check loading process
let loadSchemaGen = loadingByClass[iri];
if (loadSchemaGen) {
//console.log('WAIT START loadSchemaByClassIri', iri);
schemaObs = yield loadSchemaGen;
//console.log('WAIT END loadSchemaByClassIri', iri);
} else {
//console.log('LOAD START loadSchemaByClassIri', iri);
loadSchemaGen = loadSchemaInternal({ targetClass: iri });
loadingByClass[iri] = loadSchemaGen;
schemaObs = yield loadSchemaGen;
delete loadingByClass[iri];
//console.log('LOAD END loadSchemaByClassIri', iri);
}
return schemaObs;
}),

Expand All @@ -359,8 +391,23 @@ export const MstSchemas = types
loadSchemaByIri: flow(function* loadSchemaByIri(iri: string | undefined) {
//console.log('loadSchemaByIri', iri);
if (!iri || iri.length === 0) return;
const schemaObs = yield loadSchemaInternal({ '@_id': iri });
//console.log('END loadSchemaByIri', iri);
// check loaded repo
let schemaObs: any = self.json.get(iri);
if (schemaObs !== undefined) return schemaObs;
// check loading process
let loadSchemaGen = loadingByIri[iri];
if (loadSchemaGen) {
//console.log('WAIT START loadSchemaByIri', iri);
schemaObs = yield loadSchemaGen;
//console.log('WAIT END loadSchemaByIri', iri);
} else {
//console.log('LOAD START loadSchemaByIri', iri);
loadSchemaGen = loadSchemaInternal({ '@_id': iri });
loadingByIri[iri] = loadSchemaGen;
schemaObs = yield loadSchemaGen;
delete loadingByIri[iri];
//console.log('LOAD END loadSchemaByIri', iri);
}
return schemaObs;
}),
};
Expand Down

0 comments on commit a175db0

Please sign in to comment.