Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup react-native-reanimated.d.ts and mock.js #4160

Merged
merged 3 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
212 changes: 1 addition & 211 deletions mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,89 +8,13 @@
* ```
*/

const React = require('react');
const {
View,
Text,
Image,
Animated,
Platform,
processColor,
} = require('react-native');
const { View, Text, Image, Animated, processColor } = require('react-native');
const ReanimatedV2 = require('./src/reanimated2/mock');

function NOOP() {
// noop
}

function simulateCallbackFactory(...params) {
return (callback) => {
callback &&
setTimeout(() => {
// user defined callback
// eslint-disable-next-line node/no-callback-literal
callback(...params);
}, 0);
};
}

class Code extends React.Component {
render() {
return null;
}
}

const getValue = (node) => {
if (typeof node === 'number') {
return node;
}
return (node && node[' __value']) || 0;
};

class AnimatedValue {
constructor(val) {
this[' __value'] = val;
}

setValue(val) {
this[' __value'] = val;
}

interpolate() {
return this;
}
}

function createMockComponent(name) {
return class extends React.Component {
static displayName = name;

render() {
return this.props.children;
}
};
}

function createTransitioningComponent(Component) {
return class extends React.Component {
static displayName = `Transitioning.${
Component.displayName || Component.name || 'Component'
}`;

setNativeProps() {
// noop
}

animateNextTransition() {
// noop
}

render() {
return React.createElement(Component, this.props);
}
};
}

const Reanimated = {
SpringUtils: {
makeDefaultConfig: NOOP,
Expand All @@ -103,29 +27,6 @@ const Reanimated = {
Image,
ScrollView: Animated.ScrollView,
FlatList: Animated.FlatList,
Code,

Clock: NOOP,
Node: NOOP,
Value: AnimatedValue,

EasingNode: {
linear: NOOP,
ease: NOOP,
quad: NOOP,
cubic: NOOP,
poly: () => NOOP,
sin: NOOP,
circle: NOOP,
exp: NOOP,
elastic: () => NOOP,
back: () => NOOP,
bounce: () => NOOP,
bezier: () => NOOP,
in: () => NOOP,
out: () => NOOP,
inOut: () => NOOP,
},

Extrapolate: {
EXTEND: 'extend',
Expand All @@ -135,105 +36,8 @@ const Reanimated = {

processColor,

add: (...vals) =>
new AnimatedValue(vals.map((v) => getValue(v)).reduce((acc, v) => acc + v)),
sub: (...vals) =>
new AnimatedValue(vals.map((v) => getValue(v)).reduce((acc, v) => acc - v)),
divide: (...vals) =>
new AnimatedValue(vals.map((v) => getValue(v)).reduce((acc, v) => acc / v)),
multiply: (...vals) =>
new AnimatedValue(vals.map((v) => getValue(v)).reduce((acc, v) => acc * v)),
pow: (...vals) =>
new AnimatedValue(
vals.map((v) => getValue(v)).reduce((acc, v) => acc ** v)
),
modulo: (a, b) => new AnimatedValue(getValue(a) % getValue(b)),
sqrt: (a) => new AnimatedValue(Math.sqrt(getValue(a))),
log: (a) => new AnimatedValue(Math.log(getValue(a))),
sin: (a) => new AnimatedValue(Math.sin(getValue(a))),
cos: (a) => new AnimatedValue(Math.cos(getValue(a))),
tan: (a) => new AnimatedValue(Math.tan(getValue(a))),
acos: (a) => new AnimatedValue(Math.acos(getValue(a))),
asin: (a) => new AnimatedValue(Math.asin(getValue(a))),
atan: (a) => new AnimatedValue(Math.atan(getValue(a))),
exp: (a) => new AnimatedValue(Math.exp(getValue(a))),
round: (a) => new AnimatedValue(Math.round(getValue(a))),
floor: (a) => new AnimatedValue(Math.floor(getValue(a))),
ceil: (a) => new AnimatedValue(Math.ceil(getValue(a))),
lessThan: (a, b) => new AnimatedValue(getValue(a) < getValue(b)),
eq: (a, b) => new AnimatedValue(getValue(a) === getValue(b)),
greaterThan: (a, b) => new AnimatedValue(getValue(a) > getValue(b)),
lessOrEq: (a, b) => new AnimatedValue(getValue(a) <= getValue(b)),
greaterOrEq: (a, b) => new AnimatedValue(getValue(a) >= getValue(b)),
neq: (a, b) => new AnimatedValue(getValue(a) !== getValue(b)),
and: (a, b) => new AnimatedValue(getValue(a) && getValue(b)),
or: (a, b) => new AnimatedValue(getValue(a) || getValue(b)),
defined: (a) =>
new AnimatedValue(getValue(a) !== null && getValue(a) !== undefined),
not: (a) => new AnimatedValue(!getValue(a)),
set: (a, b) => {
a.setValue(getValue(b));
return a;
},
concat: (a, b) => `${a}${b}`,
cond: (a, b, c) => {
if (getValue(a)) {
return b;
} else {
return c;
}
},
block: (a) => a[a.length - 1],
call: (a, b) => b(a.map(getValue)),
debug: NOOP,
onChange: NOOP,
startClock: NOOP,
stopClock: NOOP,
clockRunning: NOOP,
event: NOOP,
abs: (a) => Math.abs(getValue(a)),
acc: NOOP,
color: (r, g, b, a = 1) => {
const color =
16777216 * Math.round(getValue(a) * 255) +
65536 * getValue(r) +
256 * getValue(g) +
getValue(b);
if (Platform.OS === 'android') {
// on Android color is represented as signed 32 bit int
if (color < (1 << 31) >>> 0) {
return new AnimatedValue(color);
}
return new AnimatedValue(color - 2 ** 32);
}
return new AnimatedValue(color);
},
diff: NOOP,
diffClamp: NOOP,
interpolate: NOOP,
interpolateNode: NOOP,
interpolateColor: NOOP,
interpolateColors: NOOP,
max: (a, b) => Math.max(getValue(a), getValue(b)),
min: (a, b) => Math.min(getValue(a), getValue(b)),

decay: () => ({
start: simulateCallbackFactory({ finished: true }),
stop: simulateCallbackFactory({ finished: true }),
}),
timing: () => ({
start: simulateCallbackFactory({ finished: true }),
stop: simulateCallbackFactory({ finished: true }),
}),
spring: () => ({
start: simulateCallbackFactory({ finished: true }),
stop: simulateCallbackFactory({ finished: true }),
}),

proc: (cb) => cb,

useCode: NOOP,
useValue: (a) => new AnimatedValue(a),
createAnimatedComponent: (Component) => Component,
addWhitelistedUIProps: NOOP,
addWhitelistedNativeProps: NOOP,
Expand All @@ -248,18 +52,4 @@ module.exports = {
default: {
...Reanimated,
},

Transitioning: {
View: createTransitioningComponent(View),
},

Transition: {
Sequence: createMockComponent('Transition.Sequence'),
Together: createMockComponent('Transition.Together'),
In: createMockComponent('Transition.In'),
Out: createMockComponent('Transition.Out'),
Change: createMockComponent('Transition.Change'),
},

createTransitioningComponent,
};
Loading