react-native-reanimated
Version:
More powerful alternative to Animated library for React Native.
107 lines (90 loc) • 3.04 kB
text/typescript
'use strict';
import type {
CallbackDetails,
FrameCallbackRegistryUI,
FrameInfo,
} from './commonTypes';
export function prepareUIRegistry() {
'worklet';
const frameCallbackRegistry: FrameCallbackRegistryUI = {
frameCallbackRegistry: new Map<number, CallbackDetails>(),
activeFrameCallbacks: new Set<number>(),
previousFrameTimestamp: null,
nextCallId: 0,
runCallbacks(callId) {
const loop = (timestamp: number) => {
if (callId !== this.nextCallId) {
return;
}
if (this.previousFrameTimestamp === null) {
this.previousFrameTimestamp = timestamp;
}
const delta = timestamp - this.previousFrameTimestamp;
this.activeFrameCallbacks.forEach((callbackId: number) => {
const callbackDetails = this.frameCallbackRegistry.get(callbackId)!;
const { startTime } = callbackDetails;
if (startTime === null) {
// First frame
callbackDetails.startTime = timestamp;
callbackDetails.callback({
timestamp,
timeSincePreviousFrame: null,
timeSinceFirstFrame: 0,
});
} else {
// Next frame
callbackDetails.callback({
timestamp,
timeSincePreviousFrame: delta,
timeSinceFirstFrame: timestamp - startTime,
});
}
});
if (this.activeFrameCallbacks.size > 0) {
this.previousFrameTimestamp = timestamp;
requestAnimationFrame(loop);
} else {
this.previousFrameTimestamp = null;
}
};
// runCallback() should only be called after registering a callback,
// so if there is only one active callback, then it means that there were
// zero previously and the loop isn't running yet.
if (this.activeFrameCallbacks.size === 1 && callId === this.nextCallId) {
requestAnimationFrame(loop);
}
},
registerFrameCallback(
callback: (frameInfo: FrameInfo) => void,
callbackId: number
) {
this.frameCallbackRegistry.set(callbackId, {
callback,
startTime: null,
});
},
unregisterFrameCallback(callbackId: number) {
this.manageStateFrameCallback(callbackId, false);
this.frameCallbackRegistry.delete(callbackId);
},
manageStateFrameCallback(callbackId: number, state: boolean) {
if (callbackId === -1) {
return;
}
if (state) {
if (!this.activeFrameCallbacks.has(callbackId)) {
this.activeFrameCallbacks.add(callbackId);
this.runCallbacks(this.nextCallId);
}
} else {
const callback = this.frameCallbackRegistry.get(callbackId)!;
callback.startTime = null;
this.activeFrameCallbacks.delete(callbackId);
if (this.activeFrameCallbacks.size === 0) {
this.nextCallId += 1;
}
}
},
};
global._frameCallbackRegistry = frameCallbackRegistry;
}