Skip to content

Instantly share code, notes, and snippets.

@mattduggan
Last active October 2, 2017 19:02
Show Gist options
  • Save mattduggan/87308c785e1177fcf78a8e58cf502f61 to your computer and use it in GitHub Desktop.
Save mattduggan/87308c785e1177fcf78a8e58cf502f61 to your computer and use it in GitHub Desktop.
Feature Tour - Store
(function() {
this.FeatureTourStore = function() {
var Constants = Yesware.get("FeatureTourConstants");
var Dispatcher = Yesware.get("EventDispatcher");
var NAMESPACE = "featureTourStore";
var state {
featureTours: [],
isLoading: false,
lastError: null
};
Dispatcher.on(Constants.GET_FEATURE_TOURS, NAMESPACE, function() {
state.isLoading = true;
state.lastError = null;
triggerChange();
});
Dispatcher.on(Constants.GET_FEATURE_TOURS_SUCCESS, NAMESPACE, function(featureTours) {
state.isLoading = false;
state.featureTours = featureTours;
triggerChange();
});
Dispatcher.on(Constants.GET_FEATURE_TOURS_ERROR, NAMESPACE, function(reason) {
state.isLoading = false;
state.lastError = reason;
triggerChange();
});
Dispatcher.on(Constants.PUT_FEATURE_TOUR, NAMESPACE, function() {
state.isLoading = true;
state.lastError = null;
triggerChange();
});
Dispatcher.on(Constants.PUT_FEATURE_TOUR_SUCCESS, NAMESPACE, function(featureTour) {
state.isLoading = false;
var index = state.featureTours.findIndex(function(tour) {
return tour.id === featureTour.id;
});
state.featureTours[index] = featureTour;
triggerChange();
});
Dispatcher.on(Constants.PUT_FEATURE_TOUR_ERROR, NAMESPACE, function(reason) {
state.isLoading = false;
state.lastError = reason;
triggerChange();
});
function triggerChange() {
Dispatcher.trigger("featureTours::change", state);
}
Yesware.get("Upgrade").registerCallback(function() {
Dispatcher.off("featureTours::change", NAMESPACE);
Object.keys(Constants).forEach(function(key) {
Dispatcher.off(Constants[key], NAMESPACE);
});
});
return {
onChange: function(callback) {
Dispatcher.on("featureTours::change", NAMESPACE, function() {
callback(state);
});
callback(state); // initialize with current state on registration
}
};
};
}).apply(Yesware);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment