@leafer/event 1.6.2 → 1.6.4

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.6.2",
3
+ "version": "1.6.4",
4
4
  "description": "@leafer/event",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -22,11 +22,11 @@
22
22
  "leaferjs"
23
23
  ],
24
24
  "dependencies": {
25
- "@leafer/decorator": "1.6.2",
26
- "@leafer/math": "1.6.2",
27
- "@leafer/platform": "1.6.2"
25
+ "@leafer/decorator": "1.6.4",
26
+ "@leafer/math": "1.6.4",
27
+ "@leafer/platform": "1.6.4"
28
28
  },
29
29
  "devDependencies": {
30
- "@leafer/interface": "1.6.2"
30
+ "@leafer/interface": "1.6.4"
31
31
  }
32
32
  }
@@ -0,0 +1,48 @@
1
+ import { IBoundsEvent, IEventTarget, ILeaf, IObject } from '@leafer/interface'
2
+
3
+ import { Event } from './Event'
4
+
5
+
6
+ export class BoundsEvent extends Event implements IBoundsEvent {
7
+
8
+ static RESIZE = 'bounds.resize'
9
+
10
+ static INNER = 'bounds.inner'
11
+
12
+ static LOCAL = 'bounds.local'
13
+
14
+ static WORLD = 'bounds.world'
15
+
16
+
17
+ static checkHas(leaf: IEventTarget, type: string, mode: 'on' | 'off') {
18
+ if (mode === 'on') {
19
+ type === WORLD ? leaf.__hasWorldEvent = true : leaf.__hasLocalEvent = true
20
+ } else {
21
+ leaf.__hasLocalEvent = leaf.hasEvent(RESIZE) || leaf.hasEvent(INNER) || leaf.hasEvent(LOCAL)
22
+ leaf.__hasWorldEvent = leaf.hasEvent(WORLD)
23
+ }
24
+ }
25
+
26
+ static emitLocal(leaf: ILeaf) {
27
+ if (leaf.leaferIsReady) {
28
+ const { resized } = leaf.__layout
29
+ if (resized !== 'local') {
30
+ leaf.emit(RESIZE, leaf)
31
+ if (resized === 'inner') leaf.emit(INNER, leaf)
32
+ }
33
+ leaf.emit(LOCAL, leaf)
34
+ }
35
+ }
36
+
37
+ static emitWorld(leaf: ILeaf) {
38
+ if (leaf.leaferIsReady) leaf.emit(WORLD, this)
39
+ }
40
+
41
+ }
42
+
43
+ const { RESIZE, INNER, LOCAL, WORLD } = BoundsEvent
44
+
45
+
46
+ export const boundsEventMap: IObject = {};
47
+
48
+ [RESIZE, INNER, LOCAL, WORLD].forEach(key => boundsEventMap[key] = 1)
package/src/Eventer.ts CHANGED
@@ -1,9 +1,12 @@
1
- import { IEventListener, IEventListenerMap, IEventListenerItem, IEventListenerId, IEvent, IObject, IEventTarget, IEventOption, IEventer, IEventMap, InnerId } from '@leafer/interface'
1
+ import { IEventListener, IEventListenerMap, IEventListenerItem, IEventListenerId, IEvent, IObject, IEventTarget, IEventOption, IEventer, IEventParamsMap, InnerId, IEventParams, IFunction } from '@leafer/interface'
2
2
  import { EventCreator } from '@leafer/platform'
3
3
 
4
+ import { BoundsEvent, boundsEventMap } from './BoundsEvent'
5
+
4
6
 
5
7
  const empty = {}
6
8
 
