Aracılığıyla paylaş


Observable.Defer<TValue> Yöntemi

Yeni bir gözlemci abone olduğunda gözlemlenebilir fabrikayı çağıran gözlemlenebilir bir dizi döndürür.

Ad Alanı:System.Reactive.Linq
Derleme: System.Reactive (System.Reactive.dll içinde)

Syntax

'Declaration
Public Shared Function Defer(Of TValue) ( _
    observableFactory As Func(Of IObservable(Of TValue)) _
) As IObservable(Of TValue)
'Usage
Dim observableFactory As Func(Of IObservable(Of TValue))
Dim returnValue As IObservable(Of TValue)

returnValue = Observable.Defer(observableFactory)
public static IObservable<TValue> Defer<TValue>(
    Func<IObservable<TValue>> observableFactory
)
public:
generic<typename TValue>
static IObservable<TValue>^ Defer(
    Func<IObservable<TValue>^>^ observableFactory
)
static member Defer : 
        observableFactory:Func<IObservable<'TValue>> -> IObservable<'TValue> 
JScript does not support generic types and methods.

Tür Parametreleri

  • Tvalue
    Değerin türü.

Parametreler

  • observableFactory
    Tür: System.Func<IObservable<TValue>>
    Elde edilen diziye abone olan her gözlemci için çağrılacak gözlemlenebilir fabrika işlevi.

Dönüş Değeri

Tür: System.IObservable<TValue>
Gözlemcileri verilen gözlemlenebilir fabrika işlevinin çağrısını tetikleyen gözlemlenebilir dizi.

Açıklamalar

Erteleme işleci, bir gözlemcinin diziye abone olduğu zamana kadar sıranın oluşturulmasını ertelemenize veya geciktirmenize olanak tanır. Bu, bir gözlemcinin dizinin güncelleştirmelerini veya yenilenen bir sürümünü kolayca almasına olanak sağlamak için yararlıdır.

Örnekler

Bu örnek, bir işletme veya tüketici tarafından kullanılan ürün bilgilerinin gözlemlenebilir bir dizisini oluşturarak Erteleme işlecini gösterir. Gözlemlenebilir dizi geçerli envanter düzeylerine erişim sağlar. Ertelenebilir bir gözlemlenebilir dizi oluşturarak, uygulama gözlemlenebilir diziye yeniden abone olarak uygulamaya gönderilen güncelleştirilmiş envanter düzeylerine sahip olabilir.

using System;
using System.Reactive.Linq;

namespace Example
{

  class Program
  {

    static void Main()
    {
      //*****************************************************************************************************//
      //*** Product inventories change from time to time. This example demonstrates the Defer operator    ***//
      //*** by creating an observable sequence of the Product class. The creation of the sequence is      ***//
      //*** deferred until the observer calls Subscribe and a new observable sequence is always generated ***//
      //*** at that time with the latest inventory levels to be sent to the observer.                     ***//
      //*****************************************************************************************************//
      ProductInventory myInventory = new ProductInventory();
      IObservable<Product> productObservable = Observable.Defer(myInventory.GetUpdatedInventory);


      //******************************************************//
      //*** Generate a simple table in the console window. ***//
      //******************************************************//
      Console.WriteLine("Current Inventory...\n");
      Console.WriteLine("\n{0,-13} {1,-37} {2,-18}", "Product Name", "Product ID", "Current Inventory");
      Console.WriteLine("{0,-13} {1,-37} {2,-18}", "============", "====================================",
                        "=================");

      //**********************************************************************************//
      //*** Each product in the sequence will be reported in the table using the       ***//
      //*** Observer's OnNext handler provided with the Subscribe method.              ***//
      //**********************************************************************************//
      productObservable.Subscribe(prod => Console.WriteLine(prod.ToString()));


      //******************************************************************************************************//
      //*** To get the updated sequence from the deferred observable all we have to do is subscribe again. ***//
      //******************************************************************************************************//
      Console.WriteLine("\n\nThe updated current Inventory...\n");
      Console.WriteLine("\n{0,-13} {1,-37} {2,-18}", "Product Name", "Product ID", "Current Inventory");
      Console.WriteLine("{0,-13} {1,-37} {2,-18}", "============", "====================================",
                        "=================");

      productObservable.Subscribe(prod => Console.WriteLine(prod.ToString()));


      Console.WriteLine("\nPress ENTER to exit...\n");
      Console.ReadLine();
    }



