| 1 |
<!DOCTYPE html> |
| 2 |
|
| 3 |
<html> |
| 4 |
<head> |
| 5 |
<title>universal-weak-map.js</title> |
| 6 |
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> |
| 7 |
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"> |
| 8 |
<link rel="stylesheet" media="all" href="../docco.css" /> |
| 9 |
</head> |
| 10 |
<body> |
| 11 |
<div id="container"> |
| 12 |
<div id="background"></div> |
| 13 |
|
| 14 |
<ul id="jump_to"> |
| 15 |
<li> |
| 16 |
<a class="large" href="javascript:void(0);">Jump To …</a> |
| 17 |
<a class="small" href="javascript:void(0);">+</a> |
| 18 |
<div id="jump_wrapper"> |
| 19 |
<div id="jump_page_wrapper"> |
| 20 |
<div id="jump_page"> |
| 21 |
|
| 22 |
|
| 23 |
<a class="source" href="tuple.html"> |
| 24 |
src/tuple.js |
| 25 |
</a> |
| 26 |
|
| 27 |
|
| 28 |
<a class="source" href="universal-weak-map.html"> |
| 29 |
src/universal-weak-map.js |
| 30 |
</a> |
| 31 |
|
| 32 |
|
| 33 |
<a class="source" href="util.html"> |
| 34 |
src/util.js |
| 35 |
</a> |
| 36 |
|
| 37 |
</div> |
| 38 |
</div> |
| 39 |
</li> |
| 40 |
</ul> |
| 41 |
|
| 42 |
<ul class="sections"> |
| 43 |
|
| 44 |
<li id="title"> |
| 45 |
<div class="annotation"> |
| 46 |
<h1>universal-weak-map.js</h1> |
| 47 |
</div> |
| 48 |
</li> |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
<li id="section-1"> |
| 53 |
<div class="annotation"> |
| 54 |
|
| 55 |
<div class="pilwrap "> |
| 56 |
<a class="pilcrow" href="#section-1">¶</a> |
| 57 |
</div> |
| 58 |
<p>A map data structure that holds object keys weakly, yet can also hold |
| 59 |
non-object keys, unlike the native <code>WeakMap</code>.</p> |
| 60 |
|
| 61 |
</div> |
| 62 |
|
| 63 |
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UniversalWeakMap</span> </span>{ |
| 64 |
<span class="hljs-keyword">constructor</span>() {</pre></div></div> |
| 65 |
|
| 66 |
</li> |
| 67 |
|
| 68 |
|
| 69 |
<li id="section-2"> |
| 70 |
<div class="annotation"> |
| 71 |
|
| 72 |
<div class="pilwrap "> |
| 73 |
<a class="pilcrow" href="#section-2">¶</a> |
| 74 |
</div> |
| 75 |
<p>Since a <code>WeakMap</code> cannot hold primitive values as keys, we need a |
| 76 |
backup <code>Map</code> instance to hold primitive keys. Both <code>this._weakMap</code> |
| 77 |
and <code>this._strongMap</code> are lazily initialized.</p> |
| 78 |
|
| 79 |
</div> |
| 80 |
|
| 81 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">this</span>._weakMap = <span class="hljs-literal">null</span>; |
| 82 |
<span class="hljs-keyword">this</span>._strongMap = <span class="hljs-literal">null</span>; |
| 83 |
}</pre></div></div> |
| 84 |
|
| 85 |
</li> |
| 86 |
|
| 87 |
|
| 88 |
<li id="section-3"> |
| 89 |
<div class="annotation"> |
| 90 |
|
| 91 |
<div class="pilwrap "> |
| 92 |
<a class="pilcrow" href="#section-3">¶</a> |
| 93 |
</div> |
| 94 |
<p>Since <code>get</code> and <code>set</code> are the only methods used, that’s all I’ve |
| 95 |
implemented here.</p> |
| 96 |
|
| 97 |
</div> |
| 98 |
|
| 99 |
<div class="content"><div class='highlight'><pre> |
| 100 |
get(key) { |
| 101 |
<span class="hljs-keyword">const</span> map = <span class="hljs-keyword">this</span>._getMap(key, <span class="hljs-literal">false</span>); |
| 102 |
<span class="hljs-keyword">if</span> (map) { |
| 103 |
<span class="hljs-keyword">return</span> map.get(key); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
set(key, value) { |
| 108 |
<span class="hljs-keyword">this</span>._getMap(key, <span class="hljs-literal">true</span>).set(key, value);</pre></div></div> |
| 109 |
|
| 110 |
</li> |
| 111 |
|
| 112 |
|
| 113 |
<li id="section-4"> |
| 114 |
<div class="annotation"> |
| 115 |
|
| 116 |
<div class="pilwrap "> |
| 117 |
<a class="pilcrow" href="#section-4">¶</a> |
| 118 |
</div> |
| 119 |
<p>An actual <code>Map</code> or <code>WeakMap</code> would return <code>this</code> here, but |
| 120 |
returning the <code>value</code> is more convenient for the <code>tuple</code> |
| 121 |
implementation.</p> |
| 122 |
|
| 123 |
</div> |
| 124 |
|
| 125 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">return</span> value; |
| 126 |
} |
| 127 |
|
| 128 |
_getMap(key, canCreate) { |
| 129 |
<span class="hljs-keyword">if</span> (! canCreate) { |
| 130 |
<span class="hljs-keyword">return</span> isObjRef(key) ? <span class="hljs-keyword">this</span>._weakMap : <span class="hljs-keyword">this</span>._strongMap; |
| 131 |
} |
| 132 |
<span class="hljs-keyword">if</span> (isObjRef(key)) { |
| 133 |
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>._weakMap || (<span class="hljs-keyword">this</span>._weakMap = <span class="hljs-keyword">new</span> <span class="hljs-built_in">WeakMap</span>); |
| 134 |
} |
| 135 |
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>._strongMap || (<span class="hljs-keyword">this</span>._strongMap = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Map</span>); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">isObjRef</span>(<span class="hljs-params">value</span>) </span>{ |
| 140 |
<span class="hljs-keyword">switch</span> (<span class="hljs-keyword">typeof</span> value) { |
| 141 |
<span class="hljs-keyword">case</span> <span class="hljs-string">"object"</span>: |
| 142 |
<span class="hljs-keyword">if</span> (value === <span class="hljs-literal">null</span>) { |
| 143 |
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; |
| 144 |
} |
| 145 |
<span class="hljs-keyword">case</span> <span class="hljs-string">"function"</span>: |
| 146 |
<span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>; |
| 147 |
<span class="hljs-keyword">default</span>: |
| 148 |
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; |
| 149 |
} |
| 150 |
}</pre></div></div> |
| 151 |
|
| 152 |
</li> |
| 153 |
|
| 154 |
</ul> |
| 155 |
</div> |
| 156 |
</body> |
| 157 |
</html> |