horizon-redux
Advanced tools
Comparing version 2.2.0 to 2.3.0
@@ -83,7 +83,7 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
function createActionHandler(hz, actionTakers, dispatch) { | ||
function createActionHandler(hz, actionTakers, dispatch, getState) { | ||
return function (action) { | ||
actionTakers.forEach(function (actionTaker) { | ||
if (actionTakerMatchesAction(action, actionTaker)) { | ||
var observable = actionTaker.observableQuery(hz, action); | ||
var observable = actionTaker.observableQuery(hz, action, getState); | ||
@@ -220,3 +220,3 @@ // if this actionTaker's query has a success or error handler, set up | ||
// create the actionHandler | ||
var handleAction = (0, _actionHandler.createActionHandler)(hz, actionTakers, dispatch); | ||
var handleAction = (0, _actionHandler.createActionHandler)(hz, actionTakers, dispatch, getState); | ||
@@ -223,0 +223,0 @@ return function (next) { |
@@ -1,1 +0,1 @@ | ||
!function(e,r){"object"==typeof exports&&"object"==typeof module?module.exports=r():"function"==typeof define&&define.amd?define([],r):"object"==typeof exports?exports.HorizonRedux=r():e.HorizonRedux=r()}(this,function(){return function(e){function r(n){if(t[n])return t[n].exports;var o=t[n]={exports:{},id:n,loaded:!1};return e[n].call(o.exports,o,o.exports,r),o.loaded=!0,o.exports}var t={};return r.m=e,r.c=t,r.p="",r(0)}([function(e,r,t){e.exports=t(4)},function(e,r){"use strict";function t(e,r){return"string"==typeof r.pattern&&r.pattern===e.type||Array.isArray(r.pattern)&&r.pattern.indexOf(e.type)!==-1||"function"==typeof r.pattern&&r.pattern(e)}function n(e,r,n){return function(o){r.forEach(function(r){if(t(o,r)){var u=r.observableQuery(e,o);if(r.successHandler||r.errorHandler){var i=u.subscribe(r.successHandler?function(e){return r.successHandler(e,o,n)}:void 0,r.errorHandler?function(e){return r.errorHandler(e,o,n)}:void 0);"takeEvery"===r.type?r.subscribers.push(i):"takeLatest"===r.type&&(r.subscribers.forEach(function(e){e.unsubscribe()}),r.subscribers=[i])}}})}}Object.defineProperty(r,"__esModule",{value:!0}),r.actionTakerMatchesAction=t,r.createActionHandler=n},function(e,r){"use strict";function t(e,r,t,n){var o=arguments.length>4&&void 0!==arguments[4]?arguments[4]:"takeEvery";if("string"!=typeof e&&!Array.isArray(e)&&"function"!=typeof e)throw Error("pattern must be a string, array of strings, or function");if("function"!=typeof r)throw Error("observableQuery must be a function that returns an observable horizon query");if(t&&"function"!=typeof t)throw Error("successHandler must be a function");if(n&&"function"!=typeof n)throw Error("errorHandler must be a function");return{pattern:e,observableQuery:r,successHandler:t,errorHandler:n,type:o,subscribers:[]}}function n(e,r){var t=function(){r.subscribers.forEach(function(e){e.unsubscribe()}),e.splice(e.indexOf(r),1)};return{remove:t}}Object.defineProperty(r,"__esModule",{value:!0}),r.createActionTaker=t,r.createActionTakerManager=n},function(e,r,t){"use strict";function n(e,r){return function(){return function(t){var n=t.dispatch,u=(0,o.createActionHandler)(e,r,n);return function(e){return function(r){return u(r),e(r)}}}}}Object.defineProperty(r,"__esModule",{value:!0}),r.default=n;var o=t(1)},function(e,r,t){"use strict";function n(e){return e&&e.__esModule?e:{"default":e}}function o(e){if("function"!=typeof e)throw Error("must pass HorizonRedux a Horizon.io client instance");var r=[],t=(0,i.default)(e,r),n=function(e,t,n,o,u){var i=(0,a.createActionTaker)(e,t,n,o,u);return r.push(i),(0,a.createActionTakerManager)(r,i)},o=function(e,r,t,o){return n(e,r,t,o,"takeEvery")},u=function(e,r,t,o){return n(e,r,t,o,"takeLatest")};return{createMiddleware:t,addActionTaker:n,takeEvery:o,takeLatest:u}}Object.defineProperty(r,"__esModule",{value:!0}),r.default=o;var u=t(3),i=n(u),a=t(2)}])}); | ||
!function(e,r){"object"==typeof exports&&"object"==typeof module?module.exports=r():"function"==typeof define&&define.amd?define([],r):"object"==typeof exports?exports.HorizonRedux=r():e.HorizonRedux=r()}(this,function(){return function(e){function r(n){if(t[n])return t[n].exports;var o=t[n]={exports:{},id:n,loaded:!1};return e[n].call(o.exports,o,o.exports,r),o.loaded=!0,o.exports}var t={};return r.m=e,r.c=t,r.p="",r(0)}([function(e,r,t){e.exports=t(4)},function(e,r){"use strict";function t(e,r){return"string"==typeof r.pattern&&r.pattern===e.type||Array.isArray(r.pattern)&&r.pattern.indexOf(e.type)!==-1||"function"==typeof r.pattern&&r.pattern(e)}function n(e,r,n,o){return function(u){r.forEach(function(r){if(t(u,r)){var i=r.observableQuery(e,u,o);if(r.successHandler||r.errorHandler){var a=i.subscribe(r.successHandler?function(e){return r.successHandler(e,u,n)}:void 0,r.errorHandler?function(e){return r.errorHandler(e,u,n)}:void 0);"takeEvery"===r.type?r.subscribers.push(a):"takeLatest"===r.type&&(r.subscribers.forEach(function(e){e.unsubscribe()}),r.subscribers=[a])}}})}}Object.defineProperty(r,"__esModule",{value:!0}),r.actionTakerMatchesAction=t,r.createActionHandler=n},function(e,r){"use strict";function t(e,r,t,n){var o=arguments.length>4&&void 0!==arguments[4]?arguments[4]:"takeEvery";if("string"!=typeof e&&!Array.isArray(e)&&"function"!=typeof e)throw Error("pattern must be a string, array of strings, or function");if("function"!=typeof r)throw Error("observableQuery must be a function that returns an observable horizon query");if(t&&"function"!=typeof t)throw Error("successHandler must be a function");if(n&&"function"!=typeof n)throw Error("errorHandler must be a function");return{pattern:e,observableQuery:r,successHandler:t,errorHandler:n,type:o,subscribers:[]}}function n(e,r){var t=function(){r.subscribers.forEach(function(e){e.unsubscribe()}),e.splice(e.indexOf(r),1)};return{remove:t}}Object.defineProperty(r,"__esModule",{value:!0}),r.createActionTaker=t,r.createActionTakerManager=n},function(e,r,t){"use strict";function n(e,r){return function(){return function(t){var n=t.dispatch,u=t.getState,i=(0,o.createActionHandler)(e,r,n,u);return function(e){return function(r){return i(r),e(r)}}}}}Object.defineProperty(r,"__esModule",{value:!0}),r.default=n;var o=t(1)},function(e,r,t){"use strict";function n(e){return e&&e.__esModule?e:{"default":e}}function o(e){if("function"!=typeof e)throw Error("must pass HorizonRedux a Horizon.io client instance");var r=[],t=(0,i.default)(e,r),n=function(e,t,n,o,u){var i=(0,a.createActionTaker)(e,t,n,o,u);return r.push(i),(0,a.createActionTakerManager)(r,i)},o=function(e,r,t,o){return n(e,r,t,o,"takeEvery")},u=function(e,r,t,o){return n(e,r,t,o,"takeLatest")};return{createMiddleware:t,addActionTaker:n,takeEvery:o,takeLatest:u}}Object.defineProperty(r,"__esModule",{value:!0}),r.default=o;var u=t(3),i=n(u),a=t(2)}])}); |
@@ -20,7 +20,7 @@ 'use strict'; | ||
function createActionHandler(hz, actionTakers, dispatch) { | ||
function createActionHandler(hz, actionTakers, dispatch, getState) { | ||
return function (action) { | ||
actionTakers.forEach(function (actionTaker) { | ||
if (actionTakerMatchesAction(action, actionTaker)) { | ||
var observable = actionTaker.observableQuery(hz, action); | ||
var observable = actionTaker.observableQuery(hz, action, getState); | ||
@@ -27,0 +27,0 @@ // if this actionTaker's query has a success or error handler, set up |
@@ -22,3 +22,3 @@ 'use strict'; | ||
// create the actionHandler | ||
var handleAction = (0, _actionHandler.createActionHandler)(hz, actionTakers, dispatch); | ||
var handleAction = (0, _actionHandler.createActionHandler)(hz, actionTakers, dispatch, getState); | ||
@@ -25,0 +25,0 @@ return function (next) { |
{ | ||
"name": "horizon-redux", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "A small library that helps you connect Horizon.io with Redux in a flexible, non-intrusive way.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -15,2 +15,4 @@ # horizon-redux | ||
_Interested in a different approach? See the [Alternative Approaches](#alternative-approaches) section below for some different options for integrating Horizon with Redux._ | ||
## Installation | ||
@@ -22,3 +24,3 @@ | ||
`<script src="https://npmcdn.com/horizon-redux/dist/horizon-redux.min.js"></script>` (exposes window.HorizonRedux as a global variable) | ||
`<script src="https://unpkg.com/horizon-redux/dist/horizon-redux.min.js"></script>` (exposes window.HorizonRedux as a global variable) | ||
@@ -42,3 +44,3 @@ ## Usage | ||
'WATCH_MESSAGES', | ||
(horizon, action) => horizon('messages').order('datetime', 'descending').limit(action.payload).watch(), | ||
(horizon, action, getState) => horizon('messages').order('datetime', 'descending').limit(action.payload).watch(), | ||
(result, action, dispatch) => dispatch({type: 'NEW_MESSAGES', payload: result}), | ||
@@ -100,3 +102,3 @@ (err, action, dispatch) => console.log('failed to load messages:', err) | ||
* If it's a function, matches if pattern(action) returns a truthy value | ||
2. `observableQuery` - A function that takes a Horizon client instance and an action, and returns a Horizon query. The query must be an "observable" type (`fetch()`, `watch()`, `store()`, `upsert()`, `insert()`, `replace()`, `update()`, `remove()`, or `removeAll()`). Do not call the `subscribe()` method on the query here - HorizonRedux takes care of that automatically. | ||
2. `observableQuery` - A function that takes a Horizon client instance, an action, and your Redux store's `getState` method, and returns a Horizon query. The query must be an "observable" type (`fetch()`, `watch()`, `store()`, `upsert()`, `insert()`, `replace()`, `update()`, `remove()`, or `removeAll()`). Do not call the `subscribe()` method on the query here - HorizonRedux takes care of that automatically. | ||
3. `successHandler` (optional) - A function that takes result (the result of the query), action (the action associated with that query) and the Redux store's dispatch method. You can handle the successful query however you'd like - usually by dispatching another action with the results. | ||
@@ -123,3 +125,3 @@ 4. `errorHandler` (optional) - A function that takes the error, action (the action associated with that query) and the Redux store's dispatch method. You can handle an error scenario however you'd like. | ||
'WATCH_MESSAGES', | ||
(horizon, action) => | ||
(horizon, action, getState) => | ||
horizon('messages').order('datetime', 'descending').limit(action.payload.limit || 10).watch(), | ||
@@ -154,3 +156,3 @@ (result, action, dispatch) => { | ||
'WATCH_MESSAGES', | ||
(horizon, action) => | ||
(horizon, action, getState) => | ||
horizon('messages').order('datetime', 'descending').limit(action.payload.limit || 10).watch(), | ||
@@ -189,3 +191,3 @@ (result, action, dispatch) => { | ||
'ADD_MESSAGE_REQUEST', | ||
(horizon, action) => horizon('messages').store(action.payload), | ||
(horizon, action, getState) => horizon('messages').store(action.payload), | ||
(id, action, dispatch) => dispatch(addMessageSuccess(id, action.payload)), | ||
@@ -202,5 +204,5 @@ (err, action, dispatch) => dispatch(addMessageFailure(err, action.payload)) | ||
1. [redux-saga](https://github.com/yelouafi/redux-saga) is a great option if you find that you need more power than horizon-redux offers. redux-saga is a much bigger library with a larger API. With this approach, you'll likely end up writing more code than you would with horizon-redux, but it may be necessary for more complex apps. [Check out an example app using Horizon.io with redux-saga](https://github.com/shanecav/horizon-redux-saga). | ||
1. [redux-observable](https://redux-observable.js.org/) is honestly a more elegant approach. If you aren't interested in learning RxJS, then horizon-redux will work fine, but redux-observable is a great library made by smart people (and it's worth learning RxJS if you're using Horizon). Because most Horizon.io collection methods return RxJS Observables, using redux-observable should be pretty easy to integrate. | ||
2. If you're already comfortable with RxJS, then [redux-observable](https://redux-observable.js.org/) may feel more natural to you than horizon-redux. Because most Horizon.io collection methods return RxJS Observables, using redux-observable should be pretty easy to integrate. | ||
2. [redux-saga](https://github.com/yelouafi/redux-saga) is a great option if you find that you need more power than horizon-redux offers. redux-saga is a much bigger library with a larger API. With this approach, you'll likely end up writing more code than you would with horizon-redux, but it may be necessary for more complex apps. [Check out an example app using Horizon.io with redux-saga](https://github.com/shanecav/horizon-redux-saga). | ||
@@ -207,0 +209,0 @@ ## License |
// @flow | ||
import type { HorizonInstance } from '../types/horizon' | ||
import type { Action, Dispatch } from '../types/redux' | ||
import type { Action, Dispatch, GetState } from '../types/redux' | ||
import type { | ||
@@ -35,3 +35,4 @@ ActionTaker, | ||
actionTakers: ActionTakers, | ||
dispatch: Dispatch | ||
dispatch: Dispatch, | ||
getState: GetState | ||
) { | ||
@@ -41,3 +42,3 @@ return (action: Action): void => { | ||
if (actionTakerMatchesAction(action, actionTaker)) { | ||
const observable = actionTaker.observableQuery(hz, action) | ||
const observable = actionTaker.observableQuery(hz, action, getState) | ||
@@ -44,0 +45,0 @@ // if this actionTaker's query has a success or error handler, set up |
@@ -19,3 +19,3 @@ // @flow | ||
// create the actionHandler | ||
const handleAction = createActionHandler(hz, actionTakers, dispatch) | ||
const handleAction = createActionHandler(hz, actionTakers, dispatch, getState) | ||
@@ -22,0 +22,0 @@ return (next: Dispatch) => (action: Action) => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
44314
617
204
0