@okiba/pool-system

Class to manage dinamically a pool of system

Usage no npm install needed!

<script type="module">
  import okibaPoolSystem from 'https://cdn.skypack.dev/@okiba/pool-system';
</script>

README

Okiba / PoolSystem

Manages a dinamically grown pool of DOM Elements import PoolSystem from '@okiba/pool-system'

__

const container = document.querySelector('#container')

function createEl(){
 return document.createElement('img');
}

const pool = new PoolSystem(container, createEl)

Installation

npm i --save @okiba/pool-system

Or import it directly in the browser

<script type="module" src="https://unpkg.com/@okiba/pool-system/index.js"></script>

Usage

import PoolSystem from '@okiba/pool-system'

Untranspiled code 🛑

Okiba Core packages are not transpiled, so don't forget to transpile them with your favourite bundler. For example, using Babel with Webpack, you should prevent imports from okiba to be excluded from transpilation, like follows:

{
  test: /\.js$/,
  exclude: /node_modules\/(?!(@okiba)\/).*/,
  use: {
    loader: 'babel-loader',
    options: {
      presets: ['@babel/preset-env']
    }
  }
}

constructor(parent, createEl)

Arguments

+ parent: Object

DOM Element used as container for all pool elements.

+ createEl: function

Function that creates an istance the element you want to add to the pool.

ensure(size)

Makes sure the pool can host a minimum number of elements, grows it if that's not the case.

Arguments

+ size: Number

Minimum pool size to ensure.

get()

Hands a free element from the pool. If there isn't one, it automatically grows the pool's. This provides dynamic sizing which ensures

Returns

Element A free DOM Element

free(Element)

Marks an element as elegible for reutilization, and pushes it into the pool

Arguments

+ Element: Element

to mark as free

destroy()

Nulls all references to DOM Elements