Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/AngleSharp.Js.Tests/WorkerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,23 @@ public async Task SharedWorkerOfAnotherNameIsAnother()
Assert.AreEqual("1,1", document.ExecuteScript("log.join(',')")?.ToString());
}

[Test]
public async Task SharedWorkerKeyIncludesResolvedScriptUrl()
{
var source = "var count = 0; onconnect = function (e) { count++; e.ports[0].postMessage(count); };";
var document = await OpenWithSourcesAsync(
("https://example.com/shared.js", source),
("https://example.com/other.js", source)).ConfigureAwait(false);
document.ExecuteScript(@"
window.log = [];
new SharedWorker('shared.js', 'same').port.onmessage = function (e) { log.push('a=' + e.data); };
new SharedWorker('/shared.js', 'same').port.onmessage = function (e) { log.push('b=' + e.data); };
new SharedWorker('other.js', 'same').port.onmessage = function (e) { log.push('c=' + e.data); };");

Assert.IsTrue(await WaitFor(() => document.ExecuteScript("log.length")?.ToString() == "3").ConfigureAwait(false));
Assert.AreEqual("a=1,b=2,c=1", document.ExecuteScript("log.sort().join(',')")?.ToString());
}

[Test]
public async Task UnknownWorkerTypeIsRefused()
{
Expand Down
8 changes: 4 additions & 4 deletions src/AngleSharp.Js/Dom/SharedWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public sealed class SharedWorker : EventTarget
{
#region Fields

private static readonly Dictionary<(JsScriptingService, String, String, String), Scope> Scopes = new Dictionary<(JsScriptingService, String, String, String), Scope>();
private static readonly Dictionary<Tuple<JsScriptingService, String, String, String>, Scope> Scopes = new Dictionary<Tuple<JsScriptingService, String, String, String>, Scope>();

private readonly MessagePort _port;

Expand Down Expand Up @@ -64,7 +64,7 @@ public SharedWorker(IWindow window, String source, Object options)
var scripting = document.Context.GetService<JsScriptingService>() ?? throw new DomException(DomError.NotSupported);
var url = Worker.ResolveUrl(document, source);
var name = options is WorkerOptions workerOptions ? workerOptions.Name ?? String.Empty : options?.ToString() ?? String.Empty;
var key = (scripting, document.Origin ?? String.Empty, url.Href, name);
var key = Tuple.Create(scripting, document.Origin ?? String.Empty, url.Href, name);

// the port of the window, and the one of the worker, which gets its document once started
var gate = new Object();
Expand Down Expand Up @@ -130,15 +130,15 @@ private sealed class Scope
}
})();";

private readonly (JsScriptingService, String, String, String) _key;
private readonly Tuple<JsScriptingService, String, String, String> _key;
private readonly JsScriptingService _scripting;
private readonly IEventLoop _loop;
private readonly IBrowsingContext _context;
private readonly List<IWindow> _windows = new List<IWindow>();
private IDocument _document;
private Exception _startupError;

public Scope((JsScriptingService, String, String, String) key, IDocument creator, JsScriptingService scripting, Url url)
public Scope(Tuple<JsScriptingService, String, String, String> key, IDocument creator, JsScriptingService scripting, Url url)
{
_key = key;
_scripting = scripting;
Expand Down