Partager via


WinJS internals: WinJS._Signal

WinJS contains several useful classes which are unfortunately hidden (starts with '_'). In this post we will look at one of them, WinJS._Signal.

 

A common usage of this class is to manage (completes, cancels, fails) the promise. The promise itself is simple concept. But sometimes it's awkward to complete it. 

 

Let's see the example how it's possible to create and manage a promise:

As you can see, the promise can be created if it's necessary:

  1. to wrap the asynchronous operation and completes/errors on behalf of it, shown in Example #1
  2. to use a promise as a synchronization construct (e.g. promise is completed based on some event), Example #2 shows it. 

Usage #2 is very awkward. That's the reason why there is WinJS._Signal class.

Synchronization problem

 It's quite common to synchronize two code paths and one of it depends on an event. It could be possible to write the code into the event handler and do the stuff there. But the problem is that the logic is split all over the code base. Wouldn't it be better to keep the code on one place?

Let's see how it's possible to do it with WinJS._Signal:

 

 We could enhance the previous sample and create a similar signal/event couple when the internet connection is established, join loaded and internet connection promises and then call the xhr(uri). The main advantage is that you have one construct (_Signal) using which it's possible to complete/cancel/fail the promise.

 

I hope, new WinJS will have it exposed as public class in the future release.