Exemplo n.º 1
0
        private static void Get <C>(string path, Act <C> onOpen, bool tryLock) where C : Connection <C>, new()
        {
            // get the lock
            if (tryLock && !(tryLock = _lock.TryTake))
            {
                _lock.TryLock(new Act <string, Act <C>, bool>(Get <C>, path, onOpen, false));
                return;
            }

            Type sourceType = typeof(C);
            ArrayRig <IConnection> connections;

            if (!_sources.TryGetValue(sourceType, out connections))
            {
                _sources[sourceType] = connections = new ArrayRig <IConnection>();
            }

            // try get the connection
            C connection = connections.GetSingle(c => c.Path == path) as C;

            if (connection == null)
            {
                // connection doesn't exist, create it
                connection      = onOpen.ArgA = new C();
                connection.Path = path;
                _sources[sourceType].Add(connection);
                connection.Open(onOpen);
            }
            else
            {
                onOpen.ArgA = connection;
                if (connection.State.Is(ConnectionState.Closed))
                {
                    connection.Open(onOpen);
                }
                else
                {
                    onOpen.Run();
                }
            }

            // if manually locked, unlock
            if (tryLock)
            {
                _lock.Release();
            }
        }
Exemplo n.º 2
0
        private static void LoadNode(Act <Node> onLoad, string path, string[] keys, bool takeLock)
        {
            // get the lock
            if (takeLock && !(takeLock = _lock.TryTake))
            {
                _lock.TryLock(new Act <Act <Node>, string, string[], bool>(LoadNode, onLoad, path, keys, false));
                return;
            }

            // get the configuration referencer
            WeakReferencer <Configuration> reference;

            if (!_configurations.TryGetValue(path, out reference))
            {
                reference = new WeakReferencer <Configuration>(new FuncSet <string, Configuration>(GetConfiguration, path));
                _configurations.Add(path, reference);
            }

            Configuration config = reference.Item;

            // check if configuration was loaded
            if (config.State == AssetState.Loaded)
            {
                onLoad.ArgA = config.Node[keys];
                onLoad.Run();
            }
            else
            {
                // the task will retrieve the node and run the on load function when the configuration loads
                config.Load(new Act <Configuration>(c => {
                    onLoad.ArgA = c.Node[keys];
                    onLoad.Action.Run();
                }, onLoad.Needle));
            }

            // unlock if locked
            if (takeLock)
            {
                _lock.Release();
            }
        }