[源代码]一个封装了系统服务的调用类ServiceRequestWrapper

ServiceRequestWrapper:
A simple class that provides common service Mojo.Service.Request lifetime management logic,
primarily this prevents garbage collection of request objects and their associated callbacks.

Install:
- Move service-request-wrapper.js into application source tree.
- Include service-request-wrapper.js via sources.json

Usage:
- Call ServiceRequestWrapper.request in place of all new Mojo.Service.Request calls.

Note that this wrapper will cleanup non-subscription requests. If a requests is made
as a subscription, the caller must explicitly call the cancel object for the given
request or the request object will remain in the heap until the application closes.
 
var ServiceRequestWrapper;

(function() {
    var requestId = 0,
        requests = [];

    function completeHandler(optionsIn, requestId) {
        return function(response) {
            if (!(optionsIn.parameters && optionsIn.parameters.subscribe) && requests[requestId]) {
                requests[requestId].cancel();
            }

            optionsIn.onComplete && optionsIn.onComplete(response);
        };
    }
    function cancelHandler(requestId, original) {
        return function() {
            delete requests[requestId];

            original.apply(this, arguments);
        };
    }

    ServiceRequestWrapper = {
         /**
          * Initiates a request to the 
          */
        request: function(url, optionsIn, resubscribe) {
            requestId++;

            var options = this.bindOptions(url, optionsIn, requestId),
                request = new Mojo.Service.Request(url, options, resubscribe);
            request.cancel = cancelHandler(requestId, request.cancel);

            requests[requestId] = request;
            return request;
        },

        bindOptions: function(url, optionsIn, requestId) {
            var options = Object.clone(optionsIn);
            options.onComplete = completeHandler(optionsIn, requestId);
            return options;
        }
    };
})();

 

测试类

 

function ServiceRequestWrapperTest() {}

ServiceRequestWrapperTest.prototype.exec = function(assistant, cont) {
    execChain(assistant, cont, [
            this.singleSuccessTest,
            this.singleFailureTest,
            this.subscriptionSuccessTest,
            this.subscriptionFailureTest
        ], 0)();
};

ServiceRequestWrapperTest.prototype.singleSuccessTest = function(assistant, cont) {
    var success,
        request = ServiceRequestWrapper.request('palm://com.palm.preferences/systemProperties', {
            method:"Get",
            parameters:{"key": "com.palm.properties.nduid" },
            onSuccess: function(response){
                success = true;
            },
            onComplete: function(response) {
                if (!success) {
                    assistant.failure("Did not recieve success event");
                }
                if (!request.cancelled) {
                    assistant.failure("Request not cancelled");
                }
                cont();
            }
      });
};
ServiceRequestWrapperTest.prototype.singleFailureTest = function(assistant, cont) {
    var failure,
        request = ServiceRequestWrapper.request(
        "palm://com.palm.preferences/systemProperties", {
            onFailure: function(response) {
                failure = true;
            },
            onComplete: function(response) {
                if (!failure) {
                    assistant.failure("Failure not recieved");
                }
                if (!request.cancelled) {
                    assistant.failure("Request not cancelled");
                }
                cont();
            }
        });
};
ServiceRequestWrapperTest.prototype.subscriptionSuccessTest = function(assistant, cont) {
    var sub, secondSet;

    function subComplete() {
        if (sub.cancelled) {
            assistant.failure("Request cancelled");
        }

        if (!secondSet) {
            secondSet = true;
            ServiceRequestWrapper.request('palm://com.palm.systemservice', {
                method:"setPreferences",
                parameters:{"food":"beef"},
                onFailure: failure
            });
        } else {
            sub.cancel();

            if (!sub.cancelled) {
                asssiant.failure("Requets not cancelled");
            }
            cont();
        }
    }
    function failure() {
        assistant.failure("Failure event");
        cont();
    }

    ServiceRequestWrapper.request('palm://com.palm.systemservice', {
        method:"setPreferences",
        parameters:{"food":"pizza"},
        onSuccess: function() {
            sub = ServiceRequestWrapper.request('palm://com.palm.systemservice', {
                method:"getPreferences",
                parameters:{ keys: [ "food" ], subscribe: true },
                onFailure: failure,
                onComplete: subComplete
            });
        },
        onFailure: failure
    });
};
ServiceRequestWrapperTest.prototype.subscriptionFailureTest = function(assistant, cont) {
    var failure,
        request = ServiceRequestWrapper.request(
        "palm://com.palm.preferences/systemProperties", {
            subscribe: true,
            onFailure: function(response) {
                failure = true;
            },
            onComplete: function(response) {
                if (!failure) {
                    assistant.failure("Failure not recieved");
                }
                if (!request.cancelled) {
                    assistant.failure("Request not cancelled");
                }
                cont();
            }
        });
};

你可能感兴趣的:(json,prototype,Palm)