    //**************************************************************************************************//
    //***                                                                                            ***//
    //*** The Product class holds current product inventory information and includes the ability for ***//
    //*** each product to display its information to the console window.                             ***//
    //***                                                                                            ***//
    //**************************************************************************************************//
    class Product
    {
      private readonly string productName;
      private readonly string productID;
      private int currentInventory;

      public Product(string name, int inventory)
      {
        productName = name;
        productID = Guid.NewGuid().ToString();
        currentInventory = inventory;
      }

      public void RemoveInventory(int delta)
      {
        currentInventory -= delta;

        if (currentInventory < 0)
          currentInventory = 0;
      }

      public override string ToString()
      {
        return String.Format("{0,-13} {1,-37} {2,-18}", productName, productID, currentInventory);
      }
    }



    //*****************************************************************************************************//
    //***                                                                                               ***//
    //*** The ProductInventory class initializes all our product information and provides an Observable ***//
    //*** sequence of the product inventories through the GetUpdatedInventory() method. This method     ***//
    //*** is provided to our call to Observable.Defer() so that all subscriptions against the deferred  ***//
    //*** observable get the lastest inventory information whenever Subscribe is called.                ***//
    //***                                                                                               ***//
    //*****************************************************************************************************//
    class ProductInventory
    {
      private Product[] products = new Product[5];
      private Random random = new Random();

      public ProductInventory()
      {
        for (int i = 0; i < 5; i++)
        {
          //*************************************************************//
          //*** Initial inventories will be a random count under 1000 ***//
          //*************************************************************//
          products[i] = new Product("Product " + (i + 1).ToString(), random.Next(1000));
        }
      }

      public IObservable<Product> GetUpdatedInventory()
      {
        //***************************************************************************************************//
        //*** When inventory for each product is updated up to 50 of each product is consumed or shipped. ***//
        //***************************************************************************************************//
        for (int i = 0; i < 5; i++)
          products[i].RemoveInventory(random.Next(51));

        //****************************************************************************************************//
        //*** This updated observable sequence is always provided by this method when Subscribe is called. ***//
        //****************************************************************************************************//
        IObservable<Product> updatedProductSequence = products.ToObservable();
        return updatedProductSequence;
      }
    }
  }
}

Aşağıda örnek koddan alınan örnek çıkış verilmiştir.

Current Inventory...


Product Name  Product ID                            Current Inventory
============  ====================================  =================
Product 1     04e76657-c403-4208-a300-a3ba42fbe218  808
Product 2     3bc7f823-6624-4803-b673-ec2e7d8802b7  114
Product 3     1e5755f3-6301-4faa-8e1b-35765dc73bce  2
Product 4     f691ddef-b679-42a2-99c7-83651fbf1cc5  894
Product 5     df1f331b-8a52-4a54-a2fb-63d69dda6c1a  467


The updated current Inventory...


Product Name  Product ID                            Current Inventory
============  ====================================  =================
Product 1     04e76657-c403-4208-a300-a3ba42fbe218  761
Product 2     3bc7f823-6624-4803-b673-ec2e7d8802b7  81
Product 3     1e5755f3-6301-4faa-8e1b-35765dc73bce  0
Product 4     f691ddef-b679-42a2-99c7-83651fbf1cc5  890
Product 5     df1f331b-8a52-4a54-a2fb-63d69dda6c1a  440

Press ENTER to exit...

Ayrıca Bkz.

Başvuru

Gözlemlenebilir Sınıf

System.Reactive.Linq Ad Alanı