c4se記:さっちゃんですよ☆

.。oO(さっちゃんですよヾ(〃l _ l)ノ゙☆)

.。oO(此のblogは、主に音樂考察Programming に分類されますよ。ヾ(〃l _ l)ノ゙♬♪♡)

音樂は SoundCloud に公開中です。

考察は現在は主に Scrapbox で公表中です。

Programming は GitHub で開發中です。

AzureaUtil作りました #AzureaScript #js

誰が使うの? https://gist.github.com/841702 ねえ?
あ、うん、AzureaScriptを書く時の便利ライブラリです。イベント管理を簡単にしたり、タイマー機能を使えたりします。

AzureaUtil.mixin

副産物なんですが。

AzureaUtil.mixin(hash1, hash2, overwrite=true)
//hash1へ、hash2をmixinします。

AzureaUtil.event

後の、AzureaUtil.timeの為に作ったものなんですが。

AzureaUtil.event.addEventListener(eventname, fun)
AzureaUtil.event.removeEventListener(eventname, fun)

AzureaUtilを使用する時には、必ずaddEventListenerを使って下さい。直接定義すると、AzureaUtil自身とぶつかります(コードの最後ら辺見てわかって><。)。
removeEventListenerを使いたいなら、addEventListenerの中にはfunction定義を書かず、出来合いのFunctionオブジェクトを渡してやって下さいね。removeしない場合は不要です。

AzureaUtil.time

はい、きたー。

id = AzureaUtil.time.setTimeOut(fun, ms)
AzureaUtil.time.clearTimeOut(id)
id = AzureaUtil.time.setTimeInterval(fun, ms)
AzureaUtil.time.clearTimeInterval(id)

まあ洞所かで見た様な名前ですし、洞所かで見た様な動作をします。
AzureaUtil.eventに依存します。
大きな注意として、此のタイマーは、指定時間ではなく、指定時間以降に関数が実行される事を保証します。指定時間の一時間後と云う事もまんざら否定できません(夜明け前とか特にね)。
Azureaのセッションを跨げるsetTimeEvent, clearTimeEventなんてのも考えましたが、上手い仕組みが……。

code

現時点を貼っておきます。更新は https://gist.github.com/841702 へ。

// @author = http://c4se.sakura.ne.jp/profile/ne.html
// @date = 2011-02-24
// @site = https://gist.github.com/841702
// @license = public domain


var AzureaUtil = {};

AzureaUtil.event = {};
AzureaUtil.time = {};


