UNPKG

1.92 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = BindHandlers;
7
8var _decorators = require('alt-utils/lib/decorators');
9
10function BindHandlers() {
11 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
12 args[_key] = arguments[_key];
13 }
14
15 var definitions = args.reduce(function (result, def) {
16 if (Array.isArray(def)) {
17 def.forEach(function (d) {
18 return result.push(d);
19 });
20 } else {
21 result.push(def);
22 }
23 return result;
24 }, []);
25 return function decorateStore(StoreClass) {
26 definitions.forEach(function bindActions(_ref, i) {
27 var handler = _ref.handler,
28 bindings = _ref.bindings;
29
30 if (!handler || !handler.prototype) {
31 throw new Error('Invalid action handler');
32 }
33
34 var name = '$' + i + '_' + handler.prototype.constructor.name;
35
36 var methodNames = Object.keys(bindings);
37
38 methodNames.forEach(function bindAction(methodName) {
39 if (typeof bindings[methodName] !== 'function') {
40 throw new Error('bindings.' + methodName + ' is not a function (handler: ' + name + ')');
41 }
42 if (typeof handler.prototype[methodName] !== 'function') {
43 throw new Error(name + '.' + methodName + ' is not a function');
44 }
45 var storeMethodName = name + '_' + methodName;
46
47 StoreClass.prototype[storeMethodName] = handler.prototype[methodName];
48
49 var applyBinding = (0, _decorators.bind)(bindings[methodName]);
50 applyBinding(StoreClass, storeMethodName, Object.getOwnPropertyDescriptor(StoreClass.prototype, storeMethodName));
51 });
52 });
53 return StoreClass;
54 };
55}
\No newline at end of file