xpcshell で非同期処理
で、教えていただいたので、早速実装してみた。
- xpcshell/xpcshell.js at master from teramako's scripts - GitHub*1
- xpcshell/timerTest.js at master from teramako's scripts - GitHub
xpcshell timerTest.js
で実行すると、以下のような感じなる。
[loading 'xpcshell.js'...] 0: HogeFuga 1: HogeFuga FooBar 2: HogeFuga 3: HogeFuga FooBar 4: HogeFuga 5: HogeFuga 6: HogeFuga FooBar 7: HogeFuga 8: HogeFuga END
非同期的なものはみんな、ThreadManager.pool
に収めるようにして、timeTest.jsの最後のThreadManager.wait()
でpoolに収められたものが終了するのを待っている。
一応、setTimout
,setInterval
に加えて、XMLHttpRequestのasync=true
、任意のメソッドを実行するThreadManager.callAsync
をpoolの管理対象にしている。
ただ、ThreadManager.callAsync
中にXMLHttpRequestをやったら
[Exception... "Component returned failure code: 0x80570015 (NS_ERROR_XPC_CI_RETURNED_FAILURE) [nsIJSCID.createInstance]" nsresult: "0x80570015 (NS_ERROR_XPC_CI_RETURNED_FAILURE)" location: "JS frame :: xpcshell.js :: XMLHttpRequest :: line 186" data: no]
という例外が発生してしまい動かなかった。とってもバギーな感じw