* LCHookObject.cs:

* LCApplication.cs:
* LCHttpClient.cs:
* LCIgnoreHookOperation.cs:

* LCObject.cs: chore: ignore hook
oneRain 2021-03-18 18:07:44 +08:00
parent 615f2aff9c
commit 1182eeaab4
5 changed files with 112 additions and 1 deletions

View File

@ -148,6 +148,11 @@ namespace LeanCloud.Storage.Internal.Http {
string sign = $"{hash},{timestamp}";
headers.Add("X-LC-Sign", sign);
}
if (LCApplication.AdditionalHeaders.Count > 0) {
foreach (KeyValuePair<string, string> kv in LCApplication.AdditionalHeaders) {
headers.Add(kv.Key, kv.Value);
}
}
// 当前用户 Session Token
LCUser currentUser = await LCUser.GetCurrent();
if (!headers.Contains("X-LC-Session") && currentUser != null) {

View File

@ -0,0 +1,39 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace LeanCloud.Storage.Internal.Operation {
internal class LCIgnoreHookOperation : ILCOperation {
internal HashSet<string> ignoreHooks;
internal LCIgnoreHookOperation(IEnumerable<string> hooks) {
ignoreHooks = new HashSet<string>(hooks);
}
public object Apply(object oldValue, string key) {
HashSet<object> set = new HashSet<object>();
if (oldValue != null) {
set.UnionWith(oldValue as IEnumerable<object>);
}
set.UnionWith(ignoreHooks);
return set.ToList();
}
public object Encode() {
return ignoreHooks;
}
public IEnumerable GetNewObjectList() {
return ignoreHooks;
}
public ILCOperation MergeWithPrevious(ILCOperation previousOp) {
if (previousOp is LCIgnoreHookOperation ignoreHookOp) {
ignoreHooks.UnionWith(ignoreHookOp.ignoreHooks);
return this;
}
throw new ArgumentException("Operation is invalid after previous operation.");
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using LeanCloud.Common;
using LeanCloud.Storage;
using LeanCloud.Storage.Internal.Http;
@ -42,6 +43,10 @@ namespace LeanCloud {
get; set;
}
internal static Dictionary<string, string> AdditionalHeaders {
get;
} = new Dictionary<string, string>();
public static void Initialize(string appId,
string appKey,
string server = null,
@ -68,5 +73,9 @@ namespace LeanCloud {
HttpClient = new LCHttpClient(appId, appKey, server, SDKVersion, APIVersion);
}
public static void AddHeader(string key, string value) {
AdditionalHeaders.Add(key, value);
}
}
}

View File

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using LeanCloud.Storage.Internal.Operation;
namespace LeanCloud.Storage {
public static class LCClassHook {
public const string BeforeSave = "beforeSave";
public const string AfterSave = "afterSave";
public const string BeforeUpdate = "beforeUpdate";
public const string AfterUpdate = "afterUpdate";
public const string BeforeDelete = "beforeDelete";
public const string AfterDelete = "afterDelete";
}
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,
LCClassHook.BeforeUpdate,
LCClassHook.BeforeDelete);
}
public void DisableAfterHook() {
Ignore(
LCClassHook.AfterSave,
LCClassHook.AfterUpdate,
LCClassHook.AfterDelete);
}
public void IgnoreHook(string hookName) {
if (hookName != LCClassHook.BeforeSave && hookName != LCClassHook.AfterSave &&
hookName != LCClassHook.BeforeUpdate && hookName != LCClassHook.AfterUpdate &&
hookName != LCClassHook.BeforeDelete && hookName != LCClassHook.AfterDelete) {
throw new ArgumentException($"Invalid {hookName}");
}
Ignore(hookName);
}
private void Ignore(params string[] hooks) {
LCIgnoreHookOperation op = new LCIgnoreHookOperation(hooks);
ApplyOperation(IgnoreHooksKey, op);
}
}
}

View File

@ -12,7 +12,7 @@ namespace LeanCloud.Storage {
/// <summary>
/// LeanCloud Object
/// </summary>
public class LCObject {
public partial class LCObject {
/// <summary>
/// Last synced data.
/// </summary>