-
Notifications
You must be signed in to change notification settings - Fork 1
/
redux-connect.js
75 lines (60 loc) · 2.21 KB
/
redux-connect.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
Name: Redux.connect
version: 0.1.0
Description: connect domchanger components to your Redux reducers actions and state via their props with auto updating.
Author: Michael Glazer
Link: https://github.com/magnumjs/domchanger-redux-connect
Copyright (c) 2016
License MIT
Example: http://embed.plnkr.co/quIf8csaG61DZ3Qf1DSU/
*/
"use strict";
(function(Redux) {
Redux.connect = function(component) {
var orig = Object.assign({}, component);
var filterPropsState = function(store, types) {
var state = store.getState();
var actions = {};
Object.keys(types).forEach(function(type) {
//Convention of naming action types for props access
//ADD-TODOS becomes props.addTodos each dash represents a camelCase
var camelCased = type.toLowerCase().replace(/-([a-z|A-Z])/g, function(g) {
return g[1].toUpperCase();
});
actions[camelCased] = function() {
store.dispatch(Object.assign({}, {
type: type
}, arguments))
};
});
return Object.assign({}, actions, state);
};
return function(params) {
var linked = params.link;
var reducers = params.reducers && typeof params.reducers != 'function' ? Redux.combineReducers(params.reducers) : params.reducers;
var middleware = params.middleware || [];
var initState = params.initState;
var types = linked ? linked.storeTypes : params.actionTypes || {};
var store = linked ? linked.store : Redux.createStore(reducers, initState, Redux.applyMiddleware.apply({}, middleware));
var unsubscribe = store.subscribe(function() {
component.update(store.getState());
});
component.store = store;
component.storeTypes = types;
component.destroy = function() {
unsubscribe();
orig.destroy();
};
var prev;
component.update = function(props) {
//Comparsion check to avoid re-running unnecessarily
if (JSON.stringify(props) !== prev) {
orig.update(Object.assign(props || {}, filterPropsState(store, types)))
}
prev = JSON.stringify(props);
return this;
};
return component;
}
};
}(window.Redux = window.Redux || {}));