Skip to content

Commit 7a2ba6e

Browse files
authored
fix: memory leak (#22)
1 parent 9a7501a commit 7a2ba6e

File tree

1 file changed

+100
-100
lines changed

1 file changed

+100
-100
lines changed

src/EventTarget.js

Lines changed: 100 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,100 @@
1-
const _events = new Map()
2-
3-
class Touch {
4-
constructor(touch) {
5-
// CanvasTouch{identifier, x, y}
6-
// Touch{identifier, pageX, pageY, clientX, clientY, force}
7-
this.identifier = touch.identifier
8-
9-
this.force = touch.force === undefined ? 1 : touch.force
10-
this.pageX = touch.pageX || touch.x
11-
this.pageY = touch.pageY || touch.y
12-
this.clientX = touch.clientX || touch.x
13-
this.clientY = touch.clientY || touch.y
14-
15-
this.screenX = this.pageX
16-
this.screenY = this.pageY
17-
}
18-
}
19-
20-
export default class EventTarget {
21-
constructor() {
22-
_events.set(this, {})
23-
}
24-
25-
addEventListener(type, listener, options = {}) {
26-
let events = _events.get(this)
27-
28-
if (!events) {
29-
events = {}
30-
_events.set(this, events)
31-
}
32-
if (!events[type]) {
33-
events[type] = []
34-
}
35-
events[type].push(listener)
36-
37-
if (options.capture) {
38-
// console.warn('EventTarget.addEventListener: options.capture is not implemented.')
39-
}
40-
if (options.once) {
41-
// console.warn('EventTarget.addEventListener: options.once is not implemented.')
42-
}
43-
if (options.passive) {
44-
// console.warn('EventTarget.addEventListener: options.passive is not implemented.')
45-
}
46-
}
47-
48-
removeEventListener(type, listener) {
49-
const events = _events.get(this)
50-
51-
if (events) {
52-
const listeners = events[type]
53-
54-
if (listeners && listeners.length > 0) {
55-
for (let i = listeners.length; i--; i > 0) {
56-
if (listeners[i] === listener) {
57-
listeners.splice(i, 1)
58-
break
59-
}
60-
}
61-
}
62-
}
63-
}
64-
65-
dispatchEvent(event = {}) {
66-
if (typeof event.preventDefault !== 'function') {
67-
event.preventDefault = () => {}
68-
}
69-
if (typeof event.stopPropagation !== 'function') {
70-
event.stopPropagation = () => {}
71-
}
72-
const listeners = _events.get(this)[event.type]
73-
74-
if (listeners) {
75-
for (let i = 0; i < listeners.length; i++) {
76-
listeners[i](event)
77-
}
78-
}
79-
}
80-
81-
dispatchTouchEvent(e = {}) {
82-
const target = {
83-
...this
84-
}
85-
86-
const event = {
87-
changedTouches: e.changedTouches.map(touch => new Touch(touch)),
88-
touches: e.touches.map(touch => new Touch(touch)),
89-
targetTouches: Array.prototype.slice.call(e.touches.map(touch => new Touch(touch))),
90-
timeStamp: e.timeStamp,
91-
target: target,
92-
currentTarget: target,
93-
type: e.type,
94-
cancelBubble: false,
95-
cancelable: false
96-
}
97-
98-
this.dispatchEvent(event)
99-
}
100-
}
1+
const _events = new WeakMap()
2+
3+
class Touch {
4+
constructor(touch) {
5+
// CanvasTouch{identifier, x, y}
6+
// Touch{identifier, pageX, pageY, clientX, clientY, force}
7+
this.identifier = touch.identifier
8+
9+
this.force = touch.force === undefined ? 1 : touch.force
10+
this.pageX = touch.pageX || touch.x
11+
this.pageY = touch.pageY || touch.y
12+
this.clientX = touch.clientX || touch.x
13+
this.clientY = touch.clientY || touch.y
14+
15+
this.screenX = this.pageX
16+
this.screenY = this.pageY
17+
}
18+
}
19+
20+
export default class EventTarget {
21+
constructor() {
22+
_events.set(this, {})
23+
}
24+
25+
addEventListener(type, listener, options = {}) {
26+
let events = _events.get(this)
27+
28+
if (!events) {
29+
events = {}
30+
_events.set(this, events)
31+
}
32+
if (!events[type]) {
33+
events[type] = []
34+
}
35+
events[type].push(listener)
36+
37+
if (options.capture) {
38+
// console.warn('EventTarget.addEventListener: options.capture is not implemented.')
39+
}
40+
if (options.once) {
41+
// console.warn('EventTarget.addEventListener: options.once is not implemented.')
42+
}
43+
if (options.passive) {
44+
// console.warn('EventTarget.addEventListener: options.passive is not implemented.')
45+
}
46+
}
47+
48+
removeEventListener(type, listener) {
49+
const events = _events.get(this)
50+
51+
if (events) {
52+
const listeners = events[type]
53+
54+
if (listeners && listeners.length > 0) {
55+
for (let i = listeners.length; i--; i > 0) {
56+
if (listeners[i] === listener) {
57+
listeners.splice(i, 1)
58+
break
59+
}
60+
}
61+
}
62+
}
63+
}
64+
65+
dispatchEvent(event = {}) {
66+
if (typeof event.preventDefault !== 'function') {
67+
event.preventDefault = () => {}
68+
}
69+
if (typeof event.stopPropagation !== 'function') {
70+
event.stopPropagation = () => {}
71+
}
72+
const listeners = _events.get(this)[event.type]
73+
74+
if (listeners) {
75+
for (let i = 0; i < listeners.length; i++) {
76+
listeners[i](event)
77+
}
78+
}
79+
}
80+
81+
dispatchTouchEvent(e = {}) {
82+
const target = {
83+
...this
84+
}
85+
86+
const event = {
87+
changedTouches: e.changedTouches.map(touch => new Touch(touch)),
88+
touches: e.touches.map(touch => new Touch(touch)),
89+
targetTouches: Array.prototype.slice.call(e.touches.map(touch => new Touch(touch))),
90+
timeStamp: e.timeStamp,
91+
target: target,
92+
currentTarget: target,
93+
type: e.type,
94+
cancelBubble: false,
95+
cancelable: false
96+
}
97+
98+
this.dispatchEvent(event)
99+
}
100+
}

0 commit comments

Comments
 (0)