Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/assets/icons.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/assets/icons.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 14 additions & 7 deletions docs/index.html
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
<!DOCTYPE html><html class="default" lang="en" data-base="./"><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>@libreworks/container</title><meta name="description" content="Documentation for @libreworks/container"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="assets/style.css"/><link rel="stylesheet" href="assets/highlight.css"/><script defer src="assets/main.js"></script><script async src="assets/icons.js" id="tsd-icons-script"></script><script async src="assets/search.js" id="tsd-search-script"></script><script async src="assets/navigation.js" id="tsd-nav-script"></script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os";document.body.style.display="none";setTimeout(() => window.app?app.showPage():document.body.style.removeProperty("display"),500)</script><header class="tsd-page-toolbar"><div class="tsd-toolbar-contents container"><a href="index.html" class="title">@libreworks/container</a><div id="tsd-toolbar-links"></div><button id="tsd-search-trigger" class="tsd-widget" aria-label="Search"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-search"></use></svg></button><dialog id="tsd-search" aria-label="Search"><input role="combobox" id="tsd-search-input" aria-controls="tsd-search-results" aria-autocomplete="list" aria-expanded="true" autocapitalize="off" autocomplete="off" placeholder="Search the docs" maxLength="100"/><ul role="listbox" id="tsd-search-results"></ul><div id="tsd-search-status" aria-live="polite" aria-atomic="true"><div>Preparing search index...</div></div></dialog><a href="#" class="tsd-widget menu" id="tsd-toolbar-menu-trigger" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-menu"></use></svg></a></div></header><div class="container container-main"><div class="col-content"><div class="tsd-page-title"><h1>@libreworks/container</h1></div><div class="tsd-panel tsd-typography"><h1 id="libreworkscontainer" class="tsd-anchor-link">@libreworks/container<a href="#libreworkscontainer" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h1><p><a href="https://github.com/libreworks/container/blob/main/LICENSE"><img src="https://img.shields.io/github/license/libreworks/container" alt="MIT"></a>
<!DOCTYPE html><html class="default" lang="en" data-base="./"><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>@libreworks/container</title><meta name="description" content="Documentation for @libreworks/container"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="assets/style.css"/><link rel="stylesheet" href="assets/highlight.css"/><script defer src="assets/main.js"></script><script async src="assets/icons.js" id="tsd-icons-script"></script><script async src="assets/search.js" id="tsd-search-script"></script><script async src="assets/navigation.js" id="tsd-nav-script"></script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os";document.body.style.display="none";setTimeout(() => window.app?app.showPage():document.body.style.removeProperty("display"),500)</script><header class="tsd-page-toolbar"><div class="tsd-toolbar-contents container"><a href="index.html" class="title">@libreworks/container</a><div id="tsd-toolbar-links"></div><button id="tsd-search-trigger" class="tsd-widget" aria-label="Search"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-search"></use></svg></button><dialog id="tsd-search" aria-label="Search"><input role="combobox" id="tsd-search-input" aria-controls="tsd-search-results" aria-autocomplete="list" aria-expanded="true" autocapitalize="off" autocomplete="off" placeholder="Search the docs" maxLength="100"/><ul role="listbox" id="tsd-search-results"></ul><div id="tsd-search-status" aria-live="polite" aria-atomic="true"><div>Preparing search index...</div></div></dialog><a href="#" class="tsd-widget menu" id="tsd-toolbar-menu-trigger" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-menu"></use></svg></a></div></header><div class="container container-main"><div class="col-content"><div class="tsd-page-title"><h1>@libreworks/container</h1></div><div class="tsd-panel tsd-typography"><h1 id="libreworkscontainer" class="tsd-anchor-link">@libreworks/container<a href="#libreworkscontainer" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h1>
<p><a href="https://github.com/libreworks/container/blob/main/LICENSE"><img src="https://img.shields.io/github/license/libreworks/container" alt="MIT"></a>
<a href="https://www.npmjs.com/package/@libreworks/container"><img src="https://img.shields.io/npm/v/@libreworks/container" alt="npm"></a>
<a href="https://github.com/libreworks/container/actions/workflows/release.yml"><img src="https://img.shields.io/github/workflow/status/libreworks/container/release/main?label=release" alt="GitHub Workflow Status (branch)"></a>
<a href="https://github.com/libreworks/container/releases"><img src="https://img.shields.io/github/v/release/libreworks/container?sort=semver" alt="GitHub release (latest SemVer)"></a>
<a href="https://codecov.io/gh/libreworks/container"><img src="https://codecov.io/gh/libreworks/container/branch/main/graph/badge.svg?token=OHTRGNTSPO" alt="codecov"></a></p>
<p>A simple asynchronous dependency injection container and event target.</p>
<h2 id="installation" class="tsd-anchor-link">Installation<a href="#installation" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2><pre><code class="shell"><span class="hl-0">npm</span><span class="hl-1"> </span><span class="hl-2">install</span><span class="hl-1"> </span><span class="hl-2">@libreworks/container</span>
<h2 id="installation" class="tsd-anchor-link">Installation<a href="#installation" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2>
<pre><code class="shell"><span class="hl-0">npm</span><span class="hl-1"> </span><span class="hl-2">install</span><span class="hl-1"> </span><span class="hl-2">@libreworks/container</span>
</code><button type="button">Copy</button></pre>

