New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

opool

Package Overview
Dependencies
Maintainers
2
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

opool

Simple & super fast object pool for javascript.

latest
Source
npmnpm
Version
0.1.1
Version published
Weekly downloads
8
-11.11%
Maintainers
2
Weekly downloads
 
Created
Source

OPool

CircleCI Codecov npm npm

Simple & super fast object pool for javascript

Usage

To create a new pool you just need to provide a constructor function to the exported class. You constructor function must have a prototype function called reset, which will be called upon release.

To get a new object, just call pool.get(). To add an object to the pool call pool.release(obj);.

Typescript

import Pool from 'opool';

class MyClass {
  constructor() {
    MyClass.reset(this);
  }
  static reset(obj: MyClass) {
    obj.something = null;
  }
}

export default const pool = new Pool(MyClass);

// Then somewhere else...

const obj1 = pool.get(); // returns new MyClass
const obj2 = pool.get(); // returns new MyClass

pool.release(obj1); // reset() is automatically called here

const obj3 = pool.get(); // obj3 is now identical to obj1

Javascript (ES5)

var Pool = require('opool');

function MyClass() {
  MyClass.reset(this);
}
MyClass.reset = function(obj) {
  obj.something = null;
}

var pool = new Pool(MyClass);

// Then somewhere deep down...
function() {
  var obj = pool.get();
  obj.something = 'test';

  pool.release(obj); // You should stop using `obj` now
  // obj.reset is automatically called here
}();

function() {
  var obj2 = pool.get(); // This is actually the same as `obj` above.
  console.log(obj2.something); // > null
  obj2.something = 'test';

  pool.release(obj2);
}();

Keywords

object

FAQs

Package last updated on 12 Jan 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts