@tamagui/react-native-web-lite
Version:
React Native for Web
174 lines (173 loc) • 5.7 kB
JavaScript
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all) __defProp(target, name, {
get: all[name],
enumerable: true
});
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: () => from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
value: true
}), mod);
var AnimatedNode_exports = {};
__export(AnimatedNode_exports, {
AnimatedNode: () => AnimatedNode,
default: () => AnimatedNode_default
});
module.exports = __toCommonJS(AnimatedNode_exports);
var import_NativeAnimatedHelper = require("../NativeAnimatedHelper.cjs");
var import_react_native_web_internals = require("@tamagui/react-native-web-internals");
const NativeAnimatedAPI = import_NativeAnimatedHelper.NativeAnimatedHelper.API;
let _uniqueId = 1;
class AnimatedNode {
_listeners;
_platformConfig;
__nativeAnimatedValueListener;
__attach() {}
__detach() {
if (this.__isNative && this.__nativeTag != null) {
import_NativeAnimatedHelper.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;
if (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++);
this._listeners[id] = callback;
if (this.__isNative) {
this._startListeningToNativeValueUpdates();
}
return 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];
if (this.__isNative && !this.hasListeners()) {
this._stopListeningForNativeValueUpdates();
}
}
/**
* Remove all registered listeners.
*
* See https://reactnative.dev/docs/animatedvalue#removealllisteners
*/
removeAllListeners() {
this._listeners = {};
if (this.__isNative) {
this._stopListeningForNativeValueUpdates();
}
}
hasListeners() {
return !!Object.keys(this._listeners).length;
}
_startListeningToNativeValueUpdates() {
if (this.__nativeAnimatedValueListener && !this.__shouldUpdateListenersForNewNativeTag) {
return;
}
if (this.__shouldUpdateListenersForNewNativeTag) {
this.__shouldUpdateListenersForNewNativeTag = false;
this._stopListeningForNativeValueUpdates();
}
NativeAnimatedAPI.startListeningToAnimatedNodeValue(this.__getNativeTag());
this.__nativeAnimatedValueListener = import_NativeAnimatedHelper.NativeAnimatedHelper.nativeEventEmitter.addListener("onAnimatedValueUpdate", data => {
if (data.tag !== this.__getNativeTag()) {
return;
}
this.__onAnimatedValueUpdateReceived(data.value);
});
}
__onAnimatedValueUpdateReceived(value) {
this.__callListeners(value);
}
__callListeners(value) {
for (const key in this._listeners) {
this._listeners[key]({
value
});
}
}
_stopListeningForNativeValueUpdates() {
if (!this.__nativeAnimatedValueListener) {
return;
}
this.__nativeAnimatedValueListener.remove();
this.__nativeAnimatedValueListener = null;
NativeAnimatedAPI.stopListeningToAnimatedNodeValue(this.__getNativeTag());
}
__getNativeTag() {
import_NativeAnimatedHelper.NativeAnimatedHelper.assertNativeAnimatedModule();
(0, import_react_native_web_internals.invariant)(this.__isNative, 'Attempt to get native tag from node not marked as "native"');
const nativeTag = this.__nativeTag ?? import_NativeAnimatedHelper.NativeAnimatedHelper.generateNewNodeTag();
if (this.__nativeTag == null) {
this.__nativeTag = nativeTag;
const config = this.__getNativeConfig();
if (this._platformConfig) {
config.platformConfig = this._platformConfig;
}
import_NativeAnimatedHelper.NativeAnimatedHelper.API.createAnimatedNode(nativeTag, config);
this.__shouldUpdateListenersForNewNativeTag = true;
}
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;