Skip to content

Commit c49b3b7

Browse files
committed
Fix TryTake may remove the wrong element
1 parent 4ecef47 commit c49b3b7

File tree

1 file changed

+5
-20
lines changed

1 file changed

+5
-20
lines changed

Flow.Launcher/Storage/TopMostRecord.cs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,8 @@ internal void Remove(Result result)
189189
}
190190

191191
// remove the record from the bag
192-
var recordToRemove = value.FirstOrDefault(r => r.Equals(result));
193-
if (recordToRemove != null)
194-
{
195-
value.TryTake(out recordToRemove);
196-
}
192+
var bag = new ConcurrentQueue<Record>(value.Where(r => !r.Equals(result)));
193+
records[result.OriginQuery.RawQuery] = new ConcurrentBag<Record>(bag);
197194

198195
// if the bag is empty, remove the bag from the dictionary
199196
if (value.IsEmpty)
@@ -230,21 +227,9 @@ internal void AddOrUpdate(Result result)
230227
else
231228
{
232229
// add or update the record in the bag
233-
if (value.Any(r => r.Equals(result)))
234-
{
235-
// update the record
236-
var recordToUpdate = value.FirstOrDefault(r => r.Equals(result));
237-
if (recordToUpdate != null)
238-
{
239-
value.TryTake(out recordToUpdate);
240-
value.Add(record);
241-
}
242-
}
243-
else
244-
{
245-
// add the record
246-
value.Add(record);
247-
}
230+
var bag = new ConcurrentQueue<Record>(value.Where(r => !r.Equals(result))); // make sure we don't have duplicates
231+
bag.Enqueue(record);
232+
records[result.OriginQuery.RawQuery] = new ConcurrentBag<Record>(bag);
248233
}
249234
}
250235
}

0 commit comments

Comments
 (0)