Skip to content
Open
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
22 changes: 22 additions & 0 deletions src/AngleSharp.Js.Tests/ConstructorTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System.Linq;
using AngleSharp.Dom;

namespace AngleSharp.Js.Tests
{
using NUnit.Framework;
Expand Down Expand Up @@ -35,5 +38,24 @@ public async Task CustomEventConstructedWithBubblesTrue()
var result = document.QuerySelector("#result").TextContent;
Assert.AreEqual("true", result);
}

[Test]
public void CustomEventConstructedConcurrently()
{
var ctx1 = BrowsingContext.New(Configuration.Default.WithJs());
var ctx2 = BrowsingContext.New(Configuration.Default.WithJs());
var html = "<!doctype html><div id=result></div><script>var ev = new CustomEvent('foo'); document.querySelector('#result').textContent = ev.type;</script>";
Parallel.Invoke(() => Assert(ctx1), () => Assert(ctx2));
return;

void Assert(IBrowsingContext context) => Task
.Run(async () =>
{
var document = await context.OpenAsync(m => m.Content(html));
var result = document.QuerySelector("#result").TextContent;
NUnit.Framework.Assert.AreEqual("foo", result);
})
.Wait();
}
}
}
14 changes: 7 additions & 7 deletions src/AngleSharp.Js/Cache/CreatorCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
using Jint.Native.Object;
using Jint.Runtime.Descriptors;
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Reflection;

namespace AngleSharp.Js.Cache
{
static class CreatorCache
{
private static readonly Dictionary<Type, Action<EngineInstance, ObjectInstance>> _constructorActions = new Dictionary<Type, Action<EngineInstance, ObjectInstance>>();
private static readonly ConcurrentDictionary<Type, Action<EngineInstance, ObjectInstance>> _constructorActions = new();

public static Action<EngineInstance, ObjectInstance> GetConstructorAction(this Type type)
{
Expand All @@ -36,13 +36,13 @@ public static Action<EngineInstance, ObjectInstance> GetConstructorAction(this T
action = (e, o) => { };
}

_constructorActions.Add(type, action);
_constructorActions.TryAdd(type, action);
}

return action;
}

private static readonly Dictionary<Type, Action<EngineInstance, ObjectInstance>> _constructorFunctionActions = new Dictionary<Type, Action<EngineInstance, ObjectInstance>>();
private static readonly ConcurrentDictionary<Type, Action<EngineInstance, ObjectInstance>> _constructorFunctionActions = new();

public static Action<EngineInstance, ObjectInstance> GetConstructorFunctionAction(this Type type)
{
Expand All @@ -69,13 +69,13 @@ public static Action<EngineInstance, ObjectInstance> GetConstructorFunctionActio
action = (e, o) => { };
}

_constructorFunctionActions.Add(type, action);
_constructorFunctionActions.TryAdd(type, action);
}

return action;
}

private static readonly Dictionary<Type, Action<EngineInstance, ObjectInstance>> _instanceActions = new Dictionary<Type, Action<EngineInstance, ObjectInstance>>();
private static readonly ConcurrentDictionary<Type, Action<EngineInstance, ObjectInstance>> _instanceActions = new();

public static Action<EngineInstance, ObjectInstance> GetInstanceAction(this Type type)
{
Expand Down Expand Up @@ -105,7 +105,7 @@ public static Action<EngineInstance, ObjectInstance> GetInstanceAction(this Type
action = (e, o) => { };
}

_instanceActions.Add(type, action);
_instanceActions.TryAdd(type, action);
}

return action;
Expand Down