In google io 2011, David Chandler mentioned that you can chain different request context by using append() method,but in practice, I don’t know how to chain them up while they have different receiver,using to() and then fire()?
Please help.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are two kinds of receivers: the ones bound to each method invocation (that you pass to the
Request‘sto()method), and the context-level one (that you pass to theRequestContext‘sfire()method). TheRequest‘sfire(Receiver)method is a short-hand forto(receiver).fire(), i.e. it binds theReceiverto the method.The method-level receivers depend on the method only, their generic parameterization depends on the method’s return value (the generic parameterization of the
RequestorInstanceRequest), so whether youappend()severalRequestContexts together changes absolutely nothing.The context-level receiver is always parameterized with
Void. When youappend()contexts together, they actually form a single context with several interfaces, so you only callfire()once, on any one of the appended contexts.Now let’s go back to the basics: without using
append(), you can only batch together calls for methods that are declared on the context interface. If you have two distinct context interfaces you want to use, you have to make twofire(), i.e. two HTTP requests. The introduction ofappend()allows you to batch together calls for methods declared on any context interface: simply append a context to another one and the calls on both contexts will be batched together in the same HTTP request, triggered by a uniquefire()on any one of the context being appended.Now into the technical details: internally, a context is nothing more than a thin wrapper around a state object. When you
edit()orcreate()a proxy, you add it to the internal state, and when you call a service method, the method name (actually, its obfuscated token) and the arguments are captured and pushed to the state as well. When youappend()a context, you’re only making it share its internal state with the one of the context you append it to. That way, when you call a service method on the appended context, its pushed on the exact same state as the one of the other context, and when youfire()any one of them, the state is serialized into a single HTTP request.Note that, to append a context, its own internal state has to be empty, otherwise an exception will be raised, as the state would be thrown away to be replaced by the one of the other context.
In brief, and in practice:
Note that the line that creates and appends the second context could equally be written:
The
appendmethod returns its argument as a convenience, but it’s really the same value you passed as the argument. This is only to allow writing the one-liner above, instead of being forced to use the two-liner.