Example #1
0
        public AsyncPublishContext PublishAsync <TEvent>(TEvent evt, Action <TEvent> onFinished = null, AsyncType type = AsyncType.Publish_Main_Result_Main)
        {
            // create the context-object which can be used to check for finished if e.g. from within a coroutine
            var result = new AsyncPublishContext();

            if (type == AsyncType.Publish_Main_Result_Main)
            {
                _asyncManager.AddToMainThread(() => {
                    // inside coroutine
                    Publish(evt);
                    result.finished = true;
                    result.result   = evt;
                    if (onFinished != null)
                    {
                        onFinished(evt);
                    }
                });
            }
            else
            // atm, at this point there are only the worker-options, so I don't need to query specially for those
            //if (type == AsyncType.Publish_Worker_Result_Worker || type == AsyncType.Publish_Worker_Result_Main)
            {
                _asyncManager.AddToWorkerThread(() => {
                    // inside worker-thread
                    Publish(evt);
                    result.finished = true;
                    result.result   = evt;

                    if (type == AsyncType.Publish_Worker_Result_Main)
                    {
                        // since we want the result send to the main-thread, we need to wrap it in an Action
                        // and add it to the main-thread-queue
                        _asyncManager.AddToMainThread(() => {
                            onFinished(evt);
                        });
                    }
                    else if (type == AsyncType.Publish_Worker_Result_Worker)
                    {
                        // immediately send the result to the callback which will be called from within(!) the worker-thread
                        onFinished(evt);
                    }
                }, null);
            }
            // return the context-object
            return(result);
        }