<p>This library conforms to ECMAScript Modules (ESM). You can import this module using ESM or TypeScript syntax.</p>
<pre><code class="TypeScript"><span class="hl-3">import</span><span class="hl-1"> { </span><span class="hl-4">Builder</span><span class="hl-1">, </span><span class="hl-4">Container</span><span class="hl-1">, </span><span class="hl-4">Producer</span><span class="hl-1"> } </span><span class="hl-3">from</span><span class="hl-1"> </span><span class="hl-2">&quot;@libreworks/container&quot;</span><span class="hl-1">;</span>
</code><button type="button">Copy</button></pre>

<p>If you're using CommonJS, you must use <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import">dynamic imports</a> instead.</p>
<h2 id="usage" class="tsd-anchor-link">Usage<a href="#usage" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2><p>You can use this library to create a graph of objects with intertwined dependencies. These objects and values can even be produced asynchronously.</p>
<h3 id="container" class="tsd-anchor-link">Container<a href="#container" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3><p>A container provides named values.</p>
<h2 id="usage" class="tsd-anchor-link">Usage<a href="#usage" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2>
<p>You can use this library to create a graph of objects with intertwined dependencies. These objects and values can even be produced asynchronously.</p>
<h3 id="container" class="tsd-anchor-link">Container<a href="#container" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
<p>A container provides named values.</p>
<pre><code class="typescript"><span class="hl-3">import</span><span class="hl-1"> { </span><span class="hl-4">Foobar</span><span class="hl-1"> } </span><span class="hl-3">from</span><span class="hl-1"> </span><span class="hl-2">&quot;your-package-name-here&quot;</span><span class="hl-1">;</span><br/><br/><span class="hl-5">// Assume we created a Container.</span><br/><span class="hl-6">declare</span><span class="hl-1"> </span><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">container</span><span class="hl-1">: </span><span class="hl-8">Container</span><span class="hl-1">;</span><br/><span class="hl-5">// Retrieve a value by its name.</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">myObject</span><span class="hl-1">: </span><span class="hl-8">Foobar</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">container</span><span class="hl-1">.</span><span class="hl-0">get</span><span class="hl-1">(</span><span class="hl-2">&quot;MyObject&quot;</span><span class="hl-1">);</span><br/><span class="hl-5">// Retrieve multiple values by their names.</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">allObjects</span><span class="hl-1">: </span><span class="hl-8">Foobar</span><span class="hl-1">[] = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">container</span><span class="hl-1">.</span><span class="hl-0">getAll</span><span class="hl-1">([</span><span class="hl-2">&quot;MyObject&quot;</span><span class="hl-1">, </span><span class="hl-2">&quot;Another&quot;</span><span class="hl-1">]);</span><br/><span class="hl-5">// Retrieve values by tag.</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">taggedByMe</span><span class="hl-1">: </span><span class="hl-8">Foobar</span><span class="hl-1">[] = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">container</span><span class="hl-1">.</span><span class="hl-0">getAllTagged</span><span class="hl-1">(</span><span class="hl-2">&quot;my-tag&quot;</span><span class="hl-1">);</span>
</code><button type="button">Copy</button></pre>

