public static void SetAfterUpdateDelegate(Type type, string member, AfterUpdateDelegate afterUpdateDelegate) { PropertyComparer propertyComparer = mPropertyComparers[type]; propertyComparer.SetAfterUpdateDelegateForMember(member, afterUpdateDelegate); }
public Task UpdateAtomicAsync(Func <Task <TDocument> > updateAsync, Func <TDocument, Task> afterUpdateAsync = null) { if (updateAsync == null) { return(Task.CompletedTask); } _updateDelegateAsync += () => updateAsync(); if (afterUpdateAsync != null) { _afterUpdateDelegateAsync += document => afterUpdateAsync(document); } DocumentStore.AfterCommitSuccess <TDocument>(async() => { (var locker, var locked) = await _distributedLock.TryAcquireLockAsync( _options.CacheKey + "_LOCK", TimeSpan.FromMilliseconds(_options.LockTimeout), TimeSpan.FromMilliseconds(_options.LockExpiration)); if (!locked) { return; } await using var acquiredLock = locker; TDocument document = null; foreach (var d in _updateDelegateAsync.GetInvocationList()) { document = await((UpdateDelegate)d)(); } document.Identifier ??= IdGenerator.GenerateId(); await SetInternalAsync(document); if (_afterUpdateDelegateAsync != null) { foreach (var d in _afterUpdateDelegateAsync.GetInvocationList()) { await((AfterUpdateDelegate)d)(document); } } }); return(Task.CompletedTask); }
public Task UpdateAtomicAsync(Func <Task <TDocument> > updateAsync, Func <TDocument, Task> afterUpdateAsync = null, TimeSpan?lockAcquireTimeout = null, TimeSpan?lockExpirationTime = null) { if (updateAsync == null) { return(Task.CompletedTask); } _updateDelegateAsync += () => updateAsync(); if (afterUpdateAsync != null) { _afterUpdateDelegateAsync += document => afterUpdateAsync(document); } _documentStore.AfterCommitSuccess <TDocument>(async() => { var timeout = lockAcquireTimeout ?? DefaultLockTimeout; var expiration = lockExpirationTime ?? DefaultLockExpiration; (var locker, var locked) = await _distributedLock.TryAcquireLockAsync(_lockKey, timeout, expiration); if (!locked) { return; } await using var acquiredLock = locker; TDocument document = null; foreach (var d in _updateDelegateAsync.GetInvocationList()) { document = await((UpdateDelegate)d)(); } document.Identifier ??= IdGenerator.GenerateId(); await SetInternalAsync(document); if (_afterUpdateDelegateAsync != null) { foreach (var d in _afterUpdateDelegateAsync.GetInvocationList()) { await((AfterUpdateDelegate)d)(document); } } }); return(Task.CompletedTask); }
public void SetAfterUpdateDelegateForMember(string memberName, AfterUpdateDelegate afterUpdateDelegate) { mAfterUpdateDelegates.Add(memberName, afterUpdateDelegate); }
public static void EndOfFrameActivity() { UpdateListDisplayWindow(); #region Perform Undos if pushed Control+Z if (InputManager.Keyboard.ControlZPushed() && mInstructions.Count != 0) { InstructionList instructionList = mInstructions[mInstructions.Count - 1]; for (int i = 0; i < instructionList.Count; i++) { Instruction instruction = instructionList[i]; // See if the instruction is one that has an associated delegate if (instruction is GenericInstruction) { GenericInstruction asGenericInstruction = instruction as GenericInstruction; Type targetType = asGenericInstruction.Target.GetType(); PropertyComparer propertyComparerForType = null; if (mPropertyComparers.ContainsKey(targetType)) { // There is a PropertyComparer for this exact type propertyComparerForType = mPropertyComparers[targetType]; } else { // There isn't a PropertyComparer for this exact type, so climb up the inheritance tree foreach (PropertyComparer pc in mPropertyComparers.Values) { if (pc.GenericType.IsAssignableFrom(targetType)) { propertyComparerForType = pc; break; } } } // If there's no PropertyComparer, then the app might be a UI-only app. If that's // the case, we don't want to run afterUpdateDelegates if (propertyComparerForType != null) { AfterUpdateDelegate afterUpdateDelegate = propertyComparerForType.GetAfterUpdateDelegateForMember(asGenericInstruction.Member); if (afterUpdateDelegate != null) { afterUpdateDelegate(asGenericInstruction.Target); } } } instruction.Execute(); } mInstructions.RemoveAt(mInstructions.Count - 1); } #endregion // Vic says that this will break if undos are added through this and through // the property comparers in the same frame. if (mUndosAddedThisFrame.Count != 0) { mInstructions.Add(mUndosAddedThisFrame); mUndosAddedThisFrame = new InstructionList(); } }