WXL
4 天以前 2cc85c64f1c64a2dbaeae276a3e2ca8420de76b7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
    MIT License http://www.opensource.org/licenses/mit-license.php
    Author Tobias Koppers @sokra
*/
 
"use strict";
 
const makeSerializable = require("./makeSerializable");
 
/**
 * Merges every queued iterable directly into the concrete backing set.
 * @template T
 * @param {Set<T>} targetSet set where items should be added
 * @param {Set<Iterable<T>>} toMerge iterables to be merged
 * @returns {void}
 */
const merge = (targetSet, toMerge) => {
    for (const set of toMerge) {
        for (const item of set) {
            targetSet.add(item);
        }
    }
};
 
/**
 * Flattens nested `LazySet` instances into a single collection of iterables
 * that can later be merged into the backing set.
 * @template T
 * @param {Set<Iterable<T>>} targetSet set where iterables should be added
 * @param {LazySet<T>[]} toDeepMerge lazy sets to be flattened
 * @returns {void}
 */
const flatten = (targetSet, toDeepMerge) => {
    for (const set of toDeepMerge) {
        if (set._set.size > 0) targetSet.add(set._set);
        if (set._needMerge) {
            for (const mergedSet of set._toMerge) {
                targetSet.add(mergedSet);
            }
            flatten(targetSet, set._toDeepMerge);
        }
    }
};
 
/**
 * Defines the set iterator type used by this module.
 * @template T
 * @typedef {import("typescript-iterable").SetIterator<T>} SetIterator
 */
 
/**
 * Like Set but with an addAll method to eventually add items from another iterable.
 * Access methods make sure that all delayed operations are executed.
 * Iteration methods deopts to normal Set performance until clear is called again (because of the chance of modifications during iteration).
 * @template T
 */
class LazySet {
    /**
     * Seeds the set with an optional iterable while preparing internal queues for
     * deferred merges.
     * @param {Iterable<T>=} iterable init iterable
     */
    constructor(iterable) {
        /** @type {Set<T>} */
        this._set = new Set(iterable);
        /** @type {Set<Iterable<T>>} */
        this._toMerge = new Set();
        /** @type {LazySet<T>[]} */
        this._toDeepMerge = [];
        this._needMerge = false;
        this._deopt = false;
    }
 
    /**
     * Flattens any nested lazy sets that were queued for merging.
     */
    _flatten() {
        flatten(this._toMerge, this._toDeepMerge);
        this._toDeepMerge.length = 0;
    }
 
    /**
     * Materializes all deferred additions into the backing set.
     */
    _merge() {
        this._flatten();
        merge(this._set, this._toMerge);
        this._toMerge.clear();
        this._needMerge = false;
    }
 
    /**
     * Reports whether the set is empty without forcing a full merge.
     * @returns {boolean} true when no items have been stored or queued
     */
    _isEmpty() {
        return (
            this._set.size === 0 &&
            this._toMerge.size === 0 &&
            this._toDeepMerge.length === 0
        );
    }
 
    /**
     * Returns the number of items after applying any deferred merges.
     * @returns {number} number of items in the set
     */
    get size() {
        if (this._needMerge) this._merge();
        return this._set.size;
    }
 
    /**
     * Adds a single item immediately to the concrete backing set.
     * @param {T} item an item
     * @returns {LazySet<T>} itself
     */
    add(item) {
        this._set.add(item);
        return this;
    }
 
    /**
     * Queues another iterable or lazy set for later merging so large bulk adds
     * can stay cheap until the set is read.
     * @param {Iterable<T> | LazySet<T>} iterable a immutable iterable or another immutable LazySet which will eventually be merged into the Set
     * @returns {LazySet<T>} itself
     */
    addAll(iterable) {
        if (this._deopt) {
            const _set = this._set;
            for (const item of iterable) {
                _set.add(item);
            }
        } else {
            if (iterable instanceof LazySet) {
                if (iterable._isEmpty()) return this;
                this._toDeepMerge.push(iterable);
                this._needMerge = true;
                if (this._toDeepMerge.length > 100000) {
                    this._flatten();
                }
            } else {
                this._toMerge.add(iterable);
                this._needMerge = true;
            }
            if (this._toMerge.size > 100000) this._merge();
        }
        return this;
    }
 
    /**
     * Removes all items and clears every deferred merge queue.
     */
    clear() {
        this._set.clear();
        this._toMerge.clear();
        this._toDeepMerge.length = 0;
        this._needMerge = false;
        this._deopt = false;
    }
 
    /**
     * Deletes an item after first materializing any deferred additions that may
     * contain it.
     * @param {T} value an item
     * @returns {boolean} true, if the value was in the Set before
     */
    delete(value) {
        if (this._needMerge) this._merge();
        return this._set.delete(value);
    }
 
    /**
     * Returns the set's entry iterator and permanently switches future
     * operations to eager merge mode to preserve iterator correctness.
     * @returns {SetIterator<[T, T]>} entries
     */
    entries() {
        this._deopt = true;
        if (this._needMerge) this._merge();
        return this._set.entries();
    }
 
    /**
     * Iterates over every item after forcing pending merges and switching to
     * eager mode for correctness during iteration.
     * @template K
     * @param {(value: T, value2: T, set: Set<T>) => void} callbackFn function called for each entry
     * @param {K} thisArg this argument for the callbackFn
     * @returns {void}
     */
    forEach(callbackFn, thisArg) {
        this._deopt = true;
        if (this._needMerge) this._merge();
        // eslint-disable-next-line unicorn/no-array-for-each, unicorn/no-array-method-this-argument
        this._set.forEach(callbackFn, thisArg);
    }
 
    /**
     * Checks whether an item is present after applying any deferred merges.
     * @param {T} item an item
     * @returns {boolean} true, when the item is in the Set
     */
    has(item) {
        if (this._needMerge) this._merge();
        return this._set.has(item);
    }
 
    /**
     * Returns the key iterator, eagerly materializing pending merges first.
     * @returns {SetIterator<T>} keys
     */
    keys() {
        this._deopt = true;
        if (this._needMerge) this._merge();
        return this._set.keys();
    }
 
    /**
     * Returns the value iterator, eagerly materializing pending merges first.
     * @returns {SetIterator<T>} values
     */
    values() {
        this._deopt = true;
        if (this._needMerge) this._merge();
        return this._set.values();
    }
 
    /**
     * Returns the default iterator over values after forcing pending merges.
     * @returns {SetIterator<T>} iterable iterator
     */
    [Symbol.iterator]() {
        this._deopt = true;
        if (this._needMerge) this._merge();
        return this._set[Symbol.iterator]();
    }
 
    /* istanbul ignore next */
    get [Symbol.toStringTag]() {
        return "LazySet";
    }
 
    /**
     * Serializes the fully materialized set contents into webpack's object
     * serialization stream.
     * @param {import("../serialization/ObjectMiddleware").ObjectSerializerContext} context context
     */
    serialize({ write }) {
        if (this._needMerge) this._merge();
        write(this._set.size);
        for (const item of this._set) write(item);
    }
 
    /**
     * Restores a `LazySet` from serialized item data.
     * @template T
     * @param {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} context context
     * @returns {LazySet<T>} lazy set
     */
    static deserialize({ read }) {
        const count = read();
        /** @type {T[]} */
        const items = [];
        for (let i = 0; i < count; i++) {
            items.push(read());
        }
        return new LazySet(items);
    }
}
 
makeSerializable(LazySet, "webpack/lib/util/LazySet");
 
module.exports = LazySet;