gristlabs_grist-core/app/client/lib/ObservableSet.js
Paul Fitzpatrick 1654a2681f (core) move client code to core
Summary:
This moves all client code to core, and makes minimal fix-ups to
get grist and grist-core to compile correctly.  The client works
in core, but I'm leaving clean-up around the build and bundles to
follow-up.

Test Plan: existing tests pass; server-dev bundle looks sane

Reviewers: dsagal

Reviewed By: dsagal

Differential Revision: https://phab.getgrist.com/D2627
2020-10-02 13:24:21 -04:00

75 lines
2.0 KiB
JavaScript

var _ = require('underscore');
var ko = require('knockout');
var dispose = require('./dispose');
/**
* An ObservableSet keeps track of a set of values whose membership is controlled by a boolean
* observable.
* @property {ko.observable<Number>} count: Count of items that are currently included.
*/
function ObservableSet() {
this._items = {};
this.count = ko.observable(0);
}
dispose.makeDisposable(ObservableSet);
/**
* Adds an item to keep track of. The value is added to the set whenever isIncluded observable is
* true. To stop keeping track of this item, call dispose() on the returned object.
*
* @param {ko.observable<Boolean>} isIncluded: observable for whether to include the value.
* @param {Object} value: Arbitrary value. May be omitted if you only care about the count.
* @return {Object} Object with dispose() method, which can be called to unsubscribe from
* isIncluded, and remove the value from the set.
*/
ObservableSet.prototype.add = function(isIncluded, value) {
var uniqueKey = _.uniqueId();
var sub = this.autoDispose(isIncluded.subscribe(function(include) {
if (include) {
this._add(uniqueKey, value);
} else {
this._remove(uniqueKey);
}
}, this));
if (isIncluded.peek()) {
this._add(uniqueKey, value);
}
return {
dispose: function() {
this._remove(uniqueKey);
this.disposeDiscard(sub);
}.bind(this)
};
};
/**
* Returns an array of all the values that are currently included in the set.
*/
ObservableSet.prototype.all = function() {
return _.values(this._items);
};
/**
* Internal helper to add a value to the set.
*/
ObservableSet.prototype._add = function(key, value) {
if (!this._items.hasOwnProperty(key)) {
this._items[key] = value;
this.count(this.count() + 1);
}
};
/**
* Internal helper to remove a value from the set.
*/
ObservableSet.prototype._remove = function(key) {
if (this._items.hasOwnProperty(key)) {
delete this._items[key];
this.count(this.count() - 1);
}
};
module.exports = ObservableSet;