source: imaps-frontend/node_modules/webpack/lib/util/WeakTupleMap.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 4.0 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8/**
9 * @template {any[]} T
10 * @template V
11 * @typedef {Map<object, WeakTupleMap<T, V>>} M
12 */
13/**
14 * @template {any[]} T
15 * @template V
16 * @typedef {WeakMap<object, WeakTupleMap<T, V>>} W
17 */
18
19/**
20 * @param {any} thing thing
21 * @returns {boolean} true if is weak
22 */
23const isWeakKey = thing => typeof thing === "object" && thing !== null;
24
25/**
26 * @template {any[]} T
27 * @template V
28 */
29class WeakTupleMap {
30 constructor() {
31 /** @private */
32 this.f = 0;
33 /**
34 * @private
35 * @type {any}
36 */
37 this.v = undefined;
38 /**
39 * @private
40 * @type {M<T, V> | undefined}
41 */
42 this.m = undefined;
43 /**
44 * @private
45 * @type {W<T, V> | undefined}
46 */
47 this.w = undefined;
48 }
49
50 /**
51 * @param {[...T, V]} args tuple
52 * @returns {void}
53 */
54 set(...args) {
55 /** @type {WeakTupleMap<T, V>} */
56 let node = this;
57 for (let i = 0; i < args.length - 1; i++) {
58 node = node._get(args[i]);
59 }
60 node._setValue(args[args.length - 1]);
61 }
62
63 /**
64 * @param {T} args tuple
65 * @returns {boolean} true, if the tuple is in the Set
66 */
67 has(...args) {
68 /** @type {WeakTupleMap<T, V> | undefined} */
69 let node = this;
70 for (let i = 0; i < args.length; i++) {
71 node = node._peek(args[i]);
72 if (node === undefined) return false;
73 }
74 return node._hasValue();
75 }
76
77 /**
78 * @param {T} args tuple
79 * @returns {V | undefined} the value
80 */
81 get(...args) {
82 /** @type {WeakTupleMap<T, V> | undefined} */
83 let node = this;
84 for (let i = 0; i < args.length; i++) {
85 node = node._peek(args[i]);
86 if (node === undefined) return;
87 }
88 return node._getValue();
89 }
90
91 /**
92 * @param {[...T, function(): V]} args tuple
93 * @returns {V} the value
94 */
95 provide(...args) {
96 /** @type {WeakTupleMap<T, V>} */
97 let node = this;
98 for (let i = 0; i < args.length - 1; i++) {
99 node = node._get(args[i]);
100 }
101 if (node._hasValue()) return node._getValue();
102 const fn = args[args.length - 1];
103 const newValue = fn(...args.slice(0, -1));
104 node._setValue(newValue);
105 return newValue;
106 }
107
108 /**
109 * @param {T} args tuple
110 * @returns {void}
111 */
112 delete(...args) {
113 /** @type {WeakTupleMap<T, V> | undefined} */
114 let node = this;
115 for (let i = 0; i < args.length; i++) {
116 node = node._peek(args[i]);
117 if (node === undefined) return;
118 }
119 node._deleteValue();
120 }
121
122 /**
123 * @returns {void}
124 */
125 clear() {
126 this.f = 0;
127 this.v = undefined;
128 this.w = undefined;
129 this.m = undefined;
130 }
131
132 _getValue() {
133 return this.v;
134 }
135
136 _hasValue() {
137 return (this.f & 1) === 1;
138 }
139
140 /**
141 * @param {any} v value
142 * @private
143 */
144 _setValue(v) {
145 this.f |= 1;
146 this.v = v;
147 }
148
149 _deleteValue() {
150 this.f &= 6;
151 this.v = undefined;
152 }
153
154 /**
155 * @param {any} thing thing
156 * @returns {WeakTupleMap<T, V> | undefined} thing
157 * @private
158 */
159 _peek(thing) {
160 if (isWeakKey(thing)) {
161 if ((this.f & 4) !== 4) return;
162 return /** @type {W<T, V>} */ (this.w).get(thing);
163 }
164 if ((this.f & 2) !== 2) return;
165 return /** @type {M<T, V>} */ (this.m).get(thing);
166 }
167
168 /**
169 * @private
170 * @param {any} thing thing
171 * @returns {WeakTupleMap<T, V>} value
172 */
173 _get(thing) {
174 if (isWeakKey(thing)) {
175 if ((this.f & 4) !== 4) {
176 const newMap = new WeakMap();
177 this.f |= 4;
178 const newNode = new WeakTupleMap();
179 (this.w = newMap).set(thing, newNode);
180 return newNode;
181 }
182 const entry =
183 /** @type {W<T, V>} */
184 (this.w).get(thing);
185 if (entry !== undefined) {
186 return entry;
187 }
188 const newNode = new WeakTupleMap();
189 /** @type {W<T, V>} */
190 (this.w).set(thing, newNode);
191 return newNode;
192 }
193 if ((this.f & 2) !== 2) {
194 const newMap = new Map();
195 this.f |= 2;
196 const newNode = new WeakTupleMap();
197 (this.m = newMap).set(thing, newNode);
198 return newNode;
199 }
200 const entry =
201 /** @type {M<T, V>} */
202 (this.m).get(thing);
203 if (entry !== undefined) {
204 return entry;
205 }
206 const newNode = new WeakTupleMap();
207 /** @type {M<T, V>} */
208 (this.m).set(thing, newNode);
209 return newNode;
210 }
211}
212
213module.exports = WeakTupleMap;
Note: See TracBrowser for help on using the repository browser.