diff --git a/package.json b/package.json index a404fcf..1a0e5a8 100644 --- a/package.json +++ b/package.json @@ -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 ", diff --git a/src/models/MstColl.ts b/src/models/MstColl.ts index 4bb6345..b099caf 100644 --- a/src/models/MstColl.ts +++ b/src/models/MstColl.ts @@ -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(); @@ -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(); diff --git a/src/models/MstRepository.ts b/src/models/MstRepository.ts index 817fe07..e5e9b22 100644 --- a/src/models/MstRepository.ts +++ b/src/models/MstRepository.ts @@ -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) => { @@ -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); } @@ -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; @@ -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'); }); }, diff --git a/src/models/MstSchemas.ts b/src/models/MstSchemas.ts index 25b78f2..75142c9 100644 --- a/src/models/MstSchemas.ts +++ b/src/models/MstSchemas.ts @@ -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; } }, @@ -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; }, }; }) @@ -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); } @@ -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); @@ -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; }), @@ -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; }), };