private invokeMethod()

in src/XDM.ts [277:315]


    private invokeMethod(registeredInstance: any, rpcMessage: IJsonRpcMessage) {

        if (!rpcMessage.methodName) {
            // Null/empty method name indicates to return the registered object itself.
            this._success(rpcMessage, registeredInstance, rpcMessage.handshakeToken);
            return;
        }

        var method: Function = registeredInstance[rpcMessage.methodName];
        if (typeof method !== "function") {
            this.error(rpcMessage, new Error("RPC method not found: " + rpcMessage.methodName));
            return;
        }

        try {
            // Call specified method.  Add nested success and error call backs with closure
            // so we can post back a response as a result or error as appropriate
            var methodArgs = [];
            if (rpcMessage.params) {
                methodArgs = <any[]>this._customDeserializeObject(rpcMessage.params, {});
            }

            var result = method.apply(registeredInstance, methodArgs);
            if (result && result.then && typeof result.then === "function") {
                result.then((asyncResult: any) => {
                    this._success(rpcMessage, asyncResult, rpcMessage.handshakeToken);
                }, (e: any) => {
                    this.error(rpcMessage, e);
                });
            }
            else {
                this._success(rpcMessage, result, rpcMessage.handshakeToken);
            }
        }
        catch (exception) {
            // send back as error if an exception is thrown
            this.error(rpcMessage, exception);
        }
    }