Advertisement
Guest User

Untitled

a guest
Jul 15th, 2012
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.54 KB | None | 0 0
  1.     /// <summary>
  2.     /// Used to asynchronously compute difference between two
  3.     /// differenceable objects. When the corresponding computation is
  4.     /// complete, the 'DifferenceSnapshotComputed' event is raised.
  5.     /// </summary>
  6.     public class DifferenceSnapshot
  7.     {
  8.         public event EventHandler<EventArgs> DifferenceSnapshotComputed;
  9.  
  10.         /// <summary>
  11.         /// Is equal to 'null' if the difference is not yet computed.
  12.         /// Guaranteed to be non-null when the 'DifferenceSnapshotComputed' event
  13.         /// was raised and becomes immutable after that.
  14.         /// </summary>
  15.         public IImmutableDifferenceBuffer DifferenceBuffer;
  16.  
  17.         public DifferenceSnapshot(IDifferenceable left, IDifferenceable right)
  18.         {
  19.             // ...
  20.         }
  21.     }
  22.  
  23.     public class DifferenceViewer
  24.     {
  25.         private readonly DifferenceSnapshot differenceSnapshot;
  26.        
  27.         /// <summary>
  28.         /// Called when the difference snapshot finished it's evaluation.
  29.         /// </summary>
  30.         private readonly EventHandler<EventArgs>
  31.             performPostponedDifferenceSnapshotProjection;
  32.  
  33.         public DifferenceViewer(DifferenceSnapshot differenceSnapshot)
  34.         {
  35.             this.differenceSnapshot = differenceSnapshot;
  36.  
  37.             if (differenceSnapshot.DifferenceBuffer == null)
  38.             {
  39.                 performPostponedDifferenceSnapshotProjection =
  40.                     (sender, args) =>
  41.                     {
  42.                         // This postponed projection is only done once when
  43.                         // the corresponding difference snapshot finished it's
  44.                         // computation.
  45.                         differenceSnapshot.DifferenceSnapshotComputed -=
  46.                             performPostponedDifferenceSnapshotProjection;
  47.  
  48.                         ProjectDifferenceSnapshotToView();
  49.                     };
  50.  
  51.                 differenceSnapshot.DifferenceSnapshotComputed +=
  52.                     performPostponedDifferenceSnapshotProjection;
  53.             }
  54.             else
  55.             {
  56.                 ProjectDifferenceSnapshotToView();
  57.             }
  58.         }
  59.  
  60.         private void ProjectDifferenceSnapshotToView()
  61.         {
  62.             if (differenceSnapshot.DifferenceBuffer == null)
  63.                 throw new InvalidOperationException(
  64.                     "Difference snapshot is not yet computed.");
  65.  
  66.             // Perform some actions which result in displaying the
  67.             // corresponding snapshot in the user view.
  68.         }
  69.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement