mirror of https://github.com/zulip/zulip.git
lazy_set: Convert LazySet to a real class.
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
This commit is contained in:
parent
fe54e73c77
commit
da633e953e
|
@ -865,12 +865,12 @@ function test_raw_file_drop(raw_drop_func) {
|
|||
|
||||
run_test('warn_if_private_stream_is_linked', () => {
|
||||
stream_data.add_sub(compose_state.stream_name(), {
|
||||
subscribers: LazySet([1, 2]),
|
||||
subscribers: new LazySet([1, 2]),
|
||||
});
|
||||
|
||||
let denmark = {
|
||||
name: 'Denmark',
|
||||
subscribers: LazySet([1, 2, 3]),
|
||||
subscribers: new LazySet([1, 2, 3]),
|
||||
};
|
||||
|
||||
function test_noop_case(invite_only) {
|
||||
|
@ -913,7 +913,7 @@ run_test('warn_if_private_stream_is_linked', () => {
|
|||
denmark = {
|
||||
invite_only: true,
|
||||
name: 'Denmark',
|
||||
subscribers: LazySet([1]),
|
||||
subscribers: new LazySet([1]),
|
||||
};
|
||||
|
||||
compose.warn_if_private_stream_is_linked(denmark);
|
||||
|
|
|
@ -8,7 +8,7 @@ const LazySet = zrequire('lazy_set').LazySet;
|
|||
*/
|
||||
|
||||
run_test('map', () => {
|
||||
const ls = LazySet([1, 2]);
|
||||
const ls = new LazySet([1, 2]);
|
||||
|
||||
const triple = (n) => n * 3;
|
||||
|
||||
|
@ -17,7 +17,7 @@ run_test('map', () => {
|
|||
|
||||
run_test('conversions', () => {
|
||||
blueslip.set_test_data('error', 'not a number');
|
||||
const ls = LazySet([1, 2]);
|
||||
const ls = new LazySet([1, 2]);
|
||||
ls.add('3');
|
||||
assert(ls.has('3'));
|
||||
});
|
||||
|
|
|
@ -35,9 +35,9 @@ stream_data.create_streams([
|
|||
]);
|
||||
|
||||
run_test('sort_streams', () => {
|
||||
const popular = LazySet([1, 2, 3, 4, 5, 6]);
|
||||
const popular = new LazySet([1, 2, 3, 4, 5, 6]);
|
||||
|
||||
const unpopular = LazySet([1]);
|
||||
const unpopular = new LazySet([1]);
|
||||
|
||||
let test_streams = [
|
||||
{name: 'Dev', pin_to_top: false, subscribers: unpopular, subscribed: true},
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
exports.LazySet = function (vals) {
|
||||
export class LazySet {
|
||||
/*
|
||||
This class is optimized for a very
|
||||
particular use case.
|
||||
|
@ -20,63 +20,62 @@ exports.LazySet = function (vals) {
|
|||
as has/add/delete, we convert it over
|
||||
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;
|
||||
constructor(vals) {
|
||||
this.arr = vals;
|
||||
this.set = undefined;
|
||||
}
|
||||
|
||||
self.num_items = function () {
|
||||
if (self.set !== undefined) {
|
||||
return self.set.size;
|
||||
keys() {
|
||||
if (this.set !== undefined) {
|
||||
return Array.from(this.set);
|
||||
}
|
||||
return this.arr;
|
||||
}
|
||||
|
||||
_make_set() {
|
||||
if (this.set !== undefined) {
|
||||
return;
|
||||
}
|
||||
this.set = new Set(this.arr);
|
||||
this.arr = undefined;
|
||||
}
|
||||
|
||||
num_items() {
|
||||
if (this.set !== undefined) {
|
||||
return this.set.size;
|
||||
}
|
||||
|
||||
return self.arr.length;
|
||||
};
|
||||
return this.arr.length;
|
||||
}
|
||||
|
||||
self.map = function (f) {
|
||||
return _.map(self.keys(), f);
|
||||
};
|
||||
map(f) {
|
||||
return _.map(this.keys(), f);
|
||||
}
|
||||
|
||||
self.has = function (v) {
|
||||
make_set();
|
||||
const val = self._clean(v);
|
||||
return self.set.has(val);
|
||||
};
|
||||
has(v) {
|
||||
this._make_set();
|
||||
const val = this._clean(v);
|
||||
return this.set.has(val);
|
||||
}
|
||||
|
||||
self.add = function (v) {
|
||||
make_set();
|
||||
const val = self._clean(v);
|
||||
self.set.add(val);
|
||||
};
|
||||
add(v) {
|
||||
this._make_set();
|
||||
const val = this._clean(v);
|
||||
this.set.add(val);
|
||||
}
|
||||
|
||||
self.delete = function (v) {
|
||||
make_set();
|
||||
const val = self._clean(v);
|
||||
return self.set.delete(val);
|
||||
};
|
||||
delete(v) {
|
||||
this._make_set();
|
||||
const val = this._clean(v);
|
||||
return this.set.delete(val);
|
||||
}
|
||||
|
||||
self._clean = function (v) {
|
||||
_clean(v) {
|
||||
if (typeof v !== 'number') {
|
||||
blueslip.error('not a number');
|
||||
return parseInt(v, 10);
|
||||
}
|
||||
return v;
|
||||
};
|
||||
|
||||
|
||||
return self;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -168,7 +168,7 @@ exports.unsubscribe_myself = function (sub) {
|
|||
|
||||
exports.add_sub = function (stream_name, sub) {
|
||||
if (!_.has(sub, 'subscribers')) {
|
||||
sub.subscribers = LazySet([]);
|
||||
sub.subscribers = new LazySet([]);
|
||||
}
|
||||
|
||||
stream_info.set(stream_name, sub);
|
||||
|
@ -603,7 +603,7 @@ exports.maybe_get_stream_name = function (stream_id) {
|
|||
};
|
||||
|
||||
exports.set_subscribers = function (sub, user_ids) {
|
||||
sub.subscribers = LazySet(user_ids || []);
|
||||
sub.subscribers = new LazySet(user_ids || []);
|
||||
};
|
||||
|
||||
exports.add_subscriber = function (stream_name, user_id) {
|
||||
|
|
Loading…
Reference in New Issue