littledan / proposal-proxy-transparent

Transparent Proxies--tunneling private fields, etc.
8 stars 0 forks source link

Ensure transparent unwrapping is recursive #2

Open Jamesernator opened 6 years ago

Jamesernator commented 6 years ago

Given the main principle of Proxy.transparent is that it's safe to wrap an object and re-use it in an upper majority of contexts I think it would make sense anywhere it's used in the spec for it be recursive as multiple users of the object may want to transparently observe the object for different reasons.

Using the case of private fields as an example this shouldn't fail because it's wrapped twice not just once:

import dataBinding from 'data-binding-framework'
import stateChangeLogger from 'state-change-logger'

class Counter {
  #count = 0
  increment() {
    this.#count += 1
  }

  get count() {
    return #count
  }
}

// Where stateChangeLogger returns a transparent proxy for counter
const counter = stateChangeLogger(new Counter(), ['count'])

// when dataBinding uses .count it should just work as it shouldn't
// care that there's another transparent proxy in between added by
// the state change logger
dataBinding(counter, someElement)

NOTE: This has nothing to do with whether or not Proxy.transparent.unwrap should be shallow or not, it's probably better that Proxy.transparent.unwrap remains shallow as recursive unwrapping might not cover all use cases.