@viewfly/core 0.0.18 → 0.0.20
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 +6 -6
- package/bundles/{model → foundation}/jsx-element.d.ts +3 -6
- package/bundles/foundation/renderer.d.ts +6 -3
- package/bundles/{model → foundation}/root.component.d.ts +3 -4
- package/bundles/{model → foundation}/types.d.ts +6 -4
- package/bundles/index.esm.js +648 -73
- package/bundles/index.js +668 -81
- package/bundles/public-api.d.ts +1 -2
- package/bundles/viewfly.d.ts +10 -10
- package/package.json +2 -4
- package/bundles/model/_api.d.ts +0 -5
- package/jsx-runtime/index.d.ts +0 -23
- package/jsx-runtime/index.esm.js +0 -11
- package/jsx-runtime/index.js +0 -14
- 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.esm.js
CHANGED
|
@@ -1,7 +1,188 @@
|
|
|
1
1
|
import 'reflect-metadata';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
|
|
3
|
+
class ForwardRef {
|
|
4
|
+
constructor(forwardRefFn) {
|
|
5
|
+
this.forwardRefFn = forwardRefFn;
|
|
6
|
+
}
|
|
7
|
+
getRef() {
|
|
8
|
+
return this.forwardRefFn();
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 引用后声明的类的工具函数
|
|
13
|
+
* @param fn
|
|
14
|
+
*/
|
|
15
|
+
function forwardRef(fn) {
|
|
16
|
+
return new ForwardRef(fn);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 用于保存 class 的元数据
|
|
21
|
+
*/
|
|
22
|
+
class Annotations {
|
|
23
|
+
constructor() {
|
|
24
|
+
this.classes = new Map();
|
|
25
|
+
this.props = new Map();
|
|
26
|
+
this.params = new Map();
|
|
27
|
+
}
|
|
28
|
+
setClassMetadata(token, params) {
|
|
29
|
+
this.classes.set(token, params);
|
|
30
|
+
}
|
|
31
|
+
getClassMetadata(token) {
|
|
32
|
+
return this.classes.get(token);
|
|
33
|
+
}
|
|
34
|
+
pushParamMetadata(token, params) {
|
|
35
|
+
if (this.params.has(token)) {
|
|
36
|
+
this.params.get(token).push(params);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
this.params.set(token, [params]);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
getParamMetadata(token) {
|
|
43
|
+
return this.params.get(token);
|
|
44
|
+
}
|
|
45
|
+
getPropMetadataKeys() {
|
|
46
|
+
return Array.from(this.props.keys());
|
|
47
|
+
}
|
|
48
|
+
pushPropMetadata(token, params) {
|
|
49
|
+
if (this.props.has(token)) {
|
|
50
|
+
this.props.get(token).push(params);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
this.props.set(token, [params]);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
getPropMetadata(token) {
|
|
57
|
+
return this.props.get(token);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 创建参数装饰器的工厂函数
|
|
63
|
+
*/
|
|
64
|
+
function makeParamDecorator(token, metadata) {
|
|
65
|
+
return function (target, propertyKey, parameterIndex) {
|
|
66
|
+
const annotations = getAnnotations(target);
|
|
67
|
+
annotations.pushParamMetadata(token, {
|
|
68
|
+
propertyKey: propertyKey,
|
|
69
|
+
parameterIndex,
|
|
70
|
+
metadata
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* 创建属性装饰器的工厂函数
|
|
76
|
+
*/
|
|
77
|
+
function makePropertyDecorator(token, injectToken, contextCallback) {
|
|
78
|
+
return function (target, propertyKey) {
|
|
79
|
+
const annotations = getAnnotations(target.constructor);
|
|
80
|
+
annotations.pushPropMetadata(token, {
|
|
81
|
+
injectToken: injectToken || Reflect.getMetadata('design:type', target, propertyKey),
|
|
82
|
+
propertyKey,
|
|
83
|
+
contextCallback
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* 创建类装饰器的工厂函数
|
|
89
|
+
*/
|
|
90
|
+
function makeClassDecorator(token, metadata) {
|
|
91
|
+
return function (target) {
|
|
92
|
+
const annotations = getAnnotations(target);
|
|
93
|
+
annotations.setClassMetadata(token, {
|
|
94
|
+
paramTypes: Reflect.getMetadata('design:paramtypes', target),
|
|
95
|
+
metadata
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* 获取类注解的工具函数
|
|
101
|
+
*/
|
|
102
|
+
function getAnnotations(target) {
|
|
103
|
+
const key = '__annotations__';
|
|
104
|
+
// eslint-disable-next-line no-prototype-builtins
|
|
105
|
+
if (!target.hasOwnProperty(key)) {
|
|
106
|
+
target[key] = new Annotations();
|
|
107
|
+
}
|
|
108
|
+
return target[key];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
class Scope {
|
|
112
|
+
constructor(name) {
|
|
113
|
+
this.name = name;
|
|
114
|
+
}
|
|
115
|
+
toString() {
|
|
116
|
+
return this.name || '[anonymous provide scope]';
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* 可注入类的装饰器
|
|
121
|
+
*/
|
|
122
|
+
const Injectable = function InjectableDecorator(options) {
|
|
123
|
+
if (this instanceof InjectableDecorator) {
|
|
124
|
+
this.provideIn = (options === null || options === void 0 ? void 0 : options.provideIn) || null;
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
return makeClassDecorator(Injectable, new Injectable(options));
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 生成自定义依赖注入 token 的类
|
|
133
|
+
*/
|
|
134
|
+
class InjectionToken {
|
|
135
|
+
constructor(description) {
|
|
136
|
+
this.description = description;
|
|
137
|
+
}
|
|
138
|
+
toString() {
|
|
139
|
+
return this.description || '[anonymous injection token]';
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* 查找规则
|
|
145
|
+
*/
|
|
146
|
+
var InjectFlags;
|
|
147
|
+
(function (InjectFlags) {
|
|
148
|
+
/** 默认查找规则 */
|
|
149
|
+
InjectFlags["Default"] = "Default";
|
|
150
|
+
/** 锁定当前容器 */
|
|
151
|
+
InjectFlags["Self"] = "Self";
|
|
152
|
+
/** 跳过当前容器 */
|
|
153
|
+
InjectFlags["SkipSelf"] = "SkipSelf";
|
|
154
|
+
/** 可选查找 */
|
|
155
|
+
InjectFlags["Optional"] = "Optional";
|
|
156
|
+
})(InjectFlags || (InjectFlags = {}));
|
|
157
|
+
/**
|
|
158
|
+
* DI 容器抽象基类
|
|
159
|
+
*/
|
|
160
|
+
class Injector {
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function stringify(token) {
|
|
164
|
+
if (typeof token === 'string') {
|
|
165
|
+
return token;
|
|
166
|
+
}
|
|
167
|
+
if (Array.isArray(token)) {
|
|
168
|
+
return '[' + token.map(stringify).join(', ') + ']';
|
|
169
|
+
}
|
|
170
|
+
if (token == null) {
|
|
171
|
+
return '' + token;
|
|
172
|
+
}
|
|
173
|
+
if (token.name) {
|
|
174
|
+
return `${token.name}`;
|
|
175
|
+
}
|
|
176
|
+
if (token.token) {
|
|
177
|
+
return `${token.token}`;
|
|
178
|
+
}
|
|
179
|
+
const res = token.toString();
|
|
180
|
+
if (res == null) {
|
|
181
|
+
return '' + res;
|
|
182
|
+
}
|
|
183
|
+
const newLineIndex = res.indexOf('\n');
|
|
184
|
+
return newLineIndex === -1 ? res : res.substring(0, newLineIndex);
|
|
185
|
+
}
|
|
5
186
|
|
|
6
187
|
function makeError(name) {
|
|
7
188
|
return function viewflyError(message) {
|
|
@@ -12,41 +193,356 @@ function makeError(name) {
|
|
|
12
193
|
};
|
|
13
194
|
}
|
|
14
195
|
|
|
15
|
-
|
|
196
|
+
const THROW_IF_NOT_FOUND = {
|
|
197
|
+
__debug_value__: 'THROW_IF_NOT_FOUND'
|
|
198
|
+
};
|
|
199
|
+
const nullInjectorErrorFn = (token) => {
|
|
200
|
+
return makeError('NullInjector')(`No provide for \`${stringify(token)}\`!`);
|
|
201
|
+
};
|
|
202
|
+
class NullInjector extends Injector {
|
|
203
|
+
constructor() {
|
|
204
|
+
super(...arguments);
|
|
205
|
+
this.parentInjector = null;
|
|
206
|
+
}
|
|
207
|
+
get(token, notFoundValue = THROW_IF_NOT_FOUND) {
|
|
208
|
+
if (notFoundValue === THROW_IF_NOT_FOUND) {
|
|
209
|
+
throw nullInjectorErrorFn(token);
|
|
210
|
+
}
|
|
211
|
+
return notFoundValue;
|
|
212
|
+
}
|
|
16
213
|
}
|
|
17
214
|
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
215
|
+
/**
|
|
216
|
+
* 构造函数参数装饰器,用于改变注入 token
|
|
217
|
+
*/
|
|
218
|
+
const Inject = function InjectDecorator(token) {
|
|
219
|
+
if (this instanceof Inject) {
|
|
220
|
+
this.token = token;
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
return makeParamDecorator(Inject, new Inject(token));
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
const Self = function SelfDecorator() {
|
|
227
|
+
if (!(this instanceof Self)) {
|
|
228
|
+
return makeParamDecorator(Self, new Self());
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
const SkipSelf = function SkipSelfDecorator() {
|
|
232
|
+
if (!(this instanceof SkipSelf)) {
|
|
233
|
+
return makeParamDecorator(SkipSelf, new SkipSelf());
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
const Optional = function OptionalDecorator() {
|
|
237
|
+
if (!(this instanceof Optional)) {
|
|
238
|
+
return makeParamDecorator(Optional, new Optional());
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
const Prop = function PropDecorator(token, notFoundValue = THROW_IF_NOT_FOUND, flags) {
|
|
242
|
+
if (!(this instanceof Prop)) {
|
|
243
|
+
return makePropertyDecorator(Prop, token, function (instance, propertyName, token, injector) {
|
|
244
|
+
instance[propertyName] = injector.get(token instanceof ForwardRef ? token.getRef() : token, notFoundValue, flags);
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* 标准化 provide,并返回统一数据结构
|
|
251
|
+
* @param provider
|
|
252
|
+
*/
|
|
253
|
+
function normalizeProvider(provider) {
|
|
254
|
+
if (provider.useValue) {
|
|
255
|
+
return normalizeValueProviderFactory(provider);
|
|
256
|
+
}
|
|
257
|
+
if (provider.useClass) {
|
|
258
|
+
return normalizeClassProviderFactory(provider);
|
|
259
|
+
}
|
|
260
|
+
if (provider.useExisting) {
|
|
261
|
+
return normalizeExistingProviderFactory(provider);
|
|
262
|
+
}
|
|
263
|
+
if (provider.useFactory) {
|
|
264
|
+
return normalizeFactoryProviderFactory(provider);
|
|
265
|
+
}
|
|
266
|
+
if (provider.provide) {
|
|
267
|
+
if (provider.provide instanceof InjectionToken) {
|
|
268
|
+
return normalizeValueProviderFactory(provider);
|
|
269
|
+
}
|
|
270
|
+
return normalizeConstructorProviderFactory(provider);
|
|
271
|
+
}
|
|
272
|
+
return normalizeTypeProviderFactory(provider);
|
|
273
|
+
}
|
|
274
|
+
function normalizeValueProviderFactory(provider) {
|
|
275
|
+
return {
|
|
276
|
+
provide: provider.provide,
|
|
277
|
+
scope: null,
|
|
278
|
+
generateFactory() {
|
|
279
|
+
return function () {
|
|
280
|
+
return provider.useValue;
|
|
281
|
+
};
|
|
282
|
+
},
|
|
283
|
+
deps: []
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
function normalizeClassProviderFactory(provider) {
|
|
287
|
+
let deps;
|
|
288
|
+
let provideIn = null;
|
|
289
|
+
if (provider.deps) {
|
|
290
|
+
deps = normalizeDeps(provider.provide, provider.deps);
|
|
291
|
+
}
|
|
292
|
+
else {
|
|
293
|
+
const resolvedClass = resolveClassParams(provider.useClass);
|
|
294
|
+
provideIn = resolvedClass.scope;
|
|
295
|
+
deps = normalizeDeps(provider.provide, resolvedClass.deps);
|
|
296
|
+
}
|
|
297
|
+
return {
|
|
298
|
+
provide: provider.provide,
|
|
299
|
+
scope: provideIn,
|
|
300
|
+
deps,
|
|
301
|
+
generateFactory(injector, cacheFn) {
|
|
302
|
+
return function (...args) {
|
|
303
|
+
const instance = new provider.useClass(...args);
|
|
304
|
+
cacheFn(provider.provide, instance);
|
|
305
|
+
const propMetadataKeys = getAnnotations(provider.useClass).getPropMetadataKeys();
|
|
306
|
+
propMetadataKeys.forEach(key => {
|
|
307
|
+
const propsMetadata = getAnnotations(provider.useClass).getPropMetadata(key);
|
|
308
|
+
propsMetadata.forEach(item => {
|
|
309
|
+
item.contextCallback(instance, item.propertyKey, item.injectToken, injector);
|
|
310
|
+
});
|
|
311
|
+
});
|
|
312
|
+
return instance;
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
function normalizeExistingProviderFactory(provider) {
|
|
318
|
+
return {
|
|
319
|
+
provide: provider.provide,
|
|
320
|
+
scope: null,
|
|
321
|
+
generateFactory(injector) {
|
|
322
|
+
return function () {
|
|
323
|
+
return injector.get(provider.useExisting);
|
|
324
|
+
};
|
|
325
|
+
},
|
|
326
|
+
deps: []
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
function normalizeFactoryProviderFactory(provider) {
|
|
330
|
+
return {
|
|
331
|
+
provide: provider.provide,
|
|
332
|
+
scope: null,
|
|
333
|
+
generateFactory() {
|
|
334
|
+
return function (...args) {
|
|
335
|
+
return provider.useFactory(...args);
|
|
336
|
+
};
|
|
337
|
+
},
|
|
338
|
+
deps: normalizeDeps(provider.provide, provider.deps || [])
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
function normalizeConstructorProviderFactory(provider) {
|
|
342
|
+
return normalizeClassProviderFactory(Object.assign(Object.assign({}, provider), { useClass: provider.provide }));
|
|
343
|
+
}
|
|
344
|
+
function normalizeTypeProviderFactory(provider) {
|
|
345
|
+
return normalizeClassProviderFactory({
|
|
346
|
+
provide: provider,
|
|
347
|
+
useClass: provider
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
function resolveClassParams(construct) {
|
|
351
|
+
const annotations = getAnnotations(construct);
|
|
352
|
+
const metadata = annotations.getClassMetadata(Injectable);
|
|
353
|
+
if (typeof metadata === 'undefined') {
|
|
354
|
+
throw new Error(`Class \`${stringify(construct)}\` is not injectable!`);
|
|
355
|
+
}
|
|
356
|
+
const deps = (metadata.paramTypes || []).map(i => [i]);
|
|
357
|
+
const metadataKeys = [Inject, Self, SkipSelf, Optional];
|
|
358
|
+
metadataKeys.forEach(key => {
|
|
359
|
+
(annotations.getParamMetadata(key) || []).forEach(item => {
|
|
360
|
+
deps[item.parameterIndex].push(item.metadata);
|
|
361
|
+
});
|
|
362
|
+
});
|
|
363
|
+
return {
|
|
364
|
+
scope: metadata.metadata.provideIn,
|
|
365
|
+
deps
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
function normalizeDeps(provide, deps) {
|
|
369
|
+
return deps.map((dep, index) => {
|
|
370
|
+
const r = {
|
|
371
|
+
injectKey: null,
|
|
372
|
+
optional: false,
|
|
373
|
+
visibility: null
|
|
374
|
+
};
|
|
375
|
+
if (!Array.isArray(dep)) {
|
|
376
|
+
r.injectKey = dep;
|
|
377
|
+
}
|
|
378
|
+
else {
|
|
379
|
+
for (let i = 0; i < dep.length; i++) {
|
|
380
|
+
const item = dep[i];
|
|
381
|
+
if (item instanceof Inject) {
|
|
382
|
+
r.injectKey = item.token;
|
|
383
|
+
}
|
|
384
|
+
else if (item instanceof Self || item instanceof SkipSelf) {
|
|
385
|
+
r.visibility = item;
|
|
386
|
+
}
|
|
387
|
+
else if (item instanceof Optional) {
|
|
388
|
+
r.optional = true;
|
|
389
|
+
}
|
|
390
|
+
else {
|
|
391
|
+
r.injectKey = item;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
if (typeof r.injectKey === 'undefined') {
|
|
396
|
+
throw new Error(`The ${index} th dependent parameter type of \`${stringify(provide)}\` was not obtained,
|
|
397
|
+
if the dependency is declared later, you can refer to it using \`constructor(@Inject(forwardRef(() => [Type|InjectionToken])) paramName: [Type]) {}\``);
|
|
398
|
+
}
|
|
399
|
+
return r;
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
const reflectiveInjectorErrorFn = (token) => {
|
|
404
|
+
return makeError('ReflectiveInjector')(`No provide for \`${stringify(token)}\`!`);
|
|
405
|
+
};
|
|
406
|
+
const provideScopeError = (token) => {
|
|
407
|
+
return makeError('ProvideScope')(`Can not found provide scope \`${stringify(token)}\`!`);
|
|
49
408
|
};
|
|
409
|
+
/**
|
|
410
|
+
* 反射注入器
|
|
411
|
+
*/
|
|
412
|
+
class ReflectiveInjector extends Injector {
|
|
413
|
+
constructor(parentInjector, staticProviders, scope) {
|
|
414
|
+
super();
|
|
415
|
+
this.parentInjector = parentInjector;
|
|
416
|
+
this.staticProviders = staticProviders;
|
|
417
|
+
this.scope = scope;
|
|
418
|
+
this.recordValues = new Map();
|
|
419
|
+
this.normalizedProviders = staticProviders.map(provide => {
|
|
420
|
+
return normalizeProvider(provide);
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* 用于获取当前注入器上下文内的实例、对象或数据
|
|
425
|
+
* @param token 访问 token
|
|
426
|
+
* @param notFoundValue 如未查找到的返回值
|
|
427
|
+
* @param flags 查询规则
|
|
428
|
+
*/
|
|
429
|
+
get(token, notFoundValue = THROW_IF_NOT_FOUND, flags) {
|
|
430
|
+
var _a;
|
|
431
|
+
flags = flags || InjectFlags.Default;
|
|
432
|
+
if (flags === InjectFlags.SkipSelf) {
|
|
433
|
+
if (this.parentInjector) {
|
|
434
|
+
return this.parentInjector.get(token, notFoundValue);
|
|
435
|
+
}
|
|
436
|
+
if (notFoundValue !== THROW_IF_NOT_FOUND) {
|
|
437
|
+
return notFoundValue;
|
|
438
|
+
}
|
|
439
|
+
throw reflectiveInjectorErrorFn(token);
|
|
440
|
+
}
|
|
441
|
+
if (this.recordValues.has(token)) {
|
|
442
|
+
return this.recordValues.get(token);
|
|
443
|
+
}
|
|
444
|
+
for (let i = 0; i < this.normalizedProviders.length; i++) {
|
|
445
|
+
const normalizedProvider = this.normalizedProviders[i];
|
|
446
|
+
if (normalizedProvider.provide === token) {
|
|
447
|
+
return this.getValue(token, THROW_IF_NOT_FOUND, normalizedProvider);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
if (!(token instanceof InjectionToken)) {
|
|
451
|
+
const scope = (_a = getAnnotations(token).getClassMetadata(Injectable)) === null || _a === void 0 ? void 0 : _a.metadata.provideIn;
|
|
452
|
+
if (scope) {
|
|
453
|
+
const normalizedProvider = normalizeProvider(token);
|
|
454
|
+
if (this.scope === scope) {
|
|
455
|
+
this.normalizedProviders.push(normalizedProvider);
|
|
456
|
+
return this.getValue(token, THROW_IF_NOT_FOUND, normalizedProvider);
|
|
457
|
+
}
|
|
458
|
+
const parentInjector = this.parentInjector;
|
|
459
|
+
if (!parentInjector || parentInjector instanceof NullInjector) {
|
|
460
|
+
if (normalizedProvider.scope === 'root') {
|
|
461
|
+
this.normalizedProviders.push(normalizedProvider);
|
|
462
|
+
return this.getValue(token, THROW_IF_NOT_FOUND, normalizedProvider);
|
|
463
|
+
}
|
|
464
|
+
if (notFoundValue !== THROW_IF_NOT_FOUND) {
|
|
465
|
+
return notFoundValue;
|
|
466
|
+
}
|
|
467
|
+
throw provideScopeError(normalizedProvider.scope);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
if (flags === InjectFlags.Self) {
|
|
472
|
+
if (notFoundValue === THROW_IF_NOT_FOUND) {
|
|
473
|
+
throw reflectiveInjectorErrorFn(token);
|
|
474
|
+
}
|
|
475
|
+
return notFoundValue;
|
|
476
|
+
}
|
|
477
|
+
if (this.parentInjector) {
|
|
478
|
+
return this.parentInjector.get(token, notFoundValue, flags === InjectFlags.Optional ? InjectFlags.Optional : InjectFlags.Default);
|
|
479
|
+
}
|
|
480
|
+
if (notFoundValue === THROW_IF_NOT_FOUND) {
|
|
481
|
+
throw reflectiveInjectorErrorFn(token);
|
|
482
|
+
}
|
|
483
|
+
return notFoundValue;
|
|
484
|
+
}
|
|
485
|
+
getValue(token, notFoundValue, normalizedProvider) {
|
|
486
|
+
const { generateFactory, deps } = normalizedProvider;
|
|
487
|
+
const params = this.resolveDeps(deps, notFoundValue);
|
|
488
|
+
let value = this.recordValues.get(token);
|
|
489
|
+
if (value) {
|
|
490
|
+
return value;
|
|
491
|
+
}
|
|
492
|
+
const factory = generateFactory(this, (token, value) => {
|
|
493
|
+
this.recordValues.set(token, value);
|
|
494
|
+
});
|
|
495
|
+
value = factory(...params);
|
|
496
|
+
this.recordValues.set(token, value);
|
|
497
|
+
return value;
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* 解决并获取依赖参数
|
|
501
|
+
* @param deps 依赖规则
|
|
502
|
+
* @param notFoundValue 未查找到时的返回值
|
|
503
|
+
* @private
|
|
504
|
+
*/
|
|
505
|
+
resolveDeps(deps, notFoundValue) {
|
|
506
|
+
return deps.map(dep => {
|
|
507
|
+
let reflectiveValue;
|
|
508
|
+
const tryValue = {};
|
|
509
|
+
const injectToken = dep.injectKey instanceof ForwardRef ? dep.injectKey.getRef() : dep.injectKey;
|
|
510
|
+
if (dep.visibility instanceof Self) {
|
|
511
|
+
reflectiveValue = this.get(injectToken, tryValue, InjectFlags.Self);
|
|
512
|
+
}
|
|
513
|
+
else if (dep.visibility instanceof SkipSelf) {
|
|
514
|
+
if (this.parentInjector) {
|
|
515
|
+
reflectiveValue = this.parentInjector.get(injectToken, tryValue);
|
|
516
|
+
}
|
|
517
|
+
else {
|
|
518
|
+
if (dep.optional) {
|
|
519
|
+
// if (notFoundValue === THROW_IF_NOT_FOUND) {
|
|
520
|
+
// return null
|
|
521
|
+
// }
|
|
522
|
+
return null;
|
|
523
|
+
}
|
|
524
|
+
throw reflectiveInjectorErrorFn(injectToken);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
else {
|
|
528
|
+
reflectiveValue = this.get(injectToken, tryValue);
|
|
529
|
+
}
|
|
530
|
+
if (reflectiveValue === tryValue) {
|
|
531
|
+
if (dep.optional) {
|
|
532
|
+
// if (notFoundValue === THROW_IF_NOT_FOUND) {
|
|
533
|
+
// return null
|
|
534
|
+
// }
|
|
535
|
+
// return notFoundValue
|
|
536
|
+
return null;
|
|
537
|
+
}
|
|
538
|
+
throw reflectiveInjectorErrorFn(injectToken);
|
|
539
|
+
}
|
|
540
|
+
return reflectiveValue;
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
const Type = Function;
|
|
50
546
|
|
|
51
547
|
const refKey = 'ref';
|
|
52
548
|
function getObjectChanges(newProps, oldProps) {
|
|
@@ -152,7 +648,10 @@ class Component extends ReflectiveInjector {
|
|
|
152
648
|
return this._changed;
|
|
153
649
|
}
|
|
154
650
|
constructor(context, type, props, key) {
|
|
155
|
-
super(context, [
|
|
651
|
+
super(context, [{
|
|
652
|
+
provide: Injector,
|
|
653
|
+
useFactory: () => this
|
|
654
|
+
}]);
|
|
156
655
|
this.type = type;
|
|
157
656
|
this.props = props;
|
|
158
657
|
this.key = key;
|
|
@@ -171,11 +670,11 @@ class Component extends ReflectiveInjector {
|
|
|
171
670
|
is(target) {
|
|
172
671
|
return target.$$typeOf === this.$$typeOf;
|
|
173
672
|
}
|
|
174
|
-
|
|
673
|
+
provide(providers) {
|
|
175
674
|
providers = Array.isArray(providers) ? providers : [providers];
|
|
176
675
|
this.normalizedProviders.unshift(...providers.map(i => normalizeProvider(i)));
|
|
177
676
|
}
|
|
178
|
-
|
|
677
|
+
setup() {
|
|
179
678
|
const self = this;
|
|
180
679
|
const props = new Proxy(this.props, {
|
|
181
680
|
get(_, key) {
|
|
@@ -603,7 +1102,7 @@ function useEffect(deps, effect) {
|
|
|
603
1102
|
*/
|
|
604
1103
|
function provide(provider) {
|
|
605
1104
|
const component = getSetupContext();
|
|
606
|
-
component.
|
|
1105
|
+
component.provide(provider);
|
|
607
1106
|
return component;
|
|
608
1107
|
}
|
|
609
1108
|
/**
|
|
@@ -611,7 +1110,7 @@ function provide(provider) {
|
|
|
611
1110
|
*/
|
|
612
1111
|
function inject(token, notFoundValue, flags) {
|
|
613
1112
|
const component = getSetupContext();
|
|
614
|
-
return component.
|
|
1113
|
+
return component.get(token, notFoundValue, flags || InjectFlags.SkipSelf);
|
|
615
1114
|
}
|
|
616
1115
|
|
|
617
1116
|
function Fragment(props) {
|
|
@@ -660,18 +1159,57 @@ function withMemo(shouldUpdate, render) {
|
|
|
660
1159
|
* Viewfly 根组件,用于实现组件状态更新事件通知
|
|
661
1160
|
*/
|
|
662
1161
|
class RootComponent extends Component {
|
|
663
|
-
constructor(factory, parentInjector
|
|
1162
|
+
constructor(factory, parentInjector) {
|
|
664
1163
|
super(parentInjector, factory, {});
|
|
665
|
-
this.
|
|
1164
|
+
this.onChange = null;
|
|
666
1165
|
}
|
|
667
1166
|
markAsChanged() {
|
|
1167
|
+
var _a;
|
|
668
1168
|
this._changed = true;
|
|
669
|
-
this.
|
|
1169
|
+
(_a = this.onChange) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
670
1170
|
}
|
|
671
1171
|
}
|
|
672
1172
|
|
|
1173
|
+
class NativeRenderer {
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
/******************************************************************************
|
|
1177
|
+
Copyright (c) Microsoft Corporation.
|
|
1178
|
+
|
|
1179
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
1180
|
+
purpose with or without fee is hereby granted.
|
|
1181
|
+
|
|
1182
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
1183
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
1184
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
1185
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
1186
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
1187
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
1188
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
1189
|
+
***************************************************************************** */
|
|
1190
|
+
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
1191
|
+
|
|
1192
|
+
|
|
1193
|
+
function __decorate(decorators, target, key, desc) {
|
|
1194
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
1195
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
1196
|
+
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;
|
|
1197
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1200
|
+
function __metadata(metadataKey, metadataValue) {
|
|
1201
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
1205
|
+
var e = new Error(message);
|
|
1206
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
1207
|
+
};
|
|
1208
|
+
|
|
673
1209
|
class RootComponentRef {
|
|
674
1210
|
}
|
|
1211
|
+
class HostRef {
|
|
1212
|
+
}
|
|
675
1213
|
class Atom {
|
|
676
1214
|
constructor(jsxNode, parent) {
|
|
677
1215
|
this.jsxNode = jsxNode;
|
|
@@ -682,25 +1220,30 @@ class Atom {
|
|
|
682
1220
|
}
|
|
683
1221
|
}
|
|
684
1222
|
let Renderer = class Renderer {
|
|
685
|
-
constructor(nativeRenderer, rootComponentRef) {
|
|
1223
|
+
constructor(nativeRenderer, rootComponentRef, hostRef) {
|
|
686
1224
|
this.nativeRenderer = nativeRenderer;
|
|
687
1225
|
this.rootComponentRef = rootComponentRef;
|
|
1226
|
+
this.hostRef = hostRef;
|
|
688
1227
|
this.componentAtomCaches = new WeakMap();
|
|
1228
|
+
this.isInit = true;
|
|
689
1229
|
}
|
|
690
1230
|
render() {
|
|
691
|
-
const
|
|
692
|
-
const
|
|
693
|
-
this.
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
1231
|
+
const component = this.rootComponentRef.component;
|
|
1232
|
+
const host = this.hostRef.host;
|
|
1233
|
+
if (this.isInit) {
|
|
1234
|
+
const atom = new Atom(component, null);
|
|
1235
|
+
this.buildView(atom, {
|
|
1236
|
+
isParent: true,
|
|
1237
|
+
host
|
|
1238
|
+
});
|
|
1239
|
+
}
|
|
1240
|
+
else {
|
|
1241
|
+
this.reconcile(component, {
|
|
1242
|
+
host,
|
|
1243
|
+
isParent: true
|
|
1244
|
+
});
|
|
1245
|
+
}
|
|
1246
|
+
this.isInit = false;
|
|
704
1247
|
}
|
|
705
1248
|
reconcile(component, context) {
|
|
706
1249
|
if (component.dirty) {
|
|
@@ -1018,7 +1561,7 @@ let Renderer = class Renderer {
|
|
|
1018
1561
|
}
|
|
1019
1562
|
}
|
|
1020
1563
|
componentRender(component, from) {
|
|
1021
|
-
const { template, render } = component.
|
|
1564
|
+
const { template, render } = component.setup();
|
|
1022
1565
|
if (template) {
|
|
1023
1566
|
this.linkTemplate(template, component, from);
|
|
1024
1567
|
}
|
|
@@ -1255,7 +1798,8 @@ let Renderer = class Renderer {
|
|
|
1255
1798
|
Renderer = __decorate([
|
|
1256
1799
|
Injectable(),
|
|
1257
1800
|
__metadata("design:paramtypes", [NativeRenderer,
|
|
1258
|
-
RootComponentRef
|
|
1801
|
+
RootComponentRef,
|
|
1802
|
+
HostRef])
|
|
1259
1803
|
], Renderer);
|
|
1260
1804
|
|
|
1261
1805
|
const viewflyErrorFn = makeError('Viewfly');
|
|
@@ -1264,8 +1808,7 @@ const viewflyErrorFn = makeError('Viewfly');
|
|
|
1264
1808
|
*/
|
|
1265
1809
|
class Viewfly extends ReflectiveInjector {
|
|
1266
1810
|
constructor(config) {
|
|
1267
|
-
super(new NullInjector(), [
|
|
1268
|
-
...(config.providers || []),
|
|
1811
|
+
super(config.context || new NullInjector(), [
|
|
1269
1812
|
Renderer,
|
|
1270
1813
|
{
|
|
1271
1814
|
provide: RootComponentRef,
|
|
@@ -1280,46 +1823,78 @@ class Viewfly extends ReflectiveInjector {
|
|
|
1280
1823
|
useFactory() {
|
|
1281
1824
|
throw viewflyErrorFn('You must implement the `NativeRenderer` interface to start Viewfly!');
|
|
1282
1825
|
}
|
|
1826
|
+
},
|
|
1827
|
+
{
|
|
1828
|
+
provide: HostRef,
|
|
1829
|
+
useFactory() {
|
|
1830
|
+
throw viewflyErrorFn('Viewfly has not mounted!');
|
|
1831
|
+
}
|
|
1283
1832
|
}
|
|
1284
1833
|
]);
|
|
1285
1834
|
this.config = config;
|
|
1286
1835
|
this.destroyed = false;
|
|
1287
|
-
this.
|
|
1836
|
+
this.task = null;
|
|
1288
1837
|
this.rootComponent = this.createRootComponent(config.root);
|
|
1289
1838
|
}
|
|
1839
|
+
provide(providers) {
|
|
1840
|
+
providers = Array.isArray(providers) ? providers : [providers];
|
|
1841
|
+
this.normalizedProviders.unshift(...providers.map(i => normalizeProvider(i)));
|
|
1842
|
+
return this;
|
|
1843
|
+
}
|
|
1290
1844
|
/**
|
|
1291
1845
|
* 启动 Viewfly
|
|
1292
1846
|
* @param host 应用根节点
|
|
1293
1847
|
*/
|
|
1294
1848
|
mount(host) {
|
|
1295
|
-
|
|
1296
|
-
|
|
1849
|
+
this.provide({
|
|
1850
|
+
provide: HostRef,
|
|
1851
|
+
useValue: {
|
|
1852
|
+
host
|
|
1853
|
+
}
|
|
1854
|
+
});
|
|
1297
1855
|
const renderer = this.get(Renderer);
|
|
1298
1856
|
renderer.render();
|
|
1299
1857
|
if (this.config.autoUpdate === false) {
|
|
1300
|
-
return;
|
|
1858
|
+
return this;
|
|
1301
1859
|
}
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1860
|
+
const refresh = () => {
|
|
1861
|
+
if (this.destroyed) {
|
|
1862
|
+
return;
|
|
1863
|
+
}
|
|
1864
|
+
renderer.render();
|
|
1865
|
+
};
|
|
1866
|
+
this.rootComponent.onChange = () => {
|
|
1867
|
+
this.microTask(refresh);
|
|
1868
|
+
};
|
|
1869
|
+
return this;
|
|
1870
|
+
}
|
|
1871
|
+
render() {
|
|
1872
|
+
const renderer = this.get(Renderer);
|
|
1873
|
+
renderer.render();
|
|
1305
1874
|
}
|
|
1306
1875
|
/**
|
|
1307
1876
|
* 销毁 Viewfly 实例
|
|
1308
1877
|
*/
|
|
1309
1878
|
destroy() {
|
|
1310
|
-
const renderer = this.get(Renderer);
|
|
1311
1879
|
this.destroyed = true;
|
|
1312
1880
|
this.rootComponent.markAsDirtied();
|
|
1313
|
-
this.
|
|
1314
|
-
renderer.refresh();
|
|
1881
|
+
this.render();
|
|
1315
1882
|
}
|
|
1316
1883
|
createRootComponent(rootNode) {
|
|
1317
1884
|
return new RootComponent(() => {
|
|
1318
1885
|
return () => {
|
|
1319
1886
|
return this.destroyed ? null : rootNode;
|
|
1320
1887
|
};
|
|
1321
|
-
}, this
|
|
1888
|
+
}, this);
|
|
1889
|
+
}
|
|
1890
|
+
microTask(callback) {
|
|
1891
|
+
if (!this.task) {
|
|
1892
|
+
this.task = Promise.resolve().then(() => {
|
|
1893
|
+
this.task = null;
|
|
1894
|
+
callback();
|
|
1895
|
+
});
|
|
1896
|
+
}
|
|
1322
1897
|
}
|
|
1323
1898
|
}
|
|
1324
1899
|
|
|
1325
|
-
export { Component, Fragment, JSXComponent, JSXElement, JSXText, NativeRenderer, Ref, Renderer, RootComponent, RootComponentRef, Viewfly, inject, jsx, jsxs, makeError, onDestroy, onMounted, onPropsChanged, onUpdated, provide, useDerived, useEffect, useRef, useSignal, withMemo };
|
|
1900
|
+
export { Component, ForwardRef, Fragment, HostRef, Inject, InjectFlags, Injectable, InjectionToken, Injector, JSXComponent, JSXElement, JSXText, NativeRenderer, NullInjector, Optional, Prop, Ref, ReflectiveInjector, Renderer, RootComponent, RootComponentRef, Scope, Self, SkipSelf, THROW_IF_NOT_FOUND, Type, Viewfly, forwardRef, inject, jsx, jsxs, makeError, normalizeProvider, onDestroy, onMounted, onPropsChanged, onUpdated, provide, useDerived, useEffect, useRef, useSignal, withMemo };
|