Use longs for ids

This commit is contained in:
Frank A. Krueger 2017-06-13 00:03:01 -07:00
parent 7082e425b7
commit ad3035140e
6 changed files with 50 additions and 49 deletions

View File

@ -2,12 +2,7 @@
namespace Ooui namespace Ooui
{ {
public class Button : Element public class Button : FormControl
{ {
string name = "";
public string Name {
get => name;
set => SetProperty (ref name, value);
}
} }
} }

View File

@ -17,7 +17,7 @@ function getNode (id) {
function msgCreate (m) { function msgCreate (m) {
const id = m.id; const id = m.id;
const tagName = m.v; const tagName = m.k;
const node = tagName === "text" ? const node = tagName === "text" ?
document.createTextNode ("") : document.createTextNode ("") :
document.createElement (tagName); document.createElement (tagName);
@ -41,7 +41,6 @@ function msgSet (m) {
function msgCall (m) { function msgCall (m) {
const id = m.id; const id = m.id;
const node = getNode (id); const node = getNode (id);
// \u2999
if (!node) { if (!node) {
console.error ("Unknown Node Id", m); console.error ("Unknown Node Id", m);
return; return;
@ -53,13 +52,15 @@ function msgCall (m) {
function processMessage (m) { function processMessage (m) {
switch (m.m) { switch (m.m) {
case "Create": case "nop":
break;
case "create":
msgCreate (m); msgCreate (m);
break; break;
case "Set": case "set":
msgSet (m); msgSet (m);
break; break;
case "Call": case "call":
msgCall (m); msgCall (m);
break; break;
default: default:
@ -75,8 +76,11 @@ function fixupValue (v) {
return v; return v;
} }
else if (typeof v === 'string' || v instanceof String) { else if (typeof v === 'string' || v instanceof String) {
if ((v.length === 9) && (v[0] === "\u2999")) { if ((v.length >= 2) && (v[0] === "\u2999") && (v[1] === "n")) {
return getNode (v.substr(1)); // console.log("V", v);
const id = v.substr(1);
// console.log("ID", id);
return getNode (id);
} }
} }
return v; return v;

View File

@ -7,7 +7,7 @@ namespace Ooui
string className = ""; string className = "";
public string ClassName { public string ClassName {
get => className; get => className;
set => SetProperty (ref className, value); set => SetProperty (ref className, "className", value);
} }
} }
} }

13
Ooui/FormControl.cs Normal file
View File

@ -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");
}
}
}

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Runtime.Serialization;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Converters; using Newtonsoft.Json.Converters;
@ -6,12 +7,10 @@ namespace Ooui
{ {
public class Message public class Message
{ {
[JsonProperty("t")] [JsonProperty("mid")]
[JsonConverter (typeof (MillisecondEpochConverter))] public long Id = GenerateId ();
public DateTime CreatedTime = DateTime.UtcNow;
[JsonProperty("m")] [JsonProperty("m")]
[JsonConverter (typeof (StringEnumConverter))]
public MessageType MessageType = MessageType.Nop; public MessageType MessageType = MessageType.Nop;
[JsonProperty("id")] [JsonProperty("id")]
@ -22,29 +21,24 @@ namespace Ooui
[JsonProperty("v")] [JsonProperty("v")]
public object Value = ""; public object Value = "";
static long idCounter = 0;
static long GenerateId ()
{
return System.Threading.Interlocked.Increment (ref idCounter);
}
} }
[JsonConverter (typeof (StringEnumConverter))]
public enum MessageType public enum MessageType
{ {
[EnumMember(Value = "nop")]
Nop, Nop,
[EnumMember(Value = "create")]
Create, Create,
[EnumMember(Value = "set")]
Set, Set,
[EnumMember(Value = "call")]
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);
}
}
} }

View File

@ -19,7 +19,7 @@ namespace Ooui
public IEnumerable<Message> AllMessages => public IEnumerable<Message> AllMessages =>
messages messages
.Concat (from c in children from m in c.AllMessages select m) .Concat (from c in children from m in c.AllMessages select m)
.OrderBy (x => x.CreatedTime); .OrderBy (x => x.Id);
public Node () public Node ()
{ {
@ -90,7 +90,7 @@ namespace Ooui
Log (new Message { Log (new Message {
MessageType = MessageType.Create, MessageType = MessageType.Create,
TargetId = Id, 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 { Log (new Message {
MessageType = MessageType.Set, MessageType = MessageType.Set,
TargetId = Id, TargetId = Id,
Key = Mapping.GetMemberPath (propertyName), Key = attributeName,
Value = value, Value = value,
}); });
} }
protected bool SetProperty<T> (ref T backingStore, T newValue, [System.Runtime.CompilerServices.CallerMemberName] string propertyName = "") protected bool SetProperty<T> (ref T backingStore, T newValue, string attributeName, [System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")
{ {
if (!backingStore.Equals (newValue)) { if (!backingStore.Equals (newValue)) {
backingStore = newValue; backingStore = newValue;
LogSet (propertyName, newValue); LogSet (attributeName, newValue);
return true; return true;
} }
return false; return false;
} }
const string IdChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; static long idCounter = 0;
static string GenerateId () static string GenerateId ()
{ {
var rand = new Random(); var id = System.Threading.Interlocked.Increment (ref idCounter);
var chars = new char[8]; return "n" + id;
for (var i = 0; i < chars.Length; i++) {
chars[i] = IdChars[rand.Next(0, IdChars.Length)];
}
return new string(chars);
} }
} }
} }