react-native-wishlist
Version:
The fastest List component for React Native.
120 lines (115 loc) • 4.15 kB
JavaScript
import { createRunInWishlistFn } from './WishlistJsRuntime';
let done = false;
const maybeInit = () => {
if (!done) {
done = true;
createRunInWishlistFn(() => {
'worklet';
const registry = new Map();
const mappings = new Map();
const templateValueStates = new Map();
let currentValue;
let currentRootValue;
const InflatorRegistry = {
inflateItem: (id, index, pool) => {
const inflator = registry.get(id);
if (inflator) {
const result = inflator(index, pool);
if (!result) {
return result;
}
const [item, value] = result;
return getUIInflatorRegistry().useMappings(item, value, value.type, id, pool, value // rootValue
);
} else {
console.log('Inflator not found for id: ' + id);
return undefined;
}
},
useMappings: (item, value, templateType, id, pool, rootValue) => {
var _mappings$get;
// We need to save and restore current values to support things like ForEach
// where current value can change.
const previousValue = currentValue;
const previousRootValue = currentRootValue;
currentValue = value;
currentRootValue = rootValue;
const mapping = (_mappings$get = mappings.get(id)) === null || _mappings$get === void 0 ? void 0 : _mappings$get.get(templateType);
if (mapping) {
for (const [nativeId, inflate] of mapping.entries()) {
const templateItem = item.getByWishId(nativeId);
if (templateItem) {
inflate(value, templateItem, pool, rootValue);
}
}
}
currentValue = previousValue;
currentRootValue = previousRootValue;
return item;
},
registerInflator: (id, inflateMethod) => {
console.log('InflatorRegistry::register', id);
registry.set(id, inflateMethod);
},
unregisterInflator: id => {
console.log('InflatorRegistry::unregister', id); // TODO(Szymon) It should be done on UI Thread as it may be still in use
registry.delete(id);
mappings.delete(id);
},
registerMapping: (inflatorId, nativeId, templateType, inflateMethod) => {
console.log('InflatorRegistry::registerMapping', inflatorId, nativeId, templateType, inflateMethod);
const mapping = mappings.get(inflatorId) ?? new Map();
const innerMapping = mapping.get(templateType) ?? new Map();
innerMapping.set(nativeId, inflateMethod);
mapping.set(templateType, innerMapping);
mappings.set(inflatorId, mapping);
},
getTemplateValueState: id => {
return templateValueStates.get(id);
},
setTemplateValueState: (id, state) => {
templateValueStates.set(id, state);
},
deleteTemplateValueState: id => {
templateValueStates.delete(id);
},
getCurrentValue: () => {
return currentValue;
},
getCurrentRootValue: () => {
return currentRootValue;
}
};
global.InflatorRegistry = InflatorRegistry;
console.log('InflatorRegister initialized');
})();
}
};
export function getUIInflatorRegistry() {
'worklet';
return global.InflatorRegistry;
}
export default class InflatorRepository {
static register(id, inflateMethod) {
maybeInit();
createRunInWishlistFn(() => {
'worklet';
getUIInflatorRegistry().registerInflator(id, inflateMethod);
})();
}
static unregister(id) {
maybeInit();
createRunInWishlistFn(() => {
'worklet';
getUIInflatorRegistry().unregisterInflator(id);
})();
}
static registerMapping(inflatorId, nativeId, templateType, inflateMethod) {
maybeInit();
createRunInWishlistFn(() => {
'worklet';
getUIInflatorRegistry().registerMapping(inflatorId, nativeId, templateType, inflateMethod);
})();
}
}
//# sourceMappingURL=InflatorRepository.js.map