9
+
7
10
  export class Eventer implements IEventer {
8
11
 
9
12
  public readonly innerId: InnerId
@@ -12,16 +15,20 @@ export class Eventer implements IEventer {
12
15
 
13
16
  public __bubbleMap?: IEventListenerMap
14
17
 
18
+ public __hasLocalEvent?: boolean
19
+ public __hasWorldEvent?: boolean
20
+
15
21
  public syncEventer?: IEventer
16
22
 
17
- public set event(map: IEventMap) { this.on(map) }
23
+ public set event(map: IEventParamsMap) { this.on(map) }
18
24
 
19
25
 
20
- public on(type: string | string[] | IEventMap, listener?: IEventListener, options?: IEventOption): void {
26
+ public on(type: string | string[] | IEventParams[] | IEventParamsMap, listener?: IEventListener, options?: IEventOption): void {
21
27
 
22
28
  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)
29
+ let event: IFunction | [IFunction, IEventOption]
30
+ if (type instanceof Array) (type as IEventParams[]).forEach(item => this.on(item[0], item[1], item[2]))
31
+ else for (let key in type as IEventParamsMap) (event = (type as IEventParamsMap)[key]) instanceof Array ? this.on(key, event[0], event[1]) : this.on(key, event)
25
32
  return
26
33
  }
27
34
 
@@ -50,6 +57,8 @@ export class Eventer implements IEventer {
50
57
  } else {
51
58
  map[type] = [item]
52
59
  }
60
+
61
+ if (boundsEventMap[type]) BoundsEvent.checkHas(this, type, 'on')
53
62
  }
54
63
  })
55
64
  }
@@ -74,6 +83,7 @@ export class Eventer implements IEventer {
74
83
  index = events.findIndex(item => item.listener === listener)
75
84
  if (index > -1) events.splice(index, 1)
76
85
  if (!events.length) delete map[type]
86
+ if (boundsEventMap[type]) BoundsEvent.checkHas(this, type, 'off')
77
87
  }
78
88
  }
79
89
  })
@@ -97,20 +107,26 @@ export class Eventer implements IEventer {
97
107
 
98
108
  }
99
109
 
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)
110
+ public on_(type: string | string[] | IEventParams[], listener?: IEventListener, bind?: IObject, options?: IEventOption): IEventListenerId {
111
+ if (!listener) (type instanceof Array) && (type as IEventParams[]).forEach(item => this.on(item[0], item[2] ? item[1] = item[1].bind(item[2]) : item[1], item[3]))
112
+ else this.on(type, bind ? listener = listener.bind(bind) : listener, options)
103
113
  return { type, current: this as any, listener, options }
104
114
  }
105
115
 
106
116
  public off_(id: IEventListenerId | IEventListenerId[]): void {
107
117
  if (!id) return
108
118
  const list = id instanceof Array ? id : [id]
109
- list.forEach(item => item.current.off(item.type, item.listener, item.options))
119
+ list.forEach(item => {
120
+ if (!item.listener) (item.type instanceof Array) && (item.type as IEventParams[]).forEach(v => item.current.off(v[0], v[1], v[3]))
121
+ else item.current.off(item.type as string | string[], item.listener, item.options)
122
+ })
110
123
  list.length = 0
111
124
  }
112
125
 
