Maybe we can use delegate-proxy instead of delegates?
See original GitHub issuedelegate-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:
- Created 7 years ago
- Comments:9 (9 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I still like explicit personally, we have for example
this.body=
== response.body, but with proxies this would not be clear, it could easily bereq.body
. I think clarity > a few extra LOC.https://github.com/fundon/delegate-proxy/blob/master/benchmarks/index.js https://travis-ci.org/fundon/delegate-proxy/builds/149948801
CI results: