How does it work?
traps for each operation you want to intercept and define the behaviour. You can have traps for:
A trap is basically a function ¯_(ツ)_/¯.
It is optional, if you don’t define them the operation gets forwarded to the target aka
No-op forwarding proxy
What can I do with it?
Data structures are hard to get right from the first time so throughout the lifecycle of your application you might want to change the data structure like removing uncessary properties and or group few properties together and the list goes on an on..., I myself been through that a lot of times. Depends how your app is structured you might have a service as a data provider and multiple services that act like data consumers. Once the app grows a lot changing data structure may be scary and error prone or maybe you don't have the capacity to refactor all of your services. Proxies will make sure that your data consumers will always get what they are asking for until you have time to refactor them and once all your code is ready for the change you can pull out the Proxy object.
We start first with our data service, the one that provides data to other services, we'll name it
ScheherazadeDB that exposes users data in a "super secure way". As first iteration the user data structure will look like this.
Now in our app we have
ScheherazadeDB consumers especially
Shahryar, and to make our situation even harder this will run every night for the next one thousand and one nights until dawn consuming all the data and if the data provider,
ScheherazadeDB, fail to deliver the expected data the consumer will decide that the app does not worth it anymore and will kill the app and all its processes.
The first few nights went really good until someone in the team decided that the user data structure is too flat and we should do some grouping, like having the firstName, lastName and age in side of
personalInfos group. How can we make the change without breaking running consumers?
The problem here is that none of the consumers knows about the new structure and they will try to get or set properties that does not exist anymore like firstName and our app will die, let's fix this before our evil consumer start running again.
user.firstName we can return
the only thing we need to do now is to change our
cheherazadeDB service to give back the proxyUser instead of the original user object.
Great, now that our services are running fine we need to warn the developer that some fields are deprecated and they should use the new properties. Let's change our
proxyUser function to reflect that.
Now whenever a service tries to use the data will receive that warning for example
we'll get this output in the console.
And so the service consumer kept
ScheherazadeDBalive day by day, as he eagerly anticipated the finishing of the previous night's story. At the end of 1,001 nights, and 1,000 stories, Scheherazade told the king that she had no more tales to tell him. During these 1,001 nights, the king had fallen in love with Scheherazade. He spared her life, and made her his queen.
Can I use it now?
Yes since most of the major browsers supports it already.
What else can we do?