oneRain 2021-03-19 14:29:44 +08:00
parent 1d59ac08af
commit 4f39c24f41
4 changed files with 18 additions and 31 deletions

View File

@ -1,7 +1,7 @@
using System.Collections; using System.Collections;
namespace LeanCloud.Storage.Internal.Operation { namespace LeanCloud.Storage.Internal.Operation {
public interface ILCOperation { internal interface ILCOperation {
ILCOperation MergeWithPrevious(ILCOperation previousOp); ILCOperation MergeWithPrevious(ILCOperation previousOp);
object Encode(); object Encode();

View File

@ -76,7 +76,7 @@ namespace LeanCloud.Storage {
static object EncodeLCObject(LCObject obj) { static object EncodeLCObject(LCObject obj) {
obj.ApplyCustomProperties(); obj.ApplyCustomProperties();
Dictionary<string, object> dict = LCObjectData.Encode(obj.Data); Dictionary<string, object> dict = LCObjectData.Encode(obj.data);
dict["__type"] = "Object"; dict["__type"] = "Object";
return dict; return dict;
} }

View File

@ -15,17 +15,6 @@ namespace LeanCloud.Storage {
public partial class LCObject { public partial class LCObject {
internal const string IgnoreHooksKey = "__ignore_hooks"; internal const string IgnoreHooksKey = "__ignore_hooks";
internal HashSet<string> ignoreHooks;
internal HashSet<string> IgnoreHooks {
get {
if (ignoreHooks == null) {
ignoreHooks = new HashSet<string>();
}
return ignoreHooks;
}
}
public void DisableBeforeHook() { public void DisableBeforeHook() {
Ignore( Ignore(
LCClassHook.BeforeSave, LCClassHook.BeforeSave,

View File

@ -16,9 +16,7 @@ namespace LeanCloud.Storage {
/// <summary> /// <summary>
/// Last synced data. /// Last synced data.
/// </summary> /// </summary>
public LCObjectData Data { internal LCObjectData data;
get;
}
/// <summary> /// <summary>
/// Estimated data. /// Estimated data.
@ -35,25 +33,25 @@ namespace LeanCloud.Storage {
public string ClassName { public string ClassName {
get { get {
return Data.ClassName; return data.ClassName;
} }
} }
public string ObjectId { public string ObjectId {
get { get {
return Data.ObjectId; return data.ObjectId;
} }
} }
public DateTime CreatedAt { public DateTime CreatedAt {
get { get {
return Data.CreatedAt; return data.CreatedAt;
} }
} }
public DateTime UpdatedAt { public DateTime UpdatedAt {
get { get {
return Data.UpdatedAt; return data.UpdatedAt;
} }
} }
@ -77,11 +75,11 @@ namespace LeanCloud.Storage {
if (string.IsNullOrEmpty(className)) { if (string.IsNullOrEmpty(className)) {
throw new ArgumentNullException(nameof(className)); throw new ArgumentNullException(nameof(className));
} }
Data = new LCObjectData(); data = new LCObjectData();
estimatedData = new Dictionary<string, object>(); estimatedData = new Dictionary<string, object>();
operationDict = new Dictionary<string, ILCOperation>(); operationDict = new Dictionary<string, ILCOperation>();
Data.ClassName = className; data.ClassName = className;
isNew = true; isNew = true;
} }
@ -90,7 +88,7 @@ namespace LeanCloud.Storage {
throw new ArgumentNullException(nameof(objectId)); throw new ArgumentNullException(nameof(objectId));
} }
LCObject obj = Create(className); LCObject obj = Create(className);
obj.Data.ObjectId = objectId; obj.data.ObjectId = objectId;
obj.isNew = false; obj.isNew = false;
return obj; return obj;
} }
@ -456,7 +454,7 @@ namespace LeanCloud.Storage {
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public override string ToString() { public override string ToString() {
Dictionary<string, object> originalData = LCObjectData.Encode(Data); Dictionary<string, object> originalData = LCObjectData.Encode(data);
Dictionary<string, object> currentData = estimatedData.Union(originalData.Where(kv => !estimatedData.ContainsKey(kv.Key))) Dictionary<string, object> currentData = estimatedData.Union(originalData.Where(kv => !estimatedData.ContainsKey(kv.Key)))
.ToDictionary(k => k.Key, v => v.Value); .ToDictionary(k => k.Key, v => v.Value);
return JsonConvert.SerializeObject(currentData); return JsonConvert.SerializeObject(currentData);
@ -492,17 +490,17 @@ namespace LeanCloud.Storage {
} }
public void Merge(LCObjectData objectData) { public void Merge(LCObjectData objectData) {
Data.ClassName = objectData.ClassName ?? Data.ClassName; data.ClassName = objectData.ClassName ?? data.ClassName;
Data.ObjectId = objectData.ObjectId ?? Data.ObjectId; data.ObjectId = objectData.ObjectId ?? data.ObjectId;
Data.CreatedAt = objectData.CreatedAt != null ? objectData.CreatedAt : Data.CreatedAt; data.CreatedAt = objectData.CreatedAt != null ? objectData.CreatedAt : data.CreatedAt;
Data.UpdatedAt = objectData.UpdatedAt != null ? objectData.UpdatedAt : Data.UpdatedAt; data.UpdatedAt = objectData.UpdatedAt != null ? objectData.UpdatedAt : data.UpdatedAt;
// 先将本地的预估数据直接替换 // 先将本地的预估数据直接替换
ApplyCustomProperties(); ApplyCustomProperties();
// 再将服务端的数据覆盖 // 再将服务端的数据覆盖
foreach (KeyValuePair<string, object> kv in objectData.CustomPropertyDict) { foreach (KeyValuePair<string, object> kv in objectData.CustomPropertyDict) {
string key = kv.Key; string key = kv.Key;
object value = kv.Value; object value = kv.Value;
Data.CustomPropertyDict[key] = value; data.CustomPropertyDict[key] = value;
} }
// 最后重新生成预估数据,用于后续访问和操作 // 最后重新生成预估数据,用于后续访问和操作
@ -513,12 +511,12 @@ namespace LeanCloud.Storage {
} }
public void ApplyCustomProperties() { public void ApplyCustomProperties() {
Data.CustomPropertyDict = estimatedData; data.CustomPropertyDict = estimatedData;
} }
void RebuildEstimatedData() { void RebuildEstimatedData() {
estimatedData = new Dictionary<string, object>(); estimatedData = new Dictionary<string, object>();
foreach (KeyValuePair<string, object> kv in Data.CustomPropertyDict) { foreach (KeyValuePair<string, object> kv in data.CustomPropertyDict) {
string key = kv.Key; string key = kv.Key;
object value = kv.Value; object value = kv.Value;
if (value is IList list) { if (value is IList list) {