phinajs / phina.js

phina.js is game library

Home Page:http://phinajs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

チェーンするだけのクラスとかどうですかね

omatoro opened this issue · comments

あったらすみません。
tmlib.jsやっててこんな感じの処理作ったんですけど、どうですかね。

        /**
         * @class tm.util.Chain
         * @extends tm.event.EventDispatcher
         */
        tm.define("tm.util.Chain", {
            superClass: "tm.event.EventDispatcher",

            /**
             * @constructor
             */
            init: function() {
                this.superInit();
                this.callbackList = [];
            },

            /**
             * 関数登録
             */
            call: function(callback) {
                this.callbackList.push(callback);
                return this;
            },

            /**
             * 次の関数実行
             */
            pass: function () {
                if (this.isFinish()) {
                    this.callbackList = [];
                    this.flare('finish');
                }
                this.callbackList.shift().apply(this, arguments);
                return this;
            },

            /**
             * 終了チェック
             */
            isFinish: function() {
                return (this.callbackList.length <= 0);
            },
        });

いいじゃんってなれば、phina.jsでpull request送りたいと思いまして。

これ使ったサンプルとか見てみたいです

commented

確かにサンプル見てみたいっすね.

callbackのネスト地獄になるのをやめたくて、tweenerみたいにcallを連続して作れるようにしたかったんですよね。
ただそれだけの機能です。
ゲーム作ってると、このアニメーションが終わったら次へ、みたいな処理がめちゃくちゃ増えちゃってて。
イメージとしてはES6のpromissみたいな。

var chain = tm.util.Chain()
    .call(function () {
        // 次のcallへ(引数も渡せる)
        this.pass(10);
    })
    .call(function (num) {
        var ch = this;
        console.log(num);
        // 何かのイベントを待ったりとか
        setTimeout(function () { ch.pass(); }, 100);
    });

phinaのFlowがそんな感じだった気がします

commented

@omatoro san @simiraaaa san
そうっすね, Flow がそんな感じです.
実は promise とほぼ同等の機能を自作しちゃってるのでそれ使えばぽいこと実現できます.

参考までに↓
http://goo.gl/edm4eF

なるほど!!!
flowパワーアップしてるんですね。

ありがとうございます!
ではこのissue閉じさせてもらいますね!