Skip to content
This repository has been archived by the owner on Apr 28, 2021. It is now read-only.

methodの間接利用の方式を見直す #5

Open
okunishinishi opened this issue Jul 22, 2016 · 4 comments
Open

methodの間接利用の方式を見直す #5

okunishinishi opened this issue Jul 22, 2016 · 4 comments

Comments

@okunishinishi
Copy link
Contributor

okunishinishi commented Jul 22, 2016

例えば

a = {
  x () { /* ... */ },
  y () { this.x() }
}
b = {
  z () { /*  a.y()  */}
}

という風にb.z() -> a.y() -> a.x() と間接的に呼び出すときにどうすればよいか。方式を考える必要がある

@okunishinishi
Copy link
Contributor Author

okunishinishi commented Jul 22, 2016

やっぱりモジュールベースが直感的

class Module {
   emit () { throw new Error('Not connect to sugo actor!. You need setup .... ') }
}

class Bomb extends Module {
   boom() { 
    console.log('boom!') 
    this.emit('fire!!')
   }
}

class TimeBomb extends Bomb {
   constructor () {
  }

   countDown () {
       super.boom()
   }
  boom () {
      thrown new Error('Not call me!')
  } 
}


const sugoActor('example.com', {
  moudles: {
    mybomb01: new TimeBomb()
 } 

})
let caller = sugoCaller('example.com')
let mybomb01 = caller.get('mybomb01')
mybomb01.on('fire', () => { /* ... */ })
yield mybomb01.countDown()

@okunishinishi
Copy link
Contributor Author

okunishinishi commented Jul 22, 2016

外向けに使わせるメソッドを制限したい場合はプラグインで頑張る

function limitPlugin(instance, methodsToAllow ) {
   for( let method of methodsToAllow ) {
     instance[method] = someWrap(instance[method])
   }
   return instance
}

modules: {
    b: limitPlugin(new TimeBomb(), ['countDown']
}

@okunishinishi
Copy link
Contributor Author

okunishinishi commented Jul 22, 2016

多重継承的な?

class Module {

}

const HiPeople = (superclass = Module) => class extends superclass {
  sayHi () { console.log('Hi!') }
}

const YoPeople = (superclass = Module) => class extends superclass {
  sayYo () { console.log('Yo!') }
}

class MyPerson extends HiPeople(YoPeople(Module)) {
  sayHiAndYo () {
    const s = this
    s.sayHi()
    s.sayYo()
  }
}

@okunishinishi
Copy link
Contributor Author

継承以外にも他のモジュールを間接呼び出しできるようにしたい。emiterを渡せば良いか?

class A extends Module {
   foo() { this.emit('foo') }
}

class B extends Module {
   constructor () {
      this.a = new A({ $emitter: this }) // Pipe emitter
   }
   foo () { this.a.foo() }
}



Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant