@@ -24,25 +24,43 @@ internal sealed class TypeMapper : IDisposable
2424 /// <summary>
2525 /// Storage for lazy-initialized embedded objects
2626 /// </summary>
27- private ConcurrentDictionary < EmbeddedObjectKey , Lazy < EmbeddedObject > > _lazyEmbeddedObjects =
28- new ConcurrentDictionary < EmbeddedObjectKey , Lazy < EmbeddedObject > > ( ) ;
27+ private ConcurrentDictionary < EmbeddedObjectKey , Lazy < EmbeddedObject > > _lazyEmbeddedObjects ;
2928
3029 /// <summary>
3130 /// Callback for finalization of embedded object
3231 /// </summary>
3332 private JsFinalizeCallback _embeddedObjectFinalizeCallback ;
3433
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+
3544 /// <summary>
3645 /// Storage for lazy-initialized embedded types
3746 /// </summary>
38- private ConcurrentDictionary < string , Lazy < EmbeddedType > > _lazyEmbeddedTypes =
39- new ConcurrentDictionary < string , Lazy < EmbeddedType > > ( ) ;
47+ private ConcurrentDictionary < string , Lazy < EmbeddedType > > _lazyEmbeddedTypes ;
4048
4149 /// <summary>
4250 /// Callback for finalization of embedded type
4351 /// </summary>
4452 private JsFinalizeCallback _embeddedTypeFinalizeCallback ;
4553
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+
4664 /// <summary>
4765 /// Flag indicating whether this object is disposed
4866 /// </summary>
@@ -53,10 +71,7 @@ internal sealed class TypeMapper : IDisposable
5371 /// Constructs an instance of type mapper
5472 /// </summary>
5573 public TypeMapper ( )
56- {
57- _embeddedObjectFinalizeCallback = EmbeddedObjectFinalizeCallback ;
58- _embeddedTypeFinalizeCallback = EmbeddedTypeFinalizeCallback ;
59- }
74+ { }
6075
6176
6277 /// <summary>
@@ -66,6 +81,20 @@ public TypeMapper()
6681 /// <returns>JavaScript value created from an host object</returns>
6782 public JsValue GetOrCreateScriptObject ( object obj )
6883 {
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+
6998 var embeddedObjectKey = new EmbeddedObjectKey ( obj ) ;
7099 EmbeddedObject embeddedObject = _lazyEmbeddedObjects . GetOrAdd (
71100 embeddedObjectKey ,
@@ -82,6 +111,20 @@ public JsValue GetOrCreateScriptObject(object obj)
82111 /// <returns>JavaScript value created from an host type</returns>
83112 public JsValue GetOrCreateScriptType ( Type type )
84113 {
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+
85128 string embeddedTypeKey = type . AssemblyQualifiedName ;
86129 EmbeddedType embeddedType = _lazyEmbeddedTypes . GetOrAdd (
87130 embeddedTypeKey ,
0 commit comments