Use longs for ids
This commit is contained in:
parent
7082e425b7
commit
ad3035140e
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -7,7 +7,7 @@ namespace Ooui
|
|||
string className = "";
|
||||
public string ClassName {
|
||||
get => className;
|
||||
set => SetProperty (ref className, value);
|
||||
set => SetProperty (ref className, "className", value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
23
Ooui/Node.cs
23
Ooui/Node.cs
|
@ -19,7 +19,7 @@ namespace Ooui
|
|||
public IEnumerable<Message> 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<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)) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue