public void Help(ScriptExpression expression = null) { var name = expression?.ToString(); if (name != null) { if (Descriptors.TryGetValue(name, out var descriptor)) { WriteHelpForDescriptor(descriptor); return; } throw new ArgumentException($"The builtin function `{name}` does not exist", nameof(expression)); } WriteHighlightLine($"# help [name]"); // Verify that all function/modules belong to a category var invalidRegistered = Descriptors.FirstOrDefault(x => x.Value.Category == null); if (invalidRegistered.Value != null) { throw new InvalidOperationException($"The function or module `{invalidRegistered.Key}` doesn't have a category. This is an invalid state of the program"); } var categoryToDescriptors = Descriptors.GroupBy(x => x.Value.Category).ToDictionary(x => x.Key, y => y.Select(x => x.Value).Distinct().ToList()); WriteHighlightLine($"#"); foreach (var categoryPair in categoryToDescriptors.OrderBy(x => x.Key)) { var list = categoryPair.Value; // Exclude from the list modules that have been already imported var names = list.SelectMany(x => x.Names).Where(funcName => Builtins.TryGetValue(funcName, out var funcObj) && (!(funcObj is KalkModuleWithFunctions module) || !module.IsImported) ).OrderBy(x => x).ToList(); if (names.Count > 0) { WriteHighlightLine($"# {categoryPair.Key}"); WriteHighlightAligned(" - ", string.Join(", ", names)); WriteHighlightLine(""); } } }
/// <summary> /// Gets service descriptor for re-use from already created services list /// </summary> private PoolServiceDescriptor <TService> GetFromCreatedItem(IContainerScope scope) { lock (Descriptors) { if (Type == ImplementationType.Scoped) { PoolServiceDescriptor <TService> scoped = Descriptors.FirstOrDefault(x => x.Scope == scope); if (scoped != null) { if (Options.IdleTimeout > TimeSpan.Zero) { scoped.IdleTimeout = DateTime.UtcNow + Options.IdleTimeout; } else { scoped.IdleTimeout = null; } } return(scoped); } PoolServiceDescriptor <TService> transient = Descriptors.FirstOrDefault(x => !x.Locked || x.LockExpiration < DateTime.UtcNow); if (transient == null) { return(null); } transient.Scope = scope; transient.Locked = true; transient.LockExpiration = DateTime.UtcNow.Add(Options.MaximumLockDuration); if (Options.IdleTimeout > TimeSpan.Zero) { transient.IdleTimeout = DateTime.UtcNow + Options.IdleTimeout; } else { transient.IdleTimeout = null; } return(transient); } }