mild-map 1.1.1 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +10 -20
- package/dist/utils.d.ts +2 -0
- package/dist/utils.js +19 -0
- package/package.json +5 -7
- package/readme.md +1 -1
- package/.editorconfig +0 -10
- package/src/index.ts +0 -145
- package/test/index.js +0 -70
- package/tsconfig.json +0 -6
package/dist/index.js
CHANGED
|
@@ -1,25 +1,15 @@
|
|
|
1
1
|
/* IMPORT */
|
|
2
|
-
import { isWeakReferable } from '
|
|
3
|
-
/* HELPERS */
|
|
4
|
-
const isWeakabe = (value) => {
|
|
5
|
-
return isWeakReferable(value);
|
|
6
|
-
};
|
|
2
|
+
import { isWeakReferable } from './utils.js';
|
|
7
3
|
/* MAIN */
|
|
8
4
|
class MildMap {
|
|
9
5
|
/* VARIABLES */
|
|
10
|
-
#strong;
|
|
11
|
-
#weak;
|
|
12
|
-
#size;
|
|
13
|
-
#finalizationRegistry;
|
|
14
|
-
#finalizationTokens;
|
|
6
|
+
#strong = new Map();
|
|
7
|
+
#weak = new WeakMap();
|
|
8
|
+
#size = 0;
|
|
9
|
+
#finalizationRegistry = new FinalizationRegistry(() => this.#size -= 1);
|
|
10
|
+
#finalizationTokens = new WeakMap();
|
|
15
11
|
/* CONSTRUCTOR */
|
|
16
12
|
constructor(entries) {
|
|
17
|
-
/* VARIABLES */
|
|
18
|
-
this.#strong = new Map();
|
|
19
|
-
this.#weak = new WeakMap();
|
|
20
|
-
this.#size = 0;
|
|
21
|
-
this.#finalizationRegistry = new FinalizationRegistry(() => this.#size -= 1);
|
|
22
|
-
this.#finalizationTokens = new WeakMap();
|
|
23
13
|
if (entries) {
|
|
24
14
|
for (const [key, value] of entries) {
|
|
25
15
|
this.set(key, value);
|
|
@@ -36,7 +26,7 @@ class MildMap {
|
|
|
36
26
|
if (!hasKey)
|
|
37
27
|
return false;
|
|
38
28
|
this.#size -= 1;
|
|
39
|
-
if (
|
|
29
|
+
if (isWeakReferable(key)) {
|
|
40
30
|
const token = this.#finalizationTokens.get(key);
|
|
41
31
|
if (token) {
|
|
42
32
|
this.#finalizationRegistry.unregister(token);
|
|
@@ -49,7 +39,7 @@ class MildMap {
|
|
|
49
39
|
}
|
|
50
40
|
}
|
|
51
41
|
get(key) {
|
|
52
|
-
if (
|
|
42
|
+
if (isWeakReferable(key)) {
|
|
53
43
|
return this.#weak.get(key);
|
|
54
44
|
}
|
|
55
45
|
else {
|
|
@@ -57,7 +47,7 @@ class MildMap {
|
|
|
57
47
|
}
|
|
58
48
|
}
|
|
59
49
|
has(key) {
|
|
60
|
-
if (
|
|
50
|
+
if (isWeakReferable(key)) {
|
|
61
51
|
return this.#weak.has(key);
|
|
62
52
|
}
|
|
63
53
|
else {
|
|
@@ -69,7 +59,7 @@ class MildMap {
|
|
|
69
59
|
if (!hasKey) {
|
|
70
60
|
this.#size += 1;
|
|
71
61
|
}
|
|
72
|
-
if (
|
|
62
|
+
if (isWeakReferable(key)) {
|
|
73
63
|
this.#weak.set(key, value);
|
|
74
64
|
if (!hasKey) {
|
|
75
65
|
const token = {};
|
package/dist/utils.d.ts
ADDED
package/dist/utils.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/* MAIN */
|
|
2
|
+
const isWeakReferable = (() => {
|
|
3
|
+
const SYMBOLS_ARE_WEAK_REFERABLE = (() => {
|
|
4
|
+
try {
|
|
5
|
+
new WeakSet().add(Symbol());
|
|
6
|
+
return true;
|
|
7
|
+
}
|
|
8
|
+
catch {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
})();
|
|
12
|
+
return (value) => {
|
|
13
|
+
if (value === null)
|
|
14
|
+
return false;
|
|
15
|
+
return typeof value === 'object' || typeof value === 'function' || (SYMBOLS_ARE_WEAK_REFERABLE && typeof value === 'symbol' && !Symbol.keyFor(value));
|
|
16
|
+
};
|
|
17
|
+
})();
|
|
18
|
+
/* EXPORT */
|
|
19
|
+
export { isWeakReferable };
|
package/package.json
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
"name": "mild-map",
|
|
3
3
|
"repository": "github:fabiospampinato/mild-map",
|
|
4
4
|
"description": "A WeakMap that supports any value, it holds strong references to primitives, and weak references to objects.",
|
|
5
|
-
"
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"version": "1.1.2",
|
|
6
7
|
"type": "module",
|
|
7
8
|
"main": "dist/index.js",
|
|
8
9
|
"exports": "./dist/index.js",
|
|
@@ -19,12 +20,9 @@
|
|
|
19
20
|
"strong",
|
|
20
21
|
"map"
|
|
21
22
|
],
|
|
22
|
-
"dependencies": {
|
|
23
|
-
"is": "npm:@fabiospampinato/is@^2.5.0"
|
|
24
|
-
},
|
|
25
23
|
"devDependencies": {
|
|
26
|
-
"fava": "^0.
|
|
27
|
-
"tsex": "^
|
|
28
|
-
"typescript": "^
|
|
24
|
+
"fava": "^0.3.4",
|
|
25
|
+
"tsex": "^4.0.2",
|
|
26
|
+
"typescript": "^5.7.3"
|
|
29
27
|
}
|
|
30
28
|
}
|
package/readme.md
CHANGED
package/.editorconfig
DELETED
package/src/index.ts
DELETED
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
/* IMPORT */
|
|
3
|
-
|
|
4
|
-
import {isWeakReferable} from 'is';
|
|
5
|
-
|
|
6
|
-
/* HELPERS */
|
|
7
|
-
|
|
8
|
-
const isWeakabe = ( value: unknown ): value is object => { //FIXME: Overridden type guard for convenience, delete this once TS updates its types
|
|
9
|
-
return isWeakReferable ( value );
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
/* MAIN */
|
|
13
|
-
|
|
14
|
-
class MildMap<K, V> {
|
|
15
|
-
|
|
16
|
-
/* VARIABLES */
|
|
17
|
-
|
|
18
|
-
#strong = new Map<K, V> ();
|
|
19
|
-
#weak = new WeakMap<any, V> ();
|
|
20
|
-
#size = 0;
|
|
21
|
-
|
|
22
|
-
#finalizationRegistry = new FinalizationRegistry ( () => this.#size -= 1 );
|
|
23
|
-
#finalizationTokens = new WeakMap<object, object> ();
|
|
24
|
-
|
|
25
|
-
/* CONSTRUCTOR */
|
|
26
|
-
|
|
27
|
-
constructor ( entries?: readonly (readonly [K, V])[] | null ) {
|
|
28
|
-
|
|
29
|
-
if ( entries ) {
|
|
30
|
-
|
|
31
|
-
for ( const [key, value] of entries ) {
|
|
32
|
-
|
|
33
|
-
this.set ( key, value );
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/* GETTER API */
|
|
42
|
-
|
|
43
|
-
get size () {
|
|
44
|
-
|
|
45
|
-
return this.#size;
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/* API */
|
|
50
|
-
|
|
51
|
-
delete ( key: K ): boolean {
|
|
52
|
-
|
|
53
|
-
const hasKey = this.has ( key );
|
|
54
|
-
|
|
55
|
-
if ( !hasKey ) return false;
|
|
56
|
-
|
|
57
|
-
this.#size -= 1;
|
|
58
|
-
|
|
59
|
-
if ( isWeakabe ( key ) ) {
|
|
60
|
-
|
|
61
|
-
const token = this.#finalizationTokens.get ( key );
|
|
62
|
-
|
|
63
|
-
if ( token ) {
|
|
64
|
-
|
|
65
|
-
this.#finalizationRegistry.unregister ( token );
|
|
66
|
-
this.#finalizationTokens.delete ( key );
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
return this.#weak.delete ( key );
|
|
71
|
-
|
|
72
|
-
} else {
|
|
73
|
-
|
|
74
|
-
return this.#strong.delete ( key );
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
get ( key: K ): V | undefined {
|
|
81
|
-
|
|
82
|
-
if ( isWeakabe ( key ) ) {
|
|
83
|
-
|
|
84
|
-
return this.#weak.get ( key );
|
|
85
|
-
|
|
86
|
-
} else {
|
|
87
|
-
|
|
88
|
-
return this.#strong.get ( key );
|
|
89
|
-
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
has ( key: K ): boolean {
|
|
95
|
-
|
|
96
|
-
if ( isWeakabe ( key ) ) {
|
|
97
|
-
|
|
98
|
-
return this.#weak.has ( key );
|
|
99
|
-
|
|
100
|
-
} else {
|
|
101
|
-
|
|
102
|
-
return this.#strong.has ( key );
|
|
103
|
-
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
set ( key: K, value: V ): this {
|
|
109
|
-
|
|
110
|
-
const hasKey = this.has ( key );
|
|
111
|
-
|
|
112
|
-
if ( !hasKey ) {
|
|
113
|
-
|
|
114
|
-
this.#size += 1;
|
|
115
|
-
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if ( isWeakabe ( key ) ) {
|
|
119
|
-
|
|
120
|
-
this.#weak.set ( key, value );
|
|
121
|
-
|
|
122
|
-
if ( !hasKey ) {
|
|
123
|
-
|
|
124
|
-
const token = {};
|
|
125
|
-
|
|
126
|
-
this.#finalizationRegistry.register ( key, token, token );
|
|
127
|
-
this.#finalizationTokens.set ( key, token );
|
|
128
|
-
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
} else {
|
|
132
|
-
|
|
133
|
-
this.#strong.set ( key, value );
|
|
134
|
-
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return this;
|
|
138
|
-
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/* EXPORT */
|
|
144
|
-
|
|
145
|
-
export default MildMap;
|
package/test/index.js
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
/* IMPORT */
|
|
3
|
-
|
|
4
|
-
import {describe} from 'fava';
|
|
5
|
-
import {setTimeout as delay} from 'node:timers/promises';
|
|
6
|
-
import MildMap from '../dist/index.js';
|
|
7
|
-
|
|
8
|
-
/* MAIN */
|
|
9
|
-
|
|
10
|
-
describe ( 'MildMap', it => {
|
|
11
|
-
|
|
12
|
-
it ( 'works', async t => {
|
|
13
|
-
|
|
14
|
-
const map = new MildMap ();
|
|
15
|
-
|
|
16
|
-
let primitive = 0;
|
|
17
|
-
let object = {};
|
|
18
|
-
|
|
19
|
-
t.is ( map.size, 0 );
|
|
20
|
-
|
|
21
|
-
t.is ( map.get ( primitive ), undefined );
|
|
22
|
-
t.is ( map.get ( object ), undefined );
|
|
23
|
-
|
|
24
|
-
t.false ( map.has ( primitive ) );
|
|
25
|
-
t.false ( map.has ( object ) );
|
|
26
|
-
|
|
27
|
-
t.false ( map.delete ( primitive ) );
|
|
28
|
-
t.false ( map.delete ( object ) );
|
|
29
|
-
|
|
30
|
-
map.set ( primitive, 'primitive' );
|
|
31
|
-
map.set ( object, 'object' );
|
|
32
|
-
|
|
33
|
-
t.is ( map.size, 2 );
|
|
34
|
-
|
|
35
|
-
t.is ( map.get ( primitive ), 'primitive' );
|
|
36
|
-
t.is ( map.get ( object ), 'object' );
|
|
37
|
-
|
|
38
|
-
t.true ( map.has ( primitive ) );
|
|
39
|
-
t.true ( map.has ( object ) );
|
|
40
|
-
|
|
41
|
-
t.true ( map.delete ( primitive ) );
|
|
42
|
-
t.true ( map.delete ( object ) );
|
|
43
|
-
|
|
44
|
-
t.is ( map.size, 0 );
|
|
45
|
-
|
|
46
|
-
map.set ( primitive, 'primitive' );
|
|
47
|
-
map.set ( object, 'object' );
|
|
48
|
-
|
|
49
|
-
t.is ( map.size, 2 );
|
|
50
|
-
|
|
51
|
-
/* CLEANUP */
|
|
52
|
-
|
|
53
|
-
let deleted = 0;
|
|
54
|
-
|
|
55
|
-
const registry = new FinalizationRegistry ( () => deleted++ );
|
|
56
|
-
|
|
57
|
-
registry.register ( object );
|
|
58
|
-
|
|
59
|
-
object = null;
|
|
60
|
-
|
|
61
|
-
await delay ( 500 );
|
|
62
|
-
global.gc ();
|
|
63
|
-
await delay ( 500 );
|
|
64
|
-
|
|
65
|
-
t.is ( deleted, 1 );
|
|
66
|
-
t.is ( map.size, 1 );
|
|
67
|
-
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
});
|