Example #1
0
        //----------------------------------//

        /// <summary>
        /// On a page having been parsed.
        /// </summary>
        protected void OnParsed(ElementParser parser)
        {
            // was the parse successful?
            if (parser.Error != null)
            {
                Log.Error("Element parser encountered an error. " + parser.Error);
                _processing = false;
                return;
            }

            // run on retrieved
            OnRetrieved(parser.Root);
        }
Example #2
0
        /// <summary>
        /// Get the element asynchronously.
        /// </summary>
        public async Task <Element> GetAsync()
        {
            Element result = null;

            _lock.Take();

            if (_processing)
            {
                if (_element == null)
                {
                    _lock.Release();

                    if (_path == null)
                    {
                        result = await _getElement.RunAsync();
                    }
                    else
                    {
                        var parser = new ElementParser(_path).RunSync();
                        if (parser.Error == null)
                        {
                            result = parser.Root;
                        }
                        else
                        {
                            Log.Error("Element parser error. " + parser.Error);
                        }
                    }

                    if (result == null)
                    {
                        return(null);
                    }

                    if (_onBuild != null)
                    {
                        _onBuild.ArgA = result;
                        _onBuild.Run();
                    }
                }
                else
                {
                    result = _element.Clone();

                    _lock.Release();
                }

                return(result);
            }

            // check if the link has or should be updated
            if (_element == null)
            {
                if (_path == null)
                {
                    result = await _getElement.RunAsync();
                }
                else
                {
                    var parser = new ElementParser(_path).RunSync();
                    if (parser.Error == null)
                    {
                        result = parser.Root;
                    }
                    else
                    {
                        _lock.Release();
                        Log.Error("Element parser error. " + parser.Error);
                        return(null);
                    }
                }

                _element = result;

                if (_onBuild != null)
                {
                    _onBuild.ArgA = _element;
                    _onBuild.Run();
                }

                if (_cacheTime > 0)
                {
                    _nextUpdate = Time.Milliseconds + _cacheTime;
                }
                else
                {
                    _nextUpdate = long.MaxValue;
                }
            }
            else
            {
                if (_nextUpdate < Time.Milliseconds)
                {
                    if (_path == null)
                    {
                        result = await _getElement.RunAsync();

                        if (result == null)
                        {
                            _lock.Release();
                            return(null);
                        }
                    }
                    else
                    {
                        var parser = new ElementParser(_path).RunSync();
                        if (parser.Error != null)
                        {
                            _lock.Release();
                            Log.Error("Element parser error. " + parser.Error);
                            return(null);
                        }
                        result = parser.Root;
                    }

                    _element = result;

                    if (_onBuild != null)
                    {
                        _onBuild.ArgA = _element;
                        _onBuild.Run();
                    }

                    if (_cacheTime > 0)
                    {
                        _nextUpdate = Time.Milliseconds + _cacheTime;
                    }
                    else
                    {
                        _nextUpdate = long.MaxValue;
                    }
                }
            }

            // iterate and run the callbacks
            foreach (var callback in _callbacks)
            {
                callback.ArgA = _element.Clone();
                ManagerUpdate.Control.AddSingle(callback);
            }

            _callbacks.Clear();

            result = _element.Clone();

            _lock.Release();

            return(result);
        }