@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.
Files changed (34) hide show
  1. package/bundles/di/_api.d.ts +10 -0
  2. package/bundles/di/forward-ref.d.ts +10 -0
  3. package/bundles/di/injectable.d.ts +20 -0
  4. package/bundles/di/injection-token.d.ts +8 -0
  5. package/bundles/di/injector.d.ts +22 -0
  6. package/bundles/di/metadata.d.ts +43 -0
  7. package/bundles/di/null-injector.d.ts +6 -0
  8. package/bundles/di/provider.d.ts +30 -0
  9. package/bundles/di/reflective-injector.d.ts +32 -0
  10. package/bundles/di/reflective-provider.d.ts +20 -0
  11. package/bundles/di/type.d.ts +7 -0
  12. package/bundles/di/utils/_api.d.ts +3 -0
  13. package/bundles/di/utils/annotations.d.ts +33 -0
  14. package/bundles/di/utils/decorators.d.ts +17 -0
  15. package/bundles/di/utils/stringify.d.ts +1 -0
  16. package/bundles/foundation/_api.d.ts +5 -0
  17. package/bundles/foundation/_utils.d.ts +4 -0
  18. package/bundles/{model → foundation}/component.d.ts +6 -6
  19. package/bundles/{model → foundation}/jsx-element.d.ts +3 -6
  20. package/bundles/foundation/renderer.d.ts +6 -3
  21. package/bundles/{model → foundation}/root.component.d.ts +3 -4
  22. package/bundles/{model → foundation}/types.d.ts +6 -4
  23. package/bundles/index.esm.js +648 -73
  24. package/bundles/index.js +668 -81
  25. package/bundles/public-api.d.ts +1 -2
  26. package/bundles/viewfly.d.ts +10 -10
  27. package/package.json +2 -4
  28. package/bundles/model/_api.d.ts +0 -5
  29. package/jsx-runtime/index.d.ts +0 -23
  30. package/jsx-runtime/index.esm.js +0 -11
  31. package/jsx-runtime/index.js +0 -14
  32. package/jsx-runtime/node_modules/.bin/rimraf +0 -17
  33. package/jsx-runtime/node_modules/.bin/rollup +0 -17
  34. /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
- var di = require('@tanbo/di');
5
- var stream = require('@tanbo/stream');
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,41 +195,356 @@ function makeError(name) {
13
195
  };
14
196
  }
15
197
 
16
- class NativeRenderer {
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
- Copyright (c) Microsoft Corporation.
21
-
22
- Permission to use, copy, modify, and/or distribute this software for any
23
- purpose with or without fee is hereby granted.
24
-
25
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
26
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
27
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
28
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
29
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
30
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
31
- PERFORMANCE OF THIS SOFTWARE.
32
- ***************************************************************************** */
33
- /* global Reflect, Promise, SuppressedError, Symbol */
34
-
35
-
36
- function __decorate(decorators, target, key, desc) {
37
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
38
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
39
- 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;
40
- return c > 3 && r && Object.defineProperty(target, key, r), r;
41
- }
42
-
43
- function __metadata(metadataKey, metadataValue) {
44
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
45
- }
46
-
47
- typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
48
- var e = new Error(message);
49
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
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
+ }
249
+ };
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)}\`!`);
50
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;
51
548
 
52
549
  const refKey = 'ref';
