I am using p-memoize for caching promise response.
I am getting this error when Hermes enabled, It goes away when I turn off Hermes.
After a little searching, I found that maybe it's because of Hermes engine doesn't support Reflect. I tried using this library too reflect.ownkeys since p-memoize uses this method. It didn't work.
Any way to circumvent this problem without going off Hermes?
Can you elaborate about how reflect.ownkeys didn't work? It does not require the presence of Reflect.
It does, however, use typeof to check if the global variable exists: https://github.com/es-shims/Reflect.ownKeys/blob/master/polyfill.js#L6 but that should never, in any version of any engine, throw an error.
Reflect.ownKeys() is used in the internal implementation of p-memoize. So I thought after adding an external package p-memoize might work. I was just trying to make it work, without knowing what's happening inside.
@Kailash23 if you require('reflect.ownkeys/auto') before requiring p-memoize it absolutely will work.
@ljharb I am facing the same problem.
import {readFile, DocumentDirectoryPath} from 'react-native-fs';
require('reflect.ownkeys/auto');
import pMemoize from 'p-memoize';
const basePath = DocumentDirectoryPath + '/WordPowerDB/WordMap';
const generatePath = partialWord => {
return `${basePath}/${partialWord[0]}/${partialWord}-map.json`;
};
export const memGetSuggestions = pMemoize(getSuggestions, {maxAge: 20000});
async function getSuggestions(partialWord) {
return new Promise(async resolve => {
let path = generatePath(partialWord);
try {
console.log('heavy work!');
let output = await readFile(path);
resolve(JSON.parse(output));
} catch (e) {
console.log('Fetching suggestions : ', e);
resolve([]);
}
});
}
I can't comment on what's happening with p-memoize, or why it's failing. @ljharb thanks for the help there. If you figure out Hermes is doing something wrong, please open a new task so we can work on it. But I agree, the polyfill seems like it should work.
That said, you could also enable the experimental Proxy/Reflect support. Note that this will only work with the exact versions in that comment. There seems to be at least one unidentified bug in Proxy which I haven't been able to track down yet, so we haven't yet enabled it by default, but that shouldn't affect Reflect.ownKeys, which is very simple.
@Kailash23 if you're using import, you have to import 'reflect.ownkeys/auto' FIRST, before any other imports or code. require and import can't be mixed.
That polyfill/shim will continue to do the right thing with Hermes' experimental support added, and when it ships stable.
@ljharb Still getting the same reference error.
import 'reflect.ownkeys/auto';
import {readFile, DocumentDirectoryPath} from 'react-native-fs';
import pMemoize from 'p-memoize';
const generatePath = partialWord => {
const basePath = DocumentDirectoryPath + '/WordPowerDB/WordMap';
return `${basePath}/${partialWord[0]}/${partialWord}-map.json`;
};
export const memGetSuggestions = pMemoize(getSuggestions, {maxAge: 20000});
function getSuggestions(partialWord) {
return new Promise(async resolve => {
let path = generatePath(partialWord);
try {
let output = await readFile(path);
console.log('<Heavy call>');
resolve(JSON.parse(output));
} catch (e) {
console.log('Fetching suggestions : ', e);
resolve([]);
}
});
}
I also tried importing at index.js top.
@Kailash23 feel free to find me on freenode, or some popular slacks, under this username, and i'll be happy to help you further. Failing that, please file an issue on reflect.ownkeys and we can discuss it there :-)
I had filed this issue at reflect.ownkeys. Thanks a lot : ) @ljharb @mhorowitz
Most helpful comment
@Kailash23 if you're using
import, you have toimport 'reflect.ownkeys/auto'FIRST, before any other imports or code.requireandimportcan't be mixed.That polyfill/shim will continue to do the right thing with Hermes' experimental support added, and when it ships stable.