Ejemplo n.º 1
0
        /// <summary>
        /// Tries to execute a code from embedded JavaScript resource with pre-compilation.
        /// </summary>
        /// <param name="engine">Engine to execute a code from embedded JavaScript resource with pre-compilation</param>
        /// <param name="cache">Cache used for storing the pre-compiled scripts</param>
        /// <param name="resourceName">The case-sensitive resource name</param>
        /// <param name="assembly">The assembly, which contains the embedded resource</param>
        /// <returns>true if can perform a script pre-compilation; otherwise, false.</returns>
        public static bool TryExecuteResourceWithPrecompilation(this IJsEngine engine, ICache cache,
                                                                string resourceName, Assembly assembly)
        {
            if (!CheckPrecompilationAvailability(engine, cache))
            {
                return(false);
            }

            var cacheKey          = string.Format(PRECOMPILED_JS_RESOURCE_CACHE_KEY, resourceName);
            var precompiledScript = cache.Get <IPrecompiledScript>(cacheKey);

            if (precompiledScript == null)
            {
                precompiledScript = engine.PrecompileResource(resourceName, assembly);
                cache.Set(
                    cacheKey,
                    precompiledScript,
                    slidingExpiration: PRECOMPILED_JS_CACHE_ENTRY_SLIDING_EXPIRATION
                    );
            }

            engine.Execute(precompiledScript);

            return(true);
        }