summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/catapult/third_party/polymer2/bower_components/polymer-redux/dist/polymer-redux.html
blob: fd592d46a0d639ee8ab37d0119f9ea181d17c810 (plain)
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<script type="text/javascript">var PolymerRedux = (function () {
'use strict';

var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};

var win;

if (typeof window !== "undefined") {
    win = window;
} else if (typeof commonjsGlobal !== "undefined") {
    win = commonjsGlobal;
} else if (typeof self !== "undefined") {
    win = self;
} else {
    win = {};
}

var window_1 = win;

var console_1 = console;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

// Expose globals
var CustomEvent = window_1.CustomEvent;
var Polymer = window_1.Polymer;

/**
 * Polymer Redux
 *
 * Creates a Class mixin for decorating Elements with a given Redux store.
 *
 * @polymerMixin
 *
 * @param {Object} store Redux store.
 * @return {Function} Class mixin.
 */

function PolymerRedux(store) {
	if (!store) {
		throw new TypeError('PolymerRedux: expecting a redux store.');
	} else if (!['getState', 'dispatch', 'subscribe'].every(function (k) {
		return typeof store[k] === 'function';
	})) {
		throw new TypeError('PolymerRedux: invalid store object.');
	}

	var 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.
  */
	var bind = function bind(element, properties) {
		var bindings = Object.keys(properties).filter(function (name) {
			var property = properties[name];
			if (Object.prototype.hasOwnProperty.call(property, 'statePath')) {
				if (!property.readOnly && property.notify) {
					console_1.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
   */
		var update = function update(state) {
			var propertiesChanged = false;
			bindings.forEach(function (name) {
				// Perhaps .reduce() to a boolean?
				var statePath = properties[name].statePath;

				var value = typeof statePath === 'function' ? statePath.call(element, state) : Polymer.Path.get(state, statePath);

				var changed = element._setPendingPropertyOrPath(name, value, true);
				propertiesChanged = propertiesChanged || changed;
			});
			if (propertiesChanged) {
				element._invalidateProperties();
			}
		};

		// Redux listener
		var unsubscribe = store.subscribe(function () {
			var detail = store.getState();
			update(detail);

			element.dispatchEvent(new CustomEvent('state-changed', { detail: detail }));
		});

		subscribers.set(element, unsubscribe);
		update(store.getState());

		return update;
	};

	/**
  * Unbinds an element from state changes in the Redux store.
  *
  * @private
  * @param {HTMLElement} element
  */
	var unbind = function unbind(element) {
		var 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
  */
	var collect = function collect(what, which) {
		var res = {};
		while (what) {
			res = _extends({}, 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 function (parent) {
		return class ReduxMixin extends parent {
			constructor() {
				super();

				// Collect the action creators first as property changes trigger
				// dispatches from observers, see #65, #66, #67
				var actions = collect(this.constructor, 'actions');
				Object.defineProperty(this, '_reduxActions', {
					configurable: true,
					value: actions
				});
			}

			connectedCallback() {
				var 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() {
				var _this = this;

				for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
					args[_key] = arguments[_key];
				}

				var actions = this._reduxActions;

				// Action creator
				var action = args[0];

				if (typeof action === 'string') {
					if (typeof actions[action] !== 'function') {
						throw new TypeError('PolymerRedux: <' + this.constructor.is + '> invalid action creator "' + action + '"');
					}
					action = actions[action].apply(actions, _toConsumableArray(args.slice(1)));
				}

				// Proxy async dispatch
				if (typeof action === 'function') {
					var originalAction = action;
					action = function action() {
						for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
							args[_key2] = arguments[_key2];
						}

						// Replace redux dispatch
						args.splice(0, 1, function () {
							return _this.dispatch.apply(_this, arguments);
						});
						return originalAction.apply(undefined, args);
					};

					// Copy props from the original action to the proxy.
					// see https://github.com/tur-nr/polymer-redux/issues/98
					Object.keys(originalAction).forEach(function (prop) {
						action[prop] = originalAction[prop];
					});
				}

				return store.dispatch(action);
			}

			/**
    * Gets the current state in the Redux store.
    *
    * @return {*}
    */
			getState() {
				return store.getState();
			}
		};
	};
}

return PolymerRedux;

}());
</script>