realglobe-Inc / sugos

A high-level RPC framework to make remote controlling super easy.
Apache License 2.0
9 stars 0 forks source link

actorからcallerを意識できるようにする #18

Open okunishinishi opened 7 years ago

okunishinishi commented 7 years ago

// Caller side
{
  const shoppingMall = await caller.connect('shoppingMall', {
    // Custom data to pass actor
    data: {
      account: 'hec会員01',
      password: 'hogehoge'
    }
  })

  const fruitShop = shoppingMall.get('fruitShop')

  fruitShop.buyApple(100)

  fruitShop.on('New apple arrived', () => {
    /* ... */
  })

}

// On Hub

// Actor side
{

  let fruitShop = new Module({

    buyApple (amount) {
      /* ... */
      return 1
    }
  })
  let shoppingMall = sugoActor({
    kety: 'shoppingMall',
    modules: {
      fruitShop
    }
  })

  // Namespace to hold callers
  fruitShop.fruteCustomers = {}

  // Called on caller connect
  shoppingMall.on('sugos:caller:conntect', (caller, data) => {
    /* ... */
  })

  // Called on caller connect
  fruitShop.on('sugos:caller:conntect', (caller, data) => {
    let { account, password } = data
    if (!isValud({ account, password })) {
      throw new Error('invalid!')
    }
    // Hold custom data
    fruitShop.fruteCustomers[ caller.id ] = {
      account,
      visitedAt: new Date(9)
    }
  })

  db.on('something changed', () => {
    fruitShop.emit('New apple arrived', { amount }, {
      only: Object.keys(shoppingMall.fruteCustomers) // Fiter emit target callers
    })
  })
}