UNPKG

grafo-client

Version:

Grafo Client API Library

1 lines 8.56 kB
"use strict";const async=require("async"),WebSocket=require("ws");var ShareDB=require("sharedb/lib/client"),ConfHelper=require("./conf/conf_helper"),ConversationApi=require("./conversation_api"),EkgPersistenceError=require("./errors/EkgPersistenceError");const GrafoErrorGlossary=require("./errors/GrafoErrorGlossary");var util=require("./util");function EkgShareDBAdapter(t){this.callbackId={},this.callback={},this.logger=t}module.exports=EkgShareDBAdapter,EkgShareDBAdapter.prototype.init=function(t,e,o){this.isProxy=!0,this.confHelper=new ConfHelper(t),this.sessionCookie=e,this.ekgApi=o,this.socket=new WebSocket(this.confHelper.getBaseRestUrl(),[],{headers:{Cookie:e}}),this.connection=new ShareDB.Connection(this.socket),this.ShareDBDocProxies={}},EkgShareDBAdapter.prototype.setSessionId=function(t){this.sessionId=t},EkgShareDBAdapter.prototype.initServer=function(t,e){this.isProxy=!1,this.connection=t.connect(),this.ekgApi=e,this.ShareDBDocProxies={}},EkgShareDBAdapter.prototype.getEkgProxy=function(t){return this.initConnection(t,!1)},EkgShareDBAdapter.prototype.initConnection=async function(t,e){void 0===e&&(e=!0),this.collId=t,this.isProxy?this.glbSockets=await getSockets(this):await getConnectionToCollection(this),this.ShareDBDocProxies=await getShareDBDocProxies(this),await setupEvents(this,this.ShareDBDocProxies,e)},EkgShareDBAdapter.prototype.close=async function(){void 0!==this.ShareDBDocProxies.data0&&await this.ShareDBDocProxies.data0.connection.destroy(),void 0!==this.ShareDBDocProxies.data1&&await this.ShareDBDocProxies.data1.connection.destroy()},EkgShareDBAdapter.prototype.getConversationApi=function(){return new ConversationApi(this.glbSockets)},EkgShareDBAdapter.prototype.unregisterCallback=function(t,e){console.log("Unregistering callback "+t);let o=e._this||this;delete o.callbackId[t],o.callback[t]&&delete o.callback[t]},EkgShareDBAdapter.prototype.getRegisteredCallbackId=function(t){return this.callbackId[t]},EkgShareDBAdapter.prototype.createConcept=async function(t,e){this.callbackId[t.getId()]=t.getId();let o=t.getShareDbCreatePackets();await util.eachSeries(o,submitNewObjectForAsyncIterator,{_this:this});delete this.callbackId[t.getId()]},EkgShareDBAdapter.prototype.updateConcept=async function(t,e,o){this.callbackId[t.getId()]=t.getId();let r=t.getShareDbUpdatePackets(e);await util.eachSeries(r,submitUpdateObjectForAsyncIterator,{_this:this});delete this.callbackId[t.getId()]},EkgShareDBAdapter.prototype.getConcept=function(t){return getObjectSync(this,t.getSlot(),t.getId(),"nodes")},EkgShareDBAdapter.prototype.addAttribute=async function(t,e,o){let r=await t.getIdsForCallbackRegistration(),n=(await util.eachSeries(r,async(t,e)=>{e._this.callbackId[t]=t},{_this:this}),await t.getShareDbCreatePackets());await util.eachSeries(n,submitNewObjectForAsyncIterator,{_this:this});return await util.eachSeriesCbFunctionWithoutPromise(r,this.unregisterCallback,{_this:this})},EkgShareDBAdapter.prototype.updateAttribute=async function(t,e,o,r,n){let i=await t.getIdsForCallbackRegistration(),a=(await util.eachSeries(i,async(t,e)=>{e._this.callbackId[t]=t},{_this:this}),await t.getShareDbUpdatePackets(e));await util.eachSeries(a,submitUpdateObjectForAsyncIterator,{_this:this});return await util.eachSeriesCbFunctionWithoutPromise(i,this.unregisterCallback,{_this:this})},EkgShareDBAdapter.prototype.getAttributeGroup=function(t){return getObjectSync(this,t.getSlot(),t.getId(),"attributeGroups")},EkgShareDBAdapter.prototype.isConceptAbsent=function(t){return!this.getConcept(t)},EkgShareDBAdapter.prototype.isAttributeGroupAbsent=function(t){return!this.getAttributeGroup(t)},EkgShareDBAdapter.prototype.addCoordinatesForSelfLink=function(t){submitNewObjectSync(this,t.slot,t.getId(),"coordinates",t.getCoordinates())},EkgShareDBAdapter.prototype.updateIri=function(t){submitNewObjectSync(this,t.slot,t.getId(),"iri",t.getIri())},EkgShareDBAdapter.prototype.createRelationship=async function(t,e){this.callbackId[t.getId()]=t.getId(),this.callbackId[t.linkObjForFromConcept.nodeId]=t.linkObjForFromConcept.nodeId,t.linkObjForToConcept&&(this.callbackId[t.linkObjForToConcept.nodeId]=t.linkObjForToConcept.nodeId);let o=t.getShareDbCreatePackets();await util.eachSeries(o,submitNewObjectForAsyncIterator,{_this:this});delete this.callbackId[t.getId()],delete this.callbackId[t.linkObjForFromConcept.nodeId],t.linkObjForToConcept&&delete this.callbackId[t.linkObjForToConcept.nodeId]},EkgShareDBAdapter.prototype.updateRelationship=async function(t,e,o){this.callbackId[t.getId()]=t.getId(),this.callbackId[t.linkObjForFromConcept.nodeId]=t.linkObjForFromConcept.nodeId,t.linkObjForToConcept&&(this.callbackId[t.linkObjForToConcept.nodeId]=t.linkObjForToConcept.nodeId);let r=t.getShareDbUpdatePackets(e);await util.eachSeries(r,submitUpdateObjectForAsyncIterator,{_this:this});delete this.callbackId[t.getId()],delete this.callbackId[t.linkObjForFromConcept.nodeId],t.linkObjForToConcept&&delete this.callbackId[t.linkObjForToConcept.nodeId]},EkgShareDBAdapter.prototype.getLinks=function(t){return getObjectSync(this,t.getSlot(),t.getId(),"links")},EkgShareDBAdapter.prototype.getLinksCopy=function(t){return getObjectCopySync(this,t.getSlot(),t.getId(),"links")},EkgShareDBAdapter.prototype.isLinkAbsent=function(t){return!this.getLinks(t)};var submitNewObjectForAsyncIterator=async function(t,e){return await submitNewObjectSync(e._this,t.getSlot(),t.getId(),t.getArrayName(),t.getObj())},submitUpdateObjectForAsyncIterator=async function(t,e){return await submitUpdateObjectSync(e._this,t.getSlot(),t.getId(),t.getArrayName(),t.getObj(),t.getOldObj())},getSockets=async function(t){t.socket=new WebSocket(t.confHelper.getBaseWebsocketUrl()+"/"+t.collId+"/"+t.sessionId,[],{headers:{Cookie:t.sessionCookie}}),await t.connection.bindToSocket(t.socket);var e=new WebSocket(t.confHelper.getBaseWebsocketUrl()+"/"+t.collId+"/"+t.sessionId+"/messenger",[],{headers:{Cookie:t.sessionCookie}});return{shareDBSocket:t.socket,messengerSocket:e}},getConnectionToCollection=async function(t){await t.connection.createFetchQuery(t.collId,{},{},async function(e,o){if(e)return reject(e);if(0===o.length){var r={slot:0,nodes:{},links:{},coordinates:{},attributeGroups:{},iri:{}},n=t.connection.get(t.collId,"data0");await n.create(r),r.slot=1;var i=t.connection.get(t.collId,"data1");await i.create(r)}})},getShareDBDocProxies=async function(t){var e={};return e.data0={slot:"data0",connection:t.connection.get(t.collId,"data0")},e.data1={slot:"data1",connection:t.connection.get(t.collId,"data1")},e},getObjectSync=function(t,e,o,r){return t.ekgApi.DiagramData[e][r][o]},getArraySync=function(t,e,o){return DiagramData[e][o]},setObjectSync=function(t,e,o,r,n){return t.DiagramData[e][r][o]=n},getObjectCopySync=function(t,e,o,r){return JSON.parse(JSON.stringify(t.ekgApi.DiagramData[e][r][o]))},getAllDiagramDataSync=function(t){return t.DiagramData},submitNewObjectSync=function(t,e,o,r,n){return console.log("[submitNewObjectSync] "+o+r),new Promise((i,a)=>{t.ShareDBDocProxies[e].connection.submitOp([{p:[r,o],oi:n}],{source:"api"},function(t,e){t?a(new EkgPersistenceError(GrafoErrorGlossary.ErrorLevel.ERROR,"The document size is large to insert in mongo",t.stack)):i()})})},submitDeleteObjectSync=function(t,e,o,r,n){t.ShareDBDocProxies[e].connection.submitOp([{p:[r,o],od:n}],{source:"api"})},submitUpdateObjectSync=function(t,e,o,r,n,i){return console.log("[submitUpdateObjectSync] "+o+r),new Promise((a,c)=>{t.ShareDBDocProxies[e].connection.submitOp([{p:[r,o],od:i,oi:n}],{source:"user"},function(t,e){t?c(new EkgPersistenceError(GrafoErrorGlossary.ErrorLevel.ERROR,"The document size is large to insert in mongo",t.stack)):a()})})},setupEvents=function(t,e,o){return new Promise(function(r,n){try{var i=[];for(var a in e)e.hasOwnProperty(a)&&i.push(e[a]);async.eachSeries(i,function(e,r){try{setupShareDBEvents(t,e,o).then(function(){r()}).catch(function(t){return r(t)})}catch(t){return r(t)}},function(t){return t?n(t):r(i)})}catch(t){return n(t)}})},setupShareDBEvents=function(t,e,o){return e.connection.on("op",function(o,r){try{for(var n=0;n<o.length;n++){var i=o[n].p[0],a=o[n].p[1],c=(o[n].od,o[n].oi);c&&("coordinates"===i?"user"!==r&&t.ekgApi.updateDiagramCoordinates(e.slot,a,c):t.ekgApi.updateDiagramArray(e.slot,i,a,c))}}catch(e){t.logger.error(e)}}),new Promise(function(r,n){try{e.connection.subscribe(function(i){try{if(i)throw i;if(!o)return r();t.ekgApi.updateDiagramData(e.slot,e.connection.data).then(()=>{r()}).catch(t=>{n(t)})}catch(i){return n(i)}})}catch(t){return n(t)}})};