From ad3035140e66e1219cdd8e55e32472c09a09f2e9 Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Tue, 13 Jun 2017 00:03:01 -0700 Subject: [PATCH] Use longs for ids --- Ooui/Button.cs | 7 +------ Ooui/Client.js | 18 +++++++++++------- Ooui/Element.cs | 2 +- Ooui/FormControl.cs | 13 +++++++++++++ Ooui/Message.cs | 36 +++++++++++++++--------------------- Ooui/Node.cs | 23 +++++++++-------------- 6 files changed, 50 insertions(+), 49 deletions(-) create mode 100644 Ooui/FormControl.cs diff --git a/Ooui/Button.cs b/Ooui/Button.cs index d4b3bec..5f09685 100644 --- a/Ooui/Button.cs +++ b/Ooui/Button.cs @@ -2,12 +2,7 @@ namespace Ooui { - public class Button : Element + public class Button : FormControl { - string name = ""; - public string Name { - get => name; - set => SetProperty (ref name, value); - } } } diff --git a/Ooui/Client.js b/Ooui/Client.js index 2488ede..d14abc9 100644 --- a/Ooui/Client.js +++ b/Ooui/Client.js @@ -17,7 +17,7 @@ function getNode (id) { function msgCreate (m) { const id = m.id; - const tagName = m.v; + const tagName = m.k; const node = tagName === "text" ? document.createTextNode ("") : document.createElement (tagName); @@ -41,7 +41,6 @@ function msgSet (m) { function msgCall (m) { const id = m.id; const node = getNode (id); - // \u2999 if (!node) { console.error ("Unknown Node Id", m); return; @@ -53,13 +52,15 @@ function msgCall (m) { function processMessage (m) { switch (m.m) { - case "Create": + case "nop": + break; + case "create": msgCreate (m); break; - case "Set": + case "set": msgSet (m); break; - case "Call": + case "call": msgCall (m); break; default: @@ -75,8 +76,11 @@ function fixupValue (v) { return v; } else if (typeof v === 'string' || v instanceof String) { - if ((v.length === 9) && (v[0] === "\u2999")) { - return getNode (v.substr(1)); + if ((v.length >= 2) && (v[0] === "\u2999") && (v[1] === "n")) { + // console.log("V", v); + const id = v.substr(1); + // console.log("ID", id); + return getNode (id); } } return v; diff --git a/Ooui/Element.cs b/Ooui/Element.cs index fe13d01..27fd9cb 100644 --- a/Ooui/Element.cs +++ b/Ooui/Element.cs @@ -7,7 +7,7 @@ namespace Ooui string className = ""; public string ClassName { get => className; - set => SetProperty (ref className, value); + set => SetProperty (ref className, "className", value); } } } diff --git a/Ooui/FormControl.cs b/Ooui/FormControl.cs new file mode 100644 index 0000000..1b5611b --- /dev/null +++ b/Ooui/FormControl.cs @@ -0,0 +1,13 @@ +using System; + +namespace Ooui +{ + public abstract class FormControl : Element + { + string name = ""; + public string Name { + get => name; + set => SetProperty (ref name, value, "name"); + } + } +} diff --git a/Ooui/Message.cs b/Ooui/Message.cs index 71d90e9..e60b116 100644 --- a/Ooui/Message.cs +++ b/Ooui/Message.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.Serialization; using Newtonsoft.Json; using Newtonsoft.Json.Converters; @@ -6,12 +7,10 @@ namespace Ooui { public class Message { - [JsonProperty("t")] - [JsonConverter (typeof (MillisecondEpochConverter))] - public DateTime CreatedTime = DateTime.UtcNow; + [JsonProperty("mid")] + public long Id = GenerateId (); - [JsonProperty("m")] - [JsonConverter (typeof (StringEnumConverter))] + [JsonProperty("m")] public MessageType MessageType = MessageType.Nop; [JsonProperty("id")] @@ -22,29 +21,24 @@ namespace Ooui [JsonProperty("v")] public object Value = ""; + + static long idCounter = 0; + static long GenerateId () + { + return System.Threading.Interlocked.Increment (ref idCounter); + } } + [JsonConverter (typeof (StringEnumConverter))] public enum MessageType { + [EnumMember(Value = "nop")] Nop, + [EnumMember(Value = "create")] Create, + [EnumMember(Value = "set")] Set, + [EnumMember(Value = "call")] Call, } - - class MillisecondEpochConverter : DateTimeConverterBase - { - private static readonly DateTime epoch = new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); - - public override void WriteJson (JsonWriter writer, object value, JsonSerializer serializer) - { - writer.WriteRawValue (((DateTime)value - epoch).TotalMilliseconds.ToString (System.Globalization.CultureInfo.InvariantCulture)); - } - - public override object ReadJson (JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) - { - if (reader.Value == null) return null; - return epoch.AddMilliseconds ((double)reader.Value); - } - } } diff --git a/Ooui/Node.cs b/Ooui/Node.cs index f55f4f3..252e048 100644 --- a/Ooui/Node.cs +++ b/Ooui/Node.cs @@ -19,7 +19,7 @@ namespace Ooui public IEnumerable AllMessages => messages .Concat (from c in children from m in c.AllMessages select m) - .OrderBy (x => x.CreatedTime); + .OrderBy (x => x.Id); public Node () { @@ -90,7 +90,7 @@ namespace Ooui Log (new Message { MessageType = MessageType.Create, TargetId = Id, - Value = Mapping.TagName, + Key = Mapping.TagName, }); } @@ -103,36 +103,31 @@ namespace Ooui }); } - protected void LogSet (string propertyName, object value) + protected void LogSet (string attributeName, object value) { Log (new Message { MessageType = MessageType.Set, TargetId = Id, - Key = Mapping.GetMemberPath (propertyName), + Key = attributeName, Value = value, }); } - protected bool SetProperty (ref T backingStore, T newValue, [System.Runtime.CompilerServices.CallerMemberName] string propertyName = "") + protected bool SetProperty (ref T backingStore, T newValue, string attributeName, [System.Runtime.CompilerServices.CallerMemberName] string propertyName = "") { if (!backingStore.Equals (newValue)) { backingStore = newValue; - LogSet (propertyName, newValue); + LogSet (attributeName, newValue); return true; } return false; } - const string IdChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - + static long idCounter = 0; static string GenerateId () { - var rand = new Random(); - var chars = new char[8]; - for (var i = 0; i < chars.Length; i++) { - chars[i] = IdChars[rand.Next(0, IdChars.Length)]; - } - return new string(chars); + var id = System.Threading.Interlocked.Increment (ref idCounter); + return "n" + id; } } }