Control / stop / access a service

Geo's Avatar

Geo

20 Jul, 2011 10:10 PM

I need help structuring something which at first glance seems pedestrian: a means to stop a running service request.

I have a service (Actor) which implements an interface and uses an AS3 HTTPService object to request data from a web server. The service is not a singleton (if it should be, let me know, and let me know any multithread/performance/concurrency implications). The chain of activity to start a request is: user clicks a view widget, its mediator reacts and triggers a command, the command uses an injected service object to talk to the web server and retrieve data.

Problem: the service and thus the means to stop it seems now inaccessible by any other entities in my Robotlegs application.

What's the best way to have a Stop button which once clicked, ends with the correct service's HTTPService being disconnected? I would settle for disconnecting/stopping ANY existent service requests.
Thanks.

  1. 1 Posted by Paul Robertson on 21 Jul, 2011 03:17 AM

    Paul Robertson's Avatar

    Most often I map services using mapSingletonAs()/mapSingleton(). By
    doing so, you can easily access the single instance of the service. In
    that case stopping your service is as simple as creating a method on
    your service class that wraps the appropriate call on your underlying
    HTTPService.

    This works fine if you're only hitting one service, and if you know you
    won't be performing multiple simultaneous calls. If you might be doing
    that, things get more complicated. In that case, one common pattern is
    to use some sort of "promise" system where the service returns a
    "promise" or "token" when you make the server call. You can then use
    the token or promise in subsequent calls to the service (or for
    registering event listeners for errors/results) to denote which service
    call you actually want to get results for (or cancel the request etc.).

    I haven't personally used this pattern in an app which is why I didn't
    give you some concrete code. However, here are a couple of libraries
    that provide a "promise" mechanism, both written by people who are
    known Robotlegs users (the first is by Shaun Smith, the primary author
    of Robotlegs):

    Shaun Smith's "Oil":
    https://github.com/darscan/robotlegs-extensions-Oil

    Jonnie Hallman's "Destroy Framework" and "Destroy Common"
    https://github.com/destroytoday/DestroyFramework/tree/master/src/com/destroytoday/async
    https://github.com/destroytoday/destroy-common/tree/master/src/com/destroytoday/async

    If we're lucky, maybe Stray and Joel cover this in their book that's
    coming out soon. If not, I guess I'll have to hope it makes it into the
    2nd edition =)

    Paul

  2. Support Staff 2 Posted by Stray on 21 Jul, 2011 08:35 AM

    Stray's Avatar

    Hi Geo,

    Paul's suggestion of Oil is a possibility, but you'd need a persistent object to manage the promises still - as you don't want to be mapping them in and out of the container (the injector) - so future commands won't have access to them unless you put them in an injectable holder of some kind.

    Can you explain why you don't want this service to be a singleton? As services shouldn't hold state, it's not normally a problem to use the same one each time. If you need to queue requests to the service then there are utilities for that.

    On concurrency / multithread / performance issues with a singleton service - the flash player is single-threaded, so I don't think there are any implications. In addition you're probably aware that most users will be using a browser which is limited to 2 open connections per server/domain at a time. More recent browsers up the limit but if you're supporting IE7 then 2 is your lot. (You can get around it with subdomains and proxies etc but that's a lot of hassle.)

    So - I would be inclined to say that non-singleton HTTP services are hard to justify. If you really want multiple services then you could have a gateway service (a singleton) which wraps on-demand instances of individual services. The main advantage of this is that it'll keep your logic for managing the services in one relevant place rather than spreading it throughout your application. And of course your commands can stop that service and so on later.

    You might have an interesting use case for a non-singleton HTTP service though... so do shout it out!

    Paul - the book didn't cover this, limited by page count sadly... I'll add it to the list for supporting articles or the next book :)

    I hope that helps,

    Stray

  3. Ondina D.F. closed this discussion on 02 Nov, 2011 05:37 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac