UNPKG

7.37 kBJavaScriptView Raw
1let callResource = (resource, callback) => {
2 //callback返回false则 暂停剩下的
3 let returnData;
4 let setData = data => {
5 returnData = data;
6 };
7 if (typeof resource === "function") {
8 callback(setData, resource);
9 return returnData;
10 } else if (Array.isArray(resource)) {
11 let lg = resource.length,
12 i = 0;
13 let out = null;
14 for (; i < lg; i += 1) {
15 out = callback(setData, resource[i], i, lg);
16 if (out === "break") {
17 return returnData;
18 }
19 }
20 } else if (typeof resource === "object") {
21 let key = null,
22 out = null;
23 let keys = Object.keys(resource);
24 let lg = keys.length;
25 for (key of keys) {
26 out = callback(resource[key], key, lg);
27 if (out === "break") {
28 return returnData;
29 }
30 }
31 }
32};
33
34let switchResource = (resource, callback) => {
35 //callback返回true表示相关资源找到并且运行,结束
36 let returnData;
37 let setData = data => {
38 returnData = data;
39 };
40 if (typeof resource === "function") {
41 callback(setData, resource);
42 return returnData;
43 } else if (Array.isArray(resource)) {
44 let lg = resource.length,
45 i = 0;
46 let out = null;
47 for (; i < lg; i += 1) {
48 out = callback(setData, resource[i], i, lg);
49 if (out === "break") {
50 return returnData;
51 }
52 }
53 } else if (typeof resource === "object") {
54 let key = null,
55 out = null;
56 let keys = Object.keys(resource);
57 let lg = keys.length;
58 for (key of keys) {
59 out = callback(setData, resource[key], key, lg);
60 if (out === "break") {
61 return returnData;
62 }
63 }
64 }
65};
66
67let nextArray = (name, arr) => {
68 let ind = arr.indexOf(name);
69 if (ind === -1) {
70 return false;
71 }
72 let nextInd = ind + 1;
73 if (nextInd >= arr.length) {
74 return false;
75 }
76 return arr[nextInd];
77};
78
79class Branch {
80 constructor() {
81 this.storeFunction = {};
82 this.map = [];
83 this.state = {
84 switchName: null,
85 switchType: null,
86 sn: 0,
87 store: {},
88 firstRun: null
89 };
90 }
91 done(resolve) {
92 this.done = lastData => {
93 let store = this.state.store;
94 (this.map = []), (this.storeFunction = {});
95 this.state = {
96 switchName: null,
97 switchType: null,
98 sn: 0,
99 store: {},
100 firstRun: null
101 };
102 resolve({ store, data: lastData });
103 };
104 return this;
105 }
106 fail(reject) {
107 this.fail = err => {
108 (this.map = []), (this.storeFunction = {});
109 this.state = {
110 switchName: null,
111 switchType: null,
112 sn: 0,
113 store: {},
114 firstRun: null
115 };
116 reject(err);
117 };
118 return this;
119 }
120 jump(name, data) {
121 let callback = this.storeFunction[name];
122 callback(data);
123 }
124 switchNext(name, type = "switch") {
125 //分为loop,switch
126 this.state.switchName = name;
127 this.state.switchType = type;
128 }
129 process({ callback, props = [], name }) {
130 let ts = this;
131 let state = this.state;
132 if (!name || this.map.indexOf(name) !== -1) {
133 name = state.sn += 1;
134 }
135 this.map.push(name);
136 let newCallback = data => {
137 let { store, switchName, switchType } = state;
138
139 let multipleRun = null;
140 if (switchType === "loop") {
141 multipleRun = callResource;
142 } else {
143 multipleRun = switchResource;
144 }
145
146 let fResult = multipleRun(callback, function(setData, run, key, lg) {
147 let res;
148 if (key !== undefined) {
149 if (key === switchName) {
150 res = callback.call({
151 done: ts.done.bind(ts),
152 store,
153 data: data,
154 jump: ts.jump.bind(ts),
155 switchNext: ts.switchNext.bind(ts)
156 },
157 ...props
158 );
159 setData(res);
160 return "break";
161 } else {
162 return "continue";
163 }
164 } else {
165 res = callback.call({
166 done: ts.done.bind(ts),
167 store,
168 data: data,
169 jump: ts.jump.bind(ts),
170 switchNext: ts.switchNext.bind(ts)
171 },
172 ...props
173 );
174 setData(res);
175 return "break";
176 }
177 });
178
179 if (typeof fResult === "object" && typeof fResult.then === "function") {
180 fResult
181 .then(function(data) {
182 store[name] = data;
183 let nextName = nextArray(name, ts.map);
184 if (nextName === false) {
185 //为最后一个函数
186 ts.done(data);
187 } else {
188 ts.storeFunction[nextName](data);
189 }
190 })
191 .catch(function(err) {
192 ts.fail(err);
193 });
194 } else {
195 store[name] = fResult;
196 let nextName = nextArray(name, ts.map);
197 if (nextName === false) {
198 //为最后一个函数
199 ts.done(fResult);
200 } else {
201 ts.storeFunction[nextName](fResult);
202 }
203 }
204 };
205 ts.storeFunction[name] = newCallback;
206 return newCallback;
207 }
208 set({ callback, props, name }) {
209 let firstRun = this.state.firstRun;
210 let run = this.process({ callback, props, name });
211 if (firstRun === null) {
212 this.state.firstRun = run;
213 }
214 return this;
215 }
216 run() {
217 // this.nowInstance.run();
218 let firstRun = this.state.firstRun;
219 if (typeof firstRun === "function") {
220 firstRun();
221 }
222 return this;
223 }
224}
225
226let branch = function(funcList) {
227 let loop = new Branch();
228 return new Promise((resolve, reject) => {
229 loop.done(resolve).fail(reject);
230 funcList.forEach((obj, ind) => {
231 let { props, callback, name } = obj;
232 loop.set({ callback, props, name });
233 });
234 loop.run();
235 });
236};
237
238module.exports = branch;
\No newline at end of file