1.0.13 • Published 3 years ago

redbone v1.0.13

Weekly downloads
3
License
MIT
Repository
github
Last release
3 years ago

redbone

Library for client → server → client redux dispatching

Install

npm install --save redbone

Using

Server side

//...
const redbone = require('redbone');
//...

//Watch to client action
redbone.watch('@@server/user/GET', function(socket, action, next) {
  if (!action.user_id) return next(new Error('User not found'));
  //Your logic here, getUser — just example
  getUser(action.user_id).then((user) => {
    if (!user) return next(new Error('User not found'));
    //dispatch action to client
    socket.dispatch({ '@@user/current/SET', user });
  }).catch(next);
});
//You can chain some of redbone methods
redbone.watch('@@server/user/SET', function(socket, action, next) {
  if (!action.user) return next(new Error('User is undefined'));
  setUser(action.user).then(() => socket.dispatch({ type: '@@system/SUCCESS_SAVE' )).catch(next);
}).catch((socket, err) => {
  socket.dispatch({ type: '@@system/SHOW_ERROR_MODAL', title: 'Server Error', err });
});

//io — your socket io server instance
redbone(io);

Client side

After create your store, just add

//io — your socket.io connection to server
io.on('dispatch', store.dispatch);

All socket.dispatch(action) at redbone watcher will perform action to client

Optionaly, you can add special middleware to redux for client → server dispatching

import serverDispatchMiddleware from 'redbone/client/getServerDispatchMiddleware';
//...
//io — your socket.io connection to server
middlewares.unshift(serverDispatchMiddleware(io));
//...
const createStoreWithMiddlewares = compose(applyMiddleware(...middlewares))(createStore);
const store = createStoreWithMiddlewares(reducer);

serverDispatchMiddleware takes options as second parameter. If you want to pass server-side actions to store set next property as true, but if you want to exclude some actions from passing, set array of type to exclude property:

middlewares.unshift(serverDispatchMiddleware(io, {
  next: true,
  exclude: [TYPES.EXCLUDED_TYPE, TYPES.EXCLUDED_TYPE_TOO]
}));

Watchers

documentation comming soon Look at server side example

4.0.0-beta.4

3 years ago

1.0.13

3 years ago

4.0.0-beta.3

4 years ago

4.0.0-beta.2

4 years ago

4.0.0-beta.1

4 years ago

1.0.11

5 years ago

3.0.0

6 years ago

2.4.2

7 years ago

2.4.1

7 years ago

2.4.0

7 years ago

2.3.1

7 years ago

2.3.0

7 years ago

2.2.1

7 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.4

7 years ago

2.0.3

7 years ago

2.0.2

7 years ago

2.0.0

7 years ago

1.1.0

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago