Git Product home page Git Product logo

weak-value's Introduction

WeakValue

๐Ÿ“ฃ Check out not-so-weak module for better and faster WValue alternative


Build Status Coverage Status

A Map with weakly referenced values, instead of keys, with an optional onValueCollected callback to pass aslong.

In Node.js, it requires V8 release v8.4 or greater.

import WeakValue from 'weak-value';
// const WeakValue = require('weak-value');

const wv = new WeakValue;

(() => {
  const value = {};
  wv.set('any-key', value, /* optional */ (key, map) => {
    // will log "any-key value collected" once GC kicks in
    console.log(key, 'value collected');
    console.log(map === wv);  // true
  });
})();

Please note that explicit wv.delete(key) will NOT invoke onValueCollected, as deleting a key does not mean its referenced value has been collected.

weak-value's People

Contributors

webreflection avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

im0505 uiforks

weak-value's Issues

Re-setting key can lead to deletion

The finalizer currently blindly erases the entry. If an entry is reset, the finalizer for the previously registered value will remove the current entry.

Not tested:

const k = 'key';
const wv = new WeakValue();

const v = (() => {
  const v1 = { foo: 1};
  const v2 = { foo: 2};
  wv.set(k, v1);
  wv.set(k, v2);
  return v2;
})();

setTimeout(() => {
  gc();
  const shouldBeV = wv.get(k);
  console.log(shouldBeV); // undefined;
});

You might be able to use the weakref object as an unregister token.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.