Skip to content

Commit 160bf82

Browse files
committed
fix bug
1 parent a9185af commit 160bf82

File tree

7 files changed

+98
-106
lines changed

7 files changed

+98
-106
lines changed

manifest.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@
99
],
1010
"game": [
1111
"bin-debug/mvc/core/AbstractMVHost.js",
12-
"bin-debug/mvc/core/MVCInject.js",
1312
"bin-debug/mvc/core/Attribute.js",
1413
"bin-debug/foundation/AbstactPanel.js",
1514
"bin-debug/mvc/Mediator.js",
1615
"bin-debug/mvc/Proxy.js",
1716
"bin-debug/mvc/core/EventX.js",
1817
"bin-debug/test/BagView.js",
19-
"bin-debug/mvc/core/Singleton.js",
18+
"bin-debug/mvc/core/MVCInject.js",
2019
"bin-debug/Main.js",
2120
"bin-debug/foundation/ListenerItemBox.js",
2221
"bin-debug/foundation/ReflectUtils.js",
2322
"bin-debug/foundation/SocketX.js",
2423
"bin-debug/foundation/UILocator.js",
2524
"bin-debug/mvc/Facade.js",
25+
"bin-debug/mvc/core/Singleton.js",
2626
"bin-debug/mvc/core/View.js",
2727
"bin-debug/mvc/interface/IFacade.js",
2828
"bin-debug/mvc/interface/IMediator.js",

src/mvc/Facade.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ namespace mvc {
7272
}
7373

7474

75-
public __unSafeInjectInstance(host: IMVCHost, hostName: string = "") {
75+
public __unSafeInjectInstance(host: IMVCHost, hostName?: string) {
7676
if (!hostName) {
7777
hostName = host.name;
7878
}

src/mvc/core/AbstractMVHost.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module mvc {
2+
23
export abstract class AbstractMVHost extends egret.EventDispatcher implements IMVCHost, IEventInterester, IAsync, egret.IEventDispatcher {
3-
__eventInteresting:{[index:string]:Array<mvc.InjectEventTypeHandle>};
4+
__eventInteresting:{[index:string]:Array<InjectEventTypeHandle>};
45
__injectable=true;
56

67
protected _name: string;

src/mvc/core/Attribute.ts

+71-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ const enum InjectEventType {
88

99
// 装饰器
1010
let MVC = function (classPrototype, propertyKey: string) {
11-
mvc.MVCInject.AddMVC(classPrototype, propertyKey);
11+
mvc.InjectAttribute.AddMVC(classPrototype, propertyKey);
1212
}
1313
//装饰器工厂
1414
function CMD(code: number) {
1515
return function (classPrototype, propertyKey: string, descriptor: PropertyDescriptor) {
16-
mvc.MVCInject.AddCMD(classPrototype, code, descriptor.value);
16+
mvc.InjectAttribute.AddCMD(classPrototype, code, descriptor.value);
1717
};
1818
}
1919
//装饰器工厂
@@ -37,4 +37,72 @@ function MVCE(type: InjectEventType, ...events: Array<string>) {
3737
let ins = new mvc.InjectEventTypeHandle(type, events, descriptor.value);
3838
list.push(ins);
3939
};
40-
}
40+
}
41+
42+
namespace mvc{
43+
export class InjectEventTypeHandle {
44+
public constructor(public injectType: InjectEventType, public events: Array<string>, public handle: (EventX) => void) { }
45+
}
46+
export class InjectClassData {
47+
public propertys: { [index: string]: any } = {};
48+
//指令
49+
public cmds: { [index: number]: Array<(e: IStream) => void> } = {};
50+
public constructor(public classPrototype: Function) {
51+
}
52+
/**
53+
* 取得完整类名
54+
*/
55+
getFullClassName(): any {
56+
return this.classPrototype["constructor"]["name"];
57+
}
58+
}
59+
export class InjectAttribute{
60+
private static readonly injectMapping: Array<InjectClassData> = new Array<InjectClassData>();
61+
static Get(type: any): InjectClassData {
62+
for (let item of InjectAttribute.injectMapping) {
63+
if (item.classPrototype == type) {
64+
return item;
65+
}
66+
}
67+
return null;
68+
}
69+
70+
public static AddMVC(classPrototype: Function, property: string) {
71+
let classInjectData: InjectClassData = null;
72+
for (let item of this.injectMapping) {
73+
if (item.classPrototype == classPrototype) {
74+
classInjectData = item;
75+
break;
76+
}
77+
}
78+
if (!classInjectData) {
79+
classInjectData = new InjectClassData(classPrototype);
80+
InjectAttribute.injectMapping.push(classInjectData);
81+
}
82+
classInjectData.propertys[property] = 1;
83+
}
84+
public static AddCMD(classPrototype: Function, cmd: number, handle: (e: IStream) => void) {
85+
let classInjectData = null;
86+
for (let item of InjectAttribute.injectMapping) {
87+
if (item.classPrototype == classPrototype) {
88+
classInjectData = item;
89+
break;
90+
}
91+
}
92+
if (!classInjectData) {
93+
classInjectData = new InjectClassData(classPrototype);
94+
InjectAttribute.injectMapping.push(classInjectData);
95+
}
96+
97+
let list = classInjectData.cmds[cmd];
98+
if (!list) {
99+
list = new Array<(e: IStream) => void>();
100+
classInjectData.cmds[cmd] = list;
101+
}
102+
if (list.indexOf(handle) == -1) {
103+
list.push(handle);
104+
}
105+
}
106+
107+
}
108+
}

src/mvc/core/MVCInject.ts

+10-75
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,4 @@
11
module mvc {
2-
//不导出
3-
class ClassInjectData {
4-
5-
public propertys: { [index: string]: any } = {};
6-
//typeEvents
7-
public typeEventHandles: { [index: string]: Array<InjectEventTypeHandle> } = {};
8-
//指令
9-
public cmds: { [index: number]: Array<(e: IStream) => void> } = {};
10-
public constructor(public classPrototype: Function) {
11-
}
12-
/**
13-
* 取得完整类名
14-
*/
15-
getFullClassName(): any {
16-
return this.classPrototype["constructor"]["name"];
17-
}
18-
}
19-
202
/**
213
* 注入管理类
224
*/
@@ -50,58 +32,12 @@ module mvc {
5032
}
5133
}
5234

53-
private static injectMapping: Array<ClassInjectData> = new Array<ClassInjectData>();
54-
55-
public static AddMVC(classPrototype: Function, property: string) {
56-
57-
let classInjectData: ClassInjectData = null;
58-
59-
for (let item of MVCInject.injectMapping) {
60-
if (item.classPrototype == classPrototype) {
61-
classInjectData = item;
62-
break;
63-
}
64-
}
65-
66-
if (!classInjectData) {
67-
classInjectData = new ClassInjectData(classPrototype);
68-
MVCInject.injectMapping.push(classInjectData);
69-
}
70-
71-
classInjectData.propertys[property] = 1;
72-
}
73-
74-
public static AddCMD(classPrototype: Function, cmd: number, handle: (e: IStream) => void) {
75-
76-
let classInjectData = null;
77-
for (let item of MVCInject.injectMapping) {
78-
if (item.classPrototype == classPrototype) {
79-
classInjectData = item;
80-
break;
81-
}
82-
}
83-
84-
if (!classInjectData) {
85-
classInjectData = new ClassInjectData(classPrototype);
86-
MVCInject.injectMapping.push(classInjectData);
87-
}
88-
89-
let list = classInjectData.cmds[cmd];
90-
if (!list) {
91-
list = new Array<(e: IStream) => void>();
92-
classInjectData.cmds[cmd] = list;
93-
}
94-
if (list.indexOf(handle) == -1) {
95-
list.push(handle);
96-
}
97-
}
98-
9935
private facade: IFacade;
10036
public constructor(facade: IFacade) {
10137
this.facade = facade;
10238
}
10339

104-
private getInjectClassByDef(classInjectData: ClassInjectData, property: string): new () => any {
40+
private getInjectClassByDef(classInjectData: InjectClassData, property: string): new () => any {
10541
let fullClassName = classInjectData.getFullClassName();
10642
let dic = MVCInject.injectDefMapping[fullClassName];
10743
if (!dic) {
@@ -132,19 +68,16 @@ module mvc {
13268
public inject(target: IInjectable): IInjectable {
13369
let type = Object.getPrototypeOf(target);
13470
while (type) {
135-
for (let item of MVCInject.injectMapping) {
136-
if (item.classPrototype == type) {
137-
this.doInject(item, target);
138-
break;
139-
}
140-
}
71+
let item=InjectAttribute.Get(type);
72+
if(item)this.doInject(item, target);
73+
14174
//循环找基类注入
14275
type = Object.getPrototypeOf(type);
14376
}
14477
return target;
14578
}
14679

147-
protected doInject(classInjectData: ClassInjectData, target: IInjectable) {
80+
protected doInject(classInjectData: InjectClassData, target: IInjectable) {
14881
for (let key in classInjectData.propertys) {
14982
let cls = this.getInjectClassByDef(classInjectData, key);
15083
if (cls == null) {
@@ -199,15 +132,17 @@ module mvc {
199132
let ins = this.facade.getInjectLock(aliasName);
200133
if (!ins) {
201134
ins = Singleton.__GetOrCreateOneInstance(aliasName);
135+
ins["_name"] = aliasName;
136+
137+
//可注入 必须加锁 (先调用也是想让onRegister的时候 可以访问注入对像)
202138
if (ins[MVCInject.INJECTABLE_FULLNAME]) {
203-
ins = this.inject(ins);
139+
this.facade.__unSafeInjectInstance(ins);
204140
}
141+
205142
if (isMediator) {
206-
ins["_name"] = aliasName;
207143
this.facade.registerMediator(<IMediator>ins);
208144
}
209145
else if (isProxy) {
210-
ins["_name"] = aliasName;
211146
this.facade.registerProxy(<IProxy>ins);
212147
}
213148
}

src/mvc/interface/IFacade.ts

+5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ namespace mvc {
5252
*/
5353
registerEventInterester(eventInterester:IEventInterester,injectEventType:InjectEventType, isBind?:boolean);
5454

55+
/**
56+
* 用于限制互相依赖的锁
57+
* @param className
58+
*/
5559
getInjectLock(className:string):any;
60+
__unSafeInjectInstance(toLockInstance: IMVCHost, className?: string)
5661
}
5762
}

src/mvc/interface/Interface.ts

+7-24
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,37 @@
11
namespace mvc {
2-
export class InjectEventTypeHandle {
3-
public constructor(public injectType: InjectEventType, public events: Array<string>, public handle: (EventX) => void) { }
4-
}
5-
62
export interface IEventInterester {
73
/**
84
* @property index:InjectEventType;
95
*/
10-
__eventInteresting:{[index:string]:Array<mvc.InjectEventTypeHandle>};
6+
__eventInteresting: { [index: string]: Array<InjectEventTypeHandle> };
117
getEventInterests(type: InjectEventType): Array<InjectEventTypeHandle>;
128
}
13-
14-
15-
169
export interface IView {
17-
get(name:string ):IMVCHost;
18-
register(host:IMVCHost):boolean;
19-
remove(host:IMVCHost):boolean;
10+
get(name: string): IMVCHost;
11+
register(host: IMVCHost): boolean;
12+
remove(host: IMVCHost): boolean;
2013
}
21-
22-
23-
export interface IMVCHost extends IAsync,IEventInterester,IInjectable {
14+
export interface IMVCHost extends IAsync, IEventInterester, IInjectable {
2415
readonly name: string;
2516
onRegister();
2617
onRemove();
2718
}
28-
2919
export interface IAsync {
3020
readonly isReady: boolean;
31-
3221
startSync(): boolean;
3322
addReayHandle(handle: (e: EventX) => void): boolean;
3423
}
35-
3624
export interface INotifier {
3725
simpleDispatch(type: string, data: any): boolean
3826
}
39-
4027
export interface ICommand {
4128
execute(e: EventX): void;
4229
}
43-
44-
4530
export interface IInject {
46-
inject(target:IInjectable):IInjectable;
31+
inject(target: IInjectable): IInjectable;
4732
}
48-
4933
///是否可注入
5034
export interface IInjectable {
51-
readonly __injectable:boolean;
52-
35+
readonly __injectable: boolean;
5336
}
5437
}

0 commit comments

Comments
 (0)