Skip to main content

[AS3]PureMVC Utility - AsyncNotifier

下午剛好有位朋友問了我一個問題..「PureMVC Utility AsyncCommand 有沒有辦法等待 proxy 做反應?」,因為先前沒研究過此 Command 所以稍微花了些時間看一下它的運作邏輯。雖然最後討論的結果是設好 proxy call back 應該是可以等待 proxy 做完工作再執行下一個 subCommand,但是嚴格說起來,真正實作專案後發現,比較需要的是非同步 Notification 的處理。

由於 proxy 一些動作通常都是 onResult 後直接送出 Notification,光一個畫面可能會有好幾支 proxy 需要有順序性的完成資料讀取後,才可以將最終的 data 送到畫面內。這時候順序動作的 Command 就很浪費空間,因為可能一次要處理五六個 proxy 的 call back,這樣等於要等待幾個就需要寫幾支 Command....

為了解決這種非同步順序型的處理現有的 Notifications,就花了點時間弄了一支 AsyncNotifier 來用用囉~~

Project Name: pureMVC Utility - AsyncNotifier
Version: 1.0.0
Project Owner: Erin Lin
Description: An utility is under pureMVC for AS3.
Download: Standard & MultiCore versions
pureMVC website: http://puremvc.org/

先製作 AsyncHandleCommand 重點就是將你要處理的 Notifications 一次列在同一張 Command 的 execute 內(不需要事前註冊喔):

public class AsyncHandleCommand extends SimpleCommand implements ICommand
{
override public function execute(notification:INotification):void
switch( notification.getName() ){
case FirstNotificationName:
//
break;
case SecondNotificationName:
//
break;
}
}


AsyncNotifier 是馬上執行,執行結束就清光
所以就是在你要執行時直接寫上:
Standard 用法:

import org.puremvc.as3.utilities.asyncnotifier.AsnycNotifier;
var async:AsyncNotifier = new AsyncNotifier("YourName");
async.runAsyncNotifier( AsyncHandleCommand, FirstNotificationName , SecondNotificationName...etc );


MultiCore 版本:

import org.puremvc.as3.multicore.utilities.asyncnotifier.AsnycNotifier;
var async:AsyncNotifier = new AsyncNotifier("YourName");
async.initializeNotifier( multitonKey );
async.runAsyncNotifier( AsyncHandleCommand, FirstNotificationName , SecondNotificationName...etc );


接下來就是為了可以處理收過的資料才加上的功能...請小心服用:
執行位置:在你的 AsyncHandleCommand 內

//you also can use getNotificationByName( name) to get previous notification object.
var async:AsyncNotifier = facade.retrieveMediator( yourAsyncNotifierName ) as AsyncNotifier;
//沒錯 AsyncNotifier 是一支 Mediator =)
var prevNotification:INotification = async.getNotificationByName( FirstNotificationName );
// 這樣就可以抓到前面 run 過的 notification 物件,記得是 run 過的喔...別亂抓...

Comments

Popular posts from this blog

[Swift3] weak 與 unowned 關鍵字

雖然在 Swift 中看起來"很像"是不需要煩惱內存管理的問題,不過實際上它還是遵循著自動引用計數 (ARC) 的規則,當一個物件沒有被其他對象引用時會自動被銷毀,如果三魂七魄沒有完全回位的話,就會有個靈體留在現世的空間裡,最經典的範例如下: 閉包(Closure)引用 classClassA { typealias Complete = ()->() var name : String var onComplete : Complete? init(_ name: String){ self.name = name print("Hello I am \(self.name)") onComplete = { print("\(self.name): onComplete!") // --> 閉包引用 self, 計數 + 1 } } deinit { print("deinit: \(self.name)") } } var a : ClassA? = ClassA("A") // --> 引用計數 + 1 a = nil // 2-1 = 1 還剩下 1 所以沒辦法銷毀 ---output------- Hello I am A 由於這邊的 onComplete 宣告為 Optional, 正確的做法要連同 onComplete 一起刪除才可以被回收,若不是 Optional 則會進入無法回收狀態: var b : ClassA? = ClassA("B") b?.onComplete = nil // --> 還好是 Optional 可以設成 nil 計數 - 1 b = nil // 計數 = 0 所以被回收 ---output------- Hello I am B deinit: B 但是做人不需要煩惱太多,這時候就出動 unowned 關鍵字讓物件可以順利被回收: onComplete = { [unowned self] in print...

Robotlegs AS3 MVC framework in Flex project (I)

由於今天在 奶綠茶的 Robotlegs FlashAS3 MVC framework 看到他讚賞 Robotlegs AS3 MVC framework 的好用,不禁好奇的來研究一番... 其實這個 AS3 MVC framework 出現有些時日,但是一直都沒有特別的關注它。簡單看完 Robotlegs 最佳實踐 ,其實就可以大部分的了解 Robotlegs 運作的邏輯。不如其他的 MVC framework 有很多語言版本, Robotlegs 完全是專注在 AS3 上。也由於僅有一個版本,所以訊息傳遞是利用 AS3 Event 機制來完成,對於一些想要入門實作 MVC 的 Flash 工程師來說絕對是比 pureMVC 好入門很多...=) Robotlegs 的重點就是搭著「 依賴注入( Dependency Injection) 」達成 MVCS 的架構,  擷取用最佳實踐的中文翻譯解釋: Quote: 最簡單地, 依賴注入是為對象提供實例變量或屬性的行為. 當你傳遞一個變量到一個類的構造函數, 你在使用依賴注入. 當你設置一個類的屬性, 你在使用依賴注入. 如果你不是使用嚴格的過程或線性方式編寫AS3, 很可能你現在就在使用依賴注入。 呃...依賴注入? ?簡單來講一台霜淇林機,你要賣什麼口味的霜淇林就是依賴不同的原料放置到左右兩個攪拌桶內,而每一個口味可以透過介面實作來達成依賴注入。 以下是簡單的 Dependency Injection 的 Setter injection 範例: public interface 霜淇淋介面 { function 擠():void; } 然後實作: public class 巧克力 implements 霜淇淋介面{ public function 擠():void{ trace("巧克力口味的霜淇淋"); } } public class 草莓 implements 霜淇淋介面{ public function 擠():void{ trace("草莓口味的霜淇淋"); } } public class 香草 implements 霜淇淋介面{ public function 擠():void{ ...