Skip to content

Instantly share code, notes, and snippets.

@lukebrandonfarrell
lukebrandonfarrell / ActionReducer.js
Last active March 4, 2023 12:57
A hook to listen to Redux actions in your component.
const initialState = {
type: null,
payload: null,
meta: null,
error: false
};
export default (state = initialState, action) => {
return {
...state,
@macedd
macedd / websocket-heartbeat-client.js
Created January 18, 2019 16:04
WebSockets heartbeat implementation (nodejs + browser)
/**
* Tracks server pings for determining if the connection dropped.
* Will terminate non-responsive connections.
* This close event should initiate the process of recreating the connection in the ws module manager (eg ws/user.js and modules/ws-user.js)
*/
function setupWsHeartbeat(ws) {
// will close the connection if there's no ping from the server
function heartbeat() {
clearTimeout(this.pingTimeout);
@timhudson
timhudson / pre-save-update.js
Last active July 3, 2020 08:58
Update the `lastUpdated` field every time a mongoose model is saved
var schema = mongoose.Schema({
lastUpdated: {type: Date, 'default': Date.now}
})
schema.pre('save', function(next) {
this.lastUpdated = Date.now()
next()
})