@viewfly/core 0.0.19 → 0.0.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bundles/di/_api.d.ts +10 -0
- package/bundles/di/forward-ref.d.ts +10 -0
- package/bundles/di/injectable.d.ts +20 -0
- package/bundles/di/injection-token.d.ts +8 -0
- package/bundles/di/injector.d.ts +22 -0
- package/bundles/di/metadata.d.ts +43 -0
- package/bundles/di/null-injector.d.ts +6 -0
- package/bundles/di/provider.d.ts +30 -0
- package/bundles/di/reflective-injector.d.ts +32 -0
- package/bundles/di/reflective-provider.d.ts +20 -0
- package/bundles/di/type.d.ts +7 -0
- package/bundles/di/utils/_api.d.ts +3 -0
- package/bundles/di/utils/annotations.d.ts +33 -0
- package/bundles/di/utils/decorators.d.ts +17 -0
- package/bundles/di/utils/stringify.d.ts +1 -0
- package/bundles/foundation/_api.d.ts +5 -0
- package/bundles/foundation/_utils.d.ts +4 -0
- package/bundles/{model → foundation}/component.d.ts +1 -1
- package/bundles/{model → foundation}/jsx-element.d.ts +1 -4
- package/bundles/foundation/renderer.d.ts +1 -1
- package/bundles/{model → foundation}/root.component.d.ts +3 -3
- package/bundles/{model → foundation}/types.d.ts +2 -1
- package/bundles/index.esm.js +588 -42
- package/bundles/index.js +611 -54
- package/bundles/public-api.d.ts +1 -2
- package/bundles/viewfly.d.ts +4 -4
- package/package.json +2 -4
- package/bundles/model/_api.d.ts +0 -5
- package/jsx-runtime/node_modules/.bin/rimraf +0 -17
- package/jsx-runtime/node_modules/.bin/rollup +0 -17
- /package/bundles/{model → foundation}/memo.d.ts +0 -0
package/bundles/index.js
CHANGED
|
@@ -1,8 +1,190 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
require('reflect-metadata');
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
|
|
5
|
+
class ForwardRef {
|
|
6
|
+
constructor(forwardRefFn) {
|
|
7
|
+
this.forwardRefFn = forwardRefFn;
|
|
8
|
+
}
|
|
9
|
+
getRef() {
|
|
10
|
+
return this.forwardRefFn();
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* 引用后声明的类的工具函数
|
|
15
|
+
* @param fn
|
|
16
|
+
*/
|
|
17
|
+
function forwardRef(fn) {
|
|
18
|
+
return new ForwardRef(fn);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 用于保存 class 的元数据
|
|
23
|
+
*/
|
|
24
|
+
class Annotations {
|
|
25
|
+
constructor() {
|
|
26
|
+
this.classes = new Map();
|
|
27
|
+
this.props = new Map();
|
|
28
|
+
this.params = new Map();
|
|
29
|
+
}
|
|
30
|
+
setClassMetadata(token, params) {
|
|
31
|
+
this.classes.set(token, params);
|
|
32
|
+
}
|
|
33
|
+
getClassMetadata(token) {
|
|
34
|
+
return this.classes.get(token);
|
|
35
|
+
}
|
|
36
|
+
pushParamMetadata(token, params) {
|
|
37
|
+
if (this.params.has(token)) {
|
|
38
|
+
this.params.get(token).push(params);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
this.params.set(token, [params]);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
getParamMetadata(token) {
|
|
45
|
+
return this.params.get(token);
|
|
46
|
+
}
|
|
47
|
+
getPropMetadataKeys() {
|
|
48
|
+
return Array.from(this.props.keys());
|
|
49
|
+
}
|
|
50
|
+
pushPropMetadata(token, params) {
|
|
51
|
+
if (this.props.has(token)) {
|
|
52
|
+
this.props.get(token).push(params);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
this.props.set(token, [params]);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
getPropMetadata(token) {
|
|
59
|
+
return this.props.get(token);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 创建参数装饰器的工厂函数
|
|
65
|
+
*/
|
|
66
|
+
function makeParamDecorator(token, metadata) {
|
|
67
|
+
return function (target, propertyKey, parameterIndex) {
|
|
68
|
+
const annotations = getAnnotations(target);
|
|
69
|
+
annotations.pushParamMetadata(token, {
|
|
70
|
+
propertyKey: propertyKey,
|
|
71
|
+
parameterIndex,
|
|
72
|
+
metadata
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* 创建属性装饰器的工厂函数
|
|
78
|
+
*/
|
|
79
|
+
function makePropertyDecorator(token, injectToken, contextCallback) {
|
|
80
|
+
return function (target, propertyKey) {
|
|
81
|
+
const annotations = getAnnotations(target.constructor);
|
|
82
|
+
annotations.pushPropMetadata(token, {
|
|
83
|
+
injectToken: injectToken || Reflect.getMetadata('design:type', target, propertyKey),
|
|
84
|
+
propertyKey,
|
|
85
|
+
contextCallback
|
|
86
|
+
});
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* 创建类装饰器的工厂函数
|
|
91
|
+
*/
|
|
92
|
+
function makeClassDecorator(token, metadata) {
|
|
93
|
+
return function (target) {
|
|
94
|
+
const annotations = getAnnotations(target);
|
|
95
|
+
annotations.setClassMetadata(token, {
|
|
96
|
+
paramTypes: Reflect.getMetadata('design:paramtypes', target),
|
|
97
|
+
metadata
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* 获取类注解的工具函数
|
|
103
|
+
*/
|
|
104
|
+
function getAnnotations(target) {
|
|
105
|
+
const key = '__annotations__';
|
|
106
|
+
// eslint-disable-next-line no-prototype-builtins
|
|
107
|
+
if (!target.hasOwnProperty(key)) {
|
|
108
|
+
target[key] = new Annotations();
|
|
109
|
+
}
|
|
110
|
+
return target[key];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
class Scope {
|
|
114
|
+
constructor(name) {
|
|
115
|
+
this.name = name;
|
|
116
|
+
}
|
|
117
|
+
toString() {
|
|
118
|
+
return this.name || '[anonymous provide scope]';
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* 可注入类的装饰器
|
|
123
|
+
*/
|
|
124
|
+
const Injectable = function InjectableDecorator(options) {
|
|
125
|
+
if (this instanceof InjectableDecorator) {
|
|
126
|
+
this.provideIn = (options === null || options === void 0 ? void 0 : options.provideIn) || null;
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
return makeClassDecorator(Injectable, new Injectable(options));
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* 生成自定义依赖注入 token 的类
|
|
135
|
+
*/
|
|
136
|
+
class InjectionToken {
|
|
137
|
+
constructor(description) {
|
|
138
|
+
this.description = description;
|
|
139
|
+
}
|
|
140
|
+
toString() {
|
|
141
|
+
return this.description || '[anonymous injection token]';
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* 查找规则
|
|
147
|
+
*/
|
|
148
|
+
exports.InjectFlags = void 0;
|
|
149
|
+
(function (InjectFlags) {
|
|
150
|
+
/** 默认查找规则 */
|
|
151
|
+
InjectFlags["Default"] = "Default";
|
|
152
|
+
/** 锁定当前容器 */
|
|
153
|
+
InjectFlags["Self"] = "Self";
|
|
154
|
+
/** 跳过当前容器 */
|
|
155
|
+
InjectFlags["SkipSelf"] = "SkipSelf";
|
|
156
|
+
/** 可选查找 */
|
|
157
|
+
InjectFlags["Optional"] = "Optional";
|
|
158
|
+
})(exports.InjectFlags || (exports.InjectFlags = {}));
|
|
159
|
+
/**
|
|
160
|
+
* DI 容器抽象基类
|
|
161
|
+
*/
|
|
162
|
+
class Injector {
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function stringify(token) {
|
|
166
|
+
if (typeof token === 'string') {
|
|
167
|
+
return token;
|
|
168
|
+
}
|
|
169
|
+
if (Array.isArray(token)) {
|
|
170
|
+
return '[' + token.map(stringify).join(', ') + ']';
|
|
171
|
+
}
|
|
172
|
+
if (token == null) {
|
|
173
|
+
return '' + token;
|
|
174
|
+
}
|
|
175
|
+
if (token.name) {
|
|
176
|
+
return `${token.name}`;
|
|
177
|
+
}
|
|
178
|
+
if (token.token) {
|
|
179
|
+
return `${token.token}`;
|
|
180
|
+
}
|
|
181
|
+
const res = token.toString();
|
|
182
|
+
if (res == null) {
|
|
183
|
+
return '' + res;
|
|
184
|
+
}
|
|
185
|
+
const newLineIndex = res.indexOf('\n');
|
|
186
|
+
return newLineIndex === -1 ? res : res.substring(0, newLineIndex);
|
|
187
|
+
}
|
|
6
188
|
|
|
7
189
|
function makeError(name) {
|
|
8
190
|
return function viewflyError(message) {
|
|
@@ -13,42 +195,357 @@ function makeError(name) {
|
|
|
13
195
|
};
|
|
14
196
|
}
|
|
15
197
|
|
|
16
|
-
|
|
198
|
+
const THROW_IF_NOT_FOUND = {
|
|
199
|
+
__debug_value__: 'THROW_IF_NOT_FOUND'
|
|
200
|
+
};
|
|
201
|
+
const nullInjectorErrorFn = (token) => {
|
|
202
|
+
return makeError('NullInjector')(`No provide for \`${stringify(token)}\`!`);
|
|
203
|
+
};
|
|
204
|
+
class NullInjector extends Injector {
|
|
205
|
+
constructor() {
|
|
206
|
+
super(...arguments);
|
|
207
|
+
this.parentInjector = null;
|
|
208
|
+
}
|
|
209
|
+
get(token, notFoundValue = THROW_IF_NOT_FOUND) {
|
|
210
|
+
if (notFoundValue === THROW_IF_NOT_FOUND) {
|
|
211
|
+
throw nullInjectorErrorFn(token);
|
|
212
|
+
}
|
|
213
|
+
return notFoundValue;
|
|
214
|
+
}
|
|
17
215
|
}
|
|
18
216
|
|
|
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
|
-
|
|
217
|
+
/**
|
|
218
|
+
* 构造函数参数装饰器,用于改变注入 token
|
|
219
|
+
*/
|
|
220
|
+
const Inject = function InjectDecorator(token) {
|
|
221
|
+
if (this instanceof Inject) {
|
|
222
|
+
this.token = token;
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
return makeParamDecorator(Inject, new Inject(token));
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
const Self = function SelfDecorator() {
|
|
229
|
+
if (!(this instanceof Self)) {
|
|
230
|
+
return makeParamDecorator(Self, new Self());
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
const SkipSelf = function SkipSelfDecorator() {
|
|
234
|
+
if (!(this instanceof SkipSelf)) {
|
|
235
|
+
return makeParamDecorator(SkipSelf, new SkipSelf());
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
const Optional = function OptionalDecorator() {
|
|
239
|
+
if (!(this instanceof Optional)) {
|
|
240
|
+
return makeParamDecorator(Optional, new Optional());
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
const Prop = function PropDecorator(token, notFoundValue = THROW_IF_NOT_FOUND, flags) {
|
|
244
|
+
if (!(this instanceof Prop)) {
|
|
245
|
+
return makePropertyDecorator(Prop, token, function (instance, propertyName, token, injector) {
|
|
246
|
+
instance[propertyName] = injector.get(token instanceof ForwardRef ? token.getRef() : token, notFoundValue, flags);
|
|
247
|
+
});
|
|
248
|
+
}
|
|
50
249
|
};
|
|
51
250
|
|
|
251
|
+
/**
|
|
252
|
+
* 标准化 provide,并返回统一数据结构
|
|
253
|
+
* @param provider
|
|
254
|
+
*/
|
|
255
|
+
function normalizeProvider(provider) {
|
|
256
|
+
if (provider.useValue) {
|
|
257
|
+
return normalizeValueProviderFactory(provider);
|
|
258
|
+
}
|
|
259
|
+
if (provider.useClass) {
|
|
260
|
+
return normalizeClassProviderFactory(provider);
|
|
261
|
+
}
|
|
262
|
+
if (provider.useExisting) {
|
|
263
|
+
return normalizeExistingProviderFactory(provider);
|
|
264
|
+
}
|
|
265
|
+
if (provider.useFactory) {
|
|
266
|
+
return normalizeFactoryProviderFactory(provider);
|
|
267
|
+
}
|
|
268
|
+
if (provider.provide) {
|
|
269
|
+
if (provider.provide instanceof InjectionToken) {
|
|
270
|
+
return normalizeValueProviderFactory(provider);
|
|
271
|
+
}
|
|
272
|
+
return normalizeConstructorProviderFactory(provider);
|
|
273
|
+
}
|
|
274
|
+
return normalizeTypeProviderFactory(provider);
|
|
275
|
+
}
|
|
276
|
+
function normalizeValueProviderFactory(provider) {
|
|
277
|
+
return {
|
|
278
|
+
provide: provider.provide,
|
|
279
|
+
scope: null,
|
|
280
|
+
generateFactory() {
|
|
281
|
+
return function () {
|
|
282
|
+
return provider.useValue;
|
|
283
|
+
};
|
|
284
|
+
},
|
|
285
|
+
deps: []
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
function normalizeClassProviderFactory(provider) {
|
|
289
|
+
let deps;
|
|
290
|
+
let provideIn = null;
|
|
291
|
+
if (provider.deps) {
|
|
292
|
+
deps = normalizeDeps(provider.provide, provider.deps);
|
|
293
|
+
}
|
|
294
|
+
else {
|
|
295
|
+
const resolvedClass = resolveClassParams(provider.useClass);
|
|
296
|
+
provideIn = resolvedClass.scope;
|
|
297
|
+
deps = normalizeDeps(provider.provide, resolvedClass.deps);
|
|
298
|
+
}
|
|
299
|
+
return {
|
|
300
|
+
provide: provider.provide,
|
|
301
|
+
scope: provideIn,
|
|
302
|
+
deps,
|
|
303
|
+
generateFactory(injector, cacheFn) {
|
|
304
|
+
return function (...args) {
|
|
305
|
+
const instance = new provider.useClass(...args);
|
|
306
|
+
cacheFn(provider.provide, instance);
|
|
307
|
+
const propMetadataKeys = getAnnotations(provider.useClass).getPropMetadataKeys();
|
|
308
|
+
propMetadataKeys.forEach(key => {
|
|
309
|
+
const propsMetadata = getAnnotations(provider.useClass).getPropMetadata(key);
|
|
310
|
+
propsMetadata.forEach(item => {
|
|
311
|
+
item.contextCallback(instance, item.propertyKey, item.injectToken, injector);
|
|
312
|
+
});
|
|
313
|
+
});
|
|
314
|
+
return instance;
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
function normalizeExistingProviderFactory(provider) {
|
|
320
|
+
return {
|
|
321
|
+
provide: provider.provide,
|
|
322
|
+
scope: null,
|
|
323
|
+
generateFactory(injector) {
|
|
324
|
+
return function () {
|
|
325
|
+
return injector.get(provider.useExisting);
|
|
326
|
+
};
|
|
327
|
+
},
|
|
328
|
+
deps: []
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
function normalizeFactoryProviderFactory(provider) {
|
|
332
|
+
return {
|
|
333
|
+
provide: provider.provide,
|
|
334
|
+
scope: null,
|
|
335
|
+
generateFactory() {
|
|
336
|
+
return function (...args) {
|
|
337
|
+
return provider.useFactory(...args);
|
|
338
|
+
};
|
|
339
|
+
},
|
|
340
|
+
deps: normalizeDeps(provider.provide, provider.deps || [])
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
function normalizeConstructorProviderFactory(provider) {
|
|
344
|
+
return normalizeClassProviderFactory(Object.assign(Object.assign({}, provider), { useClass: provider.provide }));
|
|
345
|
+
}
|
|
346
|
+
function normalizeTypeProviderFactory(provider) {
|
|
347
|
+
return normalizeClassProviderFactory({
|
|
348
|
+
provide: provider,
|
|
349
|
+
useClass: provider
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
function resolveClassParams(construct) {
|
|
353
|
+
const annotations = getAnnotations(construct);
|
|
354
|
+
const metadata = annotations.getClassMetadata(Injectable);
|
|
355
|
+
if (typeof metadata === 'undefined') {
|
|
356
|
+
throw new Error(`Class \`${stringify(construct)}\` is not injectable!`);
|
|
357
|
+
}
|
|
358
|
+
const deps = (metadata.paramTypes || []).map(i => [i]);
|
|
359
|
+
const metadataKeys = [Inject, Self, SkipSelf, Optional];
|
|
360
|
+
metadataKeys.forEach(key => {
|
|
361
|
+
(annotations.getParamMetadata(key) || []).forEach(item => {
|
|
362
|
+
deps[item.parameterIndex].push(item.metadata);
|
|
363
|
+
});
|
|
364
|
+
});
|
|
365
|
+
return {
|
|
366
|
+
scope: metadata.metadata.provideIn,
|
|
367
|
+
deps
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
function normalizeDeps(provide, deps) {
|
|
371
|
+
return deps.map((dep, index) => {
|
|
372
|
+
const r = {
|
|
373
|
+
injectKey: null,
|
|
374
|
+
optional: false,
|
|
375
|
+
visibility: null
|
|
376
|
+
};
|
|
377
|
+
if (!Array.isArray(dep)) {
|
|
378
|
+
r.injectKey = dep;
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
for (let i = 0; i < dep.length; i++) {
|
|
382
|
+
const item = dep[i];
|
|
383
|
+
if (item instanceof Inject) {
|
|
384
|
+
r.injectKey = item.token;
|
|
385
|
+
}
|
|
386
|
+
else if (item instanceof Self || item instanceof SkipSelf) {
|
|
387
|
+
r.visibility = item;
|
|
388
|
+
}
|
|
389
|
+
else if (item instanceof Optional) {
|
|
390
|
+
r.optional = true;
|
|
391
|
+
}
|
|
392
|
+
else {
|
|
393
|
+
r.injectKey = item;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
if (typeof r.injectKey === 'undefined') {
|
|
398
|
+
throw new Error(`The ${index} th dependent parameter type of \`${stringify(provide)}\` was not obtained,
|
|
399
|
+
if the dependency is declared later, you can refer to it using \`constructor(@Inject(forwardRef(() => [Type|InjectionToken])) paramName: [Type]) {}\``);
|
|
400
|
+
}
|
|
401
|
+
return r;
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
const reflectiveInjectorErrorFn = (token) => {
|
|
406
|
+
return makeError('ReflectiveInjector')(`No provide for \`${stringify(token)}\`!`);
|
|
407
|
+
};
|
|
408
|
+
const provideScopeError = (token) => {
|
|
409
|
+
return makeError('ProvideScope')(`Can not found provide scope \`${stringify(token)}\`!`);
|
|
410
|
+
};
|
|
411
|
+
/**
|
|
412
|
+
* 反射注入器
|
|
413
|
+
*/
|
|
414
|
+
class ReflectiveInjector extends Injector {
|
|
415
|
+
constructor(parentInjector, staticProviders, scope) {
|
|
416
|
+
super();
|
|
417
|
+
this.parentInjector = parentInjector;
|
|
418
|
+
this.staticProviders = staticProviders;
|
|
419
|
+
this.scope = scope;
|
|
420
|
+
this.recordValues = new Map();
|
|
421
|
+
this.normalizedProviders = staticProviders.map(provide => {
|
|
422
|
+
return normalizeProvider(provide);
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* 用于获取当前注入器上下文内的实例、对象或数据
|
|
427
|
+
* @param token 访问 token
|
|
428
|
+
* @param notFoundValue 如未查找到的返回值
|
|
429
|
+
* @param flags 查询规则
|
|
430
|
+
*/
|
|
431
|
+
get(token, notFoundValue = THROW_IF_NOT_FOUND, flags) {
|
|
432
|
+
var _a;
|
|
433
|
+
flags = flags || exports.InjectFlags.Default;
|
|
434
|
+
if (flags === exports.InjectFlags.SkipSelf) {
|
|
435
|
+
if (this.parentInjector) {
|
|
436
|
+
return this.parentInjector.get(token, notFoundValue);
|
|
437
|
+
}
|
|
438
|
+
if (notFoundValue !== THROW_IF_NOT_FOUND) {
|
|
439
|
+
return notFoundValue;
|
|
440
|
+
}
|
|
441
|
+
throw reflectiveInjectorErrorFn(token);
|
|
442
|
+
}
|
|
443
|
+
if (this.recordValues.has(token)) {
|
|
444
|
+
return this.recordValues.get(token);
|
|
445
|
+
}
|
|
446
|
+
for (let i = 0; i < this.normalizedProviders.length; i++) {
|
|
447
|
+
const normalizedProvider = this.normalizedProviders[i];
|
|
448
|
+
if (normalizedProvider.provide === token) {
|
|
449
|
+
return this.getValue(token, THROW_IF_NOT_FOUND, normalizedProvider);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
if (!(token instanceof InjectionToken)) {
|
|
453
|
+
const scope = (_a = getAnnotations(token).getClassMetadata(Injectable)) === null || _a === void 0 ? void 0 : _a.metadata.provideIn;
|
|
454
|
+
if (scope) {
|
|
455
|
+
const normalizedProvider = normalizeProvider(token);
|
|
456
|
+
if (this.scope === scope) {
|
|
457
|
+
this.normalizedProviders.push(normalizedProvider);
|
|
458
|
+
return this.getValue(token, THROW_IF_NOT_FOUND, normalizedProvider);
|
|
459
|
+
}
|
|
460
|
+
const parentInjector = this.parentInjector;
|
|
461
|
+
if (!parentInjector || parentInjector instanceof NullInjector) {
|
|
462
|
+
if (normalizedProvider.scope === 'root') {
|
|
463
|
+
this.normalizedProviders.push(normalizedProvider);
|
|
464
|
+
return this.getValue(token, THROW_IF_NOT_FOUND, normalizedProvider);
|
|
465
|
+
}
|
|
466
|
+
if (notFoundValue !== THROW_IF_NOT_FOUND) {
|
|
467
|
+
return notFoundValue;
|
|
468
|
+
}
|
|
469
|
+
throw provideScopeError(normalizedProvider.scope);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
if (flags === exports.InjectFlags.Self) {
|
|
474
|
+
if (notFoundValue === THROW_IF_NOT_FOUND) {
|
|
475
|
+
throw reflectiveInjectorErrorFn(token);
|
|
476
|
+
}
|
|
477
|
+
return notFoundValue;
|
|
478
|
+
}
|
|
479
|
+
if (this.parentInjector) {
|
|
480
|
+
return this.parentInjector.get(token, notFoundValue, flags === exports.InjectFlags.Optional ? exports.InjectFlags.Optional : exports.InjectFlags.Default);
|
|
481
|
+
}
|
|
482
|
+
if (notFoundValue === THROW_IF_NOT_FOUND) {
|
|
483
|
+
throw reflectiveInjectorErrorFn(token);
|
|
484
|
+
}
|
|
485
|
+
return notFoundValue;
|
|
486
|
+
}
|
|
487
|
+
getValue(token, notFoundValue, normalizedProvider) {
|
|
488
|
+
const { generateFactory, deps } = normalizedProvider;
|
|
489
|
+
const params = this.resolveDeps(deps, notFoundValue);
|
|
490
|
+
let value = this.recordValues.get(token);
|
|
491
|
+
if (value) {
|
|
492
|
+
return value;
|
|
493
|
+
}
|
|
494
|
+
const factory = generateFactory(this, (token, value) => {
|
|
495
|
+
this.recordValues.set(token, value);
|
|
496
|
+
});
|
|
497
|
+
value = factory(...params);
|
|
498
|
+
this.recordValues.set(token, value);
|
|
499
|
+
return value;
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* 解决并获取依赖参数
|
|
503
|
+
* @param deps 依赖规则
|
|
504
|
+
* @param notFoundValue 未查找到时的返回值
|
|
505
|
+
* @private
|
|
506
|
+
*/
|
|
507
|
+
resolveDeps(deps, notFoundValue) {
|
|
508
|
+
return deps.map(dep => {
|
|
509
|
+
let reflectiveValue;
|
|
510
|
+
const tryValue = {};
|
|
511
|
+
const injectToken = dep.injectKey instanceof ForwardRef ? dep.injectKey.getRef() : dep.injectKey;
|
|
512
|
+
if (dep.visibility instanceof Self) {
|
|
513
|
+
reflectiveValue = this.get(injectToken, tryValue, exports.InjectFlags.Self);
|
|
514
|
+
}
|
|
515
|
+
else if (dep.visibility instanceof SkipSelf) {
|
|
516
|
+
if (this.parentInjector) {
|
|
517
|
+
reflectiveValue = this.parentInjector.get(injectToken, tryValue);
|
|
518
|
+
}
|
|
519
|
+
else {
|
|
520
|
+
if (dep.optional) {
|
|
521
|
+
// if (notFoundValue === THROW_IF_NOT_FOUND) {
|
|
522
|
+
// return null
|
|
523
|
+
// }
|
|
524
|
+
return null;
|
|
525
|
+
}
|
|
526
|
+
throw reflectiveInjectorErrorFn(injectToken);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
else {
|
|
530
|
+
reflectiveValue = this.get(injectToken, tryValue);
|
|
531
|
+
}
|
|
532
|
+
if (reflectiveValue === tryValue) {
|
|
533
|
+
if (dep.optional) {
|
|
534
|
+
// if (notFoundValue === THROW_IF_NOT_FOUND) {
|
|
535
|
+
// return null
|
|
536
|
+
// }
|
|
537
|
+
// return notFoundValue
|
|
538
|
+
return null;
|
|
539
|
+
}
|
|
540
|
+
throw reflectiveInjectorErrorFn(injectToken);
|
|
541
|
+
}
|
|
542
|
+
return reflectiveValue;
|
|
543
|
+
});
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
const Type = Function;
|
|
548
|
+
|
|
52
549
|
const refKey = 'ref';
|
|
53
550
|
function getObjectChanges(newProps, oldProps) {
|
|
54
551
|
const changes = {
|
|
@@ -145,7 +642,7 @@ class JSXComponent {
|
|
|
145
642
|
/**
|
|
146
643
|
* Viewfly 组件管理类,用于管理组件的生命周期,上下文等
|
|
147
644
|
*/
|
|
148
|
-
class Component extends
|
|
645
|
+
class Component extends ReflectiveInjector {
|
|
149
646
|
get dirty() {
|
|
150
647
|
return this._dirty;
|
|
151
648
|
}
|
|
@@ -154,7 +651,7 @@ class Component extends di.ReflectiveInjector {
|
|
|
154
651
|
}
|
|
155
652
|
constructor(context, type, props, key) {
|
|
156
653
|
super(context, [{
|
|
157
|
-
provide:
|
|
654
|
+
provide: Injector,
|
|
158
655
|
useFactory: () => this
|
|
159
656
|
}]);
|
|
160
657
|
this.type = type;
|
|
@@ -177,7 +674,7 @@ class Component extends di.ReflectiveInjector {
|
|
|
177
674
|
}
|
|
178
675
|
provide(providers) {
|
|
179
676
|
providers = Array.isArray(providers) ? providers : [providers];
|
|
180
|
-
this.normalizedProviders.unshift(...providers.map(i =>
|
|
677
|
+
this.normalizedProviders.unshift(...providers.map(i => normalizeProvider(i)));
|
|
181
678
|
}
|
|
182
679
|
setup() {
|
|
183
680
|
const self = this;
|
|
@@ -615,7 +1112,7 @@ function provide(provider) {
|
|
|
615
1112
|
*/
|
|
616
1113
|
function inject(token, notFoundValue, flags) {
|
|
617
1114
|
const component = getSetupContext();
|
|
618
|
-
return component.get(token, notFoundValue, flags ||
|
|
1115
|
+
return component.get(token, notFoundValue, flags || exports.InjectFlags.SkipSelf);
|
|
619
1116
|
}
|
|
620
1117
|
|
|
621
1118
|
function Fragment(props) {
|
|
@@ -666,14 +1163,51 @@ function withMemo(shouldUpdate, render) {
|
|
|
666
1163
|
class RootComponent extends Component {
|
|
667
1164
|
constructor(factory, parentInjector) {
|
|
668
1165
|
super(parentInjector, factory, {});
|
|
669
|
-
this.
|
|
1166
|
+
this.onChange = null;
|
|
670
1167
|
}
|
|
671
1168
|
markAsChanged() {
|
|
1169
|
+
var _a;
|
|
672
1170
|
this._changed = true;
|
|
673
|
-
this.
|
|
1171
|
+
(_a = this.onChange) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
674
1172
|
}
|
|
675
1173
|
}
|
|
676
1174
|
|
|
1175
|
+
class NativeRenderer {
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
/******************************************************************************
|
|
1179
|
+
Copyright (c) Microsoft Corporation.
|
|
1180
|
+
|
|
1181
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
1182
|
+
purpose with or without fee is hereby granted.
|
|
1183
|
+
|
|
1184
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
1185
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
1186
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
1187
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
1188
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
1189
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
1190
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
1191
|
+
***************************************************************************** */
|
|
1192
|
+
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
1193
|
+
|
|
1194
|
+
|
|
1195
|
+
function __decorate(decorators, target, key, desc) {
|
|
1196
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
1197
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
1198
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
1199
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
1200
|
+
}
|
|
1201
|
+
|
|
1202
|
+
function __metadata(metadataKey, metadataValue) {
|
|
1203
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
|
|
1204
|
+
}
|
|
1205
|
+
|
|
1206
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
1207
|
+
var e = new Error(message);
|
|
1208
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
1209
|
+
};
|
|
1210
|
+
|
|
677
1211
|
class RootComponentRef {
|
|
678
1212
|
}
|
|
679
1213
|
class HostRef {
|
|
@@ -1264,7 +1798,7 @@ exports.Renderer = class Renderer {
|
|
|
1264
1798
|
}
|
|
1265
1799
|
};
|
|
1266
1800
|
exports.Renderer = __decorate([
|
|
1267
|
-
|
|
1801
|
+
Injectable(),
|
|
1268
1802
|
__metadata("design:paramtypes", [NativeRenderer,
|
|
1269
1803
|
RootComponentRef,
|
|
1270
1804
|
HostRef])
|
|
@@ -1274,9 +1808,9 @@ const viewflyErrorFn = makeError('Viewfly');
|
|
|
1274
1808
|
/**
|
|
1275
1809
|
* Viewfly 核心类,用于启动一个 Viewfly 应用
|
|
1276
1810
|
*/
|
|
1277
|
-
class Viewfly extends
|
|
1811
|
+
class Viewfly extends ReflectiveInjector {
|
|
1278
1812
|
constructor(config) {
|
|
1279
|
-
super(config.context || new
|
|
1813
|
+
super(config.context || new NullInjector(), [
|
|
1280
1814
|
exports.Renderer,
|
|
1281
1815
|
{
|
|
1282
1816
|
provide: RootComponentRef,
|
|
@@ -1301,12 +1835,12 @@ class Viewfly extends di.ReflectiveInjector {
|
|
|
1301
1835
|
]);
|
|
1302
1836
|
this.config = config;
|
|
1303
1837
|
this.destroyed = false;
|
|
1304
|
-
this.
|
|
1838
|
+
this.task = null;
|
|
1305
1839
|
this.rootComponent = this.createRootComponent(config.root);
|
|
1306
1840
|
}
|
|
1307
1841
|
provide(providers) {
|
|
1308
1842
|
providers = Array.isArray(providers) ? providers : [providers];
|
|
1309
|
-
this.normalizedProviders.unshift(...providers.map(i =>
|
|
1843
|
+
this.normalizedProviders.unshift(...providers.map(i => normalizeProvider(i)));
|
|
1310
1844
|
return this;
|
|
1311
1845
|
}
|
|
1312
1846
|
/**
|
|
@@ -1325,9 +1859,15 @@ class Viewfly extends di.ReflectiveInjector {
|
|
|
1325
1859
|
if (this.config.autoUpdate === false) {
|
|
1326
1860
|
return this;
|
|
1327
1861
|
}
|
|
1328
|
-
|
|
1862
|
+
const refresh = () => {
|
|
1863
|
+
if (this.destroyed) {
|
|
1864
|
+
return;
|
|
1865
|
+
}
|
|
1329
1866
|
renderer.render();
|
|
1330
|
-
}
|
|
1867
|
+
};
|
|
1868
|
+
this.rootComponent.onChange = () => {
|
|
1869
|
+
this.microTask(refresh);
|
|
1870
|
+
};
|
|
1331
1871
|
return this;
|
|
1332
1872
|
}
|
|
1333
1873
|
render() {
|
|
@@ -1340,7 +1880,6 @@ class Viewfly extends di.ReflectiveInjector {
|
|
|
1340
1880
|
destroy() {
|
|
1341
1881
|
this.destroyed = true;
|
|
1342
1882
|
this.rootComponent.markAsDirtied();
|
|
1343
|
-
this.subscription.unsubscribe();
|
|
1344
1883
|
this.render();
|
|
1345
1884
|
}
|
|
1346
1885
|
createRootComponent(rootNode) {
|
|
@@ -1350,23 +1889,47 @@ class Viewfly extends di.ReflectiveInjector {
|
|
|
1350
1889
|
};
|
|
1351
1890
|
}, this);
|
|
1352
1891
|
}
|
|
1892
|
+
microTask(callback) {
|
|
1893
|
+
if (!this.task) {
|
|
1894
|
+
this.task = Promise.resolve().then(() => {
|
|
1895
|
+
this.task = null;
|
|
1896
|
+
callback();
|
|
1897
|
+
});
|
|
1898
|
+
}
|
|
1899
|
+
}
|
|
1353
1900
|
}
|
|
1354
1901
|
|
|
1355
1902
|
exports.Component = Component;
|
|
1903
|
+
exports.ForwardRef = ForwardRef;
|
|
1356
1904
|
exports.Fragment = Fragment;
|
|
1357
1905
|
exports.HostRef = HostRef;
|
|
1906
|
+
exports.Inject = Inject;
|
|
1907
|
+
exports.Injectable = Injectable;
|
|
1908
|
+
exports.InjectionToken = InjectionToken;
|
|
1909
|
+
exports.Injector = Injector;
|
|
1358
1910
|
exports.JSXComponent = JSXComponent;
|
|
1359
1911
|
exports.JSXElement = JSXElement;
|
|
1360
1912
|
exports.JSXText = JSXText;
|
|
1361
1913
|
exports.NativeRenderer = NativeRenderer;
|
|
1914
|
+
exports.NullInjector = NullInjector;
|
|
1915
|
+
exports.Optional = Optional;
|
|
1916
|
+
exports.Prop = Prop;
|
|
1362
1917
|
exports.Ref = Ref;
|
|
1918
|
+
exports.ReflectiveInjector = ReflectiveInjector;
|
|
1363
1919
|
exports.RootComponent = RootComponent;
|
|
1364
1920
|
exports.RootComponentRef = RootComponentRef;
|
|
1921
|
+
exports.Scope = Scope;
|
|
1922
|
+
exports.Self = Self;
|
|
1923
|
+
exports.SkipSelf = SkipSelf;
|
|
1924
|
+
exports.THROW_IF_NOT_FOUND = THROW_IF_NOT_FOUND;
|
|
1925
|
+
exports.Type = Type;
|
|
1365
1926
|
exports.Viewfly = Viewfly;
|
|
1927
|
+
exports.forwardRef = forwardRef;
|
|
1366
1928
|
exports.inject = inject;
|
|
1367
1929
|
exports.jsx = jsx;
|
|
1368
1930
|
exports.jsxs = jsxs;
|
|
1369
1931
|
exports.makeError = makeError;
|
|
1932
|
+
exports.normalizeProvider = normalizeProvider;
|
|
1370
1933
|
exports.onDestroy = onDestroy;
|
|
1371
1934
|
exports.onMounted = onMounted;
|
|
1372
1935
|
exports.onPropsChanged = onPropsChanged;
|
|
@@ -1377,9 +1940,3 @@ exports.useEffect = useEffect;
|
|
|
1377
1940
|
exports.useRef = useRef;
|
|
1378
1941
|
exports.useSignal = useSignal;
|
|
1379
1942
|
exports.withMemo = withMemo;
|
|
1380
|
-
Object.keys(di).forEach(function (k) {
|
|
1381
|
-
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
1382
|
-
enumerable: true,
|
|
1383
|
-
get: function () { return di[k]; }
|
|
1384
|
-
});
|
|
1385
|
-
});
|