AS3S.ORG

ACTIONSCRIPT 3.0 SOURCES

org.as3s.Queue

複数の非同期処理の完了を通知するQueueクラス

Download: http://as3s.org/uploads/queue/queue.zip

複数のファイルを読み込む場合など、複数の非同期処理の進行状況とすべてが完了したタイミングをイベントとして受け取ることができるQueueクラスを公開しました。

なるべく標準的なイベントモデルには変更を加えずに、複数イベントの完了を通知することができます。
仕組みはいたって単純で、各イベントを受け取るリスナーを Queue(待ち行列)に追加し、イベントが呼ばれたらQueueから削除しているだけです。
Queueが削除されたタイミングとQueueがなくなったタイミングでイベントが発生し、
queue.length / queue.totalで進行状況を知ることができます。

また、エラー発生時にもリスナーをQueueから削除するようにしておけば、ファイルが一部取得できなかった場合にもQueueの完了イベントを受け取ることができます。

以下は、3つのテキストファイルを読み込んで、3つすべてが読みまれたらすべてのファイルの内容をtraceするサンプルです。いずれかのファイルが取得できなかった場合も、Queue.COMPLETEイベントが発生します。

Sample Code

package {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.net.URLLoader;
    import flash.net.URLRequest;

    import org.as3s.Queue;

    public class QueueSample extends Sprite {

        private var queue:Queue;
        private var loaderList:Array;

        public function QueueSample() {

            loaderList = new Array();

            //Queueの作成
            queue = new Queue();
            queue.addEventListener(Queue.PROGRESS, onProgress);
            queue.addEventListener(Queue.COMPLETE, onComplete);

            var loader:URLLoader;
            for (var i:int=0; i<3; i++) {
                loader = new URLLoader();
                loaderList[i] = loader;
                try {
                    loader.addEventListener(Event.COMPLETE, onLoadComplete);
                    loader.addEventListener(IOErrorEvent.IO_ERROR, onLoadIOError);
                    queue.put(onLoadComplete); //Queueに追加
                    loader.load(new URLRequest("data/test"+i+".txt"));
                } catch (error:Error) {
                    trace("Unable to load requested document.");
                    queue.get(onLoadComplete); //Queueから削除
                }
            }

        }

        private function onLoadComplete(event:Event):void {
            trace("LoadComplete");
            queue.get(onLoadComplete); //Queueから削除
        }

        private function onLoadIOError(event:Event):void {
            trace("IOError");
            queue.get(onLoadComplete); //Queueから削除
        }

        //Queueの進行状況を通知
        private function onProgress(event:Event):void {
            trace("Progress: "+(queue.total-queue.length)+"/"+queue.total);
        }

        //Queueの完了を通知
        private function onComplete(event:Event):void {
            trace("Complete");
            for each (var loader:URLLoader in loaderList) {
                trace(loader.data);
            }
            queue.removeEventListener(Queue.COMPLETE, onComplete);
        }

    }
}

▼出力結果

LoadComplete
Progress: 1/3
LoadComplete
Progress: 2/3
LoadComplete
Progress: 3/3
Complete
This is Test0.
This is Test1.
This is Test2.

org.as3s.Queue

/**
 * Queue
 * Event Queueing Class
 *
 * Download: http://as3s.org/queue/queue.zip
 *
 * Copyright (c) 2007 Hisato Ogata
 * Licensed under the MIT License
 * http://www.opensource.org/licenses/mit-license.php
 *
 * @langversion ActionScript 3.0
 * @playerversion Flash 9
 *
 * @version 1.00
 * @author Hisato Ogata
 * @see http://as3s.org/queue/
 *
 * @example usage:
 * <listing version="3.0">
 *
 *  // Create Queue Instance
 *  queue = new Queue();
 *  queue = new Queue(listener1, listener2);
 *
 *  // Add listener to the queue;
 *  queue.put(listener);
 *  queue.put(listener1, listener2);
 *
 *  // Remove listener from the queue;
 *  queue.get(listener);
 *
 *  // You can receive Queue events
 *  queue.addEventListener(Queue.PROGRESS, onProgress);
 *  queue.addEventListener(Queue.COMPLETE, onComplete);
 *
 * </listing>
 */

package org.as3s {
    import flash.events.Event;
    import flash.events.EventDispatcher;

    public class Queue extends EventDispatcher {

        public static var PROGRESS:String = "progress";
        public static var COMPLETE:String = "complete";
        private var list:Array;

        /**
        * The total number of the queue.
        */

        private var _total:int;
        public function get total():int {
            return _total;
        }

        /**
        * The current length of the queue.
        */

        public function get length():int {
            return list.length;
        }

        /**
         * Constructor for Queue class
         */

        public function Queue(...args) {
            list = args.length>0 ? args : new Array();
            _total = list.length;
        }

        /**
         * Add items to the queue.
         */

        public function put(...args):void {
            list.push.apply(list, args);
            _total++;
        }

        /**
         * Remove items from the queue.
         */

        public function get(...args):void {
            var index:int;
            for each (var item:* in args) {
                index = list.indexOf(item);
                if (index>=0) {
                    list.splice(index, 1);
                    dispatchEvent(new Event(Queue.PROGRESS));
                    if (list.length==0) dispatchEvent(new Event(Queue.COMPLETE));
                }
            }
        }

    }
}

Written by admin

July 29th, 2008 at 4:57 pm

Posted in Uncategorized