2019-12-27 13:16:22 +01:00
|
|
|
exports.LazySet = function (vals) {
|
|
|
|
/*
|
|
|
|
This class is optimized for a very
|
|
|
|
particular use case.
|
|
|
|
|
|
|
|
We often have lots of subscribers on
|
|
|
|
a stream. We get an array from the
|
|
|
|
backend, because it's JSON.
|
|
|
|
|
|
|
|
Often the only operation we need
|
|
|
|
on subscribers is to get the length,
|
|
|
|
which is plenty cheap as an array.
|
|
|
|
|
|
|
|
Making an array from a set is cheap
|
|
|
|
for one stream, but it's expensive
|
|
|
|
for all N streams at page load.
|
|
|
|
|
|
|
|
Once somebody does an operation
|
|
|
|
where sets are useful, such
|
2020-02-03 07:41:38 +01:00
|
|
|
as has/add/delete, we convert it over
|
2019-12-27 13:16:22 +01:00
|
|
|
to a set for a one-time cost.
|
|
|
|
*/
|
|
|
|
const self = {};
|
|
|
|
self.arr = vals;
|
|
|
|
self.set = undefined;
|
|
|
|
|
|
|
|
self.keys = function () {
|
|
|
|
if (self.set !== undefined) {
|
|
|
|
return Array.from(self.set);
|
|
|
|
}
|
|
|
|
return self.arr;
|
|
|
|
};
|
|
|
|
|
|
|
|
function make_set() {
|
|
|
|
if (self.set !== undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.set = new Set(self.arr);
|
|
|
|
self.arr = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.num_items = function () {
|
|
|
|
if (self.set !== undefined) {
|
|
|
|
return self.set.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.arr.length;
|
|
|
|
};
|
|
|
|
|
|
|
|
self.map = function (f) {
|
|
|
|
return _.map(self.keys(), f);
|
|
|
|
};
|
|
|
|
|
|
|
|
self.has = function (v) {
|
|
|
|
make_set();
|
2020-01-14 23:29:54 +01:00
|
|
|
const val = self._clean(v);
|
|
|
|
return self.set.has(val);
|
2019-12-27 13:16:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
self.add = function (v) {
|
|
|
|
make_set();
|
2020-01-14 23:29:54 +01:00
|
|
|
const val = self._clean(v);
|
|
|
|
self.set.add(val);
|
2019-12-27 13:16:22 +01:00
|
|
|
};
|
|
|
|
|
2020-02-03 07:41:38 +01:00
|
|
|
self.delete = function (v) {
|
2019-12-27 13:16:22 +01:00
|
|
|
make_set();
|
2020-01-14 23:29:54 +01:00
|
|
|
const val = self._clean(v);
|
2020-02-03 07:41:38 +01:00
|
|
|
return self.set.delete(val);
|
2019-12-27 13:16:22 +01:00
|
|
|
};
|
|
|
|
|
2020-01-14 23:29:54 +01:00
|
|
|
self._clean = function (v) {
|
|
|
|
if (typeof v !== 'number') {
|
|
|
|
blueslip.error('not a number');
|
|
|
|
return parseInt(v, 10);
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-12-27 13:16:22 +01:00
|
|
|
return self;
|
|
|
|
};
|