RLottie: Fix synchronous bundling (#3071)

This commit is contained in:
Alexander Zinchuk 2023-04-25 17:24:51 +04:00
parent e10f91e4f0
commit 7e53d0da57
3 changed files with 38 additions and 30 deletions

View File

@ -1,6 +1,8 @@
import type { RefObject } from 'react'; import type { RefObject } from 'react';
import type { FC } from '../../lib/teact/teact'; import type { FC } from '../../lib/teact/teact';
import type RLottieInstance from '../../lib/rlottie/RLottie';
import { requestMeasure } from '../../lib/fasterdom/fasterdom'; import { requestMeasure } from '../../lib/fasterdom/fasterdom';
import { ensureRLottie, getRLottie } from '../../lib/rlottie/RLottie.async';
import React, { import React, {
useEffect, useRef, memo, useCallback, useState, useMemo, useEffect, useRef, memo, useCallback, useState, useMemo,
@ -41,27 +43,9 @@ export type OwnProps = {
onLoop?: NoneToVoidFunction; onLoop?: NoneToVoidFunction;
}; };
type RLottieClass = typeof import('../../lib/rlottie/RLottie').default;
type RLottieInstance = import('../../lib/rlottie/RLottie').default;
let lottiePromise: Promise<RLottieClass>;
let RLottie: RLottieClass;
// Time for the main interface to completely load
const LOTTIE_LOAD_DELAY = 3000;
const THROTTLE_MS = 150; const THROTTLE_MS = 150;
const ID_STORE = {}; const ID_STORE = {};
async function ensureLottie() {
if (!lottiePromise) {
lottiePromise = import('../../lib/rlottie/RLottie') as unknown as Promise<RLottieClass>;
RLottie = (await lottiePromise as any).default;
}
return lottiePromise;
}
setTimeout(ensureLottie, LOTTIE_LOAD_DELAY);
const AnimatedSticker: FC<OwnProps> = ({ const AnimatedSticker: FC<OwnProps> = ({
ref, ref,
renderId, renderId,
@ -122,7 +106,7 @@ const AnimatedSticker: FC<OwnProps> = ({
return; return;
} }
const newAnimation = RLottie.init( const newAnimation = getRLottie().init(
tgsUrl, tgsUrl,
container, container,
renderId || generateIdFor(ID_STORE, true), renderId || generateIdFor(ID_STORE, true),
@ -152,10 +136,10 @@ const AnimatedSticker: FC<OwnProps> = ({
]); ]);
useEffect(() => { useEffect(() => {
if (RLottie) { if (getRLottie()) {
init(); init();
} else { } else {
ensureLottie().then(init); ensureRLottie().then(init);
} }
}, [init]); }, [init]);

View File

@ -1,7 +1,6 @@
import { import {
useCallback, useEffect, useRef, useCallback, useEffect, useRef,
} from '../../../../lib/teact/teact'; } from '../../../../lib/teact/teact';
import RLottie from '../../../../lib/rlottie/RLottie';
import { requestMeasure } from '../../../../lib/fasterdom/fasterdom'; import { requestMeasure } from '../../../../lib/fasterdom/fasterdom';
import type { ApiSticker } from '../../../../api/types'; import type { ApiSticker } from '../../../../api/types';
@ -23,6 +22,7 @@ import useBackgroundMode from '../../../../hooks/useBackgroundMode';
import useThrottledCallback from '../../../../hooks/useThrottledCallback'; import useThrottledCallback from '../../../../hooks/useThrottledCallback';
import useDynamicColorListener from '../../../../hooks/useDynamicColorListener'; import useDynamicColorListener from '../../../../hooks/useDynamicColorListener';
import useEffectWithPrevDeps from '../../../../hooks/useEffectWithPrevDeps'; import useEffectWithPrevDeps from '../../../../hooks/useEffectWithPrevDeps';
import { ensureRLottie } from '../../../../lib/rlottie/RLottie.async';
const SIZE = 1.25 * REM; const SIZE = 1.25 * REM;
const THROTTLE_MS = 300; const THROTTLE_MS = 300;
@ -97,7 +97,7 @@ export default function useInputCustomEmojis(
prefixId, documentId, textColor?.join(','), prefixId, documentId, textColor?.join(','),
].filter(Boolean).join('_'); ].filter(Boolean).join('_');
const animation = createPlayer({ createPlayer({
customEmoji, customEmoji,
sharedCanvasRef, sharedCanvasRef,
sharedCanvasHqRef, sharedCanvasHqRef,
@ -108,12 +108,13 @@ export default function useInputCustomEmojis(
isHq, isHq,
position: { x, y }, position: { x, y },
textColor, textColor,
}); }).then((animation) => {
if (canPlayAnimatedEmojis) { if (canPlayAnimatedEmojis) {
animation.play(); animation.play();
} }
playersById.current.set(playerId, animation); playersById.current.set(playerId, animation);
});
}); });
clearPlayers(Array.from(playerIdsToClear)); clearPlayers(Array.from(playerIdsToClear));
@ -182,7 +183,7 @@ export default function useInputCustomEmojis(
useBackgroundMode(freezeAnimation, unfreezeAnimationOnRaf); useBackgroundMode(freezeAnimation, unfreezeAnimationOnRaf);
} }
function createPlayer({ async function createPlayer({
customEmoji, customEmoji,
sharedCanvasRef, sharedCanvasRef,
sharedCanvasHqRef, sharedCanvasHqRef,
@ -204,8 +205,9 @@ function createPlayer({
position: { x: number; y: number }; position: { x: number; y: number };
isHq?: boolean; isHq?: boolean;
textColor?: [number, number, number]; textColor?: [number, number, number];
}): CustomEmojiPlayer { }): Promise<CustomEmojiPlayer> {
if (customEmoji.isLottie) { if (customEmoji.isLottie) {
const RLottie = await ensureRLottie();
const lottie = RLottie.init( const lottie = RLottie.init(
mediaUrl, mediaUrl,
isHq ? sharedCanvasHqRef.current! : sharedCanvasRef.current!, isHq ? sharedCanvasHqRef.current! : sharedCanvasRef.current!,

View File

@ -0,0 +1,22 @@
type RLottieClass = typeof import('./RLottie').default;
let promise: Promise<RLottieClass>;
let RLottie: RLottieClass;
// Time for the main interface to completely load
const LOTTIE_LOAD_DELAY = 3000;
export async function ensureRLottie() {
if (!promise) {
promise = import('./RLottie').then((module) => module.default);
RLottie = await promise;
}
return promise;
}
export function getRLottie() {
return RLottie;
}
setTimeout(ensureRLottie, LOTTIE_LOAD_DELAY);