How to use addEventPoolingTo method in Playwright Internal

Best JavaScript code snippet using playwright-internal

Run Playwright Internal automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

SyntheticEvent.js

Source: SyntheticEvent.js Github

copy
1const EVENT_POOL_SIZE = 10;
2
3const EventInterface = {
4	type: null,
5	target: null,
6	// currentTarget is set when dispatching; no use in copying it here
7	currentTarget: function() {
8		return null;
9	},
10	eventPhase: null,
11	bubbles: null,
12	cancelable: null,
13	timeStamp: function(event) {
14		return event.timeStamp || Date.now();
15	},
16	defaultPrevented: null,
17	isTrusted: null,
18};
19
20function functionThatReturnsTrue() {
21	return true;
22}
23
24function functionThatReturnsFalse() {
25	return false;
26}
27
28function SyntheticEvent(
29	dispatchConfig,
30	targetInst,
31	nativeEvent,
32	nativeEventTarget,
33) {
34	this.dispatchConfig = dispatchConfig;
35	this._targetInst = targetInst;
36	this.nativeEvent = nativeEvent;
37
38	const Interface = this.constructor.Interface;
39
40	for (const propName in Interface) {
41		if (!Interface.hasOwnProperty(propName)) {
42			continue;
43		}
44		const normalize = Interface[propName];
45		if (normalize) {
46			this[propName] = normalize(nativeEvent);
47		} else {
48			if (propName === 'target') {
49				this.target = nativeEventTarget;
50			} else {
51				this[propName] = nativeEvent[propName];
52			}
53		}
54	}
55
56	const defaultPrevented =
57		nativeEvent.defaultPrevented != null
58			? nativeEvent.defaultPrevented
59			: nativeEvent.returnValue === false;
60
61	if (defaultPrevented) {
62		this.isDefaultPrevented = functionThatReturnsTrue;
63	} else {
64		this.isDefaultPrevented = functionThatReturnsFalse;
65	}
66	this.isPropagationStopped = functionThatReturnsFalse;
67	return this;
68}
69
70Object.assign(SyntheticEvent.prototype, {
71	preventDefault: function() {
72		this.defaultPrevented = true;
73		const event = this.nativeEvent;
74		if (!event) {
75			return;
76		}
77
78		if (event.preventDefault) {
79			event.preventDefault();
80		} else if (typeof event.returnValue !== 'unknown') {
81			event.returnValue = false;
82		}
83		this.isDefaultPrevented = functionThatReturnsTrue;
84	},
85	stopPropagation: function() {
86		const event = this.nativeEvent;
87		if (!event) {
88			return;
89		}
90
91		if (event.stopPropagation) {
92			event.stopPropagation();
93		} else if (typeof event.cancelBubble !== 'unknown') {
94			event.cancelBubble = true;
95		}
96
97		this.isPropagationStopped = functionThatReturnsTrue;
98	},
99	persist: function() {
100		this.isPersistent = functionThatReturnsTrue;
101	},
102	isPersistent: functionThatReturnsFalse,
103	destructor: function() {
104		const Interface = this.constructor.Interface;
105		for (const propName in Interface) {
106			this[propName] = null;
107		}
108		this.dispatchConfig = null;
109		this._targetInst = null;
110		this.nativeEvent = null;
111		this.isDefaultPrevented = functionThatReturnsFalse;
112		this.isPropagationStopped = functionThatReturnsFalse;
113		this._dispatchListeners = null;
114		this._dispatchInstances = null;
115	},
116});
117
118SyntheticEvent.Interface = EventInterface;
119
120SyntheticEvent.extend = function(Interface) {
121	const Super = this;
122
123	const E = function() {};
124	E.prototype = Super.prototype;
125	const prototype = new E();
126
127	function Class() {
128		return Super.apply(this, arguments);
129	}
130	Object.assign(prototype, Class.prototype);
131	Class.prototype = prototype;
132	Class.prototype.constructor = Class;
133
134	Class.Interface = Object.assign({}, Super.Interface, Interface);
135	Class.extend = Super.extend;
136	addEventPoolingTo(Class);
137
138	return Class;
139};
140
141addEventPoolingTo(SyntheticEvent);
142
143function addEventPoolingTo(EventConstructor) {
144	EventConstructor.eventPool = [];
145	EventConstructor.getPooled = getPooledEvent;
146	EventConstructor.release = releasePooledEvent;
147}
148
149function getPooledEvent(dispatchConfig, targetInst, nativeEvent, nativeInst) {
150	const EventConstructor = this;
151	if (EventConstructor.eventPool.length) {
152		const instance = EventConstructor.eventPool.pop();
153		EventConstructor.call(
154			instance,
155			dispatchConfig,
156			targetInst,
157			nativeEvent,
158			nativeInst,
159		);
160		return instance;
161	}
162	return new EventConstructor(
163		dispatchConfig,
164		targetInst,
165		nativeEvent,
166		nativeInst,
167	);
168}
169
170function releasePooledEvent(event) {
171	const EventConstructor = this;
172	event.destructor();
173	if (EventConstructor.eventPool.length < EVENT_POOL_SIZE) {
174		EventConstructor.eventPool.push(event);
175	}
176}
177
178export default SyntheticEvent;
179
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Internal on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)