(function() {

function mixin(hash1,       // @param Hash:
               hash2,       // @param Hash:
               overwrite) { // @param Boolean=true:
    var key;
    
    if (typeof overwrite === 'undefined') {
        overwrite = true;
    }
    for (key in hash2) {
        if (overwrite || typeof hash1[key] === 'undefined') {
            hash1[key] = hash2[key];
        }
    }
}


AzureaUtil.mixin = mixin;


var PreProcessTimelineStatuses = [],
    PreProcessTimelineStatus = [],
    PreFilterProcessTimelineStatus = [],
    PostFilterProcessTimelineStatus = [],
    PostProcessTimelineStatus = [],
    PostProcessTimelineStatuses = [],
    PreSendUpdateStatus = [],
    PostSendUpdateStatus = [],
    ReceiveFavorite = [];


function addEventListener(eventname, // @param String:
                          fun) {     // @param Function:
    var listener = eval(eventname),
        i = -1;
    
    while (listener[++i]) {
        if (listener[i] === fun) {
            listener.splice(i, 1);
        }
    }
    listener.push(fun);
}


function removeEventListener(eventname, // @param String:
                             fun) {     // @param Function:
    var listener = eval(eventname),
        i = -1;
    
    while (listener[++i]) {
        if (listener[i] === fun) {
            listener.splice(i, 1);
            break;
        }
    }
}


mixin(AzureaUtil.event, {
    'PreProcessTimelineStatuses': PreProcessTimelineStatuses,
    'PreProcessTimelineStatus': PreProcessTimelineStatus,
    'PreFilterProcessTimelineStatus': PreFilterProcessTimelineStatus,
    'PostFilterProcessTimelineStatus': PostFilterProcessTimelineStatus,
    'PostProcessTimelineStatus': PostProcessTimelineStatus,
    'PostProcessTimelineStatuses': PostProcessTimelineStatuses,
    'PreSendUpdateStatus': PreSendUpdateStatus,
    'PostSendUpdateStatus': PostSendUpdateStatus,
    'ReceiveFavorite': ReceiveFavorite,
    'addEventListener': addEventListener,
    'removeEventListener': removeEventListener
});


var timeout_list = {},// {id: [time, fun]}
    timeinterval_list = {};// {id: [time, fun, interval]}
//    timeevent_list = (function() {
//    var timeevent_list,
//        timeEvent;
//    
//    for () {
//        timeEvent = System.settings.getValue('user.AzureaUtil', 'TimeEvent' + i);
//        timeevent_list[]
//    }
//    return timeevent_list;
//})();


function attainSchedule() {
    var now = new Date().getTime(),
        id;
    
    for (id in timeout_list) {
        if (timeout_list[id][0] <= now) {
            timeout_list[id][1]();
            delete timeout_list[id];
        }
    }
    for (id in timeinterval_list) {
        if (timeinterval_list[id][0] <= now) {
            timeinterval_list[id][0] += timeinterval_list[id][2];
            timeinterval_list[id][1]();
        }
    }
    //for (id in timeevent_list) {
    //    if (timeevent_list[id][0] <= now) {
    //        timeevent_list[id][1]();
    //        delete timeevent_list[id];
    //        System.settings.getValue('user.AzureaUtil', 'TimeEvent' + i);
    //    }
    //    
    //}
}


addEventListener('PreProcessTimelineStatus', attainSchedule);
addEventListener('PostSendUpdateStatus', attainSchedule);
addEventListener('ReceiveFavorite', attainSchedule);


function setTimeOut(fun,  // @param Function:
                    ms) { // @param Number:
                          // @return Number:
    var id = Math.floor(Math.random() * new Date().getTime()).toString(36);
    
    timeout_list[id] = [new Date().getTime() + ms, fun];
    return id;
}


function clearTimeOut(id) { // @param Number:
    delete timeout_list[id];
}


function setTimeInterval(fun,  // @param Function:
                         ms) { // @param Number:
                               // @return Number:
    var id = Math.floor(Math.random() * new Date().getTime()).toString(36);
    
    timeinterval_list[id] = [new Date().getTime() + ms, fun, ms];
    return id
}


function clearTimeInterval(id) { // @param Number:
    delete timeinterval_list[id];
}


//function setTimeEvent(fun,  // @param Function:
//                      ms) { // @param Number:
//                            // @return Number:
//    
//}
//
//
//function clearTimeEvent() {}


mixin(AzureaUtil.time, {
    'setTimeOut': setTimeOut,
    'clearTimeOut': clearTimeOut,
    'setTimeInterval': setTimeInterval,
    'clearTimeInterval': clearTimeInterval
});

})();


//function PreProcessTimelineStatuses() {}
function PreProcessTimelineStatus(status) {
    var listener = AzureaUtil.event.PreProcessTimelineStatus,
        i = -1;
    
    while (listener[++i]) {
        listener[i](status);
    }
}
function PreFilterProcessTimelineStatus(status) {
    var listener = AzureaUtil.event.PreFilterProcessTimelineStatus,
        i = -1, r, flag = false;
    
    while (listener[++i]) {
        r = listener[i](status);
        flag = flag || r;
    }
    return flag;
}
//function PostFilterProcessTimelineStatus() {}
//function PostProcessTimelineStatus() {}
//function PostProcessTimelineStatuses() {}
function PreSendUpdateStatus(status) {
    var listener = AzureaUtil.event.PreSendUpdateStatus,
        i = -1, r, flag = false;
    
    while (listener[++i]) {
        r = listener[i](status);
        flag = flag || r;
    }
    return flag;
}
function PostSendUpdateStatus() {
    var listener = AzureaUtil.event.PostSendUpdateStatus,
        i = -1;
    
    while (listener[++i]) {
        listener[i]();
    }
}
function ReceiveFavorite(source,
                         target,
                         target_object) {
    var listener = AzureaUtil.event.ReceiveFavorites,
        i = -1;
    
    while (listener[++i]) {
        listener[i](source, target, target_object);
    }
}