<h4 id="events" class="tsd-anchor-link">Events<a href="#events" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h4><p>The <code>Container</code> class extends the <code>EventTarget</code> class (<a href="https://developer.mozilla.org/en-US/docs/Web/API/EventTarget">see MDN docs</a>).</p>
<h3 id="builder" class="tsd-anchor-link">Builder<a href="#builder" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3><p>In order to put together a <code>Container</code>, you can use the <code>Builder</code> class.</p>
<h4 id="events" class="tsd-anchor-link">Events<a href="#events" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h4>
<p>The <code>Container</code> class extends the <code>EventTarget</code> class (<a href="https://developer.mozilla.org/en-US/docs/Web/API/EventTarget">see MDN docs</a>).</p>
<h3 id="builder" class="tsd-anchor-link">Builder<a href="#builder" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
<p>In order to put together a <code>Container</code>, you can use the <code>Builder</code> class.</p>
<pre><code class="typescript"><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">builder</span><span class="hl-1"> = </span><span class="hl-6">new</span><span class="hl-1"> </span><span class="hl-0">Builder</span><span class="hl-1">()</span><br/><span class="hl-1"> </span><span class="hl-5">// Register a constant</span><br/><span class="hl-1"> .</span><span class="hl-0">constant</span><span class="hl-1">(</span><span class="hl-2">&quot;ExampleValue&quot;</span><span class="hl-1">, </span><span class="hl-2">&quot;Lorem ipsum dolor sit amet&quot;</span><span class="hl-1">)</span><br/><span class="hl-1"> </span><span class="hl-5">// Register an array.</span><br/><span class="hl-1"> .</span><span class="hl-0">constant</span><span class="hl-1">(</span><span class="hl-2">&quot;my.numbers&quot;</span><span class="hl-1">, [</span><span class="hl-9">1</span><span class="hl-1">, </span><span class="hl-9">2</span><span class="hl-1">, </span><span class="hl-9">3</span><span class="hl-1">, </span><span class="hl-9">5</span><span class="hl-1">, </span><span class="hl-9">8</span><span class="hl-1">, </span><span class="hl-9">13</span><span class="hl-1">, </span><span class="hl-9">21</span><span class="hl-1">])</span><br/><span class="hl-1"> </span><span class="hl-5">// Register an object created synchronously.</span><br/><span class="hl-1"> .</span><span class="hl-0">register</span><span class="hl-1">(</span><span class="hl-2">&quot;RandomName&quot;</span><span class="hl-1">, () </span><span class="hl-6">=&gt;</span><span class="hl-1"> </span><span class="hl-4">AnotherClass</span><span class="hl-1">.</span><span class="hl-0">doSomething</span><span class="hl-1">())</span><br/><span class="hl-1"> </span><span class="hl-5">// Register an object created asynchronously.</span><br/><span class="hl-1"> .</span><span class="hl-0">register</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-2">&quot;an-async-value&quot;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-6">async</span><span class="hl-1"> (</span><span class="hl-4">c</span><span class="hl-1">) </span><span class="hl-6">=&gt;</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">myNumbers</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">c</span><span class="hl-1">.</span><span class="hl-0">get</span><span class="hl-1">(</span><span class="hl-2">&quot;my.numbers&quot;</span><span class="hl-1">);</span><br/><span class="hl-1"> </span><span class="hl-3">return</span><span class="hl-1"> </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-0">createNewThingy</span><span class="hl-1">(</span><span class="hl-4">myNumbers</span><span class="hl-1">);</span><br/><span class="hl-1"> },</span><br/><span class="hl-1"> [</span><span class="hl-2">&quot;my-tag&quot;</span><span class="hl-1">, </span><span class="hl-2">&quot;AnotherTag&quot;</span><span class="hl-1">, </span><span class="hl-2">&quot;Yet another&quot;</span><span class="hl-1">]</span><br/><span class="hl-1"> );</span><br/><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">container</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">builder</span><span class="hl-1">.</span><span class="hl-0">build</span><span class="hl-1">();</span><br/><br/><span class="hl-5">// Use the get method&#39;s generic type if you prefer.</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">anAsyncValue</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">container</span><span class="hl-1">.</span><span class="hl-0">get</span><span class="hl-1">&lt;</span><span class="hl-8">MyClassName</span><span class="hl-1">&gt;(</span><span class="hl-2">&quot;an-async-value&quot;</span><span class="hl-1">);</span>
</code><button type="button">Copy</button></pre>

