Skip to content

Instantly share code, notes, and snippets.

@mridgway
Created July 21, 2015 20:43
Show Gist options
  • Save mridgway/2924b9c71e7d8d925e55 to your computer and use it in GitHub Desktop.
Save mridgway/2924b9c71e7d8d925e55 to your computer and use it in GitHub Desktop.
var createStore = require('fluxible/addons').createStore;
module.exports = function createReducerStore(storeName, initialState, reducers, getters) {
var ReducerStore = createStore({
storeName: storeName,
handlers: {
'default': '_handle'
},
initialize: function () {
this.state = initialState;
},
_handle: function (payload, eventName) {
var reducersList = reducers[eventName];
if (reducersList) {
var startingState = this.state;
if (!Array.isArray(reducersList)) {
reducersList = [reducersList];
}
reducersList.forEach(function (reducer) {
this.state = reducer(this.state, payload);
}, this);
if (this.state !== startingState) {
this.emitChange();
}
}
},
dehydrate: function () {
return this.state;
},
rehydrate: function (state) {
this.state = state;
}
});
Object.keys(getters).forEach(function (getterName) {
var getter = getters[getterName];
ReducerStore.prototype[getterName] = function () {
return getter.apply(null, [this.state, ...arguments]);
};
});
return ReducerStore;
};
var createReducerStore = require('./createReducerStore');
var MessageStoreReducers = require('./reducers/MessageStoreReducers');
var MessageStore = createReducerStore('MessageStore', {
messages: {},
sortedByDate: []
}, {
'RECEIVE_MESSAGES': MessageStoreReducers.receiveMessages,
'OPEN_THREAD': MessageStoreReducers.openThread
}, {
getAll: function getAll(state) {
return state.messages;
},
get: function get(state, id) {
return state.messages[id];
},
getAllForThread: function getAllForThread(state, threadID) {
var threadMessages = [];
state.sortedByDate.forEach(function (key) {
var message = state.messages[key];
if (message.threadID === threadID) {
threadMessages.push(message);
}
});
return threadMessages;
}
});
module.exports = MessageStore;
module.exports.receiveMessages = function receiveMessages(state, messages) {
var oldMessages = state.messages;
var newMessages = {...oldMessages};
messages.forEach(function (message) {
newMessages[message.id] = message;
});
var sortedByDate = (newMessages && Object.keys(newMessages)) || [];
sortedByDate.sort(function(a, b) {
if (newMessages[a].date < newMessages[b].date) {
return -1;
} else if (newMessages[a].date > newMessages[b].date) {
return 1;
}
return 0;
});
return {
messages: newMessages,
sortedByDate
};
};
module.exports.openThread = function openThread(state, payload) {
// Mark all read
var oldMessages = state.messages;
var newMessages = {
...oldMessages
};
Object.keys(state.messages).forEach(function (key) {
var message = state.messages[key];
if (message.threadID === payload.threadID) {
newMessages[key] = {
...message,
text: message.text,
isRead: true
};
}
});
return {
...state,
messages: newMessages
};
};
@geekyme
Copy link

geekyme commented Oct 6, 2015

This looks great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment