lazy_set: Convert LazySet to a real class.

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
This commit is contained in:
Anders Kaseorg 2020-02-02 22:48:50 -08:00 committed by Tim Abbott
parent fe54e73c77
commit da633e953e
5 changed files with 54 additions and 55 deletions

View File

@ -865,12 +865,12 @@ function test_raw_file_drop(raw_drop_func) {
run_test('warn_if_private_stream_is_linked', () => { run_test('warn_if_private_stream_is_linked', () => {
stream_data.add_sub(compose_state.stream_name(), { stream_data.add_sub(compose_state.stream_name(), {
subscribers: LazySet([1, 2]), subscribers: new LazySet([1, 2]),
}); });
let denmark = { let denmark = {
name: 'Denmark', name: 'Denmark',
subscribers: LazySet([1, 2, 3]), subscribers: new LazySet([1, 2, 3]),
}; };
function test_noop_case(invite_only) { function test_noop_case(invite_only) {
@ -913,7 +913,7 @@ run_test('warn_if_private_stream_is_linked', () => {
denmark = { denmark = {
invite_only: true, invite_only: true,
name: 'Denmark', name: 'Denmark',
subscribers: LazySet([1]), subscribers: new LazySet([1]),
}; };
compose.warn_if_private_stream_is_linked(denmark); compose.warn_if_private_stream_is_linked(denmark);

View File

@ -8,7 +8,7 @@ const LazySet = zrequire('lazy_set').LazySet;
*/ */
run_test('map', () => { run_test('map', () => {
const ls = LazySet([1, 2]); const ls = new LazySet([1, 2]);
const triple = (n) => n * 3; const triple = (n) => n * 3;
@ -17,7 +17,7 @@ run_test('map', () => {
run_test('conversions', () => { run_test('conversions', () => {
blueslip.set_test_data('error', 'not a number'); blueslip.set_test_data('error', 'not a number');
const ls = LazySet([1, 2]); const ls = new LazySet([1, 2]);
ls.add('3'); ls.add('3');
assert(ls.has('3')); assert(ls.has('3'));
}); });

View File

@ -35,9 +35,9 @@ stream_data.create_streams([
]); ]);
run_test('sort_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 = [ let test_streams = [
{name: 'Dev', pin_to_top: false, subscribers: unpopular, subscribed: true}, {name: 'Dev', pin_to_top: false, subscribers: unpopular, subscribed: true},

View File

@ -1,4 +1,4 @@
exports.LazySet = function (vals) { export class LazySet {
/* /*
This class is optimized for a very This class is optimized for a very
particular use case. particular use case.
@ -20,63 +20,62 @@ exports.LazySet = function (vals) {
as has/add/delete, we convert it over as has/add/delete, we convert it over
to a set for a one-time cost. to a set for a one-time cost.
*/ */
const self = {};
self.arr = vals;
self.set = undefined;
self.keys = function () { constructor(vals) {
if (self.set !== undefined) { this.arr = vals;
return Array.from(self.set); this.set = undefined;
} }
return self.arr;
};
function make_set() { keys() {
if (self.set !== undefined) { if (this.set !== undefined) {
return Array.from(this.set);
}
return this.arr;
}
_make_set() {
if (this.set !== undefined) {
return; return;
} }
self.set = new Set(self.arr); this.set = new Set(this.arr);
self.arr = undefined; this.arr = undefined;
} }
self.num_items = function () { num_items() {
if (self.set !== undefined) { if (this.set !== undefined) {
return self.set.size; return this.set.size;
} }
return self.arr.length; return this.arr.length;
}; }
self.map = function (f) { map(f) {
return _.map(self.keys(), f); return _.map(this.keys(), f);
}; }
self.has = function (v) { has(v) {
make_set(); this._make_set();
const val = self._clean(v); const val = this._clean(v);
return self.set.has(val); return this.set.has(val);
}; }
self.add = function (v) { add(v) {
make_set(); this._make_set();
const val = self._clean(v); const val = this._clean(v);
self.set.add(val); this.set.add(val);
}; }
self.delete = function (v) { delete(v) {
make_set(); this._make_set();
const val = self._clean(v); const val = this._clean(v);
return self.set.delete(val); return this.set.delete(val);
}; }
self._clean = function (v) { _clean(v) {
if (typeof v !== 'number') { if (typeof v !== 'number') {
blueslip.error('not a number'); blueslip.error('not a number');
return parseInt(v, 10); return parseInt(v, 10);
} }
return v; return v;
}; }
}
return self;
};

View File

@ -168,7 +168,7 @@ exports.unsubscribe_myself = function (sub) {
exports.add_sub = function (stream_name, sub) { exports.add_sub = function (stream_name, sub) {
if (!_.has(sub, 'subscribers')) { if (!_.has(sub, 'subscribers')) {
sub.subscribers = LazySet([]); sub.subscribers = new LazySet([]);
} }
stream_info.set(stream_name, sub); 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) { 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) { exports.add_subscriber = function (stream_name, user_id) {