private static IPromise WaitForRequest <T>(AsyncOperationHandle <T> request, string message) { var p = Promise.Create(); CoroutineExtensions.WaitUntil(() => request.IsDone) .ThenDo(() => { if (!request.IsValid()) // request was auto-collected { Debug.Log(message); p.Resolve(); return; } if (request.Status == AsyncOperationStatus.Succeeded) { Debug.Log(message); p.Resolve(request.Result); } else if (request.Status == AsyncOperationStatus.Failed) { Debug.LogException(request.OperationException); p.Reject(request.OperationException); } }); return(p); }
protected override IPromise <string> OnCreate() { Promise <string> result = Promise <string> .Create(); Type linkType = fieldInfo.GetChildValueType(); if (fieldInfo != null) { LinkFilterAttribute filter = fieldInfo.GetCustomAttribute <LinkFilterAttribute>(); if (filter != null) { if (filter.interfaceType.IsInterface) { TypeSelectorWindow.Open(filter.interfaceType, "Choose implementation").Done(selectedType => { result.Resolve(CreateAsset(selectedType, linkType, fieldInfo)); }); } else { result.Resolve(CreateAsset(filter.interfaceType, linkType, fieldInfo)); } } else { result.Resolve(CreateAsset(type, linkType, fieldInfo)); } } else { result.Resolve(CreateAsset(type, linkType, fieldInfo)); } return(result); }
protected override IPromise <string> OnCreate() { Promise <string> promise = Promise <string> .Create(); Type interfaceToImplement = fieldInfo != null ? fieldInfo.GetCustomAttribute <LinkFilterAttribute>()?.interfaceType : null; List <System.Type> typesToChoose = new List <System.Type>(Util.GetAllTypes(t => t.IsGenericType == false && t.IsAbstract == false && t != type && type.IsAssignableFrom(t) && typeof(Object).IsAssignableFrom(t))); if (interfaceToImplement != null) { typesToChoose.RemoveAll(t => interfaceToImplement.IsAssignableFrom(t) == false); } TypeSelectorWindow.Open(typesToChoose, $"Choose {type.Name} implementation").Done(selectedType => { promise.Resolve(CreateAsset(selectedType, fieldInfo.GetChildValueType(), fieldInfo)); }); return(promise); }
public static IPromise <string> GetAdvertisingId() { Promise <string> result = Promise <string> .Create(); if (Application.isEditor) { result.Resolve(SystemInfo.deviceUniqueIdentifier); return(result); } Application.RequestAdvertisingIdentifierAsync((string advertisingId, bool trackingEnabled, string error) => { if (string.IsNullOrEmpty(advertisingId) == false) { result.Resolve(advertisingId); } else { Debug.LogError("Cannot get Advertising Identifier: " + error); result.Resolve(SystemInfo.deviceUniqueIdentifier); } }); Promise <string> timeout = Promise <string> .Create(); App.Core.Timer.WaitForUnscaled(5).Done(() => timeout.Resolve(SystemInfo.deviceUniqueIdentifier)); return(Promise <string> .Race(result, timeout)); }
public void PromiseShouldHaveHasValueFalseWhenCreated() { Action <int> setAction; var actual = Promise <int> .Create(out setAction); Assert.IsFalse(actual.HasValue); }
public void PromiseShouldImplicitlyCastToType() { Action <int> setAction; var actual = Promise <int> .Create(out setAction); setAction(42); Assert.AreEqual(42, actual); }
public IPromise <bool> ShowDialog(string messageKey, string yesKey = "#yes", string cancelKey = "#cancel") { promise = Promise <bool> .Create(); messageText.Text = App.Core.Local.GetText(messageKey); cancelText.Text = App.Core.Local.GetText(cancelKey); yesText.Text = App.Core.Local.GetText(yesKey); return(promise); }
public void PromiseShouldHaveValueAfterSetActionIsCalled() { Action <int> setAction; var actual = Promise <int> .Create(out setAction); setAction(42); Assert.IsTrue(actual.HasValue); Assert.AreEqual(42, actual.Value); }
public void PromiseShouldSpinWhenValueAccessedButNotSet() { Action <int> setAction; var actual = Promise <int> .Create(out setAction); using (new Timer(_ => setAction(42), null, 100, Timeout.Infinite)) { Assert.AreEqual(42, actual.Value); } }
public void createsSuccessfully() { Assert.DoesNotThrow(() => { Promise <bool> .Create(() => { return(true); }); }); Console.WriteLine("Promise was created successfully"); }
public IPromise Animate() { Promise promise = Promise.Create(); if (gameObject.activeSelf == false) { gameObject.SetActive(true); } StartCoroutine(AnimationRoutine(promise)); return(promise); }
public static IPromise <Type> Open(IEnumerable <Type> types, string title) { TypeSelectorWindow win = GetWindow <TypeSelectorWindow>(true, title, true); win.MoveToCenter(); win.types = types; win.promise = Promise <Type> .Create(); return(win.promise); }
public static IPromise UnloadAll() { if (!Loaded) { return(Promise.Resolved()); } return(Promise.All(AssetDictionary.Values.Select(o => o.OnAssetsUnloaded())) .ThenDo(() => { LoadedPromise = Promise.Create(); })); }
public void waits() { Assert.That(() => { return(Promise <bool> .Create(() => { for (int i = 1; i <= 1000; i++) { Console.WriteLine(i); } return true; }).Wait().promiseStates.Contains(Promise <bool> .State.Waiting)); }); Console.WriteLine("This waited successfully"); }
public static IPromise LoadAsync <T>(string address) { #if UNITY_EDITOR if (!Application.isPlaying) { var promise = Promise.Create(); var load = Addressables.LoadAssetAsync <T>(address); load.Completed += (result) => promise.Resolve(result); return(promise); } #endif var request = Addressables.LoadAssetAsync <T>(address); return(WaitForRequest(request, "Loaded addressable at: " + address)); }
public static IPromise <Type> Open(Type interfaceType, string title) { TypeSelectorWindow win = GetWindow <TypeSelectorWindow>(true, title, true); win.MoveToCenter(); win.types = new List <Type>(Util.GetAllTypes(t => t.IsGenericType == false && t.IsAbstract == false && interfaceType.IsAssignableFrom(t) && typeof(UnityEngine.Object).IsAssignableFrom(t)));; win.promise = Promise <Type> .Create(); return(win.promise); }
/// <summary> /// Returns a <see cref="Promise"/> that resolves when this <see cref="MenuScreen"/> finishes transitioning in. /// </summary> public IPromise TransitionIn(bool abInstant = false) { if (transitionInPromise != null) { return(transitionInPromise); } transitionInPromise = Promise.Create(); IPromise result = transitionInPromise; gameObject.SetActive(true); MenuHandler.CursorLocked = !showCursor; // TBH: This is where the pause time logic was. Interactable = false; BlocksRaycasts = false; OnStateChangedEvent?.Invoke(State, MenuScreenState.TransitioningIn); State = MenuScreenState.TransitioningIn; OnWillAppear(); OnWillAppearEvent?.Invoke(); if (transition != null) { transition.Play(MenuTransitionMode.Forward, abInstant).Done(() => { // TBH: This is where the standard resume time logic was. Interactable = true; BlocksRaycasts = true; if (DefaultSelection != null) { MenuHandler.EventSystem.SetSelectedGameObject(DefaultSelection.gameObject); } OnStateChangedEvent?.Invoke(State, MenuScreenState.In); State = MenuScreenState.In; OnDidAppear(); OnDidAppearEvent?.Invoke(); IPromise promise = transitionInPromise; transitionInPromise = null; promise.Resolve(); }); } else { // TBH: This is where the non-transitioned resume time logic was. Interactable = true; BlocksRaycasts = true; result.Resolve(); } return(result); }
public void throwsErrorIfErrorNotSpecified() { Console.WriteLine("This should throw an exception"); try { Assert.Throws <Exception>(() => { Promise <bool> .Create(() => { throw new Exception("Test Exception"); }); }); } catch { Console.WriteLine("Exception thrown successfully"); } }
/// <summary> /// Returns a <see cref="Promise"/> that resolves when this <see cref="MenuScreen"/> finishes transitioning out. /// </summary> public IPromise TransitionOut(bool abInstant = false) { if (transitionOutPromise != null) { return(transitionOutPromise); } transitionOutPromise = Promise.Create(); IPromise result = transitionOutPromise; if (rememberSelection && EventSystem.current.currentSelectedGameObject != null) { DefaultSelection = EventSystem.current.currentSelectedGameObject.GetComponent <Selectable>(); } else { DefaultSelection = defaultSelection; } EventSystem.current.SetSelectedGameObject(null); Interactable = false; BlocksRaycasts = false; OnStateChangedEvent?.Invoke(State, MenuScreenState.TransitioningOut); State = MenuScreenState.TransitioningOut; OnWillDisappear(); OnWillDisappearEvent?.Invoke(); if (transition != null) { transition.Play(MenuTransitionMode.Reverse, abInstant).Done(() => { gameObject.SetActive(false); OnStateChangedEvent?.Invoke(State, MenuScreenState.Out); State = MenuScreenState.Out; OnDidDisappear(); OnDidDisappearEvent?.Invoke(); IPromise promise = transitionOutPromise; transitionOutPromise = null; promise.Resolve(); }); } else { result.Resolve(); } return(result); }
private static void runTrvPaddy() { var baseQuery = Models.TrvPaddy.Query.GetSampleData(); Console.WriteLine("Base Query: " + baseQuery.ToJson(true)); Console.WriteLine("Local or International Flight?"); Console.WriteLine("1\tLocal"); Console.WriteLine("2\tInternational"); int option = Convert.ToInt32(Console.ReadLine()); switch (option) { case 1: var localQuery = Models.TrvPaddy.Local.Query.GetQuery(baseQuery); Promise <string> .Create(() => { Clipboard.SetText(localQuery.ToJson()); return(null); }); Console.WriteLine("Local Query: " + localQuery.ToJson(true)); Console.WriteLine("Search Url: " + localQuery.ToString()); Models.TrvPaddy.Local.Scrapper scrapper = new Models.TrvPaddy.Local.Scrapper(); var flights = scrapper.GetFlights(localQuery); Console.WriteLine(flights.ToJson(true)); break; case 2: var internationalQuery = Models.TrvPaddy.International.Query.GetQuery(baseQuery); Promise <string> .Create(() => { Clipboard.SetText(internationalQuery.ToJson()); return(null); }); Console.WriteLine("International Query: " + internationalQuery.ToJson(true)); Console.WriteLine("Search Url: " + internationalQuery.ToString()); break; default: break; } }
static void Main(string[] args) { Promises.Promise.Create(() => { Console.WriteLine($"[{Thread.CurrentThread.ManagedThreadId}] starting"); }) .Then((resolve, reject) => { Task.Run(() => { Thread.Sleep(2000); Console.WriteLine($"[{Thread.CurrentThread.ManagedThreadId}] done1"); resolve(); }); }) .Then(() => { Console.WriteLine($"[{Thread.CurrentThread.ManagedThreadId}] done2"); }) .Then((resolve, reject) => { Task.Run(() => { Thread.Sleep(2000); Console.WriteLine($"[{Thread.CurrentThread.ManagedThreadId}] done3"); resolve(); }); }) .Then(() => { Console.WriteLine($"[{Thread.CurrentThread.ManagedThreadId}] done4"); }) .Catch((ex) => { Console.WriteLine($"[{Thread.CurrentThread.ManagedThreadId}] error: {ex.Message}"); }); Promise.All(new Promise[] { Promise.Create(() => { Console.WriteLine($"[{Thread.CurrentThread.ManagedThreadId}] starting task #1"); }) .Then((resolve, reject) => { Thread.Sleep(5000); Console.WriteLine($"[{Thread.CurrentThread.ManagedThreadId}] done task #1"); resolve(); }), Promise.Create(() => { Console.WriteLine($"[{Thread.CurrentThread.ManagedThreadId}] starting task #2"); }) .Then((resolve, reject) => { Thread.Sleep(5000); Console.WriteLine($"[{Thread.CurrentThread.ManagedThreadId}] done task #2"); resolve(); }) }) .Then(() => { Console.WriteLine($"[{Thread.CurrentThread.ManagedThreadId}] done with all tasks"); }); Console.WriteLine($"[{Thread.CurrentThread.ManagedThreadId}] what are you waiting for?"); Console.ReadLine(); }
public void CreateNewResultPromise() { resultPromise = Promise <R> .Create(); Resolved = false; }