1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.factory = factory;
|
7 | exports.sortFactories = sortFactories;
|
8 | exports.create = create;
|
9 | exports.isFactory = isFactory;
|
10 | exports.assertDependencies = assertDependencies;
|
11 | exports.isOptionalDependency = isOptionalDependency;
|
12 | exports.stripOptionalNotation = stripOptionalNotation;
|
13 |
|
14 | var _array = require("./array.js");
|
15 |
|
16 | var _object = require("./object.js");
|
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 | function factory(name, dependencies, create, meta) {
|
43 | function assertAndCreate(scope) {
|
44 |
|
45 |
|
46 |
|
47 | var deps = (0, _object.pickShallow)(scope, dependencies.map(stripOptionalNotation));
|
48 | assertDependencies(name, dependencies, scope);
|
49 | return create(deps);
|
50 | }
|
51 |
|
52 | assertAndCreate.isFactory = true;
|
53 | assertAndCreate.fn = name;
|
54 | assertAndCreate.dependencies = dependencies.slice().sort();
|
55 |
|
56 | if (meta) {
|
57 | assertAndCreate.meta = meta;
|
58 | }
|
59 |
|
60 | return assertAndCreate;
|
61 | }
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | function sortFactories(factories) {
|
71 | var factoriesByName = {};
|
72 | factories.forEach(function (factory) {
|
73 | factoriesByName[factory.fn] = factory;
|
74 | });
|
75 |
|
76 | function containsDependency(factory, dependency) {
|
77 |
|
78 | if (isFactory(factory)) {
|
79 | if ((0, _array.contains)(factory.dependencies, dependency.fn || dependency.name)) {
|
80 | return true;
|
81 | }
|
82 |
|
83 | if (factory.dependencies.some(function (d) {
|
84 | return containsDependency(factoriesByName[d], dependency);
|
85 | })) {
|
86 | return true;
|
87 | }
|
88 | }
|
89 |
|
90 | return false;
|
91 | }
|
92 |
|
93 | var sorted = [];
|
94 |
|
95 | function addFactory(factory) {
|
96 | var index = 0;
|
97 |
|
98 | while (index < sorted.length && !containsDependency(sorted[index], factory)) {
|
99 | index++;
|
100 | }
|
101 |
|
102 | sorted.splice(index, 0, factory);
|
103 | }
|
104 |
|
105 |
|
106 | factories.filter(isFactory).forEach(addFactory);
|
107 |
|
108 | factories.filter(function (factory) {
|
109 | return !isFactory(factory);
|
110 | }).forEach(addFactory);
|
111 | return sorted;
|
112 | }
|
113 |
|
114 |
|
115 | function create(factories) {
|
116 | var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
117 | sortFactories(factories).forEach(function (factory) {
|
118 | return factory(scope);
|
119 | });
|
120 | return scope;
|
121 | }
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | function isFactory(obj) {
|
131 | return typeof obj === 'function' && typeof obj.fn === 'string' && Array.isArray(obj.dependencies);
|
132 | }
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | function assertDependencies(name, dependencies, scope) {
|
145 | var allDefined = dependencies.filter(function (dependency) {
|
146 | return !isOptionalDependency(dependency);
|
147 | })
|
148 | .every(function (dependency) {
|
149 | return scope[dependency] !== undefined;
|
150 | });
|
151 |
|
152 | if (!allDefined) {
|
153 | var missingDependencies = dependencies.filter(function (dependency) {
|
154 | return scope[dependency] === undefined;
|
155 | });
|
156 |
|
157 | throw new Error("Cannot create function \"".concat(name, "\", ") + "some dependencies are missing: ".concat(missingDependencies.map(function (d) {
|
158 | return "\"".concat(d, "\"");
|
159 | }).join(', '), "."));
|
160 | }
|
161 | }
|
162 |
|
163 | function isOptionalDependency(dependency) {
|
164 | return dependency && dependency[0] === '?';
|
165 | }
|
166 |
|
167 | function stripOptionalNotation(dependency) {
|
168 | return dependency && dependency[0] === '?' ? dependency.slice(1) : dependency;
|
169 | } |
\ | No newline at end of file |