question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Maybe we can use delegate-proxy instead of delegates?

See original GitHub issue

delegate-proxy

Super easy delegating method and accessor, using es6 Proxies with less than 15 lines of code

Source code

module.exports = function delegateProxy (target, origin) {
  return new Proxy(target, {
    get (target, key, receiver) {
      if (Reflect.has(target, key)) return Reflect.get(target, key, receiver)
      const value = Reflect.get(origin, key)
      // Must bound origin
      if ('function' === typeof value) return value.bind(origin)
      return value
    },
    set (target, key, value, receiver) {
      return Reflect.has(target, key) ? Reflect.set(target, key, value, receiver) : Reflect.set(origin, key, value)
    }
  })
}

Example

const delegateProxy = require('delegate-proxy')

const bar = {
  n: 1,

  add (i) {
    this.n += i
  }
}

const foo = {

  set (n) {
    this.n = n | 0
  },

  sub (i) {
    this.n -= i
  }

}

const d = delegateProxy(foo, bar)

bar       // { n: 1, end: [Function: end] }
foo       // { set: [Function: set], sub: [Function: sub] }
d         // {}

d.n       // => 1
d.add(1)
d.n       // => 2

d.sub(2)
d.n       // => 0

d.set(1)
d.n       // 1

d.n = 233
d.n       // 233

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
tjcommented, Aug 3, 2016

I still like explicit personally, we have for example this.body= == response.body, but with proxies this would not be clear, it could easily be req.body. I think clarity > a few extra LOC.

0reactions
fundoncommented, Aug 5, 2016

https://github.com/fundon/delegate-proxy/blob/master/benchmarks/index.js https://travis-ci.org/fundon/delegate-proxy/builds/149948801

CI results:

delegates#getter x 2,414,206 ops/sec ±0.81% (83 runs sampled)
delegateProxy#getter x 1,383,270 ops/sec ±0.94% (84 runs sampled)
Fastest is delegates#getter
delegates#method x 4,707,516 ops/sec ±6.25% (53 runs sampled)
delegateProxy#method x 1,339,755 ops/sec ±6.93% (65 runs sampled)
Fastest is delegates#method
delegates#access x 2,427,859 ops/sec ±8.88% (67 runs sampled)
delegateProxy#access x 1,344,403 ops/sec ±10.72% (47 runs sampled)
Fastest is delegates#access
Reflect#get x 7,481,142 ops/sec ±2.35% (82 runs sampled)
obj#key x 35,676,972 ops/sec ±0.73% (83 runs sampled)
obj.key x 35,687,692 ops/sec ±0.65% (82 runs sampled)
Fastest is obj.key,obj#key
Reflect#has x 8,369,451 ops/sec ±4.04% (77 runs sampled)
key in d0 x 10,618,353 ops/sec ±7.39% (51 runs sampled)
Fastest is key in d0
Reflect#set x 6,356,287 ops/sec ±7.22% (56 runs sampled)
obj#key= x 32,970,782 ops/sec ±1.74% (82 runs sampled)
Fastest is obj#key=
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to make a `DelegateProxy` for a pure swift delegate ...
But in my case some delegate type isn't conform to NSObjectProtocol , it's just a pure swift protocol,how can I use it as...
Read more >
Implementing `DelegateProxy` (RxSwift/RxCocoa) when ...
So my hypothesis is that it has something to do with the fact that object in DelegateProxyFactory.createProxy is a PKPhysicsWorld instead of ...
Read more >
Convert a Swift Delegate to RxSwift Observables
These sorts of methods must be implemented in the delegate proxy and need to be converted into an Observer instead of an Observable....
Read more >
Dynamic Delegate Proxy contract - solidity
I 'm thinking one way to do this would be to pass an additional parameter that tells the proxy contract which logic contract...
Read more >
Proxy Delegates - CodeProject
Use reflection and proxy delegates to interface between two separate assemblies.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found