Exemplo n.º 1
0
    // Internal for testing.
    internal async Task RunAsyncCore(CancellationToken cancellationToken, WebAssemblyCultureProvider?cultureProvider = null)
    {
        if (_started)
        {
            throw new InvalidOperationException("The host has already started.");
        }

        _started = true;

        cultureProvider ??= WebAssemblyCultureProvider.Instance !;
        cultureProvider.ThrowIfCultureChangeIsUnsupported();

        // Application developers might have configured the culture based on some ambient state
        // such as local storage, url etc as part of their Program.Main(Async).
        // This is the earliest opportunity to fetch satellite assemblies for this selection.
        await cultureProvider.LoadCurrentCultureResourcesAsync();

        var manager = Services.GetRequiredService <ComponentStatePersistenceManager>();
        var store   = !string.IsNullOrEmpty(_persistedState) ?
                      new PrerenderComponentApplicationStore(_persistedState) :
                      new PrerenderComponentApplicationStore();

        await manager.RestoreStateAsync(store);

        if (MetadataUpdater.IsSupported)
        {
            await WebAssemblyHotReload.InitializeAsync();
        }

        var tcs = new TaskCompletionSource();

        using (cancellationToken.Register(() => tcs.TrySetResult()))
        {
            var loggerFactory      = Services.GetRequiredService <ILoggerFactory>();
            var jsComponentInterop = new JSComponentInterop(_rootComponents.JSComponents);
            _renderer = new WebAssemblyRenderer(Services, loggerFactory, jsComponentInterop);

            WebAssemblyNavigationManager.Instance.CreateLogger(loggerFactory);

            var initializationTcs = new TaskCompletionSource();
            WebAssemblyCallQueue.Schedule((_rootComponents, _renderer, initializationTcs), static async state =>
Exemplo n.º 2
0
    public PageContext(
        Dispatcher dispatcher,
        AsyncServiceScope serviceScope,
        IpcSender ipcSender,
        JSComponentConfigurationStore jsComponentsConfiguration,
        string baseUrl,
        string startUrl)
    {
        _serviceScope = serviceScope;
        var services = serviceScope.ServiceProvider;

        NavigationManager = (WebViewNavigationManager)services.GetRequiredService <NavigationManager>();
        NavigationManager.AttachToWebView(ipcSender, baseUrl, startUrl);

        JSRuntime = (WebViewJSRuntime)services.GetRequiredService <IJSRuntime>();
        JSRuntime.AttachToWebView(ipcSender);

        var loggerFactory = services.GetRequiredService <ILoggerFactory>();
        var jsComponents  = new JSComponentInterop(jsComponentsConfiguration);

        Renderer = new WebViewRenderer(services, dispatcher, ipcSender, loggerFactory, JSRuntime, jsComponents);
    }
Exemplo n.º 3
0
        /// <summary>
        /// Constructs an instance of <see cref="WebRenderer"/>.
        /// </summary>
        /// <param name="serviceProvider">The <see cref="IServiceProvider"/> to be used when initializing components.</param>
        /// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
        /// <param name="jsonOptions">The <see cref="JsonSerializerOptions"/>.</param>
        /// <param name="jsComponentInterop">The <see cref="JSComponentInterop"/>.</param>
        public WebRenderer(
            IServiceProvider serviceProvider,
            ILoggerFactory loggerFactory,
            JsonSerializerOptions jsonOptions,
            JSComponentInterop jsComponentInterop)
            : base(serviceProvider, loggerFactory)
        {
            _serviceProvider         = serviceProvider;
            _interopMethodsReference = DotNetObjectReference.Create(
                new WebRendererInteropMethods(this, jsonOptions, jsComponentInterop));

            // Supply a DotNetObjectReference to JS that it can use to call us back for events etc.
            jsComponentInterop.AttachToRenderer(this);
            var jsRuntime = _serviceProvider.GetRequiredService <IJSRuntime>();

            jsRuntime.InvokeVoidAsync(
                "Blazor._internal.attachWebRendererInterop",
                RendererId,
                _interopMethodsReference,
                jsComponentInterop.Configuration.JSComponentParametersByIdentifier,
                jsComponentInterop.Configuration.JSComponentIdentifiersByInitializer).Preserve();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Enables support for adding, updating, and removing root components from JavaScript.
        /// </summary>
        /// <param name="interop">The object that provides JS-callable methods for adding, updating, and removing root components.</param>
        /// <returns>A task representing the completion of the operation.</returns>
        protected ValueTask InitializeJSComponentSupportAsync(JSComponentInterop interop)
        {
            var jsRuntime = _serviceProvider.GetRequiredService <IJSRuntime>();

            return(interop.InitializeAsync(jsRuntime, this));
        }
Exemplo n.º 5
0
 public WebRendererInteropMethods(WebRenderer renderer, JsonSerializerOptions jsonOptions, JSComponentInterop jsComponentInterop)
 {
     _renderer           = renderer;
     _jsonOptions        = jsonOptions;
     _jsComponentInterop = jsComponentInterop;
 }
Exemplo n.º 6
0
    public WebAssemblyRenderer(IServiceProvider serviceProvider, ILoggerFactory loggerFactory, JSComponentInterop jsComponentInterop)
        : base(serviceProvider, loggerFactory, DefaultWebAssemblyJSRuntime.Instance.ReadJsonSerializerOptions(), jsComponentInterop)
    {
        // The WebAssembly renderer registers and unregisters itself with the static registry
        RendererId = RendererRegistry.Add(this);
        _logger    = loggerFactory.CreateLogger <WebAssemblyRenderer>();

        ElementReferenceContext = DefaultWebAssemblyJSRuntime.Instance.ElementReferenceContext;
    }
Exemplo n.º 7
0
        public ValueTask InitializeJSComponentSupportAsync(JSComponentConfigurationStore configuration, JsonSerializerOptions jsonOptions)
        {
            var interop = new JSComponentInterop(configuration, jsonOptions);

            return(InitializeJSComponentSupportAsync(interop));
        }