Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
elderjs/src/utils/createReadOnlyProxy.ts
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
18 lines (17 sloc)
602 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createReadOnlyProxy(obj: object, objName: string, location: string) { | |
// proxies only work on objects/arrays. | |
try { | |
if (typeof obj !== 'object' && !Array.isArray(obj)) return obj; | |
return new Proxy(obj, { | |
set() { | |
console.log( | |
`Object ${objName} is not mutable from ${location}. Check the error below for the hook/plugin that is attempting to mutate properties outside of the rules in hookInterface.ts or in other restricted areas.`, | |
); | |
return false; | |
}, | |
}); | |
} catch (e) { | |
return obj; | |
} | |
} | |
export default createReadOnlyProxy; |