我正在使用UniRx库扩展,它是基于System.Reactive构建的统一的反应式扩展。我有这个订阅者,负责打印接收到的值(在这种情况下,不仅如此,而且整个代码也没有用)
StoreCreator.GetInstance.store.State.
ObserveEveryValueChanged(x => x.fetchInventoryState).
Subscribe(fetchInventoryState =>
{
Debug.Log(fetchInventoryState);
});
这是流程,我确切地知道它更改了“ State”变量,我提供了一种方法来通知自己更改,但我想从此更改为响应式编程,以使语法更加清晰。
单击按钮将调用一个将状态更改为工作状态的函数(ObserveEveryValueChanged未接收到该状态),此后,我调用一个异步方法,该方法将在await方法完成时将“状态”更改为成功。
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();
}
});
}