Created
April 21, 2021 05:04
-
-
Save jupegarnica/d90b15cde7d7d987165f918d80549580 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
type AnyObject = { [key: string ]: any }; | |
const makeHandler = (path: Keys) => { | |
return { | |
set(target: AnyObject, key: string, value: any, receiver: AnyObject) { | |
throw new Error("Inmutable data"); | |
// if (typeof value === "object") { | |
// value = deepProxy(value, [...path, key]); | |
// } | |
// target[key] = value; | |
// // if (dp._handler.set) { | |
// // dp._handler.set(target, [...path, key], value, receiver); | |
// // } | |
// return true; | |
}, | |
deleteProperty(target: AnyObject, key: string) { | |
throw new Error("Inmutable data"); | |
// if (Reflect.has(target, key)) { | |
// let deleted = Reflect.deleteProperty(target, key); | |
// return deleted; | |
// } | |
// return false; | |
}, | |
}; | |
}; | |
// ( { | |
// get(target: AnyObject, key: string, receiver: AnyObject) { | |
// // const _target = Reflect.get(target, dataToSet) ?? target; | |
// // console.log(Reflect.get(target, dataToSet)); | |
// console.log("get", key); | |
// return target[key]; | |
// }, | |
// deleteProperty(target: AnyObject, key: string | symbol) { | |
// console.log("del", key); | |
// if (key === allowSetOnProxy) { | |
// delete target[key]; | |
// return true; | |
// } | |
// throw new Error("Inmutable data delete"); | |
// }, | |
// set(target: AnyObject, key: string, value: any, receiver: AnyObject) { | |
// const allowSet = Reflect.get(target, allowSetOnProxy); | |
// // console.log(key, allowSet); | |
// console.log("set", key); | |
// if (allowSet) { | |
// Reflect.set(target, key, value); | |
// return true; | |
// } | |
// throw new Error("Inmutable data"); | |
// // return false; | |
// }) | |
export const deepProxy = (obj: Value, path: Keys = []) => { | |
if (!isObject(obj)) return obj; | |
for (const key in obj) { | |
Reflect.set(obj, key, deepProxy(obj[key], [...path, key])); | |
} | |
// const root = { root: obj }; | |
const proxy = (new Proxy(obj, makeHandler(path))) as Value; | |
return proxy; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment