@leafer/event 1.0.1 → 1.0.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer/event",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "@leafer/event",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -22,10 +22,11 @@
22
22
  "leaferjs"
23
23
  ],
24
24
  "dependencies": {
25
- "@leafer/decorator": "1.0.1",
26
- "@leafer/platform": "1.0.1"
25
+ "@leafer/decorator": "1.0.3",
26
+ "@leafer/math": "1.0.3",
27
+ "@leafer/platform": "1.0.3"
27
28
  },
28
29
  "devDependencies": {
29
- "@leafer/interface": "1.0.1"
30
+ "@leafer/interface": "1.0.3"
30
31
  }
31
32
  }
package/src/ChildEvent.ts CHANGED
@@ -7,7 +7,11 @@ export class ChildEvent extends Event implements IChildEvent {
7
7
 
8
8
  static ADD = 'child.add'
9
9
  static REMOVE = 'child.remove'
10
- static DESTROY = 'child.destroy'
10
+
11
+ static CREATED = 'created'
12
+ static MOUNTED = 'mounted'
13
+ static UNMOUNTED = 'unmounted'
14
+ static DESTROY = 'destroy'
11
15
 
12
16
  readonly parent?: ILeaf
13
17
  readonly child?: ILeaf
package/src/Eventer.ts ADDED
@@ -0,0 +1,177 @@
1
+ import { IEventListener, IEventListenerMap, IEventListenerItem, IEventListenerId, IEvent, IObject, IEventTarget, IEventOption, IEventer, IEventMap, InnerId } from '@leafer/interface'
2
+ import { EventCreator } from '@leafer/platform'
3
+
4
+
5
+ const empty = {}
6
+
7
+ export class Eventer implements IEventer {
8
+
9
+ public readonly innerId: InnerId
10
+
11
+ public __captureMap?: IEventListenerMap
12
+
13
+ public __bubbleMap?: IEventListenerMap
14
+
15
+ public syncEventer?: IEventer
16
+
17
+ public set event(map: IEventMap) { this.on(map) }
18
+
19
+
20
+ public on(type: string | string[] | IEventMap, listener?: IEventListener, options?: IEventOption): void {
21
+
22
+ if (!listener) {
23
+ let event, map = type as IEventMap
24
+ for (let key in map) event = map[key], event instanceof Array ? this.on(key, event[0], event[1]) : this.on(key, event)
25
+ return
26
+ }
27
+
28
+ let capture: boolean, once: boolean
29
+ if (options) {
30
+ if (options === 'once') {
31
+ once = true
32
+ } else if (typeof options === 'boolean') {
33
+ capture = options
34
+ } else {
35
+ capture = options.capture
36
+ once = options.once
37
+ }
38
+ }
39
+
40
+ let events: IEventListenerItem[]
41
+ const map = __getListenerMap(this, capture, true)
42
+ const typeList = typeof type === 'string' ? type.split(' ') : type as string[]
43
+ const item = once ? { listener, once } : { listener }
44
+
45
+ typeList.forEach(type => {
46
+ if (type) {
47
+ events = map[type]
48
+ if (events) {
49
+ if (events.findIndex(item => item.listener === listener) === -1) events.push(item)
50
+ } else {
51
+ map[type] = [item]
52
+ }
53
+ }
54
+ })
55
+ }
56
+
57
+ public off(type?: string | string[], listener?: IEventListener, options?: IEventOption): void {
58
+ if (type) {
59
+
60
+ const typeList = typeof type === 'string' ? type.split(' ') : type
61
+
62
+ if (listener) {
63
+
64
+ let capture: boolean
65
+ if (options) capture = typeof options === 'boolean' ? options : (options === 'once' ? false : options.capture)
66
+
67
+ let events: IEventListenerItem[], index: number
68
+ const map = __getListenerMap(this, capture)
69
+
70
+ typeList.forEach(type => {
71
+ if (type) {
72
+ events = map[type]
73
+ if (events) {
74
+ index = events.findIndex(item => item.listener === listener)
75
+ if (index > -1) events.splice(index, 1)
76
+ if (!events.length) delete map[type]
77
+ }
78
+ }
79
+ })
80
+
81
+ } else {
82
+
83
+ // off type
84
+ const { __bubbleMap: b, __captureMap: c } = this
85
+ typeList.forEach(type => {
86
+ if (b) delete b[type]
87
+ if (c) delete c[type]
88
+ })
89
+
90
+ }
91
+
92
+ } else {
93
+
94
+ this.__bubbleMap = this.__captureMap = undefined // off all
95
+
96
+ }
97
+
98
+ }
99
+
100
+ public on_(type: string | string[], listener: IEventListener, bind?: IObject, options?: IEventOption): IEventListenerId {
101
+ if (bind) listener = listener.bind(bind)
102
+ this.on(type, listener, options)
103
+ return { type, current: this as any, listener, options }
104
+ }
105
+
106
+ public off_(id: IEventListenerId | IEventListenerId[]): void {
107
+ if (!id) return
108
+ const list = id instanceof Array ? id : [id]
109
+ list.forEach(item => item.current.off(item.type, item.listener, item.options))
110
+ list.length = 0
111
+ }
112
+
113
+ public once(type: string | string[], listener: IEventListener, capture?: boolean): void {
114
+ this.on(type, listener, { once: true, capture })
115
+ }
116
+
117
+ public emit(type: string, event?: IEvent | IObject, capture?: boolean): void {
118
+ if (!event && EventCreator.has(type)) event = EventCreator.get(type, { type, target: this, current: this } as IEvent)
119
+
120
+ const map = __getListenerMap(this, capture)
121
+ const list = map[type]
122
+ if (list) {
123
+ let item: IEventListenerItem
124
+ for (let i = 0, len = list.length; i < len; i++) {
125
+ item = list[i]
126
+ item.listener(event)
127
+ if (item.once) {
128
+ this.off(type, item.listener, capture)
129
+ i--, len--
130
+ }
131
+ if (event && (event as IEvent).isStopNow) break
132
+ }
133
+ }
134
+
135
+ this.syncEventer && this.syncEventer.emitEvent(event, capture)
136
+ }
137
+
138
+ public emitEvent(event: IEvent, capture?: boolean): void {
139
+ event.current = this
140
+ this.emit(event.type, event, capture)
141
+ }
142
+
143
+ public hasEvent(type: string, capture?: boolean): boolean {
144
+ if (this.syncEventer && this.syncEventer.hasEvent(type, capture)) return true
145
+
146
+ const { __bubbleMap: b, __captureMap: c } = this
147
+ const hasB = b && b[type], hasC = c && c[type]
148
+ return !!(capture === undefined ? (hasB || hasC) : (capture ? hasC : hasB))
149
+ }
150
+
151
+ public destroy(): void {
152
+ this.__captureMap = this.__bubbleMap = this.syncEventer = null
153
+ }
154
+ }
155
+
156
+
157
+ function __getListenerMap(eventer: IEventTarget, capture?: boolean, create?: boolean): IEventListenerMap {
158
+ if (capture) {
159
+
160
+ const { __captureMap: c } = eventer
161
+ if (c) {
162
+ return c
163
+ } else {
164
+ return create ? eventer.__captureMap = {} : empty
165
+ }
166
+
167
+ } else {
168
+
169
+ const { __bubbleMap: b } = eventer
170
+ if (b) {
171
+ return b
172
+ } else {
173
+ return create ? eventer.__bubbleMap = {} : empty
174
+ }
175
+
176
+ }
177
+ }
package/src/index.ts CHANGED
@@ -4,8 +4,8 @@ export { ImageEvent } from './ImageEvent'
4
4
  export { ResizeEvent } from './ResizeEvent'
5
5
  export { WatchEvent } from './WatchEvent'
6
6
  export { LayoutEvent } from './LayoutEvent'
7
- export { AnimateEvent } from './AnimateEvent'
8
7
  export { RenderEvent } from './RenderEvent'
9
8
  export { LeaferEvent } from './LeaferEvent'
10
9
 
11
- export { Event } from './Event'
10
+ export { Event } from './Event'
11
+ export { Eventer } from './Eventer'
package/types/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { IEvent, IObject, IEventTarget, IChildEvent, ILeaf, IPropertyEvent, IImageEvent, ILeaferImage, IResizeEvent, IScreenSizeData, IWatchEvent, IWatchEventData, ILayoutEvent, ILayoutBlockData, IAnimateEvent, IRenderEvent, IBounds, IRenderOptions, ILeaferEvent } from '@leafer/interface';
1
+ import { IEvent, IObject, IEventTarget, IChildEvent, ILeaf, IPropertyEvent, IImageEvent, ILeaferImage, IResizeEvent, IScreenSizeData, IWatchEvent, IWatchEventData, ILayoutEvent, ILayoutBlockData, IRenderEvent, IBounds, IRenderOptions, ILeaferEvent, IEventer, InnerId, IEventListenerMap, IEventMap, IEventListener, IEventOption, IEventListenerId } from '@leafer/interface';
2
2
 
3
3
  declare class Event implements IEvent {
4
4
  readonly origin: IObject;
@@ -19,6 +19,9 @@ declare class Event implements IEvent {
19
19
  declare class ChildEvent extends Event implements IChildEvent {
20
20
  static ADD: string;
21
21
  static REMOVE: string;
22
+ static CREATED: string;
23
+ static MOUNTED: string;
24
+ static UNMOUNTED: string;
22
25
  static DESTROY: string;
23
26
  readonly parent?: ILeaf;
24
27
  readonly child?: ILeaf;
@@ -78,10 +81,6 @@ declare class LayoutEvent extends Event implements ILayoutEvent {
78
81
  constructor(type: string, data?: ILayoutBlockData[], times?: number);
79
82
  }
80
83
 
81
- declare class AnimateEvent extends Event implements IAnimateEvent {
82
- static FRAME: string;
83
- }
84
-
85
84
  declare class RenderEvent extends Event implements IRenderEvent {
86
85
  static REQUEST: string;
87
86
  static START: string;
@@ -109,4 +108,21 @@ declare class LeaferEvent extends Event implements ILeaferEvent {
109
108
  static END: string;
110
109
  }
111
110
 
112
- export { AnimateEvent, ChildEvent, Event, ImageEvent, LayoutEvent, LeaferEvent, PropertyEvent, RenderEvent, ResizeEvent, WatchEvent };
111
+ declare class Eventer implements IEventer {
112
+ readonly innerId: InnerId;
113
+ __captureMap?: IEventListenerMap;
114
+ __bubbleMap?: IEventListenerMap;
115
+ syncEventer?: IEventer;
116
+ set event(map: IEventMap);
117
+ on(type: string | string[] | IEventMap, listener?: IEventListener, options?: IEventOption): void;
118
+ off(type?: string | string[], listener?: IEventListener, options?: IEventOption): void;
119
+ on_(type: string | string[], listener: IEventListener, bind?: IObject, options?: IEventOption): IEventListenerId;
120
+ off_(id: IEventListenerId | IEventListenerId[]): void;
121
+ once(type: string | string[], listener: IEventListener, capture?: boolean): void;
122
+ emit(type: string, event?: IEvent | IObject, capture?: boolean): void;
123
+ emitEvent(event: IEvent, capture?: boolean): void;
124
+ hasEvent(type: string, capture?: boolean): boolean;
125
+ destroy(): void;
126
+ }
127
+
128
+ export { ChildEvent, Event, Eventer, ImageEvent, LayoutEvent, LeaferEvent, PropertyEvent, RenderEvent, ResizeEvent, WatchEvent };
@@ -1,10 +0,0 @@
1
- import { IAnimateEvent } from '@leafer/interface'
2
-
3
- import { Event } from './Event'
4
-
5
-
6
- export class AnimateEvent extends Event implements IAnimateEvent {
7
-
8
- static FRAME = 'animate.frame'
9
-
10
- }