2

I am using UniRx library extension which is a reactive extension for unity, build on System.Reactive. I have this subscriber with the role of printing the value it received (not only that but the entire code of it is useless in this case)

StoreCreator.GetInstance.store.State.
    ObserveEveryValueChanged(x => x.fetchInventoryState).
        Subscribe(fetchInventoryState =>
        {
            Debug.Log(fetchInventoryState);
        });

This is the flow, and I know exactly that it changes the "State" variable, I made a way to notify myself of changes but I wanted to change from it to reactive programming to make the syntax more clear.

Clicking on a button call a function which change the state to working (the one which is not received by ObserveEveryValueChanged), after this I call an async method which will change the "State" to success when an await method finish.

public void FetchMoreItems(string accountName, string cursor)
{
    _store.Dispatch(new FetchMoreItems());
    _services.fetchInventoryService.FethInventoryMore(
        accountName,
        cursor,
        (_cursor, _items) => _store.Dispatch(new FetchMoreItemsSucces(_cursor, _items)),
        () => _store.Dispatch(new FetchItemFail()),
        (_cursor, _items) => _store.Dispatch(new InventoryFinished(_cursor, _items)));
}



public async void FethInventoryMore(string accountName, string cursor, Action<string, List<Item>> success, System.Action fail, Action<string, List<Item>> finishedInventory)
{
    await MockFetchInventory(accountName, cursor, success, fail, finishedInventory);
}

private Task MockFetchInventory(string accountName, string cursor, Action<string, List<Item>> success, System.Action fail, Action<string, List<Item>> finishedInventory)
{
    return Task.Factory.StartNew(() =>
    {
        if (accountName == "John" && (cursor == "newCursor" || cursor == "secondNewCusor"))
        {
            List<Item> items = new List<Item>
                {
                    new Item(null, ItemType.Movement, 1, 1, 1, 3),
                    new Item(null, ItemType.Digger, 1, 3, 1, 1),
                    new Item(null, ItemType.Chest,230, 0, 0, 0),
                    new Item(null, ItemType.Key, 0, 0, 0, 0),
                    new Item(null, ItemType.Locator, 1, 1, 2 ,4)
                };

            if (cursor == "newCursor")
            {
                success("secondNewCusor", items);
            }
            else
            {
                items = new List<Item>
                {
                    new Item(null, ItemType.Movement, 1, 1, 1, 3),
                    new Item(null, ItemType.Digger, 1, 3, 1, 1)
                };
                finishedInventory("", items);
            }
        }
        else
        {
            fail();
        }
    });
}
derHugo
  • 83,094
  • 9
  • 75
  • 115

0 Answers0