UNPKG

mgraph.graph

Version:

Modern graph data structure for JavaScript – a refactoring of ngraph.graph using mgraph.events

2 lines (1 loc) 3.96 kB
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(n="undefined"!=typeof globalThis?globalThis:n||self).createGraph=t()}(this,(function(){"use strict";function n(n,t){this.id=n,this.links=null,this.data=t}function t(n,t){n.links?n.links.add(t):n.links=new Set([t])}function e(n,t,e,o){this.fromId=n,this.toId=t,this.data=e,this.id=o}function o(n,t){return n.toString()+"👉"+t.toString()}return function(i={}){if("uniqueLinkId"in i&&(console.warn("mgraph.graph: `uniqueLinkId` is deprecated. Use `multigraph` instead."),i.multigraph=i.uniqueLinkId),void 0===i.multigraph&&(i.multigraph=!1),"function"!=typeof Map)throw new Error("mgraph.graph requires Map to be defined. Please polyfill it.");const r=new Map,f=new Map,u={};let c=0;const s=[];let a=x,d=x,l=x,p=x;const h=i.multigraph?function(n,t,i){let r=o(n,t);const f=Object.prototype.hasOwnProperty.call(u,r);if(f||E(n,t)){f||(u[r]=0);const e="@"+ ++u[r];r=o(n+e,t+e)}return new e(n,t,i,r)}:function(n,t,i){const r=o(n,t),u=f.get(r);if(u)return u.data=i,u;return new e(n,t,i,r)},g={version:1,addNode:m,addLink:function(n,e,o){l();const i=v(n)||m(n),r=v(e)||m(e),u=h(n,e,o),c=f.has(u.id);f.set(u.id,u),t(i,u),n!==e&&t(r,u);return a(u,c?"update":"add"),p(),u},removeLink:function(n,t){void 0!==t&&(n=E(n,t));return L(n)},removeNode:w,getNode:v,hasNode:function(n){return r.has(n)},getLink:E,hasLink:function(n,t){return void 0!==n&&void 0!==t&&f.has(o(n,t))},getNodeCount:b,getNodesCount:b,getLinkCount:I,getLinksCount:I,getLinks:function(n){const t=v(n);return t?t.links:null},forEachNode:function(n){if("function"!=typeof n)return;for(const t of r.values())if(n(t))return!0;return!1},forEachLink:function(n){if("function"!=typeof n)return;for(const t of f.values())if(n(t))return!0;return!1},forEachLinkedNode:function(n,t,e){const o=v(n);if(!o||!o.links||"function"!=typeof t)return;return e?function(n,t,e){for(const o of n.values())if(o.fromId===t){const n=r.get(o.toId);if(n&&e(n,o))return!0}return!1}(o.links,n,t):function(n,t,e){for(const o of n.values()){const n=o.fromId===t?o.toId:o.fromId,i=r.get(n);if(i&&e(i,o))return!0}return!1}(o.links,n,t)},beginUpdate:N,endUpdate:j,clear:function(){l();Array.from(r.keys()).forEach((n=>w(n))),p()}};return function(n){if(!n)throw new Error("Eventify cannot use a falsy object as events subject");for(const t of["on","off","fire"])if(Object.prototype.hasOwnProperty.call(n,t))throw new Error(`Subject already has property '${t}'`);const t=new Map;n.on=function(e,o,i){if("function"!=typeof o)throw new Error("Callback is expected to be a function");return t.has(e)||t.set(e,[]),t.get(e).push({callback:o,ctx:i}),n},n.off=function(e,o){if(void 0===e)return t.clear(),n;if(t.has(e))if("function"!=typeof o)t.delete(e);else{const n=t.get(e).filter((n=>n.callback!==o));n.length?t.set(e,n):t.delete(e)}return n},n.fire=function(e,...o){const i=t.get(e);if(i)for(const{callback:n,ctx:t}of i)n.apply(t,o);return n}}(g),function(){const n=g.on;g.on=function(...t){return g.beginUpdate=l=N,g.endUpdate=p=j,a=k,d=y,g.on=n,n.apply(g,t)}}(),g;function k(n,t){s.push({link:n,changeType:t})}function y(n,t){s.push({node:n,changeType:t})}function m(t,e){if(void 0===t)throw new Error("Invalid node identifier");l();let o=v(t);return o?(o.data=e,d(o,"update")):(o=new n(t,e),r.set(t,o),d(o,"add")),p(),o}function v(n){return r.get(n)}function w(n){const t=v(n);if(!t)return!1;if(l(),t.links){Array.from(t.links).forEach((n=>L(n)))}return r.delete(n),d(t,"remove"),p(),!0}function b(){return r.size}function I(){return f.size}function L(n){if(!n||!f.has(n.id))return!1;l(),f.delete(n.id);const t=v(n.fromId),e=v(n.toId);return t&&t.links&&t.links.delete(n),e&&e.links&&t!==e&&e.links.delete(n),a(n,"remove"),p(),!0}function E(n,t){if(void 0!==n&&void 0!==t)return f.get(o(n,t))}function N(){c++}function j(){c--,0===c&&s.length>0&&(g.fire("changed",s),s.length=0)}function x(){}}}));