import { NestedObjectValues } from '../commonTypes'; import JSReanimated from './JSReanimated'; import MutableValue from './MutableValue'; export default class Mapper { static MAPPER_ID = 1; id: number; inputs: MutableValue[]; outputs: MutableValue[]; mapper: () => void; dirty = true; constructor( module: JSReanimated, mapper: () => void, inputs: NestedObjectValues>[] = [], outputs: NestedObjectValues>[] = [] ) { this.id = Mapper.MAPPER_ID++; this.inputs = this.extractMutablesFromArray(inputs); this.outputs = this.extractMutablesFromArray(outputs); this.mapper = mapper; const markDirty = () => { this.dirty = true; module.maybeRequestRender(); }; this.inputs.forEach((input) => { input.addListener(markDirty); }); } execute(): void { this.dirty = false; this.mapper(); } extractMutablesFromArray( array: NestedObjectValues> ): MutableValue[] { const res: MutableValue[] = []; function extractMutables(value: NestedObjectValues>) { if (value instanceof MutableValue) { res.push(value); } else if (Array.isArray(value)) { value.forEach((v) => extractMutables(v)); } else if (typeof value === 'object') { Object.keys(value).forEach((key) => { extractMutables(value[key]); }); } } extractMutables(array); return res; } }