113
- public once(type: string | string[], listener: IEventListener, capture?: boolean): void {
126
+ public once(type: string | string[] | IEventParams[], listener?: IEventListener, captureOrBind?: boolean | IObject, capture?: boolean): void {
127
+ if (!listener) return (type instanceof Array) && (type as IEventParams[]).forEach(item => this.once(item[0], item[1], item[2], item[3]))
128
+ if (typeof captureOrBind === 'object') listener = listener.bind(captureOrBind)
129
+ else capture = captureOrBind
114
130
  this.on(type, listener, { once: true, capture })
115
131
  }
116
132
 
package/src/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export { ChildEvent } from './ChildEvent'
2
2
  export { PropertyEvent } from './PropertyEvent'
3
3
  export { ImageEvent } from './ImageEvent'
4
+ export { BoundsEvent } from './BoundsEvent'
4
5
  export { ResizeEvent } from './ResizeEvent'
5
6
  export { WatchEvent } from './WatchEvent'
6
7
  export { LayoutEvent } from './LayoutEvent'
package/types/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { IEvent, IObject, IEventTarget, IChildEvent, ILeaf, IPropertyEvent, IImageEvent, ILeaferImage, IResizeEvent, INumberMap, IScreenSizeData, IWatchEvent, IWatchEventData, ILayoutEvent, ILayoutBlockData, IRenderEvent, IBounds, IRenderOptions, ILeaferEvent, IEventer, InnerId, IEventListenerMap, IEventMap, IEventListener, IEventOption, IEventListenerId } from '@leafer/interface';
1
+ import { IEvent, IObject, IEventTarget, IChildEvent, ILeaf, IPropertyEvent, IImageEvent, ILeaferImage, IBoundsEvent, IResizeEvent, INumberMap, IScreenSizeData, IWatchEvent, IWatchEventData, ILayoutEvent, ILayoutBlockData, IRenderEvent, IBounds, IRenderOptions, ILeaferEvent, IEventer, InnerId, IEventListenerMap, IEventParamsMap, IEventParams, IEventListener, IEventOption, IEventListenerId } from '@leafer/interface';
2
2
 
3
3
  declare class Event implements IEvent {
4
4
  readonly origin: IObject;
@@ -48,6 +48,16 @@ declare class ImageEvent extends Event implements IImageEvent {
48
48
  constructor(type: string, data: IImageEvent);
49
49
  }
50
50
 
51
+ declare class BoundsEvent extends Event implements IBoundsEvent {
52
+ static RESIZE: string;
53
+ static INNER: string;
54
+ static LOCAL: string;
55
+ static WORLD: string;
56
+ static checkHas(leaf: IEventTarget, type: string, mode: 'on' | 'off'): void;
57
+ static emitLocal(leaf: ILeaf): void;
58
+ static emitWorld(leaf: ILeaf): void;
59
+ }
60
+
51
61
  declare class ResizeEvent extends Event implements IResizeEvent {
52
62
  static RESIZE: string;
53
63
  static resizingKeys: INumberMap;
@@ -115,17 +125,19 @@ declare class Eventer implements IEventer {
115
125
  readonly innerId: InnerId;
116
126
  __captureMap?: IEventListenerMap;
117
127
  __bubbleMap?: IEventListenerMap;
128
+ __hasLocalEvent?: boolean;
129
+ __hasWorldEvent?: boolean;
118
130
  syncEventer?: IEventer;
119
- set event(map: IEventMap);
120
- on(type: string | string[] | IEventMap, listener?: IEventListener, options?: IEventOption): void;
131
+ set event(map: IEventParamsMap);
132
+ on(type: string | string[] | IEventParams[] | IEventParamsMap, listener?: IEventListener, options?: IEventOption): void;
121
133
  off(type?: string | string[], listener?: IEventListener, options?: IEventOption): void;
122
- on_(type: string | string[], listener: IEventListener, bind?: IObject, options?: IEventOption): IEventListenerId;
134
+ on_(type: string | string[] | IEventParams[], listener?: IEventListener, bind?: IObject, options?: IEventOption): IEventListenerId;
123
135
  off_(id: IEventListenerId | IEventListenerId[]): void;
124
- once(type: string | string[], listener: IEventListener, capture?: boolean): void;
136
+ once(type: string | string[] | IEventParams[], listener?: IEventListener, captureOrBind?: boolean | IObject, capture?: boolean): void;
125
137
  emit(type: string, event?: IEvent | IObject, capture?: boolean): void;
126
138
  emitEvent(event: IEvent, capture?: boolean): void;
127
139
  hasEvent(type: string, capture?: boolean): boolean;
128
140
  destroy(): void;
129
141
  }
130
142
 
131
- export { ChildEvent, Event, Eventer, ImageEvent, LayoutEvent, LeaferEvent, PropertyEvent, RenderEvent, ResizeEvent, WatchEvent };
143
+ export { BoundsEvent, ChildEvent, Event, Eventer, ImageEvent, LayoutEvent, LeaferEvent, PropertyEvent, RenderEvent, ResizeEvent, WatchEvent };