|
| 1 | +// * MIT License |
| 2 | +// * |
| 3 | +// * Copyright (c) Darío Kondratiuk |
| 4 | +// * |
| 5 | +// * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// * of this software and associated documentation files (the "Software"), to deal |
| 7 | +// * in the Software without restriction, including without limitation the rights |
| 8 | +// * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// * copies of the Software, and to permit persons to whom the Software is |
| 10 | +// * furnished to do so, subject to the following conditions: |
| 11 | +// * |
| 12 | +// * The above copyright notice and this permission notice shall be included in all |
| 13 | +// * copies or substantial portions of the Software. |
| 14 | +// * |
| 15 | +// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// * SOFTWARE. |
| 22 | + |
| 23 | +using System.Threading.Tasks; |
| 24 | +using PuppeteerSharp.Bidi.Core; |
| 25 | +using PuppeteerSharp.Helpers; |
| 26 | + |
| 27 | +namespace PuppeteerSharp.Bidi; |
| 28 | + |
| 29 | +internal class BidiWorkerRealm : BidiRealm |
| 30 | +{ |
| 31 | + private readonly DedicatedWorkerRealm _realm; |
| 32 | + private readonly BidiWebWorker _worker; |
| 33 | + private readonly TaskQueue _puppeteerUtilQueue = new(); |
| 34 | + private IJSHandle _puppeteerUtil; |
| 35 | + |
| 36 | + private BidiWorkerRealm(DedicatedWorkerRealm realm, BidiWebWorker worker) |
| 37 | + : base(realm, worker.TimeoutSettings) |
| 38 | + { |
| 39 | + _realm = realm; |
| 40 | + _worker = worker; |
| 41 | + } |
| 42 | + |
| 43 | + internal override IEnvironment Environment => _worker; |
| 44 | + |
| 45 | + internal BidiWebWorker Worker => _worker; |
| 46 | + |
| 47 | + public static BidiWorkerRealm From(DedicatedWorkerRealm realm, BidiWebWorker worker) |
| 48 | + { |
| 49 | + var workerRealm = new BidiWorkerRealm(realm, worker); |
| 50 | + workerRealm.Initialize(); |
| 51 | + return workerRealm; |
| 52 | + } |
| 53 | + |
| 54 | + public override async Task<IJSHandle> GetPuppeteerUtilAsync() |
| 55 | + { |
| 56 | + var scriptInjector = _realm.Session.ScriptInjector; |
| 57 | + |
| 58 | + await _puppeteerUtilQueue.Enqueue(async () => |
| 59 | + { |
| 60 | + if (_puppeteerUtil == null) |
| 61 | + { |
| 62 | + await scriptInjector.InjectAsync( |
| 63 | + async (script) => |
| 64 | + { |
| 65 | + if (_puppeteerUtil != null) |
| 66 | + { |
| 67 | + await _puppeteerUtil.DisposeAsync().ConfigureAwait(false); |
| 68 | + } |
| 69 | + |
| 70 | + _puppeteerUtil = await EvaluateExpressionHandleAsync(script).ConfigureAwait(false); |
| 71 | + }, |
| 72 | + _puppeteerUtil == null).ConfigureAwait(false); |
| 73 | + } |
| 74 | + }).ConfigureAwait(false); |
| 75 | + |
| 76 | + return _puppeteerUtil; |
| 77 | + } |
| 78 | + |
| 79 | + protected override void Initialize() |
| 80 | + { |
| 81 | + base.Initialize(); |
| 82 | + |
| 83 | + _realm.Destroyed += (sender, args) => Dispose(); |
| 84 | + _realm.Updated += (sender, args) => |
| 85 | + { |
| 86 | + // Reset PuppeteerUtil when the realm is updated |
| 87 | + _puppeteerUtil = null; |
| 88 | + TaskManager.RerunAll(); |
| 89 | + }; |
| 90 | + } |
| 91 | +} |
0 commit comments