53
550
  function getObjectChanges(newProps, oldProps) {
@@ -145,7 +642,7 @@ class JSXComponent {
145
642
  /**
146
643
  * Viewfly 组件管理类,用于管理组件的生命周期,上下文等
147
644
  */
148
- class Component extends di.ReflectiveInjector {
645
+ class Component extends ReflectiveInjector {
149
646
  get dirty() {
150
647
  return this._dirty;
151
648
  }
@@ -153,7 +650,10 @@ class Component extends di.ReflectiveInjector {
153
650
  return this._changed;
154
651
  }
155
652
  constructor(context, type, props, key) {
156
- super(context, []);
653
+ super(context, [{
654
+ provide: Injector,
655
+ useFactory: () => this
656
+ }]);
157
657
  this.type = type;
158
658
  this.props = props;
159
659
  this.key = key;
@@ -172,11 +672,11 @@ class Component extends di.ReflectiveInjector {
172
672
  is(target) {
173
673
  return target.$$typeOf === this.$$typeOf;
174
674
  }
175
- addProvide(providers) {
675
+ provide(providers) {
176
676
  providers = Array.isArray(providers) ? providers : [providers];
177
- this.normalizedProviders.unshift(...providers.map(i => di.normalizeProvider(i)));
677
+ this.normalizedProviders.unshift(...providers.map(i => normalizeProvider(i)));
178
678
  }
179
- init() {
679
+ setup() {
180
680
  const self = this;
181
681
  const props = new Proxy(this.props, {
182
682
  get(_, key) {
@@ -604,7 +1104,7 @@ function useEffect(deps, effect) {
604
1104
  */
605
1105
  function provide(provider) {
606
1106
  const component = getSetupContext();
607
- component.addProvide(provider);
1107
+ component.provide(provider);
608
1108
  return component;
609
1109
  }
610
1110
  /**
@@ -612,7 +1112,7 @@ function provide(provider) {
612
1112
  */
613
1113
  function inject(token, notFoundValue, flags) {
614
1114
  const component = getSetupContext();
615
- return component.parentInjector.get(token, notFoundValue, flags);
1115
+ return component.get(token, notFoundValue, flags || exports.InjectFlags.SkipSelf);
616
1116
  }
617
1117
 
618
1118
  function Fragment(props) {
@@ -661,18 +1161,57 @@ function withMemo(shouldUpdate, render) {
661
1161
  * Viewfly 根组件,用于实现组件状态更新事件通知
662
1162
  */
663
1163
  class RootComponent extends Component {
664
- constructor(factory, parentInjector = new di.NullInjector()) {
1164
+ constructor(factory, parentInjector) {
665
1165
  super(parentInjector, factory, {});
666
- this.changeEmitter = new stream.Subject();
1166
+ this.onChange = null;
667
1167
  }
668
1168
  markAsChanged() {
1169
+ var _a;
669
1170
  this._changed = true;
670
- this.changeEmitter.next();
1171
+ (_a = this.onChange) === null || _a === void 0 ? void 0 : _a.call(this);
671
1172
  }
672
1173
  }
673
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
+
674
1211
  class RootComponentRef {
675
1212
  }
1213
+ class HostRef {
1214
+ }
676
1215
  class Atom {
677
1216
  constructor(jsxNode, parent) {
678
1217
  this.jsxNode = jsxNode;
@@ -683,25 +1222,30 @@ class Atom {
683
1222
  }
684
1223
  }
685
1224
  exports.Renderer = class Renderer {
686
- constructor(nativeRenderer, rootComponentRef) {
1225
+ constructor(nativeRenderer, rootComponentRef, hostRef) {
687
1226
  this.nativeRenderer = nativeRenderer;
688
1227
  this.rootComponentRef = rootComponentRef;
1228
+ this.hostRef = hostRef;
689
1229
  this.componentAtomCaches = new WeakMap();
1230
+ this.isInit = true;
690
1231
  }
691
1232
  render() {
692
- const { component, host } = this.rootComponentRef;
693
- const atom = new Atom(component, null);
694
- this.buildView(atom, {
695
- isParent: true,
696
- host
697
- });
698
- }
699
- refresh() {
700
- const { component, host } = this.rootComponentRef;
701
- this.reconcile(component, {
702
- host,
703
- isParent: true
704
- });
1233
+ const component = this.rootComponentRef.component;
1234
+ const host = this.hostRef.host;
1235
+ if (this.isInit) {
1236
+ const atom = new Atom(component, null);
1237
+ this.buildView(atom, {
1238
+ isParent: true,
1239
+ host
1240
+ });
1241
+ }
1242
+ else {
1243
+ this.reconcile(component, {
1244
+ host,
1245
+ isParent: true
1246
+ });
1247
+ }
1248
+ this.isInit = false;
705
1249
  }
706
1250
  reconcile(component, context) {
707
1251
  if (component.dirty) {
@@ -1019,7 +1563,7 @@ exports.Renderer = class Renderer {
1019
1563
  }
1020
1564
  }
1021
1565
  componentRender(component, from) {
1022
- const { template, render } = component.init();
1566
+ const { template, render } = component.setup();
1023
1567
  if (template) {
1024
1568
  this.linkTemplate(template, component, from);
1025
1569
  }
@@ -1254,19 +1798,19 @@ exports.Renderer = class Renderer {
1254
1798
  }
1255
1799
  };
1256
1800
  exports.Renderer = __decorate([
1257
- di.Injectable(),
1801
+ Injectable(),
1258
1802
  __metadata("design:paramtypes", [NativeRenderer,
1259
- RootComponentRef])
1803
+ RootComponentRef,
1804
+ HostRef])
1260
1805
  ], exports.Renderer);
1261
1806
 
1262
1807
  const viewflyErrorFn = makeError('Viewfly');
1263
1808
  /**
1264
1809
  * Viewfly 核心类,用于启动一个 Viewfly 应用
1265
1810
  */
1266
- class Viewfly extends di.ReflectiveInjector {
1811
+ class Viewfly extends ReflectiveInjector {
1267
1812
  constructor(config) {
1268
- super(new di.NullInjector(), [
1269
- ...(config.providers || []),
1813
+ super(config.context || new NullInjector(), [
1270
1814
  exports.Renderer,
1271
1815
  {
1272
1816
  provide: RootComponentRef,
@@ -1281,62 +1825,111 @@ class Viewfly extends di.ReflectiveInjector {
1281
1825
  useFactory() {
1282
1826
  throw viewflyErrorFn('You must implement the `NativeRenderer` interface to start Viewfly!');
1283
1827
  }
1828
+ },
1829
+ {
1830
+ provide: HostRef,
1831
+ useFactory() {
1832
+ throw viewflyErrorFn('Viewfly has not mounted!');
1833
+ }
1284
1834
  }
1285
1835
  ]);
1286
1836
  this.config = config;
1287
1837
  this.destroyed = false;
1288
- this.subscription = new stream.Subscription();
1838
+ this.task = null;
1289
1839
  this.rootComponent = this.createRootComponent(config.root);
1290
1840
  }
1841
+ provide(providers) {
1842
+ providers = Array.isArray(providers) ? providers : [providers];
1843
+ this.normalizedProviders.unshift(...providers.map(i => normalizeProvider(i)));
1844
+ return this;
1845
+ }
1291
1846
  /**
1292
1847
  * 启动 Viewfly
1293
1848
  * @param host 应用根节点
1294
1849
  */
1295
1850
  mount(host) {
1296
- const rootComponentRef = this.get(RootComponentRef);
1297
- rootComponentRef.host = host;
1851
+ this.provide({
1852
+ provide: HostRef,
1853
+ useValue: {
1854
+ host
1855
+ }
1856
+ });
1298
1857
  const renderer = this.get(exports.Renderer);
1299
1858
  renderer.render();
1300
1859
  if (this.config.autoUpdate === false) {
1301
- return;
1860
+ return this;
1302
1861
  }
1303
- this.subscription.add(this.rootComponent.changeEmitter.pipe(stream.microTask()).subscribe(() => {
1304
- renderer.refresh();
1305
- }));
1862
+ const refresh = () => {
1863
+ if (this.destroyed) {
1864
+ return;
1865
+ }
1866
+ renderer.render();
1867
+ };
1868
+ this.rootComponent.onChange = () => {
1869
+ this.microTask(refresh);
1870
+ };
1871
+ return this;
1872
+ }
1873
+ render() {
1874
+ const renderer = this.get(exports.Renderer);
1875
+ renderer.render();
1306
1876
  }
1307
1877
  /**
1308
1878
  * 销毁 Viewfly 实例
1309
1879
  */
1310
1880
  destroy() {
1311
- const renderer = this.get(exports.Renderer);
1312
1881
  this.destroyed = true;
1313
1882
  this.rootComponent.markAsDirtied();
1314
- this.subscription.unsubscribe();
1315
- renderer.refresh();
1883
+ this.render();
1316
1884
  }
1317
1885
  createRootComponent(rootNode) {
1318
1886
  return new RootComponent(() => {
1319
1887
  return () => {
1320
1888
  return this.destroyed ? null : rootNode;
1321
1889
  };
1322
- }, this.config.context);
1890
+ }, this);
1891
+ }
1892
+ microTask(callback) {
1893
+ if (!this.task) {
1894
+ this.task = Promise.resolve().then(() => {
1895
+ this.task = null;
1896
+ callback();
1897
+ });
1898
+ }
1323
1899
  }
1324
1900
  }
1325
1901
 
1326
1902
  exports.Component = Component;
1903
+ exports.ForwardRef = ForwardRef;
1327
1904
  exports.Fragment = Fragment;
1905
+ exports.HostRef = HostRef;
1906
+ exports.Inject = Inject;
1907
+ exports.Injectable = Injectable;
1908
+ exports.InjectionToken = InjectionToken;
1909
+ exports.Injector = Injector;
1328
1910
  exports.JSXComponent = JSXComponent;
1329
1911
  exports.JSXElement = JSXElement;
1330
1912
  exports.JSXText = JSXText;
1331
1913
  exports.NativeRenderer = NativeRenderer;
1914
+ exports.NullInjector = NullInjector;
1915
+ exports.Optional = Optional;
1916
+ exports.Prop = Prop;
1332
1917
  exports.Ref = Ref;
1918
+ exports.ReflectiveInjector = ReflectiveInjector;
1333
1919
  exports.RootComponent = RootComponent;
1334
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;
1335
1926
  exports.Viewfly = Viewfly;
1927
+ exports.forwardRef = forwardRef;
1336
1928
  exports.inject = inject;
1337
1929
  exports.jsx = jsx;
1338
1930
  exports.jsxs = jsxs;
1339
1931
  exports.makeError = makeError;
1932
+ exports.normalizeProvider = normalizeProvider;
1340
1933
  exports.onDestroy = onDestroy;
1341
1934
  exports.onMounted = onMounted;
1342
1935
  exports.onPropsChanged = onPropsChanged;
@@ -1347,9 +1940,3 @@ exports.useEffect = useEffect;
1347
1940
  exports.useRef = useRef;
1348
1941
  exports.useSignal = useSignal;
1349
1942
  exports.withMemo = withMemo;
1350
- Object.keys(di).forEach(function (k) {
1351
- if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
1352
- enumerable: true,
1353
- get: function () { return di[k]; }
1354
- });
1355
- });