-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstractApplicationContext.ts
99 lines (87 loc) · 2.49 KB
/
abstractApplicationContext.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { Exception } from "../java/exception";
import { AbstractBeanFactory } from "../org.springframework.beans/abstractBeanFactory";
import { ApplicationEvent } from "./applicationEvent";
import { eventsToken, TEventListenerRecord } from "./annotations";
import {
ApplicationContextStartEvent,
ApplicationContextStopEvent
} from "./applicationContextEvent";
//
// Context is a Ioc container with event system
//
const PFX = "[ABSTRACT APPLICATION CONTEXT]:";
/**
* Abstract implementation of the ApplicationContext interface.
* @remark https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/support/AbstractApplicationContext.html
*/
export class AbstractApplicationContext extends AbstractBeanFactory {
protected parentBeanFactory: AbstractApplicationContext;
constructor() {
super();
this.parentBeanFactory = null;
}
/**
* Broadcast event to all subscribed beans in context.
* @param event - application event to broadcast.
*/
public publishEvent(event: ApplicationEvent) {
if (this.beansMap) {
this.beansMap.forEach(bean => {
if (Reflect.hasMetadata(eventsToken, bean.constructor)) {
const eventListenerRecords = Reflect.getMetadata(
eventsToken,
bean.constructor
);
eventListenerRecords.forEach((record: TEventListenerRecord) => {
if (event instanceof record.eventClass) {
bean[record.key].call(bean, event);
}
});
}
});
} else {
throw new Exception(
`${PFX} could not publish event - context not started`
);
}
// broadcast to parent contexts
if (this.parentBeanFactory && this.parentBeanFactory.isRunning()) {
this.parentBeanFactory.publishEvent(event);
}
}
//
// ILifecycle implementation
//
/**
* Start context.
*/
public start() {
super.start();
this.publishEvent(new ApplicationContextStartEvent(this));
}
/**
* Destroy all beans in context but keep names mappings.
*/
public stop() {
this.publishEvent(new ApplicationContextStopEvent(this));
super.stop();
}
/**
* Destroy all beans in context and delete beans mappings.
*/
public close() {
super.close();
}
//
// Overrides
//
/**
* Get resource from server.
* @param url - json resource url
*/
public async getResource(url: string): Promise<string> {
const response = await fetch(url);
const data = await response.json();
return data;
}
}