|
12 | 12 | <a href="https://www.npmjs.com/package/weakref"><img alt="Version" src="https://img.shields.io/npm/v/weakref.svg?style=flat-square&logo=npm" /></a>
|
13 | 13 | <a href="https://npmcharts.com/compare/weakref?minimal=true"><img alt="Downloads" src="https://img.shields.io/npm/dt/weakref.svg?style=flat-square" /></a>
|
14 | 14 | </p>
|
| 15 | + |
| 16 | +Weak Collection Library for Deno and Node.js. |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +### with Deno |
| 21 | + |
| 22 | +```ts |
| 23 | +import { |
| 24 | + IterableWeakMap, |
| 25 | + IterableWeakSet, |
| 26 | +} from "https://deno.land/x/weakref/mod.ts"; |
| 27 | + |
| 28 | +const set = new IterableWeakSet(); |
| 29 | +const map = new IterableWeakMap(); |
| 30 | +``` |
| 31 | + |
| 32 | +### with Node.js & Browser |
| 33 | + |
| 34 | +**Install** |
| 35 | + |
| 36 | +```bash |
| 37 | +npm install weakref |
| 38 | +``` |
| 39 | + |
| 40 | +```ts |
| 41 | +import { IterableWeakMap, IterableWeakSet } from "weakref"; |
| 42 | +``` |
| 43 | + |
| 44 | +## Features |
| 45 | + |
| 46 | +### IterableWeakSet |
| 47 | + |
| 48 | +**Interface** |
| 49 | + |
| 50 | +```ts |
| 51 | +class IterableWeakSet<T extends object> implements WeakSet<T>, Set<T> { |
| 52 | + constructor(values?: readonly T[] | null); |
| 53 | + constructor(iterable: Iterable<T>); |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +**Example** |
| 58 | + |
| 59 | +```ts |
| 60 | +const set = new IterableWeakSet(); |
| 61 | + |
| 62 | +for (let i = 0; i < 100; i++) { |
| 63 | + set.add({}); |
| 64 | +} |
| 65 | + |
| 66 | +for (const item of set) { |
| 67 | + console.log(item); // will print 100 items |
| 68 | +} |
| 69 | + |
| 70 | +// after garbage collection, {n} items will be collected |
| 71 | + |
| 72 | +for (const item of set) { |
| 73 | + console.log(item); // will print (100 - {n}) items |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +### IterableWeakMap |
| 78 | + |
| 79 | +**Interface** |
| 80 | + |
| 81 | +```ts |
| 82 | +class IterableWeakMap<K extends object, V> implements WeakMap<K, V>, Map<K, V> { |
| 83 | + constructor(entries?: readonly (readonly [K, V])[] | null); |
| 84 | + constructor(iterable: Iterable<readonly [K, V]>); |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +**Example** |
| 89 | + |
| 90 | +```ts |
| 91 | +const map = new IterableWeakMap(); |
| 92 | + |
| 93 | +for (let i = 0; i < 100; i++) { |
| 94 | + map.set({}, i); |
| 95 | +} |
| 96 | + |
| 97 | +for (const [key, value] of map) { |
| 98 | + console.log(key, value); // will print 100 items |
| 99 | +} |
| 100 | + |
| 101 | +// after garbage collection, {n} items will be collected |
| 102 | + |
| 103 | +for (const [key, value] of map) { |
| 104 | + console.log(key, value); // will print (100 - {n}) items |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +### InvertedWeakMap |
| 109 | + |
| 110 | +**Interface** |
| 111 | + |
| 112 | +```ts |
| 113 | +class InvertedWeakMap<K, V extends object> implements Map<K, V> { |
| 114 | + constructor(entries?: readonly (readonly [K, V])[] | null); |
| 115 | + constructor(iterable: Iterable<readonly [K, V]>); |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +**Example** |
| 120 | + |
| 121 | +```ts |
| 122 | +const map = new InvertedWeakMap(); |
| 123 | + |
| 124 | +for (let i = 0; i < 100; i++) { |
| 125 | + map.set(i, {}); |
| 126 | +} |
| 127 | + |
| 128 | +for (const [key, value] of map) { |
| 129 | + console.log(key, value); // will print 100 items |
| 130 | +} |
| 131 | + |
| 132 | +// after garbage collection, {n} items will be collected |
| 133 | + |
| 134 | +for (const [key, value] of map) { |
| 135 | + console.log(key, value); // will print (100 - {n}) items |
| 136 | +} |
| 137 | +``` |
0 commit comments