Tcl Library Source Code

View Ticket
Login
Ticket UUID: e7d1eeebb48912e5d4f2861630234f9a42d43e1b
Title: Dynamic callbacks without re-creating the interface
Type: RFE Version: 1.0.2
Submitter: Wrenky Created on: 2016-03-12 00:52:28
Subsystem: rest Assigned To: nobody
Priority: 5 Medium Severity: Minor
Status: Open Last Modified: 2016-03-12 00:52:28
Resolution: None Closed By: nobody
    Closed on:
Description:
With how ::rest works now, you can define a single callback for a route. The only ways to get a different callback then configured in the ::rest::create step is to recreate the entire interface! Example:
=====================================================================
proc callback {method code response} {
...
}

set test(issue) [list\
    method get\
    url "http://basicUrl.com/routes/"\
    auth basic\
    content-type "application-type"\
    callback callback\
    result json]

::rest::create_interface test

after 5000 {set done 1}
::test::issue ;# Same callback
::test::issue ;# Same callback again
::test::issue ;# Same callback again
vwait done

=====================================================================
All three ::test::issue calls above will call the same callback- It would be great if we could have some way to dynamically control that like we can with URL arguments! We've gotten around this lack of functionality like so:
=====================================================================
# Assoc. array to store callbacks
array set callbackHandler [list]

proc callbackDispatcher {method code {response ""}} {
    upvar token token
    set mySock $token(sock)
    puts "I should call: ::callbackHandler($mySock) == $::callbackHandler($mySock)"
   {*}$::callbackHandler($mySock) $method $code $response
    unset ::callbackHandler($mySock)
}

proc otherCallback {method code {response "-"}} {
    puts "othercallback"
    puts "\tArg Method: --$method--"
    puts "\tArg Code:   --$code--"
}

proc otherCallback_2 {name method code {response "-"}} {
    upvar token token
    puts "otherCallback_2"
    puts "\tArg Name:   --$name--"
    puts "\tArg Method: --$method--"
    puts "\tArg Code:   --$code--"
}


set test(issue) [list\
    method get\
    url "http://basicUrl.com/routes/"\
    auth basic\
    content-type "application-type"\
    callback callbackDispatcher\
    result json]

::rest::create_interface test

after 5000 {set done 1}
set myTok [::test::issue]
upvar 0 $myTok httpToken
set callbackHandler($httpToken(sock)) otherCallback


set myTok2 [::test::issue]
upvar 0 $myTok2 httpToken2
set callbackHandler($httpToken2(sock)) [list otherCallback_2 "otherVar"]

vwait done
=====================================================================
Which allows us dynamic callbacks! Its a little strange to rely on the http sock as our unique identifier but there is nothing else unique in the http token array.

If we could instead do something like
=====================================================================
::test::issue -callback otherCallback
::test::issue -callback [list otherCallback_2 "otherVar"]
=====================================================================
in addition to the URL arguments that would be awesome! If that's not possible, getting a unique identifier in the http token that isn't a socket handle would be nice as well :)