private void InvokeEngineHelper(string exportName = null, object[] args = null) { Task <bool> cachedTask = _jsService.TryInvokeFromCacheAsync(ENGINE_HELPERS_RESOURCE_NAME, exportName, args); bool success = cachedTask.ConfigureAwait(false).GetAwaiter().GetResult(); if (!success) { Type type = typeof(NodeJsEngine); Assembly assembly = type.Assembly; using (Stream resourceStream = assembly.GetManifestResourceStream(ENGINE_HELPERS_RESOURCE_NAME)) { Task task = _jsService.InvokeFromStreamAsync(resourceStream, ENGINE_HELPERS_RESOURCE_NAME, exportName, args); task.ConfigureAwait(false).GetAwaiter().GetResult(); } } }
/// <inheritdoc /> public virtual async Task <string?> HighlightAsync(string code, string languageAlias, string classPrefix = "hljs-", CancellationToken cancellationToken = default) { if (_disposed) { throw new ObjectDisposedException(nameof(HighlightJSService)); } if (code == null) { throw new ArgumentNullException(nameof(code), Strings.Exception_ParameterCannotBeNull); } if (string.IsNullOrWhiteSpace(code)) { // Nothing to highlight return(code); } if (!await IsValidLanguageAliasAsync(languageAlias, cancellationToken).ConfigureAwait(false)) { // languageAlias is invalid throw new ArgumentException(string.Format(Strings.Exception_InvalidHighlightJSLanguageAlias, languageAlias)); } object[] args = new object[] { code, languageAlias, string.IsNullOrWhiteSpace(classPrefix) ? "" : classPrefix }; // Invoke from cache (bool success, string?result) = await _nodeJSService.TryInvokeFromCacheAsync <string>(_moduleCacheIdentifier, "highlight", args, cancellationToken).ConfigureAwait(false); if (success) { return(result); } // Invoke from stream since module is not cached using Stream moduleStream = _embeddedResourcesService.ReadAsStream(typeof(HighlightJSService).GetTypeInfo().Assembly, BUNDLE_NAME); // Invoking from stream is 2+x faster than reading the resource as a string and invoking as string. This is because invoking as string causes almost // 1000x more memory to be allocated, resulting in gen 1+ gcs. return(await _nodeJSService.InvokeFromStreamAsync <string>(moduleStream, _moduleCacheIdentifier, "highlight", args, cancellationToken).ConfigureAwait(false)); }
public Task <T> Invoke <T>(string function, IMemoryOwner <char> code, CancellationToken cancellationToken = default) { var args = new object[] { code }; return(_nodeJsService.InvokeFromStreamAsync <T>( () => _embeddedResourcesService.ReadAsStream(GetType().Assembly, BUNDLENAME), MODULE_CACHE_IDENTIFIER, function, args, cancellationToken)); }