<p>You provide a name and a &quot;factory&quot; function when you call the <code>register</code> method. The factory function needs to return the value you want to register; it can be asynchronous or return a <code>Promise</code> as well.</p>
<p>Once the container is built, a call to <code>get</code> using the same name will invoke the factory function. The container provides itself as the first argument. That way, you can recursively locate other dependencies immediately before constructing the return value.</p>
<p>Since the container is an <code>EventTarget</code>, the factory function could broadcast an event, or the value returned by the factory function can register itself as an event listener. This feature allows objects inside the container to communicate in a loosely-coupled way.</p>
<p>The factory function will only ever be invoked once, no matter how many times <code>get</code> is invoked.</p>
<h4 id="eager-creation" class="tsd-anchor-link">Eager Creation<a href="#eager-creation" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h4><p>Normally, values are lazy-loaded; they are created on-demand. However, you can provide the <code>@eager</code> tag when you call the <code>register</code> method to ensure your objects initialize themselves when the container is created.</p>
<h4 id="eager-creation" class="tsd-anchor-link">Eager Creation<a href="#eager-creation" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h4>
<p>Normally, values are lazy-loaded; they are created on-demand. However, you can provide the <code>@eager</code> tag when you call the <code>register</code> method to ensure your objects initialize themselves when the container is created.</p>
<pre><code class="typescript"><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">builder</span><span class="hl-1"> = </span><span class="hl-6">new</span><span class="hl-1"> </span><span class="hl-0">Builder</span><span class="hl-1">().</span><span class="hl-0">register</span><span class="hl-1">(</span><span class="hl-2">&quot;FooBar&quot;</span><span class="hl-1">, () </span><span class="hl-6">=&gt;</span><span class="hl-1"> </span><span class="hl-6">new</span><span class="hl-1"> </span><span class="hl-0">Thingy</span><span class="hl-1">(), [</span><br/><span class="hl-1"> </span><span class="hl-2">&quot;@eager&quot;</span><span class="hl-1">,</span><br/><span class="hl-1">]);</span><br/><span class="hl-6">const</span><span class="hl-1"> </span><span class="hl-7">container</span><span class="hl-1"> = </span><span class="hl-3">await</span><span class="hl-1"> </span><span class="hl-4">builder</span><span class="hl-1">.</span><span class="hl-0">build</span><span class="hl-1">();</span><br/><span class="hl-5">// Thingy has already been instantiated.</span>
</code><button type="button">Copy</button></pre>

Expand Down
4 changes: 2 additions & 2 deletions package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading