npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

memory-manager-service

v1.1.5

Published

This service can be used to generate an internal private data that gets automatically disposed when it's not used anymore.

Downloads

6

Readme

Memory Manager

This service can be used to generate an internal private data that gets automatically disposed when it's not used anymore.

Imagine it as a more advanced WeakMap with an internal Garbage Collector that tries to keep itself clean and tidy.

The Garbage Collector is executed inside ad web worker which avoid that the data processing affects the main UI thread.

How to install

Download the package from npm

npm install memory-manager-service

And then inport it in your code

import Memory from 'memory-manager-service';

How to use

const obj = {};

// Initialise the data.
Memory.create(obj);

// An id for the data is automatically generated.
const id = Memory.get(obj, "id");

// You can store all the data you want passing property name and value.
Memory.set(obj, "test", true);
// Or using the update method
Memory.update(obj, {
    "data": "test"
});

// You can get a reference to the original object
console.log(Memory.reference(id) === obj); // true

You can create cross references between data objects by storing the data inside another object.

import Memory from 'memory-manager-service';

const obj = {};
const linked = {};

// Initialise the data.
Memory.create(obj);

// Store the cross reference
Memory.create(linked, {
    "reference": obj
});

This will stop the obj data to be garbage collected until the linked object is not disposed.

Garbage collection can be forced manually, even if the object is cross referenced.

// To force an object to be disposed even if it has been recently updated and/or is cross referenced.
Memory.dispose(obj);

console.log(Memory.get(linked, "reference")); // null

// Force a garbage collection
Memory.flush();

Array Proxies

In order to keep the garbage collector always up to date, whenever you store an array inside a data object, this will replaced with a Proxy object that will work as a middlewher between you and the actual array. The object itself will work exactly as an array, but it will look weird if you log it on the console.

const arr = ["My Array"];

Memory.set(obj, "arr", arr);

const proxy = Memory.get(obj, "arr");

console.log(proxy); // Proxy {0: "My Array"}
console.log(proxy[0]); // "My Array"
console.log(proxy.length); // 1
console.log(Array.isArray(proxy)); // true
console.log(proxy === arr); // false

Use case

It's not really up to me to tell you how to use the data. In my case, I created this service in order to be able to create composed classes (like mixins), but with a common private area where data can be easily passed across all the composing classes.

Anyway, here is a very simple example on how to use the data:

import Memory from 'memory-manager-service';

class Person {
    constructor() {
        Memory.create(this, {
            "name": ""
        });
    }
    
    get id() {
        return Memory.get(this, "id");
    }
    
    get name() {
        return Memory.get(this, "name");
    }
    
    set name(value) {
        Memory.set(this, "name", value);
    }
}

Callbacks

The service will allow you to add callbacks for each object to monitor whenever the object is updated and/or disposed.

const person = new Person();

Memory.onUpdate(person, () => console.log(`Hello, ${person.name}!`));
Memory.onDispose(person, () => console.log(`Oh no! They disposed ${person.name}!`));

person.name = "Kenny"; // Hello, Kenny!
Memory.dispose(person); // Oh no! They disposed Kenny!