public void Start()
        {
            getStorage = setupStorageAsync();

            monitor.ManifestChanged += async () =>
            {
                var storage = await GetStorageAsync();
                var contentsUpdated = await updateStorageAsync(storage);

                onStorageUpdated();
                if (contentsUpdated)
                {
                    onStorageContentsUpdated();
                }
            };

            monitor.Start();

            async Task<ManifestStorage> setupStorageAsync()
            {
                var storage = new ManifestStorage();
                var manifest = await reader.ReadAsync();

                // if we've failed to retrieve asset manifest
                // in the very beginning, on the application
                // startup, then there is no way to go further
                // so we're throwing the exception
                if (manifest == null)
                {
                    var message = "Failed to retrieve webpack asset manifest. " +
                        $"File path: '{context.ManifestPhysicalPath}'. " +
                        "Check out the file exists and it's a valid json asset manifest";

                    logger.LogError(message);

                    throw new WebpackException(message);
                }

                // set up the manifest storage
                storage.Setup(manifest);
                logger.LogInformation(
                    "Webpack asset manifest storage has been set up. " +
                    $"Keys: ({keysFormatted(manifest.Keys)})"
                );

                return storage;
            }

            async Task<bool> updateStorageAsync(ManifestStorage storage)
            {
                var manifest = await reader.ReadAsync();

                // it's normal if we are failed to read the manifest
                // during the file updates. In this case we're keeping
                // the storage untouched and waiting for the next update
                if (manifest == null)
                {
                    logger.LogDebug($"Webpack asset manifest storage can not be updated now");
                    return false;
                }

                // update the storage
                var updatedKeys = storage.Update(manifest);

                // log if we have any updated or added records
                if (updatedKeys.Any())
                {
                    logger.LogInformation(
                        "Webpack asset manifest storage has been updated. " +
                        $"Updated keys: ({keysFormatted(updatedKeys)})"
                    );

                    return true;
                }

                return false;
            }

            void onStorageUpdated()
            {
                var callback = StorageUpdated;
                if (callback != null)
                {
                    callback.Invoke();
                }
            }

            void onStorageContentsUpdated()
            {
                var callback = StorageContentsUpdated;
                if (callback != null)
                {
                    callback.Invoke();
                }
            }

            string keysFormatted(IEnumerable<string> keys) => string.Join(" ", keys);
        }
Ejemplo n.º 2
0
        public void Start()
        {
            setupStorage().Wait();

            // run a background job which checks the manifest file
            // for changes and updates the storage
            Task.Run(async() =>
            {
                while (true)
                {
                    if (await monitor.WaitForChangesAsync())
                    {
                        await updateStorage();
                    }
                }
            });

            async Task setupStorage()
            {
                var manifest = await reader.ReadAsync();

                // if we've failed to retrieve asset manifest
                // in the very beginning, on the application
                // startup, then there is no way to go further
                // so we're throwing the exception
                if (manifest == null)
                {
                    var message = "Failed to retrieve webpack asset manifest. " +
                                  $"File path: '{context.ManifestPhysicalPath}'. " +
                                  "Check out the file exists and it's a valid json asset manifest";

                    logger.LogError(message);

                    throw new WebpackException(message);
                }

                // set up the manifest storage
                storage.Setup(manifest);
                logger.LogInformation(
                    "Webpack asset manifest storage has been set up. " +
                    $"Keys: ({keysFormatted(manifest.Keys)})"
                    );
            }

            async Task updateStorage()
            {
                var manifest = await reader.ReadAsync();

                // it's normal if we are failed to read the manifest
                // during the file updates. In this case we're keeping
                // the storage untouched and waiting for the next update
                if (manifest == null)
                {
                    logger.LogDebug($"Webpack asset manifest storage can not be updated now");
                    return;
                }

                // update the storage
                var updatedKeys = storage.Update(manifest);

                // log if we have any updated or added records
                if (updatedKeys.Any())
                {
                    logger.LogInformation(
                        "Webpack asset manifest storage has been updated. " +
                        $"Updated keys: ({keysFormatted(updatedKeys)})"
                        );
                }
            }

            string keysFormatted(IEnumerable <string> keys) => string.Join(" ", keys);
        }