기타/BONDI2010. 8. 12. 16:33

BONDI interface API에서 PendingOperation은 비동기 호출에 있어서 중요한 요소이다.

이는 비동기 호출을 동기적으로 할 수 있는 방안을 제공하기도 한다. 즉 비동기 호출이지만 동기적으로 처리하고 싶을 때 사용할 수 있다.

아래와 같이 두개의 함수가 준비되어 있다.

PendingOperation.

        interface PendingOperation {
                boolean cancel();

                void wait();
        };

동작을 취소할 수 있는 방안을 제공하기 위해 고안된 비동기적으로 리턴하는 인터페이스이다.

cancel 함수

비동기적으로 백그라운드로 동작중인 동작의 취소를 호출한다. 이 함수는 항상 바로 리턴한다. false 리턴은 pending operation이 이미 끝났거나 기술적인 한계로 인해 cancel이 성공하지 못했을 때 한다. 결과적으로 동작은 완료 여부는 연계된 callback에 의해 결정난다. true 리턴은 취소가 성공했을 때 한다. 취소된 pending operation의 callback은 호출되지 않는다. wait중인 pending operation에 대해 cancel을 호출하면 false를 리턴한다. 즉 cancel은 성공하지 않는다.

wait 함수

비동기적인 동작이 끝날때 까지 대기한다. BONDI 비동기 메소드를 동기메소드인양 사용코자 할 때 사용가능하다.  동작이 끝날때 까지 thread는 대기 한다. 관련된 callback이 모두 처리된 후 리턴한다. 


아래의 예에서 "Feature loading accomplished"보다 "SUCCESS"나 "ERROR"가 먼저 뜨게 된다.
Code example
 var loadingsuccessful = 0;
 
 function successCB()
 {
                alert("SUCCESS");
                loadingsuccessful = 1;
 }
 
 function errorCB()
 {
                alert("ERROR");
                loadingsuccessful = 0;
 }
 
 function loadFeatureSynchronously()
 {
                var asyncop = bondi.requestFeature(successCB, errorCB, "http://bondi.omtp.org/api/messaging");
                asyncop.wait(); // waiting for messaging API to be loaded
                alert("Feature loading accomplished");
      return loadingsuccessful; // if 1, the messaging API was loaded synchronously and successfully
 }
 
 // The bondi.requestFeature() method is asynchronous by design.
 // bondi.requestFeature() start dummy asynchronous operation.
 // The call to loadFeatureSynchronously() results in SUCCESS or ERROR prompt prior to Feature loading accomplished prompt.
 
Posted by 삼스