dart - Add a property to an object (or at least similar outcome) -
first, context of i'm doing. running httpserver handling httprequests.
httpserver.bind(address, port).then((httpserver server) { listensubscription = server.listen(onrequest); }); void onrequest(httprequest request) { //handle request here }
i'd add logging this, , due asynchronous nature of all, want add identifying marker requests (so can match request receipts responses, fer example). code inside of onrequest()
calls bunch of other functions different things (handle vs post requests, etc.), generating id @ top cumbersome solution i'd have pass around through other function calls. am, however, passing around httprequest
object, thought nice throw id
field on it, in javascript, except dart doesn't work way.
thoughts went subclassing httprequest
class, converting httprequest
object onrequest()
method receives seemed more trouble , overhead needs required.
so ask, there idiomatic dart way attach data existing object? if there isn't idiomatic, simplest (both in code , runtime complexity) way can think of accomplish this?
well, there's expando, don't know performance implications.
something like:
// somewhere top level. create once. final loggingid = new expando(); ... // inside of onrequest loggingid[request] = generateid(); ... // later inside log() print(loggingid[request]);
expandos weak-reference maps, understanding.
Comments
Post a Comment