Skip to content

Commit 68ecda2

Browse files
committed
Use GUIDs for container IDs, rather than sequential IDs. Closes #230
1 parent b432a4f commit 68ecda2

File tree

8 files changed

+85
-19
lines changed

8 files changed

+85
-19
lines changed

src/React.Core/GuidExtensions.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2016-Present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
using System;
11+
12+
namespace React
13+
{
14+
/// <summary>
15+
/// Extension methods relating to GUIDs.
16+
/// </summary>
17+
public static class GuidExtensions
18+
{
19+
/// <summary>
20+
/// Returns a short identifier for this GUID.
21+
/// </summary>
22+
/// <param name="guid">The unique identifier.</param>
23+
/// <returns>A short version of the unique identifier</returns>
24+
public static string ToShortGuid(this Guid guid)
25+
{
26+
return Convert.ToBase64String(guid.ToByteArray())
27+
.Replace("/", string.Empty)
28+
.Replace("+", string.Empty)
29+
.TrimEnd('=');
30+
}
31+
}
32+
}

src/React.Core/React.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
<Compile Include="Exceptions\BabelNotLoadedException.cs" />
9999
<Compile Include="Exceptions\ReactNotInitialisedException.cs" />
100100
<Compile Include="FileSystemExtensions.cs" />
101+
<Compile Include="GuidExtensions.cs" />
101102
<Compile Include="JavaScriptEngineUtils.cs" />
102103
<Compile Include="VroomJsEngine.cs" />
103104
<Compile Include="Exceptions\ClearScriptV8InitialisationException.cs" />

src/React.Core/ReactComponent.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* of patent rights can be found in the PATENTS file in the same directory.
88
*/
99

10+
using System;
1011
using System.Linq;
1112
using System.Text.RegularExpressions;
1213
using JavaScriptEngineSwitcher.Core;
@@ -75,7 +76,7 @@ public ReactComponent(IReactEnvironment environment, IReactSiteConfiguration con
7576
_environment = environment;
7677
_configuration = configuration;
7778
ComponentName = componentName;
78-
ContainerId = containerId;
79+
ContainerId = string.IsNullOrEmpty(containerId) ? GenerateId() : containerId;
7980
ContainerTag = "div";
8081
}
8182

@@ -188,5 +189,14 @@ internal static void EnsureComponentNameValid(string componentName)
188189
));
189190
}
190191
}
192+
193+
/// <summary>
194+
/// Generates a unique identifier for this component, if one was not passed in.
195+
/// </summary>
196+
/// <returns></returns>
197+
private static string GenerateId()
198+
{
199+
return "react_" + Guid.NewGuid().ToShortGuid();
200+
}
191201
}
192202
}

src/React.Core/ReactEnvironment.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ namespace React
2626
/// </summary>
2727
public class ReactEnvironment : IReactEnvironment, IDisposable
2828
{
29-
/// <summary>
30-
/// Format string used for React component container IDs
31-
/// </summary>
32-
protected const string CONTAINER_ELEMENT_NAME = "react{0}";
33-
3429
/// <summary>
3530
/// JavaScript variable set when user-provided scripts have been loaded
3631
/// </summary>
@@ -75,10 +70,6 @@ public class ReactEnvironment : IReactEnvironment, IDisposable
7570
/// </summary>
7671
protected readonly Lazy<IJsEngine> _engineFromPool;
7772

78-
/// <summary>
79-
/// Number of components instantiated in this environment
80-
/// </summary>
81-
protected int _maxContainerId = 0;
8273
/// <summary>
8374
/// List of all components instantiated in this environment
8475
/// </summary>
@@ -269,12 +260,6 @@ public virtual bool HasVariable(string name)
269260
public virtual IReactComponent CreateComponent<T>(string componentName, T props, string containerId = null)
270261
{
271262
EnsureUserScriptsLoaded();
272-
if (string.IsNullOrEmpty(containerId))
273-
{
274-
_maxContainerId++;
275-
containerId = string.Format(CONTAINER_ELEMENT_NAME, _maxContainerId);
276-
}
277-
278263
var component = new ReactComponent(this, _config, componentName, containerId)
279264
{
280265
Props = props
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2016-Present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
using System;
11+
using NUnit.Framework;
12+
13+
namespace React.Tests.Core
14+
{
15+
[TestFixture]
16+
public class GuidExtensionsTests
17+
{
18+
[TestCase]
19+
public void ToShortGuid()
20+
{
21+
var guid = Guid.Parse("c027191d-3785-485d-9fd7-5e0b376bd547");
22+
Assert.AreEqual("HRknwIU3XUif114LN2vVRw", guid.ToShortGuid());
23+
}
24+
}
25+
}

src/React.Tests/Core/ReactComponentTest.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,5 +173,17 @@ public void TestEnsureComponentNameValid(string input, bool expected)
173173
}
174174
Assert.AreEqual(expected, isValid);
175175
}
176+
177+
178+
[Test]
179+
public void GeneratesContainerIdIfNotProvided()
180+
{
181+
var environment = new Mock<IReactEnvironment>();
182+
var config = new Mock<IReactSiteConfiguration>();
183+
184+
var component = new ReactComponent(environment.Object, config.Object, "Foo", null);
185+
StringAssert.StartsWith("react_", component.ContainerId);
186+
}
187+
176188
}
177189
}

src/React.Tests/Core/ReactEnvironmentTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ public void GeneratesContainerIdIfNotProvided()
9191

9292
var component1 = environment.CreateComponent("ComponentName", new { });
9393
var component2 = environment.CreateComponent("ComponentName", new { });
94-
Assert.AreEqual("react1", component1.ContainerId);
95-
Assert.AreEqual("react2", component2.ContainerId);
94+
StringAssert.StartsWith("react_", component1.ContainerId);
95+
StringAssert.StartsWith("react_", component2.ContainerId);
9696
}
9797

9898
[Test]
@@ -105,7 +105,7 @@ public void UsesProvidedContainerId()
105105
var component1 = environment.CreateComponent("ComponentName", new { }, "foo");
106106
var component2 = environment.CreateComponent("ComponentName", new { });
107107
Assert.AreEqual("foo", component1.ContainerId);
108-
Assert.AreEqual("react1", component2.ContainerId);
108+
StringAssert.StartsWith("react_", component2.ContainerId);
109109
}
110110

111111
private class Mocks

src/React.Tests/React.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
<Compile Include="Core\FileCacheHashTests.cs" />
100100
<Compile Include="Core\FileSystemBaseTests.cs" />
101101
<Compile Include="Core\FileSystemExtensionsTest.cs" />
102+
<Compile Include="Core\GuidExtensionsTests.cs" />
102103
<Compile Include="Core\JavaScriptEngineFactoryTest.cs" />
103104
<Compile Include="Core\JavaScriptEngineUtilsTests.cs" />
104105
<Compile Include="Core\BabelTransformerTests.cs" />

0 commit comments

Comments
 (0)