@oinone/kunlun-event 6.2.0
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/dist/oinone-kunlun-event.esm.js +15 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/src/bus/abstract/AbstractEventConsumer.d.ts +17 -0
- package/dist/types/src/bus/abstract/AbstractEventEngine.d.ts +21 -0
- package/dist/types/src/bus/abstract/AbstractEventProducer.d.ts +15 -0
- package/dist/types/src/bus/abstract/index.d.ts +2 -0
- package/dist/types/src/bus/bus/basic.d.ts +12 -0
- package/dist/types/src/bus/bus/html-event.d.ts +6 -0
- package/dist/types/src/bus/bus/html-keyboard-event.d.ts +6 -0
- package/dist/types/src/bus/bus/index.d.ts +18 -0
- package/dist/types/src/bus/hook/consumer.d.ts +1 -0
- package/dist/types/src/bus/hook/index.d.ts +2 -0
- package/dist/types/src/bus/hook/producer.d.ts +1 -0
- package/dist/types/src/bus/index.d.ts +5 -0
- package/dist/types/src/bus/operator/consumer.d.ts +11 -0
- package/dist/types/src/bus/operator/index.d.ts +2 -0
- package/dist/types/src/bus/operator/producer.d.ts +10 -0
- package/dist/types/src/bus/spi/consumer.d.ts +8 -0
- package/dist/types/src/bus/spi/index.d.ts +2 -0
- package/dist/types/src/bus/spi/producer.d.ts +8 -0
- package/dist/types/src/bus/typing/basic.d.ts +64 -0
- package/dist/types/src/bus/typing/category.d.ts +5 -0
- package/dist/types/src/bus/typing/index.d.ts +3 -0
- package/dist/types/src/bus/typing/keyboard.d.ts +16 -0
- package/dist/types/src/effects/effectbox.d.ts +3 -0
- package/dist/types/src/effects/fieldEffects.d.ts +61 -0
- package/dist/types/src/effects/index.d.ts +2 -0
- package/dist/types/src/effects/viewEffects.d.ts +69 -0
- package/dist/types/src/helper.d.ts +14 -0
- package/dist/types/src/html-event/basic/AbstractHTMLEventProducer.d.ts +7 -0
- package/dist/types/src/html-event/basic/HTMLEventConsumer.d.ts +4 -0
- package/dist/types/src/html-event/basic/HTMLEventProducer.d.ts +5 -0
- package/dist/types/src/html-event/basic/index.d.ts +3 -0
- package/dist/types/src/html-event/index.d.ts +2 -0
- package/dist/types/src/html-event/keyboard/HTMLKeyboardEventConsumer.d.ts +14 -0
- package/dist/types/src/html-event/keyboard/HTMLKeyboardEventProducer.d.ts +16 -0
- package/dist/types/src/html-event/keyboard/index.d.ts +2 -0
- package/dist/types/src/index.d.ts +6 -0
- package/dist/types/src/lifecycle/heart.d.ts +75 -0
- package/dist/types/src/lifecycle/index.d.ts +2 -0
- package/dist/types/src/lifecycle/lifecycle.d.ts +7 -0
- package/dist/types/src/typing/index.d.ts +1 -0
- package/dist/types/src/typing/lifecycle.d.ts +92 -0
- package/index.ts +1 -0
- package/package.json +19 -0
- package/rollup.config.js +4 -0
- package/src/bus/abstract/AbstractEventConsumer.ts +79 -0
- package/src/bus/abstract/AbstractEventEngine.ts +79 -0
- package/src/bus/abstract/AbstractEventProducer.ts +64 -0
- package/src/bus/abstract/index.ts +2 -0
- package/src/bus/bus/basic.ts +124 -0
- package/src/bus/bus/html-event.ts +43 -0
- package/src/bus/bus/html-keyboard-event.ts +46 -0
- package/src/bus/bus/index.ts +46 -0
- package/src/bus/hook/consumer.ts +1 -0
- package/src/bus/hook/index.ts +2 -0
- package/src/bus/hook/producer.ts +1 -0
- package/src/bus/index.ts +5 -0
- package/src/bus/operator/consumer.ts +83 -0
- package/src/bus/operator/index.ts +2 -0
- package/src/bus/operator/producer.ts +56 -0
- package/src/bus/spi/consumer.ts +34 -0
- package/src/bus/spi/index.ts +2 -0
- package/src/bus/spi/producer.ts +27 -0
- package/src/bus/typing/basic.ts +126 -0
- package/src/bus/typing/category.ts +6 -0
- package/src/bus/typing/index.ts +3 -0
- package/src/bus/typing/keyboard.ts +21 -0
- package/src/effects/effectbox.ts +22 -0
- package/src/effects/fieldEffects.ts +99 -0
- package/src/effects/index.ts +2 -0
- package/src/effects/viewEffects.ts +111 -0
- package/src/helper.ts +31 -0
- package/src/html-event/basic/AbstractHTMLEventProducer.ts +30 -0
- package/src/html-event/basic/HTMLEventConsumer.ts +26 -0
- package/src/html-event/basic/HTMLEventProducer.ts +10 -0
- package/src/html-event/basic/index.ts +3 -0
- package/src/html-event/index.ts +2 -0
- package/src/html-event/keyboard/HTMLKeyboardEventConsumer.ts +160 -0
- package/src/html-event/keyboard/HTMLKeyboardEventProducer.ts +72 -0
- package/src/html-event/keyboard/index.ts +2 -0
- package/src/index.ts +6 -0
- package/src/lifecycle/heart.ts +160 -0
- package/src/lifecycle/index.ts +2 -0
- package/src/lifecycle/lifecycle.ts +29 -0
- package/src/typing/index.ts +1 -0
- package/src/typing/lifecycle.ts +104 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { LifeCycle } from './lifecycle';
|
|
2
|
+
import { LifeCycleTypes, RegistryType } from '../typing';
|
|
3
|
+
/**
|
|
4
|
+
* HeartLifeCycles = [{
|
|
5
|
+
* registryType: Field,
|
|
6
|
+
* type: onFieldChange,
|
|
7
|
+
* pattern: {
|
|
8
|
+
* identifier: displayName
|
|
9
|
+
* lifes: [LifeCycle]
|
|
10
|
+
* }
|
|
11
|
+
* }]
|
|
12
|
+
*/
|
|
13
|
+
declare type HeartLifeCycles = {
|
|
14
|
+
type: LifeCycleTypes;
|
|
15
|
+
registryType: RegistryType;
|
|
16
|
+
pattern: {
|
|
17
|
+
identifier: string;
|
|
18
|
+
lifes: LifeCycle[];
|
|
19
|
+
}[];
|
|
20
|
+
}[];
|
|
21
|
+
/**
|
|
22
|
+
* 操作生命周期的api
|
|
23
|
+
*/
|
|
24
|
+
declare class LifeCycleHeart {
|
|
25
|
+
private static lifeCycles;
|
|
26
|
+
private static heartType;
|
|
27
|
+
static initialize(lifeCycles: HeartLifeCycles): void;
|
|
28
|
+
private static getLifeCycleIndex;
|
|
29
|
+
static getLifeCycle(): HeartLifeCycles;
|
|
30
|
+
static getLifeCycleByType(type: LifeCycleTypes): {
|
|
31
|
+
type: LifeCycleTypes;
|
|
32
|
+
registryType: RegistryType;
|
|
33
|
+
pattern: {
|
|
34
|
+
identifier: string;
|
|
35
|
+
lifes: LifeCycle<any>[];
|
|
36
|
+
}[];
|
|
37
|
+
} | null;
|
|
38
|
+
/**
|
|
39
|
+
* 设置生命周期的注册类型,
|
|
40
|
+
*
|
|
41
|
+
* @param {registryType} RegistryType 类型
|
|
42
|
+
*/
|
|
43
|
+
static setType(registryType: RegistryType): void;
|
|
44
|
+
/**
|
|
45
|
+
* 设置生命周期
|
|
46
|
+
* 调用该函数之前要先调用 `setType` 设置注册类型
|
|
47
|
+
*
|
|
48
|
+
* @param {type} LifeCycleTypes 生命周期的类型
|
|
49
|
+
* @param {identifier} string 唯一标识符
|
|
50
|
+
* @param {lifeCycle} LifeCycle 生命周期的实例
|
|
51
|
+
*
|
|
52
|
+
*/
|
|
53
|
+
static setLifeCycle(type: LifeCycleTypes, identifier: string, lifeCycle: LifeCycle): false | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* 触发生命周期的回调函数
|
|
56
|
+
*
|
|
57
|
+
* @param {type} LifeCycleTypes 生命周期的类型
|
|
58
|
+
* @param {identifier} string 唯一标识符
|
|
59
|
+
* @param {ctx} Context 当前「视图/字段/Action」的上下文
|
|
60
|
+
*/
|
|
61
|
+
static publish<C = any>(type: LifeCycleTypes, identifier: string, ctx: C): false | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* @deprecated 销毁生命周期
|
|
64
|
+
*
|
|
65
|
+
* @param {identifier} string 唯一标识符
|
|
66
|
+
*/
|
|
67
|
+
static disposeLifeCycle(identifier: string): void;
|
|
68
|
+
/**
|
|
69
|
+
* 销毁生命周期
|
|
70
|
+
*
|
|
71
|
+
* @param {identifier} string 唯一标识符
|
|
72
|
+
*/
|
|
73
|
+
static dispose(identifier: string): void;
|
|
74
|
+
}
|
|
75
|
+
export { LifeCycleHeart };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './lifecycle';
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
export declare enum RegistryType {
|
|
2
|
+
FIELD = "Field",
|
|
3
|
+
VIEW = "View",
|
|
4
|
+
ACTION = "Action"
|
|
5
|
+
}
|
|
6
|
+
export declare enum LifeCycleTypes {
|
|
7
|
+
/**
|
|
8
|
+
* View LifeCycle
|
|
9
|
+
* */
|
|
10
|
+
ON_VIEW_BEFORE_CREATED = "onViewBeforeCreated",
|
|
11
|
+
ON_VIEW_CREATED = "onViewCreated",
|
|
12
|
+
ON_VIEW_BEFORE_MOUNT = "onViewBeforeMount",
|
|
13
|
+
ON_VIEW_MOUNTED = "onViewMounted",
|
|
14
|
+
ON_VIEW_BEFORE_UPDATE = "onViewBeforeUpdate",
|
|
15
|
+
ON_VIEW_UPDATED = "onViewUpdated",
|
|
16
|
+
ON_VIEW_ACTIVATED = "onViewActivated",
|
|
17
|
+
ON_VIEW_BEFORE_UNMOUNT = "onViewBeforeUnmount",
|
|
18
|
+
ON_VIEW_UNMOUNTED = "onViewUnmounted",
|
|
19
|
+
ON_VIEW_SUBMIT = "onViewSubmit",
|
|
20
|
+
ON_VIEW_SUBMIT_START = "onViewSubmitStart",
|
|
21
|
+
ON_VIEW_SUBMIT_END = "onViewSubmitEnd",
|
|
22
|
+
ON_VIEW_SUBMIT_SUCCESS = "onViewSubmitSuccess",
|
|
23
|
+
ON_VIEW_SUBMIT_FAILED = "onViewSubmitFailed",
|
|
24
|
+
ON_VIEW_VALIDATE_START = "onViewValidateStart",
|
|
25
|
+
ON_VIEW_VALIDATE_SUCCESS = "onViewValidateSuccess",
|
|
26
|
+
ON_VIEW_VALIDATE_FAILED = "onViewValidateFailed",
|
|
27
|
+
ON_VIEW_VALIDATE_END = "onViewValidateEnd",
|
|
28
|
+
/**
|
|
29
|
+
* Field LifeCycle
|
|
30
|
+
* */
|
|
31
|
+
ON_FIELD_BEFORE_CREATED = "onFieldBeforeCreated",
|
|
32
|
+
ON_FIELD_CREATED = "onFieldCreated",
|
|
33
|
+
ON_FIELD_BEFORE_MOUNT = "onFieldBeforeMount",
|
|
34
|
+
ON_FIELD_MOUNTED = "onFieldMounted",
|
|
35
|
+
ON_FIELD_BEFORE_UPDATE = "onFieldBeforeUpdate",
|
|
36
|
+
ON_FIELD_UPDATED = "onFieldUpdated",
|
|
37
|
+
ON_FIELD_ACTIVATED = "onFieldActivated",
|
|
38
|
+
ON_FIELD_BEFORE_UNMOUNT = "onFieldBeforeUnmount",
|
|
39
|
+
ON_FIELD_UNMOUNTED = "onFieldUnmounted",
|
|
40
|
+
ON_FIELD_FOCUS = "onFieldFocus",
|
|
41
|
+
ON_FIELD_CHANGE = "onFieldChange",
|
|
42
|
+
ON_FIELD_BLUR = "onFieldBlur",
|
|
43
|
+
ON_FIELD_VALIDATE_START = "onFieldValidateStart",
|
|
44
|
+
ON_FIELD_VALIDATE_SUCCESS = "onFieldValidateSuccess",
|
|
45
|
+
ON_FIELD_VALIDATE_FAILED = "onFieldValidateFailed",
|
|
46
|
+
ON_FIELD_VALIDATE_END = "onFieldValidateEnd"
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* View internal event
|
|
50
|
+
*/
|
|
51
|
+
export declare enum ViewEventNames {
|
|
52
|
+
viewBeforeCreated = "onViewBeforeCreated",
|
|
53
|
+
viewCreated = "onViewCreated",
|
|
54
|
+
viewBeforeMount = "onViewBeforeMount",
|
|
55
|
+
viewMounted = "onViewMounted",
|
|
56
|
+
viewBeforeUpdate = "onViewBeforeUpdate",
|
|
57
|
+
viewUpdated = "onViewUpdated",
|
|
58
|
+
viewActivated = "onViewActivated",
|
|
59
|
+
viewBeforeUnmount = "onViewBeforeUnmount",
|
|
60
|
+
viewUnmounted = "onViewUnmounted",
|
|
61
|
+
viewSubmit = "onViewSubmit",
|
|
62
|
+
viewSubmitStart = "onViewSubmitStart",
|
|
63
|
+
viewSubmitEnd = "onViewSubmitEnd",
|
|
64
|
+
viewSubmitSuccess = "onViewSubmitSuccess",
|
|
65
|
+
viewSubmitFailed = "onViewSubmitFailed",
|
|
66
|
+
viewValidateStart = "onViewValidateStart",
|
|
67
|
+
viewValidateSuccess = "onViewValidateSuccess",
|
|
68
|
+
viewValidateFailed = "onViewValidateFailed",
|
|
69
|
+
viewValidateEnd = "onViewValidateEnd"
|
|
70
|
+
}
|
|
71
|
+
export declare type ViewEventName = keyof typeof ViewEventNames;
|
|
72
|
+
/**
|
|
73
|
+
* Field internal event
|
|
74
|
+
*/
|
|
75
|
+
export declare enum FieldEventNames {
|
|
76
|
+
beforeCreated = "onFieldBeforeCreated",
|
|
77
|
+
created = "onFieldCreated",
|
|
78
|
+
beforeMount = "onFieldBeforeMount",
|
|
79
|
+
mount = "onFieldMounted",
|
|
80
|
+
beforeUpdate = "onFieldBeforeUpdate",
|
|
81
|
+
updated = "onViewUpdated",
|
|
82
|
+
beforeUnmount = "onFieldBeforeUnmount",
|
|
83
|
+
unmounted = "onFieldUnmounted",
|
|
84
|
+
focus = "onFieldFocus",
|
|
85
|
+
change = "onFieldChange",
|
|
86
|
+
blur = "onFieldBlur",
|
|
87
|
+
validateStart = "onFieldValidateStart",
|
|
88
|
+
validateSuccess = "onFieldValidateSuccess",
|
|
89
|
+
validateFailed = "onFieldValidateFailed",
|
|
90
|
+
validateEnd = "onFieldValidateEnd"
|
|
91
|
+
}
|
|
92
|
+
export declare type FieldEventName = keyof typeof FieldEventNames;
|
package/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './src';
|
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@oinone/kunlun-event",
|
|
3
|
+
"version": "6.2.0",
|
|
4
|
+
"main": "index.ts",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"prebuild": "rimraf dist",
|
|
7
|
+
"build": "rollup -c",
|
|
8
|
+
"dist": "npm run prepublishOnly",
|
|
9
|
+
"index": "npm run postpublish",
|
|
10
|
+
"x-prepublishOnly": "node ../../scripts/prepublish-only.js",
|
|
11
|
+
"x-postpublish": "node ../../scripts/postpublish.js"
|
|
12
|
+
},
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"@oinone/kunlun-environment": "6.2.0",
|
|
15
|
+
"@oinone/kunlun-shared": "6.2.0",
|
|
16
|
+
"@oinone/kunlun-spi": "6.2.0"
|
|
17
|
+
},
|
|
18
|
+
"gitHead": "b65bbf1d649da1017810c5608473d5daa1a41f94"
|
|
19
|
+
}
|
package/rollup.config.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { ConsumerOperator } from '../operator';
|
|
2
|
+
import {
|
|
3
|
+
EventCategoryType,
|
|
4
|
+
EventConsumer,
|
|
5
|
+
EventConsumerConfig,
|
|
6
|
+
EventConsumerFilter,
|
|
7
|
+
EventConsumerFunction,
|
|
8
|
+
EventConsumerOptions,
|
|
9
|
+
EventMessage
|
|
10
|
+
} from '../typing';
|
|
11
|
+
import { AbstractEventEngine } from './AbstractEventEngine';
|
|
12
|
+
|
|
13
|
+
let counter = 0;
|
|
14
|
+
|
|
15
|
+
export abstract class AbstractEventConsumer<
|
|
16
|
+
K = string,
|
|
17
|
+
EV = unknown,
|
|
18
|
+
V = EV,
|
|
19
|
+
M extends EventMessage<V> = EventMessage<V>,
|
|
20
|
+
C extends EventConsumerConfig = EventConsumerConfig
|
|
21
|
+
>
|
|
22
|
+
extends AbstractEventEngine<K>
|
|
23
|
+
implements EventConsumer<K, EV, V, M, C>
|
|
24
|
+
{
|
|
25
|
+
public el: HTMLElement | undefined;
|
|
26
|
+
|
|
27
|
+
private readonly _config: C;
|
|
28
|
+
|
|
29
|
+
private readonly _rawFilter: EventConsumerFilter<EV, V, M>;
|
|
30
|
+
|
|
31
|
+
private readonly _rawConsumer: EventConsumerFunction<K, EV, V, M, C>;
|
|
32
|
+
|
|
33
|
+
protected constructor(
|
|
34
|
+
category: EventCategoryType,
|
|
35
|
+
type: K,
|
|
36
|
+
consumer: EventConsumerFunction<K, EV, V, M, C>,
|
|
37
|
+
options?: EventConsumerOptions<EV, V, M, C>
|
|
38
|
+
) {
|
|
39
|
+
super(category, type);
|
|
40
|
+
this._config = options?.config || ({} as C);
|
|
41
|
+
this._rawFilter = options?.filter || this.$$filter.bind(this);
|
|
42
|
+
this._rawConsumer = consumer;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public get config(): C {
|
|
46
|
+
return this._config;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
protected get rawFilter(): EventConsumerFilter<EV, V, M> {
|
|
50
|
+
return this._rawFilter;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
protected get rawConsumer(): EventConsumerFunction<K, EV, V, M, C> {
|
|
54
|
+
return this._rawConsumer;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
public filter(ev: EV, message: M): boolean {
|
|
58
|
+
if (this.isActivated) {
|
|
59
|
+
return this.rawFilter(ev, message);
|
|
60
|
+
}
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
protected $$filter(ev: EV, message: M) {
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public consumer(message: M): void {
|
|
69
|
+
return this.rawConsumer(this as unknown as EventConsumer, message);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
protected generatorKey(): string {
|
|
73
|
+
return `event_consumer_${counter++}`;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
protected $$dispose(): void {
|
|
77
|
+
ConsumerOperator.dispose(this);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { EventCategoryType, EventEngine } from '../typing';
|
|
2
|
+
|
|
3
|
+
export abstract class AbstractEventEngine<K = string> implements EventEngine<K> {
|
|
4
|
+
private readonly _key: string;
|
|
5
|
+
|
|
6
|
+
private readonly _category: EventCategoryType;
|
|
7
|
+
|
|
8
|
+
private readonly _type: K;
|
|
9
|
+
|
|
10
|
+
private _isActivated: boolean;
|
|
11
|
+
|
|
12
|
+
private _isDisposed: boolean;
|
|
13
|
+
|
|
14
|
+
protected constructor(category: EventCategoryType, type: K) {
|
|
15
|
+
this._key = this.generatorKey();
|
|
16
|
+
this._category = category;
|
|
17
|
+
this._type = type;
|
|
18
|
+
this._isActivated = false;
|
|
19
|
+
this._isDisposed = false;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public get key(): string {
|
|
23
|
+
return this._key;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public get category(): EventCategoryType {
|
|
27
|
+
return this._category;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public get type(): K {
|
|
31
|
+
return this._type;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public get isActivated(): boolean {
|
|
35
|
+
return this._isActivated;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public get isDisposed(): boolean {
|
|
39
|
+
return this._isDisposed;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public start(): boolean {
|
|
43
|
+
if (this.isActivated) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
this._isActivated = true;
|
|
47
|
+
this.$$start();
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
protected $$start(): void {}
|
|
52
|
+
|
|
53
|
+
public stop(): boolean {
|
|
54
|
+
if (this.isActivated) {
|
|
55
|
+
this._isActivated = false;
|
|
56
|
+
this.$$stop();
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
protected $$stop(): void {}
|
|
63
|
+
|
|
64
|
+
public dispose(): boolean {
|
|
65
|
+
if (this.isDisposed) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
if (this.isActivated) {
|
|
69
|
+
this.stop();
|
|
70
|
+
}
|
|
71
|
+
this._isDisposed = true;
|
|
72
|
+
this.$$dispose();
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
protected $$dispose(): void {}
|
|
77
|
+
|
|
78
|
+
protected abstract generatorKey(): string;
|
|
79
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { ConsumerOperator, ProducerOperator } from '../operator';
|
|
2
|
+
import {
|
|
3
|
+
EventCategoryType,
|
|
4
|
+
EventConvertFunction,
|
|
5
|
+
EventMessage,
|
|
6
|
+
EventProducer,
|
|
7
|
+
EventProducerOptions,
|
|
8
|
+
EventPublishFunction
|
|
9
|
+
} from '../typing';
|
|
10
|
+
import { AbstractEventEngine } from './AbstractEventEngine';
|
|
11
|
+
|
|
12
|
+
let counter = 0;
|
|
13
|
+
|
|
14
|
+
export abstract class AbstractEventProducer<K = string, EV = unknown, V = EV>
|
|
15
|
+
extends AbstractEventEngine<K>
|
|
16
|
+
implements EventProducer<K, EV, V>
|
|
17
|
+
{
|
|
18
|
+
private readonly _rawConvert: EventConvertFunction<K, EV, V>;
|
|
19
|
+
|
|
20
|
+
private readonly _rawPublish: EventPublishFunction<K, EV, V>;
|
|
21
|
+
|
|
22
|
+
protected constructor(category: EventCategoryType, type: K, options?: EventProducerOptions<K, EV, V>) {
|
|
23
|
+
super(category, type);
|
|
24
|
+
this._rawConvert = options?.convert || this.$$convert;
|
|
25
|
+
this._rawPublish = options?.publish || this.$$publish;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public get rawConvert(): EventConvertFunction<K, EV, V> {
|
|
29
|
+
return this._rawConvert;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public get rawPublish(): EventPublishFunction<K, EV, V> {
|
|
33
|
+
return this._rawPublish;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public convert(ev: EV): EventMessage<V> | undefined {
|
|
37
|
+
return this.rawConvert(this, ev);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
protected abstract $$convert(self: EventProducer<K, EV, V>, ev: EV): EventMessage<V> | undefined;
|
|
41
|
+
|
|
42
|
+
public publish(ev: EV): void {
|
|
43
|
+
this.rawPublish(this, ev);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
protected $$publish(self: EventProducer<K, EV, V>, ev: EV): void {
|
|
47
|
+
const message = this.convert(ev);
|
|
48
|
+
if (!message) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const consumers = ConsumerOperator.getConsumers(message);
|
|
52
|
+
if (consumers && consumers.length) {
|
|
53
|
+
consumers.filter((v) => v.filter(ev, message)).map(async (v) => v.consumer(message));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
protected generatorKey(): string {
|
|
58
|
+
return `event_producer_${counter++}`;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
protected $$dispose(): void {
|
|
62
|
+
ProducerOperator.dispose(ProducerOperator.generatorKeyByEngine(this));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { ConsumerOperator, ProducerOperator } from '../operator';
|
|
2
|
+
import {
|
|
3
|
+
EventConsumer,
|
|
4
|
+
EventConsumerConfig,
|
|
5
|
+
EventConsumerFunction,
|
|
6
|
+
EventConsumerOptions,
|
|
7
|
+
EventEngineOptions,
|
|
8
|
+
EventMessage,
|
|
9
|
+
EventProducer,
|
|
10
|
+
EventProducerOptions
|
|
11
|
+
} from '../typing';
|
|
12
|
+
|
|
13
|
+
export class Basic {
|
|
14
|
+
private static consumerMap: Map<Function, EventConsumer<unknown, unknown, unknown>> = new Map<
|
|
15
|
+
EventConsumerFunction,
|
|
16
|
+
EventConsumer
|
|
17
|
+
>();
|
|
18
|
+
|
|
19
|
+
public static init<K = string, EV = unknown, V = EV>(
|
|
20
|
+
options: EventEngineOptions,
|
|
21
|
+
producerOptions?: EventProducerOptions<K, EV, V>
|
|
22
|
+
): EventProducer<K, EV, V> {
|
|
23
|
+
const { category, type } = options;
|
|
24
|
+
let producer = ProducerOperator.getProducer<K, EV, V>(category, type);
|
|
25
|
+
if (!producer) {
|
|
26
|
+
producer = ProducerOperator.construct<K, EV, V>(
|
|
27
|
+
{
|
|
28
|
+
category,
|
|
29
|
+
type
|
|
30
|
+
},
|
|
31
|
+
producerOptions
|
|
32
|
+
);
|
|
33
|
+
ProducerOperator.init(producer);
|
|
34
|
+
}
|
|
35
|
+
return producer;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public static startProducer(options: EventEngineOptions): boolean {
|
|
39
|
+
const { category, type } = options;
|
|
40
|
+
const producer = ProducerOperator.getProducer(category, type);
|
|
41
|
+
if (producer) {
|
|
42
|
+
return producer.start();
|
|
43
|
+
}
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public static stopProducer(options: EventEngineOptions): boolean {
|
|
48
|
+
const { category, type } = options;
|
|
49
|
+
const producer = ProducerOperator.getProducer(category, type);
|
|
50
|
+
if (producer) {
|
|
51
|
+
return producer.stop();
|
|
52
|
+
}
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
public static publish<K = string, EV = unknown, V = EV>(options: EventEngineOptions, ev: EV): void {
|
|
57
|
+
const { category, type } = options;
|
|
58
|
+
const producer = ProducerOperator.getProducer<K, EV, V>(category, type);
|
|
59
|
+
if (!producer) {
|
|
60
|
+
throw new Error('Invalid producer. please initialize the producer before publish.');
|
|
61
|
+
}
|
|
62
|
+
producer.publish(ev);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
public static subscribe<
|
|
66
|
+
K = string,
|
|
67
|
+
EV = unknown,
|
|
68
|
+
V = EV,
|
|
69
|
+
M extends EventMessage<V> = EventMessage<V>,
|
|
70
|
+
C extends EventConsumerConfig = EventConsumerConfig
|
|
71
|
+
>(
|
|
72
|
+
options: EventEngineOptions,
|
|
73
|
+
consumerFn: EventConsumerFunction<K, EV, V, M, C>,
|
|
74
|
+
consumerOptions?: EventConsumerOptions<EV, V, M, C>
|
|
75
|
+
): EventConsumer<K, EV, V, M, C> {
|
|
76
|
+
const consumer = ConsumerOperator.construct<K, EV, V, M, C>(options, consumerFn, consumerOptions);
|
|
77
|
+
ConsumerOperator.init(consumer);
|
|
78
|
+
Basic.consumerMap.set(consumerFn, consumer);
|
|
79
|
+
return consumer;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public static unsubscribe<
|
|
83
|
+
K = string,
|
|
84
|
+
EV = unknown,
|
|
85
|
+
V = EV,
|
|
86
|
+
M extends EventMessage<V> = EventMessage<V>,
|
|
87
|
+
C extends EventConsumerConfig = EventConsumerConfig
|
|
88
|
+
>(consumerFn: EventConsumerFunction<K, EV, V, M, C>): EventConsumer<K, EV, V> | undefined {
|
|
89
|
+
const consumer = Basic.consumerMap.get(consumerFn);
|
|
90
|
+
if (consumer) {
|
|
91
|
+
Basic.consumerMap.delete(consumerFn);
|
|
92
|
+
ConsumerOperator.dispose(consumer);
|
|
93
|
+
}
|
|
94
|
+
return consumer as EventConsumer<K, EV, V> | undefined;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
public static startConsumer<
|
|
98
|
+
K = string,
|
|
99
|
+
EV = unknown,
|
|
100
|
+
V = EV,
|
|
101
|
+
M extends EventMessage<V> = EventMessage<V>,
|
|
102
|
+
C extends EventConsumerConfig = EventConsumerConfig
|
|
103
|
+
>(consumerFn: EventConsumerFunction<K, EV, V, M, C>): boolean {
|
|
104
|
+
const consumer = Basic.consumerMap.get(consumerFn);
|
|
105
|
+
if (consumer) {
|
|
106
|
+
return consumer.start();
|
|
107
|
+
}
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
public static stopConsumer<
|
|
112
|
+
K = string,
|
|
113
|
+
EV = unknown,
|
|
114
|
+
V = EV,
|
|
115
|
+
M extends EventMessage<V> = EventMessage<V>,
|
|
116
|
+
C extends EventConsumerConfig = EventConsumerConfig
|
|
117
|
+
>(consumerFn: EventConsumerFunction<K, EV, V, M, C>): boolean {
|
|
118
|
+
const consumer = Basic.consumerMap.get(consumerFn);
|
|
119
|
+
if (consumer) {
|
|
120
|
+
return consumer.stop();
|
|
121
|
+
}
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { ProducerOperator } from '../operator';
|
|
2
|
+
import {
|
|
3
|
+
EventCategory,
|
|
4
|
+
EventConsumer,
|
|
5
|
+
EventConsumerConfig,
|
|
6
|
+
EventConsumerFunction,
|
|
7
|
+
EventConsumerOptions,
|
|
8
|
+
EventMessage,
|
|
9
|
+
EventProducer
|
|
10
|
+
} from '../typing';
|
|
11
|
+
import { Basic } from './basic';
|
|
12
|
+
|
|
13
|
+
export class HTMLEvent {
|
|
14
|
+
public static init<K extends keyof HTMLElementEventMap, EV = HTMLElementEventMap[K]>(
|
|
15
|
+
type: K | '*'
|
|
16
|
+
): EventProducer<K, EV> {
|
|
17
|
+
return Basic.init({ category: EventCategory.html, type });
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public static dispose<K extends keyof HTMLElementEventMap>(
|
|
21
|
+
type: K | '*'
|
|
22
|
+
): EventProducer<K, HTMLElementEventMap[K]> | undefined {
|
|
23
|
+
return ProducerOperator.dispose(`${EventCategory.html}#${type}`);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public static subscribe<
|
|
27
|
+
K extends keyof HTMLElementEventMap,
|
|
28
|
+
EV = HTMLElementEventMap[K],
|
|
29
|
+
V = EV,
|
|
30
|
+
M extends EventMessage<V> = EventMessage<V>,
|
|
31
|
+
C extends EventConsumerConfig = EventConsumerConfig
|
|
32
|
+
>(
|
|
33
|
+
type: K | '*',
|
|
34
|
+
consumerFn: EventConsumerFunction<K, EV, V, M, C>,
|
|
35
|
+
consumerOptions?: EventConsumerOptions<EV, V, M, C>,
|
|
36
|
+
autoInitProducer = true
|
|
37
|
+
): EventConsumer<K, EV, V, M, C> {
|
|
38
|
+
if (autoInitProducer) {
|
|
39
|
+
HTMLEvent.init(type);
|
|
40
|
+
}
|
|
41
|
+
return Basic.subscribe({ category: EventCategory.html, type }, consumerFn, consumerOptions);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { ProducerOperator } from '../operator';
|
|
2
|
+
import {
|
|
3
|
+
EventCategory,
|
|
4
|
+
EventConsumer,
|
|
5
|
+
EventConsumerFunction,
|
|
6
|
+
EventConsumerOptions,
|
|
7
|
+
EventProducer,
|
|
8
|
+
HTMLKeyboardEventConsumerConfig,
|
|
9
|
+
KeyboardEventMessage
|
|
10
|
+
} from '../typing';
|
|
11
|
+
import { Basic } from './basic';
|
|
12
|
+
|
|
13
|
+
const PRODUCER_TYPE = 'global';
|
|
14
|
+
|
|
15
|
+
export class HTMLKeyboardEvent {
|
|
16
|
+
public static init(): EventProducer {
|
|
17
|
+
return Basic.init({ category: EventCategory.keyboard, type: PRODUCER_TYPE });
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public static dispose(): EventProducer | undefined {
|
|
21
|
+
return ProducerOperator.dispose(`${EventCategory.keyboard}#${PRODUCER_TYPE}`);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public static subscribe(
|
|
25
|
+
type: string | '*',
|
|
26
|
+
consumerFn: EventConsumerFunction<
|
|
27
|
+
string,
|
|
28
|
+
KeyboardEvent,
|
|
29
|
+
KeyboardEvent,
|
|
30
|
+
KeyboardEventMessage,
|
|
31
|
+
HTMLKeyboardEventConsumerConfig
|
|
32
|
+
>,
|
|
33
|
+
consumerOptions?: EventConsumerOptions<
|
|
34
|
+
KeyboardEvent,
|
|
35
|
+
KeyboardEvent,
|
|
36
|
+
KeyboardEventMessage,
|
|
37
|
+
HTMLKeyboardEventConsumerConfig
|
|
38
|
+
>,
|
|
39
|
+
autoInitProducer = true
|
|
40
|
+
): EventConsumer<string, KeyboardEvent, KeyboardEvent, KeyboardEventMessage, HTMLKeyboardEventConsumerConfig> {
|
|
41
|
+
if (autoInitProducer) {
|
|
42
|
+
HTMLKeyboardEvent.init();
|
|
43
|
+
}
|
|
44
|
+
return Basic.subscribe({ category: EventCategory.keyboard, type }, consumerFn, consumerOptions);
|
|
45
|
+
}
|
|
46
|
+
}
|