UNPKG

@tamagui/react-native-web-lite

Version:
118 lines (117 loc) 4.29 kB
import NativeAnimatedHelper from "../NativeAnimatedHelper"; import { invariant } from "@tamagui/react-native-web-internals"; const NativeAnimatedAPI = NativeAnimatedHelper.API; let _uniqueId = 1; class AnimatedNode { _listeners; _platformConfig; __nativeAnimatedValueListener; __attach() { } __detach() { this.__isNative && this.__nativeTag != null && (NativeAnimatedHelper.API.dropAnimatedNode(this.__nativeTag), this.__nativeTag = void 0); } __getValue() { } __getAnimatedValue() { return this.__getValue(); } __addChild(child) { } __removeChild(child) { } __getChildren() { return []; } /* Methods and props used by native Animated impl */ __isNative; __nativeTag; __shouldUpdateListenersForNewNativeTag; constructor() { this._listeners = {}; } __makeNative(platformConfig) { if (!this.__isNative) throw new Error('This node cannot be made a "native" animated node'); this._platformConfig = platformConfig, this.hasListeners() && this._startListeningToNativeValueUpdates(); } /** * Adds an asynchronous listener to the value so you can observe updates from * animations. This is useful because there is no way to * synchronously read the value because it might be driven natively. * * See https://reactnative.dev/docs/animatedvalue#addlistener */ addListener(callback) { const id = String(_uniqueId++); return this._listeners[id] = callback, this.__isNative && this._startListeningToNativeValueUpdates(), id; } /** * Unregister a listener. The `id` param shall match the identifier * previously returned by `addListener()`. * * See https://reactnative.dev/docs/animatedvalue#removelistener */ removeListener(id) { delete this._listeners[id], this.__isNative && !this.hasListeners() && this._stopListeningForNativeValueUpdates(); } /** * Remove all registered listeners. * * See https://reactnative.dev/docs/animatedvalue#removealllisteners */ removeAllListeners() { this._listeners = {}, this.__isNative && this._stopListeningForNativeValueUpdates(); } hasListeners() { return !!Object.keys(this._listeners).length; } _startListeningToNativeValueUpdates() { this.__nativeAnimatedValueListener && !this.__shouldUpdateListenersForNewNativeTag || (this.__shouldUpdateListenersForNewNativeTag && (this.__shouldUpdateListenersForNewNativeTag = !1, this._stopListeningForNativeValueUpdates()), NativeAnimatedAPI.startListeningToAnimatedNodeValue(this.__getNativeTag()), this.__nativeAnimatedValueListener = NativeAnimatedHelper.nativeEventEmitter.addListener( "onAnimatedValueUpdate", (data) => { data.tag === this.__getNativeTag() && this.__onAnimatedValueUpdateReceived(data.value); } )); } __onAnimatedValueUpdateReceived(value) { this.__callListeners(value); } __callListeners(value) { for (const key in this._listeners) this._listeners[key]({ value }); } _stopListeningForNativeValueUpdates() { this.__nativeAnimatedValueListener && (this.__nativeAnimatedValueListener.remove(), this.__nativeAnimatedValueListener = null, NativeAnimatedAPI.stopListeningToAnimatedNodeValue(this.__getNativeTag())); } __getNativeTag() { NativeAnimatedHelper.assertNativeAnimatedModule(), invariant( this.__isNative, 'Attempt to get native tag from node not marked as "native"' ); const nativeTag = this.__nativeTag ?? NativeAnimatedHelper.generateNewNodeTag(); if (this.__nativeTag == null) { this.__nativeTag = nativeTag; const config = this.__getNativeConfig(); this._platformConfig && (config.platformConfig = this._platformConfig), NativeAnimatedHelper.API.createAnimatedNode(nativeTag, config), this.__shouldUpdateListenersForNewNativeTag = !0; } return nativeTag; } __getNativeConfig() { throw new Error("This JS animated node type cannot be used as native animated node"); } toJSON() { return this.__getValue(); } __getPlatformConfig() { return this._platformConfig; } __setPlatformConfig(platformConfig) { this._platformConfig = platformConfig; } } var AnimatedNode_default = AnimatedNode; export { AnimatedNode_default as default }; //# sourceMappingURL=AnimatedNode.js.map