UNPKG

3.12 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
8
9function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
10
11/**
12 * Used to store context-agnostic dependencies, object instances
13 * (services, router, etc.) and data (config, etc.).
14 *
15 * @CLIENT_SERVER
16 */
17var Provider = function () {
18
19 /**
20 * @param {Object} config
21 * @param {ReverseRouter} router
22 * @param {boolean} isServerInstance
23 */
24
25
26 /**
27 * @type {{[key: string]: any}}
28 */
29
30
31 /**
32 * @type {Object}
33 */
34 function Provider(config, router, isServerInstance) {
35 _classCallCheck(this, Provider);
36
37 this._config = config;
38 this._router = router;
39 this._cache = {};
40 this._factories = {};
41 this._isServerInstance = isServerInstance;
42 }
43
44 /**
45 * @param {Function} factory
46 */
47
48
49 /**
50 * @type {boolean}
51 */
52
53
54 /**
55 * @type {{[key: string}: Function}}
56 */
57
58 /**
59 * @type {ReverseRouter}
60 */
61
62
63 _createClass(Provider, [{
64 key: 'provide',
65 value: function provide(factory) {
66 var name = factory.factoryName;
67 if (typeof name != 'string') {
68 throw new Error('Factory registered doesn\'t have a name: ' + factory + '.');
69 }
70 if (this._factories.hasOwnProperty(name)) {
71 if (this._factories[name] == factory) {
72 // Return cache if the factory is the same.
73 return this._cache[name];
74 }
75 if (this._isServerInstance) {
76 // Disallow replacement of factories at server side. The same Provider
77 // instance is shared with plenty of requests at server side. We don't
78 // want anything fishy to happen.
79 throw new Error('Factory name clash! The name \'' + name + '\' is claimed by two different factories!');
80 }
81 }
82
83 // Allow replacement of factories if at client side. This comes in handy if
84 // the user is hot reloading factories. Since new page instances are created
85 // per request anyway, this doesn't matter in client.
86 this._factories[name] = factory;
87 this._cache = factory(this._config);
88 }
89
90 /**
91 * @returns {ReverseRouter}
92 */
93
94 }, {
95 key: 'getRouter',
96 value: function getRouter() {
97 return this._router;
98 }
99
100 /**
101 * @returns {Object}
102 */
103
104 }, {
105 key: 'getConfig',
106 value: function getConfig() {
107 return this._config;
108 }
109 }]);
110
111 return Provider;
112}();
113
114exports.default = Provider;
115//# sourceMappingURL=Provider.js.map
\No newline at end of file