Skip to content

Commit af0e50f

Browse files
v. 1.4.0
1 parent b1aaa11 commit af0e50f

File tree

9 files changed

+172
-94
lines changed

9 files changed

+172
-94
lines changed

README-RU.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1221,12 +1221,22 @@ namespace ObservableComputationsExamples
12211221

12221222
### Блокировка установки значений свойств обработчиков запросов на изменение результатов вычислений
12231223

1224-
Свойства обработчиков запросов на изменение вычислений являются публичными. По умолчанию любой код, который имеет ссылку на вычисление может установить или перезаписать значение этого свойства. Есть возможность управлять возможностью установки значений этих свойств с помощью методов класса [*CollectionComputing<TItem>*](#полный-список-операторов):
1224+
Свойства обработчиков запросов на изменение вычислений являются публичными. По умолчанию любой код, который имеет ссылку на вычисление может установить или перезаписать значение этого свойства. Есть возможность управлять возможностью установки значений этих свойств с помощью
1225+
1226+
методов класса [*CollectionComputing<TItem>*](#полный-список-операторов):
12251227

12261228
* void LockModifyChangeAction(CollectionChangeAction collectionChangeAction, object key)
12271229
* void UnlockModifyChangeAction(CollectionChangeAction collectionChangeAction, object key)
12281230
* bool IsModifyChangeActionLocked(CollectionChangeAction collectionChangeAction)
12291231

1232+
и методов класса [*ScalarComputing<TValue>*](#полный-список-операторов):
1233+
1234+
* void LockModifySetValueAction(object key)
1235+
* void UnlockModifySetValueAction(object key)
1236+
* bool IsModifySetValueActionLocked()
1237+
1238+
1239+
12301240
## Обработка изменений результатов вычислений
12311241
### Обработка измениний в ObservableCollection<T>
12321242

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1213,12 +1213,20 @@ Properties similar to *InsertItemAction* exist for all other operations: ([remov
12131213

12141214
### Lock setting properties of computation result change request handlers
12151215

1216-
Properties of the computation change request handlers are public. By default, any code that has a reference to the computation can set or overwrite the value of this property. It is possible to control the ability to set the values of these properties using methods of [*CollectionComputing<TItem>* class](#full-list-of-operators):
1216+
Properties of the computation change request handlers are public. By default, any code that has a reference to the computation can set or overwrite the value of this property. It is possible to control the ability to set the values of these properties using
1217+
1218+
methods of [*CollectionComputing<TItem>* class](#full-list-of-operators):
12171219

12181220
* void LockModifyChangeAction(CollectionChangeAction collectionChangeAction, object key)
12191221
* void UnlockModifyChangeAction(CollectionChangeAction collectionChangeAction, object key)
12201222
* bool IsModifyChangeActionLocked(CollectionChangeAction collectionChangeAction)
12211223

1224+
and methods of [*ScalarComputing<TValue>* class](#full-list-of-operators):
1225+
1226+
* void LockModifySetValueAction(object key)
1227+
* void UnlockModifySetValueAction(object key)
1228+
* bool IsModifySetValueActionLocked()
1229+
12221230
## Processing changes of computation results
12231231
### Change handling in ObservableCollection<T>
12241232

src/ObservableComputations/Collections/ItemsProcessing.cs

Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ public class ItemsProcessing<TSourceItem, TReturnValue> : CollectionComputing<TR
2424

2525
public Func<TSourceItem, ItemsProcessing<TSourceItem, TReturnValue>, object, EventArgs, TReturnValue> NewItemProcessor => _newItemProcessor;
2626
public Action<TSourceItem, ItemsProcessing<TSourceItem, TReturnValue>, TReturnValue, object, EventArgs> OldItemProcessor => _oldItemProcessor;
27+
public Action<TSourceItem, ItemsProcessing<TSourceItem, TReturnValue>, TReturnValue, object, EventArgs> MoveItemProcessor => _moveItemProcessor;
2728

2829
private readonly Func<TSourceItem, ItemsProcessing<TSourceItem, TReturnValue>, object, EventArgs, TReturnValue> _newItemProcessor;
2930
private readonly Action<TSourceItem, ItemsProcessing<TSourceItem, TReturnValue>, TReturnValue, object, EventArgs> _oldItemProcessor;
31+
private readonly Action<TSourceItem, ItemsProcessing<TSourceItem, TReturnValue>, TReturnValue, object, EventArgs> _moveItemProcessor;
3032

3133
// ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable
3234
private readonly PropertyChangedEventHandler _sourceScalarPropertyChangedEventHandler;
@@ -45,7 +47,8 @@ public class ItemsProcessing<TSourceItem, TReturnValue> : CollectionComputing<TR
4547
public ItemsProcessing(
4648
IReadScalar<INotifyCollectionChanged> sourceScalar,
4749
Func<TSourceItem, ItemsProcessing<TSourceItem, TReturnValue>, object, EventArgs, TReturnValue> newItemProcessor = null,
48-
Action<TSourceItem, ItemsProcessing<TSourceItem, TReturnValue>, TReturnValue, object, EventArgs> oldItemProcessor = null) : this(newItemProcessor, oldItemProcessor, Utils.getCapacity(sourceScalar))
50+
Action<TSourceItem, ItemsProcessing<TSourceItem, TReturnValue>, TReturnValue, object, EventArgs> oldItemProcessor = null,
51+
Action<TSourceItem, ItemsProcessing<TSourceItem, TReturnValue>, TReturnValue, object, EventArgs> moveItemProcessor = null) : this(newItemProcessor, oldItemProcessor, moveItemProcessor, Utils.getCapacity(sourceScalar))
4952
{
5053
_sourceScalar = sourceScalar;
5154
_sourceScalarPropertyChangedEventHandler = handleSourceScalarValueChanged;
@@ -59,19 +62,22 @@ public ItemsProcessing(
5962
public ItemsProcessing(
6063
INotifyCollectionChanged source,
6164
Func<TSourceItem, ItemsProcessing<TSourceItem, TReturnValue>, object, EventArgs, TReturnValue> newItemProcessor = null,
62-
Action<TSourceItem, ItemsProcessing<TSourceItem, TReturnValue>, TReturnValue, object, EventArgs> oldItemProcessor = null) : this(newItemProcessor, oldItemProcessor, Utils.getCapacity(source))
65+
Action<TSourceItem, ItemsProcessing<TSourceItem, TReturnValue>, TReturnValue, object, EventArgs> oldItemProcessor = null,
66+
Action<TSourceItem, ItemsProcessing<TSourceItem, TReturnValue>, TReturnValue, object, EventArgs> moveItemProcessor = null) : this(newItemProcessor, oldItemProcessor, moveItemProcessor, Utils.getCapacity(source))
6367
{
6468
_source = source;
6569
initializeFromSource(null, null);
6670
}
6771

6872
private ItemsProcessing(
6973
Func<TSourceItem, ItemsProcessing<TSourceItem, TReturnValue>, object, EventArgs, TReturnValue> newItemProcessor,
70-
Action<TSourceItem, ItemsProcessing<TSourceItem, TReturnValue>, TReturnValue, object, EventArgs> oldItemProcessor,
74+
Action<TSourceItem, ItemsProcessing<TSourceItem, TReturnValue>, TReturnValue, object, EventArgs> oldItemProcessor,
75+
Action<TSourceItem, ItemsProcessing<TSourceItem, TReturnValue>, TReturnValue, object, EventArgs> moveItemProcessor,
7176
int capacity) : base(capacity)
7277
{
7378
_newItemProcessor = newItemProcessor;
7479
_oldItemProcessor = oldItemProcessor;
80+
_moveItemProcessor = moveItemProcessor;
7581
}
7682

7783
private void initializeFromSource(object sender, EventArgs eventArgs)
@@ -86,7 +92,7 @@ private void initializeFromSource(object sender, EventArgs eventArgs)
8692
TSourceItem sourceItem = _sourceAsList[i];
8793
TReturnValue returnValue = this[0];
8894
baseRemoveItem(0);
89-
processOldItem(sourceItem, returnValue, sender, eventArgs);
95+
if (_oldItemProcessor != null) processOldItem(sourceItem, returnValue, sender, eventArgs);
9096
}
9197

9298
if (_rootSourceWrapper)
@@ -125,7 +131,7 @@ private void initializeFromSource(object sender, EventArgs eventArgs)
125131
for (int index = 0; index < count; index++)
126132
{
127133
TSourceItem sourceItem = _sourceAsList[index];
128-
TReturnValue returnValue = processNewItem(sourceItem, sender, eventArgs);
134+
TReturnValue returnValue = _newItemProcessor != null ? processNewItem(sourceItem, sender, eventArgs) : default(TReturnValue);
129135

130136
baseInsertItem(index, returnValue);
131137
}
@@ -172,7 +178,7 @@ private void handleSourceCollectionChanged(object sender, NotifyCollectionChange
172178
_isConsistent = false;
173179
int newStartingIndex = e.NewStartingIndex;
174180
TSourceItem addedItem = _sourceAsList[newStartingIndex];
175-
TReturnValue returnValue = processNewItem(addedItem, sender, e);
181+
TReturnValue returnValue = _newItemProcessor != null ? processNewItem(addedItem, sender, e) : default(TReturnValue);
176182

177183
baseInsertItem(newStartingIndex, returnValue);
178184
_isConsistent = true;
@@ -184,7 +190,7 @@ private void handleSourceCollectionChanged(object sender, NotifyCollectionChange
184190
TSourceItem removedItem = (TSourceItem) e.OldItems[0];
185191
TReturnValue returnValue1 = this[oldStartingIndex];
186192
baseRemoveItem(oldStartingIndex);
187-
processOldItem(removedItem, returnValue1, sender, e);
193+
if (_oldItemProcessor != null) processOldItem(removedItem, returnValue1, sender, e);
188194
_isConsistent = true;
189195
raiseConsistencyRestored();
190196
break;
@@ -195,9 +201,9 @@ private void handleSourceCollectionChanged(object sender, NotifyCollectionChange
195201
TSourceItem newItem = _sourceAsList[newStartingIndex1];
196202
TReturnValue returnValueOld = this[newStartingIndex1];
197203

198-
TReturnValue returnValue2 = processNewItem(newItem, sender, e);
204+
TReturnValue returnValue2 = _newItemProcessor != null ? processNewItem(newItem, sender, e) : default;
199205
baseSetItem(newStartingIndex1, returnValue2);
200-
processOldItem(oldItem, returnValueOld, sender, e);
206+
if (_oldItemProcessor != null) processOldItem(oldItem, returnValueOld, sender, e);
201207
_isConsistent = true;
202208
raiseConsistencyRestored();
203209
break;
@@ -207,6 +213,7 @@ private void handleSourceCollectionChanged(object sender, NotifyCollectionChange
207213
if (oldStartingIndex2 != newStartingIndex2)
208214
{
209215
baseMoveItem(oldStartingIndex2, newStartingIndex2);
216+
if (_moveItemProcessor!= null) processMovedItem(_sourceAsList[newStartingIndex2], this[newStartingIndex2], sender, e);
210217
}
211218
break;
212219
case NotifyCollectionChangedAction.Reset:
@@ -220,46 +227,57 @@ private void handleSourceCollectionChanged(object sender, NotifyCollectionChange
220227

221228
private TReturnValue processNewItem(TSourceItem sourceItem, object sender, EventArgs eventArgs)
222229
{
223-
if (_newItemProcessor != null)
230+
if (Configuration.TrackComputingsExecutingUserCode)
224231
{
225-
if (Configuration.TrackComputingsExecutingUserCode)
226-
{
227-
Thread currentThread = Thread.CurrentThread;
228-
DebugInfo._computingsExecutingUserCode.TryGetValue(currentThread, out IComputing computing);
229-
DebugInfo._computingsExecutingUserCode[currentThread] = this;
230-
231-
TReturnValue returnValue = _newItemProcessor(sourceItem, this, sender, eventArgs);
232+
Thread currentThread = Thread.CurrentThread;
233+
DebugInfo._computingsExecutingUserCode.TryGetValue(currentThread, out IComputing computing);
234+
DebugInfo._computingsExecutingUserCode[currentThread] = this;
235+
236+
TReturnValue returnValue = _newItemProcessor(sourceItem, this, sender, eventArgs);
237+
238+
if (computing == null) DebugInfo._computingsExecutingUserCode.Remove(currentThread);
239+
else DebugInfo._computingsExecutingUserCode[currentThread] = computing;
240+
return returnValue;
241+
}
232242

233-
if (computing == null) DebugInfo._computingsExecutingUserCode.Remove(currentThread);
234-
else DebugInfo._computingsExecutingUserCode[currentThread] = computing;
235-
return returnValue;
236-
}
243+
return _newItemProcessor(sourceItem, this, sender, eventArgs);
244+
}
245+
246+
private void processOldItem(TSourceItem sourceItem, TReturnValue returnValue, object sender, EventArgs eventArgs)
247+
{
248+
if (Configuration.TrackComputingsExecutingUserCode)
249+
{
250+
Thread currentThread = Thread.CurrentThread;
251+
DebugInfo._computingsExecutingUserCode.TryGetValue(currentThread, out IComputing computing);
252+
DebugInfo._computingsExecutingUserCode[currentThread] = this;
253+
254+
_oldItemProcessor(sourceItem, this, returnValue, sender, eventArgs);
237255

238-
return _newItemProcessor(sourceItem, this, sender, eventArgs);
256+
if (computing == null) DebugInfo._computingsExecutingUserCode.Remove(currentThread);
257+
else DebugInfo._computingsExecutingUserCode[currentThread] = computing;
258+
return;
239259
}
240260

241-
return default;
261+
_oldItemProcessor(sourceItem, this, returnValue, sender, eventArgs);
242262
}
243263

244-
private void processOldItem(TSourceItem sourceItem, TReturnValue returnValue, object sender, EventArgs eventArgs)
264+
265+
private void processMovedItem(TSourceItem sourceItem, TReturnValue returnValue, object sender, EventArgs eventArgs)
245266
{
246-
if (_oldItemProcessor != null)
267+
if (Configuration.TrackComputingsExecutingUserCode)
247268
{
248-
if (Configuration.TrackComputingsExecutingUserCode)
249-
{
250-
Thread currentThread = Thread.CurrentThread;
251-
DebugInfo._computingsExecutingUserCode.TryGetValue(currentThread, out IComputing computing);
252-
DebugInfo._computingsExecutingUserCode[currentThread] = this;
253-
254-
_oldItemProcessor(sourceItem, this, returnValue, sender, eventArgs);
255-
256-
if (computing == null) DebugInfo._computingsExecutingUserCode.Remove(currentThread);
257-
else DebugInfo._computingsExecutingUserCode[currentThread] = computing;
258-
return;
259-
}
269+
Thread currentThread = Thread.CurrentThread;
270+
DebugInfo._computingsExecutingUserCode.TryGetValue(currentThread, out IComputing computing);
271+
DebugInfo._computingsExecutingUserCode[currentThread] = this;
272+
273+
_moveItemProcessor(sourceItem, this, returnValue, sender, eventArgs);
260274

261-
_oldItemProcessor(sourceItem, this, returnValue, sender, eventArgs);
275+
if (computing == null) DebugInfo._computingsExecutingUserCode.Remove(currentThread);
276+
else DebugInfo._computingsExecutingUserCode[currentThread] = computing;
277+
return;
262278
}
279+
280+
_moveItemProcessor(sourceItem, this, returnValue, sender, eventArgs);
263281
}
264282

265283
~ItemsProcessing()

0 commit comments

Comments
 (0)