redux-beacon
Advanced tools
Comparing version 2.0.0-rc.2 to 2.0.0
@@ -1,6 +0,6 @@ | ||
import { EventsMap, Extensions, Target } from './types'; | ||
import { EventsMap, EventsMapper, Extensions, Target } from './types'; | ||
/** | ||
* Create a meta reducer that synchronizes actions to analytics events. | ||
*/ | ||
declare function createMetaReducer(eventsMap: EventsMap, target: Target, extensions?: Extensions): (reducer: any) => (prevState: any, action: any) => any; | ||
declare function createMetaReducer(eventsMap: EventsMap | EventsMapper, target: Target, extensions?: Extensions): (reducer: any) => (prevState: any, action: any) => any; | ||
export default createMetaReducer; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var flatten = require("array-flatten"); | ||
var create_events_1 = require("./create-events"); | ||
@@ -11,2 +12,5 @@ var get_events_with_matching_key_1 = require("./get-events-with-matching-key"); | ||
if (extensions === void 0) { extensions = {}; } | ||
var getEvents = typeof eventsMap === 'function' | ||
? function (action) { return flatten([eventsMap(action)]); } | ||
: function (action) { return get_events_with_matching_key_1.default(eventsMap, action.type); }; | ||
/* Why not arrow functions? AOT... */ | ||
@@ -17,3 +21,3 @@ /* tslint:disable: only-arrow-functions */ | ||
var nextState = reducer(prevState, action); | ||
var events = create_events_1.default(get_events_with_matching_key_1.default(eventsMap, action.type), prevState, action, nextState); | ||
var events = create_events_1.default(getEvents(action), prevState, action, nextState); | ||
register_events_1.default(events, target, extensions, prevState, action, nextState); | ||
@@ -20,0 +24,0 @@ return nextState; |
@@ -1,6 +0,6 @@ | ||
import { EventsMap, Extensions, Target } from './types'; | ||
import { EventsMap, EventsMapper, Extensions, Target } from './types'; | ||
/** | ||
* Create Redux middleware that synchronizes actions to analytics events. | ||
*/ | ||
declare function createMiddleware(eventsMap: EventsMap, target: Target, extensions?: Extensions): (store: any) => (next: any) => (action: any) => any; | ||
declare function createMiddleware(eventsMap: EventsMap | EventsMapper, target: Target, extensions?: Extensions): (store: any) => (next: any) => (action: any) => any; | ||
export default createMiddleware; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var flatten = require("array-flatten"); | ||
var create_events_1 = require("./create-events"); | ||
@@ -11,2 +12,5 @@ var get_events_with_matching_key_1 = require("./get-events-with-matching-key"); | ||
if (extensions === void 0) { extensions = {}; } | ||
var getEvents = typeof eventsMap === 'function' | ||
? function (action) { return flatten([eventsMap(action)]); } | ||
: function (action) { return get_events_with_matching_key_1.default(eventsMap, action.type); }; | ||
return function (store) { return function (next) { return function (action) { | ||
@@ -16,3 +20,3 @@ var prevState = store.getState(); | ||
var nextState = store.getState(); | ||
var events = create_events_1.default(get_events_with_matching_key_1.default(eventsMap, action.type), prevState, action, nextState); | ||
var events = create_events_1.default(getEvents(action), prevState, action, nextState); | ||
register_events_1.default(events, target, extensions, prevState, action, nextState); | ||
@@ -19,0 +23,0 @@ return result; |
@@ -5,2 +5,2 @@ import createEvents from './create-events'; | ||
export { createMiddleware, createMetaReducer, createEvents }; | ||
export { EventDefinition, EventsMap, LoggerExtension, OfflineStorageExtension, Target, ConnectivitySelector, PurgedEventsHandler } from './types'; | ||
export { EventDefinition, EventsMap, EventsMapper, LoggerExtension, OfflineStorageExtension, Target, ConnectivitySelector, PurgedEventsHandler } from './types'; |
@@ -11,2 +11,5 @@ export declare type Target = (events: any[]) => void; | ||
}, prevState: any, nextState: any) => E; | ||
export declare type EventsMapper = (action: { | ||
[key: string]: any; | ||
}) => EventDefinition | EventDefinition[]; | ||
/** | ||
@@ -13,0 +16,0 @@ * A map between your actions and your analytics events. Each key |
{ | ||
"name": "redux-beacon", | ||
"version": "2.0.0-rc.2", | ||
"version": "2.0.0", | ||
"description": "Analytics integration for Redux and ngrx/store", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -88,2 +88,39 @@ import { applyMiddleware, createStore } from 'redux'; | ||
}); | ||
describe('When given an eventsMapper instead of an eventsMap', () => { | ||
it('correctly maps dispatched actions to event definitions', () => { | ||
const eventsMapper = action => { | ||
switch (action.type) { | ||
case 'ACTION_A': | ||
return [ | ||
() => ({ event: 'ACTION_A_EVENT_1' }), | ||
() => ({ event: 'ACTION_A_EVENT_2' }), | ||
]; | ||
case 'ACTION_C': | ||
return () => ({ event: 'ACTION_C_EVENT' }); | ||
default: | ||
return []; | ||
} | ||
}; | ||
const target = jest.fn(); | ||
const reducer = state => state; | ||
const store = prepareStore(reducer, eventsMapper, target); | ||
// dispatch an action with an associated event definition: | ||
store.dispatch({ type: 'ACTION_A' }); | ||
// dispatch an action with no associated event definition: | ||
store.dispatch({ type: 'ACTION_B' }); | ||
// dispatch an action with an associated event definition: | ||
store.dispatch({ type: 'ACTION_C' }); | ||
expect(target).toHaveBeenCalledWith([ | ||
{ event: 'ACTION_A_EVENT_1' }, | ||
{ event: 'ACTION_A_EVENT_2' }, | ||
]); | ||
expect(target).toHaveBeenCalledWith([{ event: 'ACTION_C_EVENT' }]); | ||
}); | ||
}); | ||
}); | ||
@@ -93,11 +130,14 @@ } | ||
runIntegrationTests( | ||
'createMiddleware(eventsMap, target, extensions?)', | ||
(reducer, eventsMap, target) => | ||
createStore(reducer, applyMiddleware(createMiddleware(eventsMap, target))) | ||
'createMiddleware(eventsMap | eventsMapper, target, extensions?)', | ||
(reducer, eventsMapOrMapper, target) => | ||
createStore( | ||
reducer, | ||
applyMiddleware(createMiddleware(eventsMapOrMapper, target)) | ||
) | ||
); | ||
runIntegrationTests( | ||
'createMetaReducer(eventsMap, target, extensions?)', | ||
'createMetaReducer(eventsMap | eventsMapper, target, extensions?)', | ||
(reducer, eventsMap, target) => | ||
createStore(createMetaReducer(eventsMap, target)(reducer)) | ||
); |
@@ -0,5 +1,13 @@ | ||
import * as flatten from 'array-flatten'; | ||
import createEvents from './create-events'; | ||
import getEventsWithMatchingKey from './get-events-with-matching-key'; | ||
import registerEvents from './register-events'; | ||
import { EventsMap, Extensions, Target } from './types'; | ||
import { | ||
EventDefinition, | ||
EventsMap, | ||
EventsMapper, | ||
Extensions, | ||
Target, | ||
} from './types'; | ||
@@ -10,6 +18,11 @@ /** | ||
function createMetaReducer( | ||
eventsMap: EventsMap, | ||
eventsMap: EventsMap | EventsMapper, | ||
target: Target, | ||
extensions: Extensions = {} | ||
) { | ||
const getEvents = | ||
typeof eventsMap === 'function' | ||
? action => flatten<EventDefinition>([eventsMap(action)]) | ||
: action => getEventsWithMatchingKey(eventsMap, action.type); | ||
/* Why not arrow functions? AOT... */ | ||
@@ -20,4 +33,5 @@ /* tslint:disable: only-arrow-functions */ | ||
const nextState = reducer(prevState, action); | ||
const events = createEvents( | ||
getEventsWithMatchingKey(eventsMap, action.type), | ||
getEvents(action), | ||
prevState, | ||
@@ -24,0 +38,0 @@ action, |
@@ -0,5 +1,13 @@ | ||
import * as flatten from 'array-flatten'; | ||
import createEvents from './create-events'; | ||
import getEventsWithMatchingKey from './get-events-with-matching-key'; | ||
import registerEvents from './register-events'; | ||
import { EventsMap, Extensions, Target } from './types'; | ||
import { | ||
EventDefinition, | ||
EventsMap, | ||
EventsMapper, | ||
Extensions, | ||
Target, | ||
} from './types'; | ||
@@ -10,6 +18,11 @@ /** | ||
function createMiddleware( | ||
eventsMap: EventsMap, | ||
eventsMap: EventsMap | EventsMapper, | ||
target: Target, | ||
extensions: Extensions = {} | ||
) { | ||
const getEvents = | ||
typeof eventsMap === 'function' | ||
? action => flatten<EventDefinition>([eventsMap(action)]) | ||
: action => getEventsWithMatchingKey(eventsMap, action.type); | ||
return store => next => action => { | ||
@@ -21,3 +34,3 @@ const prevState = store.getState(); | ||
const events = createEvents( | ||
getEventsWithMatchingKey(eventsMap, action.type), | ||
getEvents(action), | ||
prevState, | ||
@@ -24,0 +37,0 @@ action, |
@@ -10,2 +10,3 @@ import createEvents from './create-events'; | ||
EventsMap, | ||
EventsMapper, | ||
LoggerExtension, | ||
@@ -12,0 +13,0 @@ OfflineStorageExtension, |
@@ -15,2 +15,6 @@ export type Target = (events: any[]) => void; | ||
export type EventsMapper = ( | ||
action: { [key: string]: any } | ||
) => EventDefinition | EventDefinition[]; | ||
/** | ||
@@ -17,0 +21,0 @@ * A map between your actions and your analytics events. Each key |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
44114
1286
1