CC-Archived / promise-as3

Promises/A+ compliant implementation in ActionScript 3.0
168 stars 55 forks source link

Promise.when adapter strategy #12

Closed darscan closed 11 years ago

darscan commented 11 years ago

One approach to solving: #9 ( https://github.com/CodeCatalyst/promise-as3/issues/9 ).

Removes import mx.rpc.AsyncToken from core Promise class.

This is just one approach - you could create an "adapter" interface etc, etc, but I think this is a decent, simple solution.

johnyanarella commented 11 years ago

A reasonable approach for solving the issues re: dependency on the Flex SDK.

(Major kudos for including unit tests.)

I still like the simplicity and readability of when() being able to transparently provide that adaptation when necessary.

In context:

Promise.when( service.getUser( userId ) ).then( ... );

just feels like it reads better than:

AsyncTokenAdapter.adapt( service.getUser( userId ) ).then( ... );

I'm drawn to your adapter interface suggestion, especially if we could maintain API compatibility. I'm imagining a scenario where adapters are registered such that when() can later internally identify and use the appropriate adapter. However, I'm not sure there would be many (any?) other adapters that would ever be written. Maybe that would be overengineering it...

Pondering this one.

darscan commented 11 years ago

I'm imagining a scenario where adapters are registered such that when() can later internally identify and use the appropriate adapter.

Heh, that's what this patch actually does :) It's used like this:

// In one of your application configuration files somewhere you do this once:
Promise.registerAdapter(AsyncTokenAdapter);

// And from then on you'd use Promise.when as normal:
Promise.when( service.getUser( userId ) ).then( ... );

You can register multiple adapters - the first one to return a promise is used. And you can unregister them later if need be (when unit testing, for example).

darscan commented 11 years ago

I switched the adapter strategy to use a simple function reference. This allows one to create an adapter preloaded with configuration, for example:

var adapter:CrazyAdapter = new CrazyAdapter(some, weird, args);
Promise.registerAdapter(adapter.adapt);

I also update the docs to make usage a little clearer (hopefully!).

joeatsalot commented 11 years ago

Thanks very much, I'm really pleased to see this improvement.

johnyanarella commented 11 years ago

On my initial review I somehow missed your changes to the Promise class to implement the registrable adapter approach. Excellent work - I should have known, given your reputation. I really like your solution.

Thanks again!