@@ -24,25 +24,43 @@ internal sealed class TypeMapper : IDisposable
24
24
/// <summary>
25
25
/// Storage for lazy-initialized embedded objects
26
26
/// </summary>
27
- private ConcurrentDictionary < EmbeddedObjectKey , Lazy < EmbeddedObject > > _lazyEmbeddedObjects =
28
- new ConcurrentDictionary < EmbeddedObjectKey , Lazy < EmbeddedObject > > ( ) ;
27
+ private ConcurrentDictionary < EmbeddedObjectKey , Lazy < EmbeddedObject > > _lazyEmbeddedObjects ;
29
28
30
29
/// <summary>
31
30
/// Callback for finalization of embedded object
32
31
/// </summary>
33
32
private JsFinalizeCallback _embeddedObjectFinalizeCallback ;
34
33
34
+ /// <summary>
35
+ /// Synchronizer of embedded object storage's initialization
36
+ /// </summary>
37
+ private readonly object _embeddedObjectStorageInitializationSynchronizer = new object ( ) ;
38
+
39
+ /// <summary>
40
+ /// Flag indicating whether the embedded object storage is initialized
41
+ /// </summary>
42
+ private bool _embeddedObjectStorageInitialized ;
43
+
35
44
/// <summary>
36
45
/// Storage for lazy-initialized embedded types
37
46
/// </summary>
38
- private ConcurrentDictionary < string , Lazy < EmbeddedType > > _lazyEmbeddedTypes =
39
- new ConcurrentDictionary < string , Lazy < EmbeddedType > > ( ) ;
47
+ private ConcurrentDictionary < string , Lazy < EmbeddedType > > _lazyEmbeddedTypes ;
40
48
41
49
/// <summary>
42
50
/// Callback for finalization of embedded type
43
51
/// </summary>
44
52
private JsFinalizeCallback _embeddedTypeFinalizeCallback ;
45
53
54
+ /// <summary>
55
+ /// Synchronizer of embedded type storage's initialization
56
+ /// </summary>
57
+ private readonly object _embeddedTypeStorageInitializationSynchronizer = new object ( ) ;
58
+
59
+ /// <summary>
60
+ /// Flag indicating whether the embedded type storage is initialized
61
+ /// </summary>
62
+ private bool _embeddedTypeStorageInitialized ;
63
+
46
64
/// <summary>
47
65
/// Flag indicating whether this object is disposed
48
66
/// </summary>
@@ -53,10 +71,7 @@ internal sealed class TypeMapper : IDisposable
53
71
/// Constructs an instance of type mapper
54
72
/// </summary>
55
73
public TypeMapper ( )
56
- {
57
- _embeddedObjectFinalizeCallback = EmbeddedObjectFinalizeCallback ;
58
- _embeddedTypeFinalizeCallback = EmbeddedTypeFinalizeCallback ;
59
- }
74
+ { }
60
75
61
76
62
77
/// <summary>
@@ -66,6 +81,20 @@ public TypeMapper()
66
81
/// <returns>JavaScript value created from an host object</returns>
67
82
public JsValue GetOrCreateScriptObject ( object obj )
68
83
{
84
+ if ( ! _embeddedObjectStorageInitialized )
85
+ {
86
+ lock ( _embeddedObjectStorageInitializationSynchronizer )
87
+ {
88
+ if ( ! _embeddedObjectStorageInitialized )
89
+ {
90
+ _lazyEmbeddedObjects = new ConcurrentDictionary < EmbeddedObjectKey , Lazy < EmbeddedObject > > ( ) ;
91
+ _embeddedObjectFinalizeCallback = EmbeddedObjectFinalizeCallback ;
92
+
93
+ _embeddedObjectStorageInitialized = true ;
94
+ }
95
+ }
96
+ }
97
+
69
98
var embeddedObjectKey = new EmbeddedObjectKey ( obj ) ;
70
99
EmbeddedObject embeddedObject = _lazyEmbeddedObjects . GetOrAdd (
71
100
embeddedObjectKey ,
@@ -82,6 +111,20 @@ public JsValue GetOrCreateScriptObject(object obj)
82
111
/// <returns>JavaScript value created from an host type</returns>
83
112
public JsValue GetOrCreateScriptType ( Type type )
84
113
{
114
+ if ( ! _embeddedTypeStorageInitialized )
115
+ {
116
+ lock ( _embeddedTypeStorageInitializationSynchronizer )
117
+ {
118
+ if ( ! _embeddedTypeStorageInitialized )
119
+ {
120
+ _lazyEmbeddedTypes = new ConcurrentDictionary < string , Lazy < EmbeddedType > > ( ) ;
121
+ _embeddedTypeFinalizeCallback = EmbeddedTypeFinalizeCallback ;
122
+
123
+ _embeddedTypeStorageInitialized = true ;
124
+ }
125
+ }
126
+ }
127
+
85
128
string embeddedTypeKey = type . AssemblyQualifiedName ;
86
129
EmbeddedType embeddedType = _lazyEmbeddedTypes . GetOrAdd (
87
130
embeddedTypeKey ,
0 commit comments