Make published paths thread safe

This commit is contained in:
Frank A. Krueger 2017-06-15 17:35:20 -07:00
parent abab07b8e6
commit 2729418006
1 changed files with 13 additions and 7 deletions

View File

@ -56,7 +56,7 @@ namespace Ooui
public static void Publish (string path, Func<Element> elementCtor) public static void Publish (string path, Func<Element> elementCtor)
{ {
Console.WriteLine ($"PUBLISH {path}"); Console.WriteLine ($"PUBLISH {path}");
publishedPaths[path] = elementCtor; lock (publishedPaths) publishedPaths[path] = elementCtor;
Start (); Start ();
} }
@ -148,12 +148,16 @@ namespace Ooui
s.Write (clientJsBytes, 0, clientJsBytes.Length); s.Write (clientJsBytes, 0, clientJsBytes.Length);
} }
} }
else if (publishedPaths.TryGetValue (path, out ctor)) {
WriteElementHtml (path, response);
}
else { else {
response.StatusCode = 404; var found = false;
response.Close (); lock (publishedPaths) found = publishedPaths.TryGetValue (path, out ctor);
if (found) {
WriteElementHtml (path, response);
}
else {
response.StatusCode = 404;
response.Close ();
}
} }
} }
@ -184,7 +188,9 @@ namespace Ooui
var path = url.LocalPath; var path = url.LocalPath;
Func<Element> ctor; Func<Element> ctor;
if (!publishedPaths.TryGetValue (path, out ctor)) { var found = false;
lock (publishedPaths) found = publishedPaths.TryGetValue (path, out ctor);
if (!found) {
listenerContext.Response.StatusCode = 404; listenerContext.Response.StatusCode = 404;
listenerContext.Response.Close (); listenerContext.Response.Close ();
return; return;