UNPKG

@tamagui/react-native-web-lite

Version:
118 lines (117 loc) 6.07 kB
import { invariant } from "@tamagui/react-native-web-internals"; import NativeAnimatedHelper from "./NativeAnimatedHelper.native.js"; import { shouldUseNativeDriver } from "./NativeAnimatedHelper.native.js"; import AnimatedValue from "./nodes/AnimatedValue.native.js"; function _class_call_check(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0, "value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor); } } function _create_class(Constructor, protoProps, staticProps) { return protoProps && _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), Constructor; } var __DEV__ = process.env.NODE_ENV !== "production"; function attachNativeEvent(viewRef, eventName, argMapping) { var eventMappings = [], traverse = function (value, path) { if (value instanceof AnimatedValue) value.__makeNative(), eventMappings.push({ nativeEventPath: path, animatedValueTag: value.__getNativeTag() });else if (typeof value == "object") for (var _key in value) traverse(value[_key], path.concat(_key)); }; return invariant(argMapping[0] && argMapping[0].nativeEvent, "Native driven events only support animated values contained inside `nativeEvent`."), traverse(argMapping[0].nativeEvent, []), viewRef != null && eventMappings.forEach(function (mapping) { NativeAnimatedHelper.API.addAnimatedEventToView(viewRef, eventName, mapping); }), { detach() { viewRef != null && eventMappings.forEach(function (mapping) { NativeAnimatedHelper.API.removeAnimatedEventFromView(viewRef, eventName, // $FlowFixMe[incompatible-call] mapping.animatedValueTag); }); } }; } function validateMapping(argMapping, args) { var validate = function (recMapping, recEvt, key) { if (recMapping instanceof AnimatedValue) { invariant(typeof recEvt == "number", "Bad mapping of event key " + key + ", should be number but got " + typeof recEvt); return; } if (typeof recEvt == "number") { invariant(recMapping instanceof AnimatedValue, "Bad mapping of type " + typeof recMapping + " for key " + key + ", event value must map to AnimatedValue"); return; } invariant(typeof recMapping == "object", "Bad mapping of type " + typeof recMapping + " for key " + key), invariant(typeof recEvt == "object", "Bad event of type " + typeof recEvt + " for key " + key); for (var mappingKey in recMapping) validate(recMapping[mappingKey], recEvt[mappingKey], mappingKey); }; invariant(args.length >= argMapping.length, "Event has less arguments than mapping"), argMapping.forEach(function (mapping, idx) { validate(mapping, args[idx], "arg" + idx); }); } var AnimatedEvent = /* @__PURE__ */function () { function AnimatedEvent2(argMapping, config) { _class_call_check(this, AnimatedEvent2), this._listeners = [], this._argMapping = argMapping, config == null && (console.warn("Animated.event now requires a second argument for options"), config = { useNativeDriver: !1 }), config.listener && this.__addListener(config.listener), this._callListeners = this._callListeners.bind(this), this._attachedEvent = null, this.__isNative = shouldUseNativeDriver(config); } return _create_class(AnimatedEvent2, [{ key: "__addListener", value: function (callback) { this._listeners.push(callback); } }, { key: "__removeListener", value: function (callback) { this._listeners = this._listeners.filter(function (listener) { return listener !== callback; }); } }, { key: "__attach", value: function (viewRef, eventName) { invariant(this.__isNative, "Only native driven events need to be attached."), this._attachedEvent = attachNativeEvent(viewRef, eventName, this._argMapping); } }, { key: "__detach", value: function (viewTag, eventName) { invariant(this.__isNative, "Only native driven events need to be detached."), this._attachedEvent && this._attachedEvent.detach(); } }, { key: "__getHandler", value: function () { var _this = this; if (this.__isNative) if (__DEV__) { var _validatedMapping = !1; return function () { for (var _len = arguments.length, args = new Array(_len), _key2 = 0; _key2 < _len; _key2++) args[_key2] = arguments[_key2]; _validatedMapping || (validateMapping(_this._argMapping, args), _validatedMapping = !0), _this._callListeners(...args); }; } else return this._callListeners; var validatedMapping = !1; return function () { for (var _len2 = arguments.length, args = new Array(_len2), _key3 = 0; _key3 < _len2; _key3++) args[_key3] = arguments[_key3]; __DEV__ && !validatedMapping && (validateMapping(_this._argMapping, args), validatedMapping = !0); var traverse = function (recMapping, recEvt, key) { if (recMapping instanceof AnimatedValue) typeof recEvt == "number" && recMapping.setValue(recEvt);else if (typeof recMapping == "object") for (var mappingKey in recMapping) traverse(recMapping[mappingKey], recEvt[mappingKey], mappingKey); }; _this._argMapping.forEach(function (mapping, idx) { traverse(mapping, args[idx], "arg" + idx); }), _this._callListeners(...args); }; } }, { key: "_callListeners", value: function () { for (var _len3 = arguments.length, args = new Array(_len3), _key4 = 0; _key4 < _len3; _key4++) args[_key4] = arguments[_key4]; this._listeners.forEach(function (listener) { return listener(...args); }); } }]), AnimatedEvent2; }(); export { AnimatedEvent, attachNativeEvent }; //# sourceMappingURL=AnimatedEvent.native.js.map