@viewfly/core 0.6.3 → 1.0.0-alpha.1
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/injector.d.ts +1 -1
- package/bundles/di/metadata.d.ts +2 -2
- package/bundles/di/null-injector.d.ts +2 -2
- package/bundles/di/reflective-injector.d.ts +2 -2
- package/bundles/foundation/component.d.ts +20 -5
- package/bundles/foundation/types.d.ts +6 -1
- package/bundles/index.esm.js +50 -32
- package/bundles/index.js +50 -32
- package/package.json +2 -2
package/bundles/di/injector.d.ts
CHANGED
|
@@ -22,5 +22,5 @@ export type ExtractValueType<T> = T extends Type<any> ? InstanceType<T> : T exte
|
|
|
22
22
|
*/
|
|
23
23
|
export declare abstract class Injector {
|
|
24
24
|
abstract parentInjector: Injector | null;
|
|
25
|
-
abstract get<T extends Type<any> | AbstractType<any> | InjectionToken<any>, U = never>(token: T,
|
|
25
|
+
abstract get<T extends Type<any> | AbstractType<any> | InjectionToken<any>, U = never>(token: T, flags?: InjectFlags, notFoundValue?: U): ExtractValueType<T> | U;
|
|
26
26
|
}
|
package/bundles/di/metadata.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AbstractType, Type } from './type';
|
|
2
|
-
import { InjectFlags } from './injector';
|
|
2
|
+
import { ExtractValueType, InjectFlags } from './injector';
|
|
3
3
|
import { InjectionToken } from './injection-token';
|
|
4
4
|
import { ForwardRef } from './forward-ref';
|
|
5
5
|
export interface Inject {
|
|
@@ -37,7 +37,7 @@ export declare const Optional: OptionalDecorator;
|
|
|
37
37
|
export interface Prop {
|
|
38
38
|
}
|
|
39
39
|
export interface PropDecorator {
|
|
40
|
-
<T
|
|
40
|
+
<T extends Type<any> | AbstractType<any> | InjectionToken<any>, U = never>(token?: T | ForwardRef<ExtractValueType<T>>, flags?: InjectFlags, notFoundValue?: U): PropertyDecorator;
|
|
41
41
|
new (token: any): Prop;
|
|
42
42
|
}
|
|
43
43
|
export declare const Prop: PropDecorator;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Injector } from './injector';
|
|
1
|
+
import { InjectFlags, Injector } from './injector';
|
|
2
2
|
export declare const THROW_IF_NOT_FOUND: any;
|
|
3
3
|
export declare class NullInjector extends Injector {
|
|
4
4
|
parentInjector: null;
|
|
5
|
-
get(token: any, notFoundValue?: any): any;
|
|
5
|
+
get(token: any, flag?: InjectFlags, notFoundValue?: any): any;
|
|
6
6
|
}
|
|
@@ -17,10 +17,10 @@ export declare class ReflectiveInjector extends Injector {
|
|
|
17
17
|
/**
|
|
18
18
|
* 用于获取当前注入器上下文内的实例、对象或数据
|
|
19
19
|
* @param token 访问 token
|
|
20
|
-
* @param notFoundValue 如未查找到的返回值
|
|
21
20
|
* @param flags 查询规则
|
|
21
|
+
* @param notFoundValue 如未查找到的返回值
|
|
22
22
|
*/
|
|
23
|
-
get<T extends Type<any> | AbstractType<any> | InjectionToken<any>, U = never>(token: T,
|
|
23
|
+
get<T extends Type<any> | AbstractType<any> | InjectionToken<any>, U = never>(token: T, flags?: InjectFlags, notFoundValue?: U): ExtractValueType<T> | U;
|
|
24
24
|
private getValue;
|
|
25
25
|
/**
|
|
26
26
|
* 解决并获取依赖参数
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AbstractType, InjectFlags, InjectionToken, Injector, Provider, ReflectiveInjector, Type } from '../di/_api';
|
|
1
|
+
import { AbstractType, ExtractValueType, InjectFlags, InjectionToken, Injector, Provider, ReflectiveInjector, Type } from '../di/_api';
|
|
2
2
|
import { Key, Props } from './jsx-element';
|
|
3
3
|
import { ComponentView } from './_utils';
|
|
4
4
|
/**
|
|
@@ -215,14 +215,29 @@ export declare function watch<T, T1, T2, T3, T4, T5, T6, T7>(deps: [Signal<T>, S
|
|
|
215
215
|
export declare function watch<T>(deps: () => T, callback: WatchCallback<T, T>): () => void;
|
|
216
216
|
export declare function watch<T = any>(deps: Signal<any>[], callback: WatchCallback<T[], T[]>): () => void;
|
|
217
217
|
/**
|
|
218
|
-
*
|
|
219
|
-
* @param
|
|
218
|
+
* 给组件添加注解
|
|
219
|
+
* @param annotation
|
|
220
|
+
* @param componentSetup
|
|
221
|
+
* @example
|
|
222
|
+
* ```ts
|
|
223
|
+
* export customScope = new Scope('scopeName')
|
|
224
|
+
* export const App = withAnnotation({
|
|
225
|
+
* scope: customScope,
|
|
226
|
+
* providers: [
|
|
227
|
+
* ExampleService
|
|
228
|
+
* ]
|
|
229
|
+
* }, function(props: Props) {
|
|
230
|
+
* return () => {
|
|
231
|
+
* return <div>...</div>
|
|
232
|
+
* }
|
|
233
|
+
* })
|
|
234
|
+
* ```
|
|
220
235
|
*/
|
|
221
|
-
export declare function
|
|
236
|
+
export declare function withAnnotation<T extends JSXInternal.ComponentSetup>(annotation: JSXInternal.ComponentAnnotation, componentSetup: T): T;
|
|
222
237
|
/**
|
|
223
238
|
* 通过组件上下文获取 IoC 容器内数据的勾子方法
|
|
224
239
|
*/
|
|
225
|
-
export declare function inject<T
|
|
240
|
+
export declare function inject<T extends Type<any> | AbstractType<any> | InjectionToken<any>, U = never>(token: T, flags?: InjectFlags, notFoundValue?: U): ExtractValueType<T> | U;
|
|
226
241
|
/**
|
|
227
242
|
* 获取当前组件实例
|
|
228
243
|
*/
|
|
@@ -2,6 +2,7 @@ import { Key } from './jsx-element';
|
|
|
2
2
|
import { ExtractInstanceType, DynamicRef } from './component';
|
|
3
3
|
import { Scope } from '../di/injectable';
|
|
4
4
|
import { NativeNode } from './injection-tokens';
|
|
5
|
+
import { Provider } from '../di/provider';
|
|
5
6
|
export type ViewNode = JSXInternal.ViewNode;
|
|
6
7
|
declare global {
|
|
7
8
|
namespace JSXInternal {
|
|
@@ -12,9 +13,13 @@ declare global {
|
|
|
12
13
|
$useMemo?(currentProps: P, prevProps: P): boolean;
|
|
13
14
|
}
|
|
14
15
|
type ViewNode = Element | JSXInternal.ElementClass | string | number | boolean | null | undefined | Iterable<ViewNode>;
|
|
16
|
+
interface ComponentAnnotation {
|
|
17
|
+
scope?: Scope;
|
|
18
|
+
providers?: Provider[];
|
|
19
|
+
}
|
|
15
20
|
interface ComponentSetup<P = any> {
|
|
16
21
|
(props: P): (() => ViewNode) | ComponentInstance<P>;
|
|
17
|
-
|
|
22
|
+
annotation?: ComponentAnnotation;
|
|
18
23
|
}
|
|
19
24
|
type Element<P = any, C extends string | ComponentSetup<P> = string | ComponentSetup<P>> = C extends string ? IntrinsicElements[C] : (() => Element) | ComponentInstance<P>;
|
|
20
25
|
interface IntrinsicAttributes {
|
package/bundles/index.esm.js
CHANGED
|
@@ -205,7 +205,7 @@ class NullInjector extends Injector {
|
|
|
205
205
|
super(...arguments);
|
|
206
206
|
this.parentInjector = null;
|
|
207
207
|
}
|
|
208
|
-
get(token, notFoundValue = THROW_IF_NOT_FOUND) {
|
|
208
|
+
get(token, flag, notFoundValue = THROW_IF_NOT_FOUND) {
|
|
209
209
|
if (notFoundValue === THROW_IF_NOT_FOUND) {
|
|
210
210
|
throw nullInjectorErrorFn(token);
|
|
211
211
|
}
|
|
@@ -239,10 +239,10 @@ const Optional = function OptionalDecorator() {
|
|
|
239
239
|
return makeParamDecorator(Optional, new Optional());
|
|
240
240
|
}
|
|
241
241
|
};
|
|
242
|
-
const Prop = function PropDecorator(token, notFoundValue = THROW_IF_NOT_FOUND
|
|
242
|
+
const Prop = function PropDecorator(token, flags, notFoundValue = THROW_IF_NOT_FOUND) {
|
|
243
243
|
if (!(this instanceof Prop)) {
|
|
244
244
|
return makePropertyDecorator(Prop, token, function (instance, propertyName, token, injector) {
|
|
245
|
-
instance[propertyName] = injector.get(token instanceof ForwardRef ? token.getRef() : token,
|
|
245
|
+
instance[propertyName] = injector.get(token instanceof ForwardRef ? token.getRef() : token, flags, notFoundValue);
|
|
246
246
|
});
|
|
247
247
|
}
|
|
248
248
|
};
|
|
@@ -424,15 +424,15 @@ class ReflectiveInjector extends Injector {
|
|
|
424
424
|
/**
|
|
425
425
|
* 用于获取当前注入器上下文内的实例、对象或数据
|
|
426
426
|
* @param token 访问 token
|
|
427
|
-
* @param notFoundValue 如未查找到的返回值
|
|
428
427
|
* @param flags 查询规则
|
|
428
|
+
* @param notFoundValue 如未查找到的返回值
|
|
429
429
|
*/
|
|
430
|
-
get(token, notFoundValue = THROW_IF_NOT_FOUND
|
|
430
|
+
get(token, flags = InjectFlags.Default, notFoundValue = THROW_IF_NOT_FOUND) {
|
|
431
431
|
var _a;
|
|
432
432
|
flags = flags || InjectFlags.Default;
|
|
433
433
|
if (flags === InjectFlags.SkipSelf) {
|
|
434
434
|
if (this.parentInjector) {
|
|
435
|
-
return this.parentInjector.get(token, notFoundValue);
|
|
435
|
+
return this.parentInjector.get(token, InjectFlags.Default, notFoundValue);
|
|
436
436
|
}
|
|
437
437
|
if (notFoundValue !== THROW_IF_NOT_FOUND) {
|
|
438
438
|
return notFoundValue;
|
|
@@ -476,7 +476,7 @@ class ReflectiveInjector extends Injector {
|
|
|
476
476
|
return notFoundValue;
|
|
477
477
|
}
|
|
478
478
|
if (this.parentInjector) {
|
|
479
|
-
return this.parentInjector.get(token,
|
|
479
|
+
return this.parentInjector.get(token, flags === InjectFlags.Optional ? InjectFlags.Optional : InjectFlags.Default, notFoundValue);
|
|
480
480
|
}
|
|
481
481
|
if (notFoundValue === THROW_IF_NOT_FOUND) {
|
|
482
482
|
throw reflectiveInjectorErrorFn(token);
|
|
@@ -508,11 +508,11 @@ class ReflectiveInjector extends Injector {
|
|
|
508
508
|
const tryValue = {};
|
|
509
509
|
const injectToken = dep.injectKey instanceof ForwardRef ? dep.injectKey.getRef() : dep.injectKey;
|
|
510
510
|
if (dep.visibility instanceof Self) {
|
|
511
|
-
reflectiveValue = this.get(injectToken,
|
|
511
|
+
reflectiveValue = this.get(injectToken, InjectFlags.Self, tryValue);
|
|
512
512
|
}
|
|
513
513
|
else if (dep.visibility instanceof SkipSelf) {
|
|
514
514
|
if (this.parentInjector) {
|
|
515
|
-
reflectiveValue = this.parentInjector.get(injectToken, tryValue);
|
|
515
|
+
reflectiveValue = this.parentInjector.get(injectToken, InjectFlags.Default, tryValue);
|
|
516
516
|
}
|
|
517
517
|
else {
|
|
518
518
|
if (dep.optional) {
|
|
@@ -525,7 +525,7 @@ class ReflectiveInjector extends Injector {
|
|
|
525
525
|
}
|
|
526
526
|
}
|
|
527
527
|
else {
|
|
528
|
-
reflectiveValue = this.get(injectToken, tryValue);
|
|
528
|
+
reflectiveValue = this.get(injectToken, InjectFlags.Default, tryValue);
|
|
529
529
|
}
|
|
530
530
|
if (reflectiveValue === tryValue) {
|
|
531
531
|
if (dep.optional) {
|
|
@@ -656,7 +656,12 @@ class Component extends ReflectiveInjector {
|
|
|
656
656
|
return this._changed;
|
|
657
657
|
}
|
|
658
658
|
constructor(parentComponent, type, props, key) {
|
|
659
|
-
|
|
659
|
+
const annotation = type.annotation || {};
|
|
660
|
+
const providers = annotation.providers || [];
|
|
661
|
+
super(parentComponent, [...providers, {
|
|
662
|
+
provide: Injector,
|
|
663
|
+
useFactory: () => this
|
|
664
|
+
}], annotation.scope);
|
|
660
665
|
this.parentComponent = parentComponent;
|
|
661
666
|
this.type = type;
|
|
662
667
|
this.props = props;
|
|
@@ -1199,19 +1204,37 @@ function watch(deps, callback) {
|
|
|
1199
1204
|
return destroyFn;
|
|
1200
1205
|
}
|
|
1201
1206
|
/**
|
|
1202
|
-
*
|
|
1203
|
-
* @param
|
|
1207
|
+
* 给组件添加注解
|
|
1208
|
+
* @param annotation
|
|
1209
|
+
* @param componentSetup
|
|
1210
|
+
* @example
|
|
1211
|
+
* ```ts
|
|
1212
|
+
* export customScope = new Scope('scopeName')
|
|
1213
|
+
* export const App = withAnnotation({
|
|
1214
|
+
* scope: customScope,
|
|
1215
|
+
* providers: [
|
|
1216
|
+
* ExampleService
|
|
1217
|
+
* ]
|
|
1218
|
+
* }, function(props: Props) {
|
|
1219
|
+
* return () => {
|
|
1220
|
+
* return <div>...</div>
|
|
1221
|
+
* }
|
|
1222
|
+
* })
|
|
1223
|
+
* ```
|
|
1204
1224
|
*/
|
|
1205
|
-
function
|
|
1206
|
-
const
|
|
1207
|
-
|
|
1225
|
+
function withAnnotation(annotation, componentSetup) {
|
|
1226
|
+
const setup = function setup(props) {
|
|
1227
|
+
return componentSetup(props);
|
|
1228
|
+
};
|
|
1229
|
+
setup.annotation = annotation;
|
|
1230
|
+
return setup;
|
|
1208
1231
|
}
|
|
1209
1232
|
/**
|
|
1210
1233
|
* 通过组件上下文获取 IoC 容器内数据的勾子方法
|
|
1211
1234
|
*/
|
|
1212
|
-
function inject(token,
|
|
1235
|
+
function inject(token, flags = InjectFlags.Default, notFoundValue = THROW_IF_NOT_FOUND) {
|
|
1213
1236
|
const component = getSetupContext();
|
|
1214
|
-
return component.get(token,
|
|
1237
|
+
return component.get(token, flags, notFoundValue);
|
|
1215
1238
|
}
|
|
1216
1239
|
/**
|
|
1217
1240
|
* 获取当前组件实例
|
|
@@ -1848,19 +1871,19 @@ class RootComponent extends Component {
|
|
|
1848
1871
|
const viewflyErrorFn = makeError('Viewfly');
|
|
1849
1872
|
function viewfly(config) {
|
|
1850
1873
|
const { context, nativeRenderer, autoUpdate, root } = Object.assign({ autoUpdate: true }, config);
|
|
1851
|
-
const appProviders = [{
|
|
1852
|
-
provide: NativeRenderer,
|
|
1853
|
-
useValue: nativeRenderer
|
|
1854
|
-
}];
|
|
1855
1874
|
const modules = [];
|
|
1856
1875
|
let destroyed = false;
|
|
1857
1876
|
let appHost = null;
|
|
1858
|
-
const rootComponent = new RootComponent(context || null, (
|
|
1859
|
-
|
|
1877
|
+
const rootComponent = new RootComponent(context || null, withAnnotation({
|
|
1878
|
+
providers: [{
|
|
1879
|
+
provide: NativeRenderer,
|
|
1880
|
+
useValue: nativeRenderer
|
|
1881
|
+
}]
|
|
1882
|
+
}, () => {
|
|
1860
1883
|
return () => {
|
|
1861
1884
|
return destroyed ? null : root;
|
|
1862
1885
|
};
|
|
1863
|
-
}, function () {
|
|
1886
|
+
}), function () {
|
|
1864
1887
|
if (destroyed) {
|
|
1865
1888
|
return;
|
|
1866
1889
|
}
|
|
@@ -1882,12 +1905,7 @@ function viewfly(config) {
|
|
|
1882
1905
|
}
|
|
1883
1906
|
const app = {
|
|
1884
1907
|
provide(providers) {
|
|
1885
|
-
|
|
1886
|
-
appProviders.unshift(...providers);
|
|
1887
|
-
}
|
|
1888
|
-
else {
|
|
1889
|
-
appProviders.unshift(providers);
|
|
1890
|
-
}
|
|
1908
|
+
rootComponent.provide(providers);
|
|
1891
1909
|
return app;
|
|
1892
1910
|
},
|
|
1893
1911
|
use(module) {
|
|
@@ -1937,4 +1955,4 @@ function viewfly(config) {
|
|
|
1937
1955
|
return app;
|
|
1938
1956
|
}
|
|
1939
1957
|
|
|
1940
|
-
export { Component, DynamicRef, ForwardRef, Fragment, Inject, InjectFlags, Injectable, InjectionToken, Injector, JSXNodeFactory, NativeRenderer, NullInjector, Optional, Prop, ReflectiveInjector, RootComponent, Scope, Self, SkipSelf, StaticRef, THROW_IF_NOT_FOUND, Type, createDerived, createDynamicRef, createRef, createRenderer, createSignal, forwardRef, getCurrentInstance, inject, jsx, jsxs, makeError, normalizeProvider, onMounted, onPropsChanged, onUnmounted, onUpdated,
|
|
1958
|
+
export { Component, DynamicRef, ForwardRef, Fragment, Inject, InjectFlags, Injectable, InjectionToken, Injector, JSXNodeFactory, NativeRenderer, NullInjector, Optional, Prop, ReflectiveInjector, RootComponent, Scope, Self, SkipSelf, StaticRef, THROW_IF_NOT_FOUND, Type, createDerived, createDynamicRef, createRef, createRenderer, createSignal, forwardRef, getCurrentInstance, inject, jsx, jsxs, makeError, normalizeProvider, onMounted, onPropsChanged, onUnmounted, onUpdated, viewfly, watch, withAnnotation, withMemo };
|
package/bundles/index.js
CHANGED
|
@@ -207,7 +207,7 @@ class NullInjector extends Injector {
|
|
|
207
207
|
super(...arguments);
|
|
208
208
|
this.parentInjector = null;
|
|
209
209
|
}
|
|
210
|
-
get(token, notFoundValue = THROW_IF_NOT_FOUND) {
|
|
210
|
+
get(token, flag, notFoundValue = THROW_IF_NOT_FOUND) {
|
|
211
211
|
if (notFoundValue === THROW_IF_NOT_FOUND) {
|
|
212
212
|
throw nullInjectorErrorFn(token);
|
|
213
213
|
}
|
|
@@ -241,10 +241,10 @@ const Optional = function OptionalDecorator() {
|
|
|
241
241
|
return makeParamDecorator(Optional, new Optional());
|
|
242
242
|
}
|
|
243
243
|
};
|
|
244
|
-
const Prop = function PropDecorator(token, notFoundValue = THROW_IF_NOT_FOUND
|
|
244
|
+
const Prop = function PropDecorator(token, flags, notFoundValue = THROW_IF_NOT_FOUND) {
|
|
245
245
|
if (!(this instanceof Prop)) {
|
|
246
246
|
return makePropertyDecorator(Prop, token, function (instance, propertyName, token, injector) {
|
|
247
|
-
instance[propertyName] = injector.get(token instanceof ForwardRef ? token.getRef() : token,
|
|
247
|
+
instance[propertyName] = injector.get(token instanceof ForwardRef ? token.getRef() : token, flags, notFoundValue);
|
|
248
248
|
});
|
|
249
249
|
}
|
|
250
250
|
};
|
|
@@ -426,15 +426,15 @@ class ReflectiveInjector extends Injector {
|
|
|
426
426
|
/**
|
|
427
427
|
* 用于获取当前注入器上下文内的实例、对象或数据
|
|
428
428
|
* @param token 访问 token
|
|
429
|
-
* @param notFoundValue 如未查找到的返回值
|
|
430
429
|
* @param flags 查询规则
|
|
430
|
+
* @param notFoundValue 如未查找到的返回值
|
|
431
431
|
*/
|
|
432
|
-
get(token, notFoundValue = THROW_IF_NOT_FOUND
|
|
432
|
+
get(token, flags = exports.InjectFlags.Default, notFoundValue = THROW_IF_NOT_FOUND) {
|
|
433
433
|
var _a;
|
|
434
434
|
flags = flags || exports.InjectFlags.Default;
|
|
435
435
|
if (flags === exports.InjectFlags.SkipSelf) {
|
|
436
436
|
if (this.parentInjector) {
|
|
437
|
-
return this.parentInjector.get(token, notFoundValue);
|
|
437
|
+
return this.parentInjector.get(token, exports.InjectFlags.Default, notFoundValue);
|
|
438
438
|
}
|
|
439
439
|
if (notFoundValue !== THROW_IF_NOT_FOUND) {
|
|
440
440
|
return notFoundValue;
|
|
@@ -478,7 +478,7 @@ class ReflectiveInjector extends Injector {
|
|
|
478
478
|
return notFoundValue;
|
|
479
479
|
}
|
|
480
480
|
if (this.parentInjector) {
|
|
481
|
-
return this.parentInjector.get(token,
|
|
481
|
+
return this.parentInjector.get(token, flags === exports.InjectFlags.Optional ? exports.InjectFlags.Optional : exports.InjectFlags.Default, notFoundValue);
|
|
482
482
|
}
|
|
483
483
|
if (notFoundValue === THROW_IF_NOT_FOUND) {
|
|
484
484
|
throw reflectiveInjectorErrorFn(token);
|
|
@@ -510,11 +510,11 @@ class ReflectiveInjector extends Injector {
|
|
|
510
510
|
const tryValue = {};
|
|
511
511
|
const injectToken = dep.injectKey instanceof ForwardRef ? dep.injectKey.getRef() : dep.injectKey;
|
|
512
512
|
if (dep.visibility instanceof Self) {
|
|
513
|
-
reflectiveValue = this.get(injectToken,
|
|
513
|
+
reflectiveValue = this.get(injectToken, exports.InjectFlags.Self, tryValue);
|
|
514
514
|
}
|
|
515
515
|
else if (dep.visibility instanceof SkipSelf) {
|
|
516
516
|
if (this.parentInjector) {
|
|
517
|
-
reflectiveValue = this.parentInjector.get(injectToken, tryValue);
|
|
517
|
+
reflectiveValue = this.parentInjector.get(injectToken, exports.InjectFlags.Default, tryValue);
|
|
518
518
|
}
|
|
519
519
|
else {
|
|
520
520
|
if (dep.optional) {
|
|
@@ -527,7 +527,7 @@ class ReflectiveInjector extends Injector {
|
|
|
527
527
|
}
|
|
528
528
|
}
|
|
529
529
|
else {
|
|
530
|
-
reflectiveValue = this.get(injectToken, tryValue);
|
|
530
|
+
reflectiveValue = this.get(injectToken, exports.InjectFlags.Default, tryValue);
|
|
531
531
|
}
|
|
532
532
|
if (reflectiveValue === tryValue) {
|
|
533
533
|
if (dep.optional) {
|
|
@@ -658,7 +658,12 @@ class Component extends ReflectiveInjector {
|
|
|
658
658
|
return this._changed;
|
|
659
659
|
}
|
|
660
660
|
constructor(parentComponent, type, props, key) {
|
|
661
|
-
|
|
661
|
+
const annotation = type.annotation || {};
|
|
662
|
+
const providers = annotation.providers || [];
|
|
663
|
+
super(parentComponent, [...providers, {
|
|
664
|
+
provide: Injector,
|
|
665
|
+
useFactory: () => this
|
|
666
|
+
}], annotation.scope);
|
|
662
667
|
this.parentComponent = parentComponent;
|
|
663
668
|
this.type = type;
|
|
664
669
|
this.props = props;
|
|
@@ -1201,19 +1206,37 @@ function watch(deps, callback) {
|
|
|
1201
1206
|
return destroyFn;
|
|
1202
1207
|
}
|
|
1203
1208
|
/**
|
|
1204
|
-
*
|
|
1205
|
-
* @param
|
|
1209
|
+
* 给组件添加注解
|
|
1210
|
+
* @param annotation
|
|
1211
|
+
* @param componentSetup
|
|
1212
|
+
* @example
|
|
1213
|
+
* ```ts
|
|
1214
|
+
* export customScope = new Scope('scopeName')
|
|
1215
|
+
* export const App = withAnnotation({
|
|
1216
|
+
* scope: customScope,
|
|
1217
|
+
* providers: [
|
|
1218
|
+
* ExampleService
|
|
1219
|
+
* ]
|
|
1220
|
+
* }, function(props: Props) {
|
|
1221
|
+
* return () => {
|
|
1222
|
+
* return <div>...</div>
|
|
1223
|
+
* }
|
|
1224
|
+
* })
|
|
1225
|
+
* ```
|
|
1206
1226
|
*/
|
|
1207
|
-
function
|
|
1208
|
-
const
|
|
1209
|
-
|
|
1227
|
+
function withAnnotation(annotation, componentSetup) {
|
|
1228
|
+
const setup = function setup(props) {
|
|
1229
|
+
return componentSetup(props);
|
|
1230
|
+
};
|
|
1231
|
+
setup.annotation = annotation;
|
|
1232
|
+
return setup;
|
|
1210
1233
|
}
|
|
1211
1234
|
/**
|
|
1212
1235
|
* 通过组件上下文获取 IoC 容器内数据的勾子方法
|
|
1213
1236
|
*/
|
|
1214
|
-
function inject(token,
|
|
1237
|
+
function inject(token, flags = exports.InjectFlags.Default, notFoundValue = THROW_IF_NOT_FOUND) {
|
|
1215
1238
|
const component = getSetupContext();
|
|
1216
|
-
return component.get(token,
|
|
1239
|
+
return component.get(token, flags, notFoundValue);
|
|
1217
1240
|
}
|
|
1218
1241
|
/**
|
|
1219
1242
|
* 获取当前组件实例
|
|
@@ -1850,19 +1873,19 @@ class RootComponent extends Component {
|
|
|
1850
1873
|
const viewflyErrorFn = makeError('Viewfly');
|
|
1851
1874
|
function viewfly(config) {
|
|
1852
1875
|
const { context, nativeRenderer, autoUpdate, root } = Object.assign({ autoUpdate: true }, config);
|
|
1853
|
-
const appProviders = [{
|
|
1854
|
-
provide: NativeRenderer,
|
|
1855
|
-
useValue: nativeRenderer
|
|
1856
|
-
}];
|
|
1857
1876
|
const modules = [];
|
|
1858
1877
|
let destroyed = false;
|
|
1859
1878
|
let appHost = null;
|
|
1860
|
-
const rootComponent = new RootComponent(context || null, (
|
|
1861
|
-
|
|
1879
|
+
const rootComponent = new RootComponent(context || null, withAnnotation({
|
|
1880
|
+
providers: [{
|
|
1881
|
+
provide: NativeRenderer,
|
|
1882
|
+
useValue: nativeRenderer
|
|
1883
|
+
}]
|
|
1884
|
+
}, () => {
|
|
1862
1885
|
return () => {
|
|
1863
1886
|
return destroyed ? null : root;
|
|
1864
1887
|
};
|
|
1865
|
-
}, function () {
|
|
1888
|
+
}), function () {
|
|
1866
1889
|
if (destroyed) {
|
|
1867
1890
|
return;
|
|
1868
1891
|
}
|
|
@@ -1884,12 +1907,7 @@ function viewfly(config) {
|
|
|
1884
1907
|
}
|
|
1885
1908
|
const app = {
|
|
1886
1909
|
provide(providers) {
|
|
1887
|
-
|
|
1888
|
-
appProviders.unshift(...providers);
|
|
1889
|
-
}
|
|
1890
|
-
else {
|
|
1891
|
-
appProviders.unshift(providers);
|
|
1892
|
-
}
|
|
1910
|
+
rootComponent.provide(providers);
|
|
1893
1911
|
return app;
|
|
1894
1912
|
},
|
|
1895
1913
|
use(module) {
|
|
@@ -1976,7 +1994,7 @@ exports.onMounted = onMounted;
|
|
|
1976
1994
|
exports.onPropsChanged = onPropsChanged;
|
|
1977
1995
|
exports.onUnmounted = onUnmounted;
|
|
1978
1996
|
exports.onUpdated = onUpdated;
|
|
1979
|
-
exports.provide = provide;
|
|
1980
1997
|
exports.viewfly = viewfly;
|
|
1981
1998
|
exports.watch = watch;
|
|
1999
|
+
exports.withAnnotation = withAnnotation;
|
|
1982
2000
|
exports.withMemo = withMemo;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@viewfly/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0-alpha.1",
|
|
4
4
|
"description": "Viewfly is a simple and easy-to-use JavaScript framework with an intuitive development experience.",
|
|
5
5
|
"main": "./bundles/index.js",
|
|
6
6
|
"module": "./bundles/index.esm.js",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"bugs": {
|
|
48
48
|
"url": "https://github.com/viewfly/viewfly.git/issues"
|
|
49
49
|
},
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "8a25e6e350ba9cade6036506e7fe2aba4e4396ac",
|
|
51
51
|
"dependencies": {
|
|
52
52
|
"reflect-metadata": "^0.1.13"
|
|
53
53
|
}
|