UNPKG

aquinas

Version:

dependency inversion w/o classes

1 lines 3.41 kB
"use strict";var e=require("@owja/ioc"),t=class extends Error{constructor(e,t){const n=t?"string"==typeof t?t:t.message:"no additional info";super(n?`${e} - ${n}`:e),this.name="AquinasError"}},n=class{constructor(e){this.name=e,this.id=Symbol.for(e),this.toJSON=()=>e}id;__type;toJSON};function r(e){return e instanceof n}var i=class{reference;implementation;constructor(e,t){this.reference=e,this.implementation=t}},o=class e{constructor(e,t={},n){this.reference=e,this.refs=t,this.stateFactory=n}deps(t){return new e(this.reference,{...this.refs,...t},this.stateFactory)}init(t){return new e(this.reference,this.refs,t)}implements(e){const t=(e,t={})=>({...t,...this.stateFactory?this.stateFactory(t):{},ctx:e??f()});return new i(this.reference,n=>{const r=n.get(this.refs);return e(t(n,r))})}};function c(e){return e instanceof i}var s=class{referenceRegistry;bindingRegistry;container;constructor(){this.container=new e.Container,this.referenceRegistry=new Set,this.bindingRegistry=new Map}bindReference(e,n,r){this.referenceRegistry.add(e),this.bindingRegistry.set(e,n);try{r?.rebind?this.container.rebind(e.id).toFactory(()=>n(this)).inSingletonScope():this.container.bind(e.id).toFactory(()=>n(this)).inSingletonScope()}catch(n){throw new t(`failed to bind reference with name "${e.name}"`,n)}}merge(...e){for(const n of e){if(!a(n))throw new t("Invalid dock","expected a Dock object but got "+typeof n);for(const e of n.referenceRegistry){const r=n.bindingRegistry.get(e);if(!r)throw new t("Missing implementation during merge");this.bindReference(e,r)}}}add(...e){for(const n of e){if(!c(n))throw new t("Invalid injectable","expected an Injectable object but got "+typeof n);this.bindReference(n.reference,n.implementation)}}update(e,n){if(!c(n)&&"function"!=typeof n)throw new t("Invalid update","expected an Injectable or implementation function but got "+typeof n);c(n)?this.update(e,n.implementation):this.bindReference(e,n,{rebind:!0})}delete(e){if(!r(e))throw new t("Invalid reference","expected a Reference object but got "+typeof e);this.referenceRegistry.delete(e),this.bindingRegistry.delete(e),this.container.remove(e.id)}get(...e){if(e.length>1){const n=[];for(const i of e){if(!r(i))throw new t("Invalid reference","expected a Reference object but got "+typeof i);try{n.push(this.container.get(i.id))}catch(e){throw new t(`Failed to get reference with name "${i.name}"`,e)}}return n}const n=e[0];if("id"in n){if(!r(n))throw new t("Invalid reference","expected a Reference object but got "+typeof n);try{return this.container.get(n.id)}catch(e){throw new t(`Failed to get reference with name "${n.name}"`,e)}}const i={};for(const e in n){const o=n[e];if(!r(o))throw new t(`Invalid reference for key "${e}"`,"expected a Reference object but got "+typeof o);i[e]=this.container.get(o.id)}return i}safeGet(e){if(!r(e))throw new t("Invalid reference","expected a Reference object but got "+typeof e);try{return this.container.get(e.id)}catch{return}}find(e){const t=[];for(const n of this.referenceRegistry)try{if(e(n)){const e=this.container.get(n.id);t.push(e)}}catch{}return t}};function f(){return new s}function a(e){return e instanceof s}exports.cloneDock=function(e){const t=new s;return t.merge(e),t},exports.derivedReference=function(e,t){return new n(`${e}:${t.toJSON()}`)},exports.dock=f,exports.injectable=function(e){return new o(e)},exports.reference=function(e){return new n(e)};