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;
namespace LeanCloud.Storage.Internal.Operation {
public interface ILCOperation {
internal interface ILCOperation {
ILCOperation MergeWithPrevious(ILCOperation previousOp);
object Encode();

View File

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

View File

@ -15,17 +15,6 @@ namespace LeanCloud.Storage {
public partial class LCObject {
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() {
Ignore(
LCClassHook.BeforeSave,

View File

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