tag.on.grvt Maven / Gradle / Ivy
/*******************************************************************************
* © 2018 Disney | ABC Television Group
*
* Licensed under the Apache License, Version 2.0 (the "Apache License")
* with the following modification; you may not use this file except in
* compliance with the Apache License and the following modification to it:
* Section 6. Trademarks. is deleted and replaced with:
*
* 6. Trademarks. This License does not grant permission to use the trade
* names, trademarks, service marks, or product names of the Licensor
* and its affiliates, except as required to comply with Section 4(c) of
* the License and to reproduce the content of the NOTICE file.
*
* You may obtain a copy of the Apache License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Apache License with the above modification is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the Apache License for the specific
* language governing permissions and limitations under the Apache License.
*******************************************************************************/
@Tag(
info="Open an async channel to receive events from remote clients",
body="closure to process incoming messages from remote clients; the message data (if any) is passed as the first argument to the closure, and if the closure accepts a second argument the websocket session is also passed in",
sample="on(event:'reverseMessage'){ it.reverse() }",
returns="Some value that will be supplied to the remote callback function, if any",
attrs=[
@Attr(
name = "event",
info="The name of the remote event to react to",
required = true
),
@Attr(
name = "var",
info="the name of the variable to bind the AsyncChannel object that can be used to close this acceptor, or check its state",
required = false
),
@Attr(
name = "completed",
info = '''optional callback to execute when the channel is completed after being closed or halted,
it is passed the last result of the handler call and may investigate any 'error',
and should return the final result for the channel''',
required = false
),
@Attr(
name = "q",
info="Optionally, a maximum queue size for this acceptor, defaults to unbounded. Behavior with a full queue is determined by policy",
required = false
),
@Attr(
name = "policy",
info="what to do with new messages when queue is full, one of (drop|evict|block), defaults to block (backpressure)",
required = false
)
]
)
class On{
@SkipStatistics
def tag(Map atts, Closure body){
def event = resolve(atts,'event',String.class)
assert event
accept(*:atts, channel: [remote: event]){ msg->
async{
def rval
try{
if(body.maximumNumberOfParameters > 1){
rval = body.call(msg.data, msg.session)
}
else{
rval = body.call(msg.data)
}
}
catch(Throwable th){
def err = "Error processing remote message on channel '${atts.event}'"
log(error: err, thrown: th)
rval = [error: "${err}: ${th.message}"]
}
if(msg.callback){
msg.callback(rval)
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy