0.0.1 • Published 8 years ago

cyrano v0.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

Cyrano

メソッドチェイン式のイベント管理クラス

Installation

npm install cyrano --save

Usage

ゲームのシナリオをプログラムで表現するのは困難です。 このライブラリは、非同期処理を柔軟に解決できるPromiseをベースに、メソッドチェインでロジックを表現することを目的としています。

var EventManager= require('cyrano')
var eventManager= new EventManager()
eventManager
  .fn(function(){
    console.log('1')
  })
  .fn(function(){
    console.log('2')
  })
  .fn(function(){
    console.log('3')
  })

上記と下記はほとんど同じ結果です。

Promise.resolve()
  .then(function(){
    console.log('1')
  })
  .then(function(){
    console.log('2')
  })
  .then(function(){
    console.log('3')
  })

API

プログラムのコメントも日本語で表記しているので、詳細は直接参照してください

class EventManager(properties) -> eventManager

インスタンスは待ち行列(キュー)を一つだけ持ちます。 コンストラクタに第一引数としてオブジェクトを渡すと、インスタンスのプロパティとして定義します。

.getProperties -> properties

定義したプロパティを返します。

.delay(msec=1000) -> this

次のイベントの実行をミリ秒遅らせます。

.fn(callback) -> this

子キューを作成し、イベント群を定義します。

.loop(callback) -> this

子キューを作成し、イベント群を実行し続けます。

.loopEnd -> this

実行し続けているイベント群を中断します(.loopを抜けます)。

Example

下記の例では、「helloworld」を1文字ずつ印字し、最後に少し待ってから「done」を出力します。

var EventManager= require('cyrano')

var message= 'helloworld'
var eventManager= new EventManager()

eventManager
  .loop(function(i){
    if(i===undefined){
      i= 0
    }
    if(message[i]===undefined){
      return this.loopEnd()
    }
    
    return this
      .delay(1000 - i*100)
      .fn(function(){
        console.log(message[i++])
        return i
      })
  })
  .delay(500)
  .then(function(){
    console.log('done')
  })
node example.js
# h
# e
# l
# l
# o
# w
# o
# r
# l
# d
# done

その他、test/index.js内にコード例が幾つかあります。参考にしてください。

Test

git clone https://github.com/59naga/cyrano.git
cd cyrano

npm install
npm test
# or ...
DEBUG=cyrano npm test

License

MIT

0.0.1

8 years ago

0.0.0

8 years ago

0.0.0-beta.1

8 years ago

0.0.0-beta.0

8 years ago

0.0.0-beta

8 years ago

0.0.0-alpha

8 years ago