mirror of
https://github.com/gristlabs/grist-core.git
synced 2026-03-02 04:09:24 +00:00
(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
This commit is contained in:
74
app/client/lib/ObservableSet.js
Normal file
74
app/client/lib/ObservableSet.js
Normal file
@@ -0,0 +1,74 @@
|
||||
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;
|
||||
Reference in New Issue
Block a user