Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 0 additions & 32 deletions src/React.Core/GuidExtensions.cs

This file was deleted.

11 changes: 1 addition & 10 deletions src/React.Core/ReactComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public ReactComponent(IReactEnvironment environment, IReactSiteConfiguration con
_environment = environment;
_configuration = configuration;
ComponentName = componentName;
ContainerId = string.IsNullOrEmpty(containerId) ? GenerateId() : containerId;
ContainerId = string.IsNullOrEmpty(containerId) ? ReactIdGenerator.Generate() : containerId;
ContainerTag = "div";
}

Expand Down Expand Up @@ -229,14 +229,5 @@ internal static void EnsureComponentNameValid(string componentName)
));
}
}

/// <summary>
/// Generates a unique identifier for this component, if one was not passed in.
/// </summary>
/// <returns></returns>
private static string GenerateId()
{
return "react_" + Guid.NewGuid().ToShortGuid();
}
}
}
62 changes: 62 additions & 0 deletions src/React.Core/ReactIdGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2016-Present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

using System;
using System.Threading;

namespace React
{
/// <summary>
/// Extension methods relating to GUIDs.
/// </summary>
public static class ReactIdGenerator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to add an interface for this, and use the dependency injection framework to inject it into ReactComponent. That way, anyone can swap out the implementation if they want to (eg. if they want the old one back).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(see AssemblyRegistration.cs, use .AsSingleton())

Copy link
Contributor Author

@DaniilSokolyuk DaniilSokolyuk Apr 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice idea, i can inject it to ReactEnvironment and create property IReactIdGenerator (like IBabel) and call it from constructor in ReactComponent like "environment.ReactIdGenerator.Generate(...)"

{
private static readonly string _encode32Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUV";

private static long _random = DateTime.UtcNow.Ticks;

private static readonly char[] reactPrefix = "react_".ToCharArray();

[ThreadStatic]
private static char[] _chars;

/// <summary>
/// Returns a short react identifier starts with "react_".
/// </summary>
/// <returns></returns>
public static string Generate()
{
var chars = _chars;
if (chars == null)
{
_chars = chars = new char[19];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 19?

Array.Copy(reactPrefix, 0, chars, 0, reactPrefix.Length);
}

var id = Interlocked.Increment(ref _random);

//reactPrefix.Length == 6
chars[6] = _encode32Chars[(int)(id >> 60) & 31];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a lot of magic numbers in this code. It'd be good to pull these out as constants and add comments explaining what they're doing.

chars[7] = _encode32Chars[(int)(id >> 55) & 31];
chars[8] = _encode32Chars[(int)(id >> 50) & 31];
chars[9] = _encode32Chars[(int)(id >> 45) & 31];
chars[10] = _encode32Chars[(int)(id >> 40) & 31];
chars[11] = _encode32Chars[(int)(id >> 35) & 31];
chars[12] = _encode32Chars[(int)(id >> 30) & 31];
chars[13] = _encode32Chars[(int)(id >> 25) & 31];
chars[14] = _encode32Chars[(int)(id >> 20) & 31];
chars[15] = _encode32Chars[(int)(id >> 15) & 31];
chars[16] = _encode32Chars[(int)(id >> 10) & 31];
chars[17] = _encode32Chars[(int)(id >> 5) & 31];
chars[18] = _encode32Chars[(int)id & 31];

return new string(chars, 0, 19);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull out 19 as a constant

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok!, const will be inlined without overhead 👍

}
}
}
24 changes: 0 additions & 24 deletions tests/React.Tests/Core/GuidExtensionsTests.cs

This file was deleted.

42 changes: 42 additions & 0 deletions tests/React.Tests/Core/ReactIdGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2016-Present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

using System;
using Xunit;

namespace React.Tests.Core
{
public class ReactIdGeneratorTests
{
[Fact]
public void GuidShouldHaveFixedRange()
{
var shortGuid = ReactIdGenerator.Generate();

Assert.Equal(19, shortGuid.Length);
}

[Fact]
public void ShouldStartsWithReact()
{
var shortGuid = ReactIdGenerator.Generate();

Assert.StartsWith("react_", shortGuid);
}

[Fact]
public void TwoGuidsShouldNotEqual()
{
var shortGuid1 = ReactIdGenerator.Generate();
var shortGuid2 = ReactIdGenerator.Generate();

Assert.NotEqual(shortGuid1, shortGuid2);
}
}
}