-
Notifications
You must be signed in to change notification settings - Fork 0
/
marsfarm-polymer-redux.js
228 lines (205 loc) · 5.98 KB
/
marsfarm-polymer-redux.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import { get, set } from "@polymer/polymer/lib/utils/path.js";
/**
* This is taken from https://github.com/tur-nr/polymer-redux/blob/master/src/index.js
* LitElement doesn't use propertyEffects, so we've modified it to directly map
* state changes in redux to our components.
*/
// Expose globals
const { CustomEvent } = window;
/**
* Polymer Redux
*
* Creates a Class mixin for decorating Elements with a given Redux store.
*
* @polymerMixin
*
* @param {Object} store Redux store.
* @return {Function} Class mixin.
*/
export default function PolymerRedux(store) {
if (!store) {
throw new TypeError("PolymerRedux: expecting a redux store.");
} else if (
!["getState", "dispatch", "subscribe"].every(
k => typeof store[k] === "function"
)
) {
throw new TypeError("PolymerRedux: invalid store object.");
}
const subscribers = new Map();
/**
* Binds element's properties to state changes from the Redux store.
*
* @example
* const update = bind(el, props) // set bindings
* update(state) // manual update
*
* @private
* @param {HTMLElement} element
* @param {Object} properties
* @return {Function} Update function.
*/
const bind = (element, properties) => {
const bindings = Object.keys(properties).filter(name => {
const property = properties[name];
if (Object.prototype.hasOwnProperty.call(property, "statePath")) {
if (!property.readOnly && property.notify) {
console.warn(
`PolymerRedux: <${
element.constructor.is
}>.${name} has "notify" enabled, two-way bindings goes against Redux's paradigm`
);
}
return true;
}
return false;
});
/**
* Updates an element's properties with the given state.
*
* @private
* @param {Object} state
*/
const update = state => {
let propertiesChanged = false;
bindings.forEach(name => {
// Perhaps .reduce() to a boolean?
const { statePath } = properties[name];
const value =
typeof statePath === "function"
? statePath.call(element, state)
: get(state, statePath); // get imported from polymer utils
propertiesChanged = set(element, name, value);
return propertiesChanged;
});
};
// Redux listener
const unsubscribe = store.subscribe(() => {
const detail = store.getState();
update(detail);
element.dispatchEvent(new CustomEvent("state-changed", { detail }));
});
subscribers.set(element, unsubscribe);
update(store.getState());
return update;
};
/**
* Unbinds an element from state changes in the Redux store.
*
* @private
* @param {HTMLElement} element
*/
const unbind = element => {
const off = subscribers.get(element);
if (typeof off === "function") {
off();
}
};
/**
* Merges a property's object value using the defaults way.
*
* @private
* @param {Object} what Initial prototype
* @param {String} which Property to collect.
* @return {Object} the collected values
*/
const collect = (what, which) => {
let res = {};
while (what) {
res = Object.assign({}, what[which], res); // Respect prototype priority
what = Object.getPrototypeOf(what);
}
return res;
};
/**
* ReduxMixin
*
* @example
* const ReduxMixin = PolymerRedux(store)
* class Foo extends ReduxMixin(Polymer.Element) { }
*
* @polymerMixinClass
*
* @param {Polymer.Element} parent The polymer parent element.
* @return {Function} PolymerRedux mixed class.
*/
return parent =>
class ReduxMixin extends parent {
constructor() {
super();
// Collect the action creators first as property changes trigger
// dispatches from observers, see #65, #66, #67
const actions = collect(this.constructor, "actions");
Object.defineProperty(this, "_reduxActions", {
configurable: true,
value: actions
});
}
connectedCallback() {
const properties = collect(this.constructor, "properties");
bind(this, properties);
super.connectedCallback();
}
disconnectedCallback() {
unbind(this);
super.disconnectedCallback();
}
/**
* Dispatches an action to the Redux store.
*
* @example
* element.dispatch({ type: 'ACTION' })
*
* @example
* element.dispatch('actionCreator', 'foo', 'bar')
*
* @example
* element.dispatch((dispatch) => {
* dispatch({ type: 'MIDDLEWARE'})
* })
*
* @param {...*} args
* @return {Object} The action.
*/
dispatch(...args) {
const actions = this._reduxActions;
// Action creator
let [action] = args;
if (typeof action === "string") {
if (typeof actions[action] !== "function") {
throw new TypeError(
`PolymerRedux: <${
this.constructor.is
}> invalid action creator "${action}"`
);
}
action = actions[action](...args.slice(1));
}
// Proxy async dispatch
if (typeof action === "function") {
const originalAction = action;
action = (...args) => {
// Replace redux dispatch
args.splice(0, 1, (...args) => {
return this.dispatch(...args);
});
return originalAction(...args);
};
// Copy props from the original action to the proxy.
// see https://github.com/tur-nr/polymer-redux/issues/98
Object.keys(originalAction).forEach(prop => {
action[prop] = originalAction[prop];
});
}
return store.dispatch(action);
}
/**
* Gets the current state in the Redux store.
*
* @return {*}
*/
getState() {
return store.getState();
}
};
}