mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Animated: Store Listeners w/ Map
Summary: Currently, `AnimatedNode.prototype.getListeners` creates an array with `Object.keys()` to determine the number of listeners. This is a relatively hot code path for animation-intensive user interfaces. Although `Object.keys()` is fast, every unnecessary memory allocation is an unnecessary opportunity to create garbage that requires collection. Using an object as a dictionary performs worst than using a `Map` anyway, so this switches `AnimatedNode` to use a `Map`. Changelog: [Internal] Reviewed By: javache Differential Revision: D62267352 fbshipit-source-id: 8629861a64109a3a711c0f66a345029d0bfcd440
This commit is contained in:
1 parent
d1ebe02c19
commit
2f8676924d
2 files changed
+11
-13
No files matched your search
+10
-10
@@ -32,7 +32,7 @@ let _assertNativeAnimatedModule: ?() => void = () => {
|
||||
// Note(vjeux): this would be better as an interface but flow doesn't
|
||||
// support them yet
|
||||
export default class AnimatedNode {
|
||||
_listeners: {[key: string]: ValueListenerCallback, ...} = {};
|
||||
#listeners: Map<string, ValueListenerCallback> = new Map();
|
||||
_platformConfig: ?PlatformConfig = undefined;
|
||||
__nativeAnimatedValueListener: ?EventSubscription = null;
|
||||
__attach(): void {}
|
||||
@@ -64,7 +64,7 @@ export default class AnimatedNode {
|
||||
}
|
||||
|
||||
this._platformConfig = platformConfig;
|
||||
if (this.hasListeners()) {
|
||||
if (this.#listeners.size > 0) {
|
||||
this._startListeningToNativeValueUpdates();
|
||||
}
|
||||
}
|
||||
@@ -78,7 +78,7 @@ export default class AnimatedNode {
|
||||
*/
|
||||
addListener(callback: (value: any) => mixed): string {
|
||||
const id = String(_uniqueId++);
|
||||
this._listeners[id] = callback;
|
||||
this.#listeners.set(id, callback);
|
||||
if (this.__isNative) {
|
||||
this._startListeningToNativeValueUpdates();
|
||||
}
|
||||
@@ -92,8 +92,8 @@ export default class AnimatedNode {
|
||||
* See https://reactnative.dev/docs/animatedvalue#removelistener
|
||||
*/
|
||||
removeListener(id: string): void {
|
||||
delete this._listeners[id];
|
||||
if (this.__isNative && !this.hasListeners()) {
|
||||
this.#listeners.delete(id);
|
||||
if (this.__isNative && this.#listeners.size === 0) {
|
||||
this._stopListeningForNativeValueUpdates();
|
||||
}
|
||||
}
|
||||
@@ -104,14 +104,14 @@ export default class AnimatedNode {
|
||||
* See https://reactnative.dev/docs/animatedvalue#removealllisteners
|
||||
*/
|
||||
removeAllListeners(): void {
|
||||
this._listeners = {};
|
||||
this.#listeners.clear();
|
||||
if (this.__isNative) {
|
||||
this._stopListeningForNativeValueUpdates();
|
||||
}
|
||||
}
|
||||
|
||||
hasListeners(): boolean {
|
||||
return !!Object.keys(this._listeners).length;
|
||||
return this.#listeners.size > 0;
|
||||
}
|
||||
|
||||
_startListeningToNativeValueUpdates() {
|
||||
@@ -146,9 +146,9 @@ export default class AnimatedNode {
|
||||
|
||||
__callListeners(value: number): void {
|
||||
const event = {value};
|
||||
for (const key in this._listeners) {
|
||||
this._listeners[key](event);
|
||||
}
|
||||
this.#listeners.forEach(listener => {
|
||||
listener(event);
|
||||
});
|
||||
}
|
||||
|
||||
_stopListeningForNativeValueUpdates() {
|
||||
|
||||
@@ -903,9 +903,7 @@ exports[`public API should not change unintentionally Libraries/Animated/nodes/A
|
||||
`;
|
||||
|
||||
exports[`public API should not change unintentionally Libraries/Animated/nodes/AnimatedNode.js 1`] = `
|
||||
"type ValueListenerCallback = (state: { value: number, ... }) => mixed;
|
||||
declare export default class AnimatedNode {
|
||||
_listeners: { [key: string]: ValueListenerCallback, ... };
|
||||
"declare export default class AnimatedNode {
|
||||
_platformConfig: ?PlatformConfig;
|
||||
__nativeAnimatedValueListener: ?EventSubscription;
|
||||
__attach(): void;
|
||||
|
||||
Reference in new issue
Block a user