«Aggiungi account» ha una sola casella: mentre Alberto scrive compaiono subito i suggerimenti locali, poi i profili di Instagram con la foto, come chiesto in #37. Il browser di Febe scrive nella casella «Cerca» di Instagram con tasti veri da una mappa chiusa (mai Invio, Tab o Esc), controlla prima il fuoco, legge la risposta della pagina e non clicca mai un risultato. La ricerca ha una categoria propria nel Governatore, con tetti larghi che non consumano quelli della raccolta, e si dà il turno con il motore sull'unica pagina; a fine sessione la pagina torna comunque al riposo. Le foto passano dal CDN con un permesso ciascuna e restano solo in memoria. Le risposte di Alberto del 15 novembre (controllo del fuoco, turni, evento requestWillBeSent, niente foto dei seguiti per ora) sono in §6.1 e in docs/decisioni.md. Su Instagram vero la ricerca parte quando i bersagli search-open e search-box saranno raccolti dal vivo. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
371 lines
10 KiB
C#
371 lines
10 KiB
C#
using System.Diagnostics;
|
|
|
|
namespace Febe.Core.Engine;
|
|
|
|
/// <summary>Who uses the one page of Febe's browser.</summary>
|
|
public enum PageUser
|
|
{
|
|
/// <summary>The engine: a recipe of a session.</summary>
|
|
Engine,
|
|
|
|
/// <summary>The profile search (issue #37).</summary>
|
|
Search,
|
|
}
|
|
|
|
/// <summary>
|
|
/// One page, two users (issue #37): the engine's recipes and the profile search take turns on Febe's single browser
|
|
/// page. A FIFO queue with one holder at a time; cancellable; no timers. The engine takes a turn at the start of each
|
|
/// recipe and gives it back before an action that opens a page when the search is waiting (<see cref="YieldAsync"/>);
|
|
/// the search takes it for one step (and keeps it while Alberto keeps typing, bounded by the search itself).
|
|
/// </summary>
|
|
/// <remarks>The session never stops for a search: the search only slides in between two engine actions.</remarks>
|
|
public sealed class PageTurns
|
|
{
|
|
private readonly Lock _gate = new();
|
|
private readonly LinkedList<Waiter> _queue = new();
|
|
private PageTurn? _holder;
|
|
private PageUser? _lastUser;
|
|
private bool _searchTouchedPage;
|
|
private bool _restOwed;
|
|
private long _generation;
|
|
private List<TaskCompletionSource>? _freeWaiters;
|
|
|
|
/// <summary>Raised after every rest of the page (the search's photo queue forgets its addresses).</summary>
|
|
public event Action? Rested;
|
|
|
|
/// <summary>Raised when the rest of the page is left to the search (<see cref="TakeForRestAsync"/>).</summary>
|
|
public event Action? RestLeftToSearch;
|
|
|
|
/// <summary>
|
|
/// Changes every time a page is opened or put to rest: what the search typed in the box holds only within one
|
|
/// generation (the engine may open pages between two search steps without the search seeing it).
|
|
/// </summary>
|
|
public long Generation
|
|
{
|
|
get
|
|
{
|
|
lock (_gate)
|
|
{
|
|
return _generation;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// The engine wanted to rest the page while the search held or waited for it (end of a session, Alberto let go
|
|
/// of the browser): the search rests it when it is done, no session running. Cleared by a rest, by a page the
|
|
/// engine opens (the next session rests it), or by <see cref="ForgetOwedRest"/>.
|
|
/// </summary>
|
|
public bool RestOwed
|
|
{
|
|
get
|
|
{
|
|
lock (_gate)
|
|
{
|
|
return _restOwed;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>Who opened the page last (null before anyone did, or after a rest).</summary>
|
|
public PageUser? LastUser
|
|
{
|
|
get
|
|
{
|
|
lock (_gate)
|
|
{
|
|
return _lastUser;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>True from a search step until the next page the engine opens: the page state of a recipe may be gone.</summary>
|
|
public bool SearchTouchedPage
|
|
{
|
|
get
|
|
{
|
|
lock (_gate)
|
|
{
|
|
return _searchTouchedPage;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>Who holds the page now, if anyone.</summary>
|
|
public PageUser? Holder
|
|
{
|
|
get
|
|
{
|
|
lock (_gate)
|
|
{
|
|
return _holder?.User;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>True while <paramref name="user"/> waits for a turn.</summary>
|
|
public bool IsWaiting(PageUser user)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
return _queue.Any(w => w.Turn.User == user);
|
|
}
|
|
}
|
|
|
|
/// <summary>Waits for a turn of <paramref name="user"/>; dispose the result to give it back.</summary>
|
|
public async ValueTask<PageTurn> TakeAsync(PageUser user, CancellationToken ct)
|
|
{
|
|
var turn = new PageTurn(this, user);
|
|
await AcquireAsync(turn, ct).ConfigureAwait(false);
|
|
return turn;
|
|
}
|
|
|
|
/// <summary>
|
|
/// When the other user waits, gives the page to it and waits to have it back (FIFO, so right after the other's
|
|
/// step). Returns true when it yielded. Does nothing when <paramref name="turn"/> does not hold the page.
|
|
/// </summary>
|
|
public async ValueTask<bool> YieldAsync(PageTurn turn, CancellationToken ct)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(turn);
|
|
var other = turn.User == PageUser.Engine ? PageUser.Search : PageUser.Engine;
|
|
lock (_gate)
|
|
{
|
|
if (!ReferenceEquals(_holder, turn) || !_queue.Any(w => w.Turn.User == other))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
ReleaseLocked(turn);
|
|
}
|
|
|
|
await AcquireAsync(turn, ct).ConfigureAwait(false);
|
|
return true;
|
|
}
|
|
|
|
/// <summary>The engine's current turn, for the recipes' yield points; null when the engine holds none.</summary>
|
|
public PageTurn? EngineTurn
|
|
{
|
|
get
|
|
{
|
|
lock (_gate)
|
|
{
|
|
return _holder is { User: PageUser.Engine } turn ? turn : null;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// A turn for a navigation of the engine outside its recipes (the rest page, Alberto's login page): at once when
|
|
/// the page is free. When the search holds or waits for the page, returns null and records that the rest is left to
|
|
/// the search (<see cref="RestOwed"/>): it rests the page itself when it is done. Otherwise waits for the turn.
|
|
/// </summary>
|
|
public async ValueTask<PageTurn?> TakeForRestAsync(CancellationToken ct)
|
|
{
|
|
var turn = new PageTurn(this, PageUser.Engine);
|
|
bool leftToSearch;
|
|
lock (_gate)
|
|
{
|
|
if (_holder is null && _queue.Count == 0)
|
|
{
|
|
_holder = turn;
|
|
return turn;
|
|
}
|
|
|
|
leftToSearch = _holder?.User == PageUser.Search || _queue.Any(w => w.Turn.User == PageUser.Search);
|
|
if (leftToSearch)
|
|
{
|
|
_restOwed = true;
|
|
}
|
|
}
|
|
|
|
if (leftToSearch)
|
|
{
|
|
RestLeftToSearch?.Invoke();
|
|
return null;
|
|
}
|
|
|
|
await AcquireAsync(turn, ct).ConfigureAwait(false);
|
|
return turn;
|
|
}
|
|
|
|
/// <summary>The search will not rest the page after all (it stays as it is after a verification, no browser).</summary>
|
|
public void ForgetOwedRest()
|
|
{
|
|
lock (_gate)
|
|
{
|
|
_restOwed = false;
|
|
}
|
|
}
|
|
|
|
/// <summary>Completes when nobody holds the page and nobody waits.</summary>
|
|
public Task WhenFreeAsync(CancellationToken ct)
|
|
{
|
|
TaskCompletionSource tcs;
|
|
lock (_gate)
|
|
{
|
|
if (_holder is null && _queue.Count == 0)
|
|
{
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
(_freeWaiters ??= []).Add(tcs);
|
|
}
|
|
|
|
return tcs.Task.WaitAsync(ct);
|
|
}
|
|
|
|
/// <summary>A page was opened by <paramref name="user"/>.</summary>
|
|
public void NoteOpened(PageUser user)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
_lastUser = user;
|
|
_searchTouchedPage = user == PageUser.Search;
|
|
_generation++;
|
|
if (user == PageUser.Engine)
|
|
{
|
|
// A session is on the page again: its end rests it.
|
|
_restOwed = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>The search used the page (typed, opened its panel): the engine's page state may be gone.</summary>
|
|
public void NoteSearchUsedPage()
|
|
{
|
|
lock (_gate)
|
|
{
|
|
_searchTouchedPage = true;
|
|
}
|
|
}
|
|
|
|
/// <summary>The page went to rest (spec §6.1).</summary>
|
|
public void NoteRested()
|
|
{
|
|
lock (_gate)
|
|
{
|
|
_lastUser = null;
|
|
_searchTouchedPage = false;
|
|
_restOwed = false;
|
|
_generation++;
|
|
}
|
|
|
|
Rested?.Invoke();
|
|
}
|
|
|
|
internal void Release(PageTurn turn)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
if (ReferenceEquals(_holder, turn))
|
|
{
|
|
ReleaseLocked(turn);
|
|
}
|
|
}
|
|
}
|
|
|
|
private async ValueTask AcquireAsync(PageTurn turn, CancellationToken ct)
|
|
{
|
|
Waiter waiter;
|
|
lock (_gate)
|
|
{
|
|
if (_holder is null && _queue.Count == 0)
|
|
{
|
|
_holder = turn;
|
|
return;
|
|
}
|
|
|
|
waiter = new Waiter(turn, new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously));
|
|
waiter.Node = _queue.AddLast(waiter);
|
|
}
|
|
|
|
try
|
|
{
|
|
await waiter.Ready.Task.WaitAsync(ct).ConfigureAwait(false);
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
if (waiter.Node?.List is not null)
|
|
{
|
|
_queue.Remove(waiter.Node);
|
|
NotifyFreeLocked();
|
|
throw;
|
|
}
|
|
|
|
// Granted while being cancelled: give it back at once.
|
|
if (ReferenceEquals(_holder, turn))
|
|
{
|
|
ReleaseLocked(turn);
|
|
}
|
|
}
|
|
|
|
throw;
|
|
}
|
|
}
|
|
|
|
private void ReleaseLocked(PageTurn turn)
|
|
{
|
|
Debug.Assert(ReferenceEquals(_holder, turn), "Only the holder gives the page back.");
|
|
_holder = null;
|
|
if (_queue.First is { } first)
|
|
{
|
|
_queue.RemoveFirst();
|
|
first.Value.Node = null;
|
|
_holder = first.Value.Turn;
|
|
first.Value.Ready.TrySetResult();
|
|
return;
|
|
}
|
|
|
|
NotifyFreeLocked();
|
|
}
|
|
|
|
private void NotifyFreeLocked()
|
|
{
|
|
if (_holder is not null || _queue.Count > 0 || _freeWaiters is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
foreach (var waiter in _freeWaiters)
|
|
{
|
|
waiter.TrySetResult();
|
|
}
|
|
|
|
_freeWaiters = null;
|
|
}
|
|
|
|
private sealed class Waiter(PageTurn turn, TaskCompletionSource ready)
|
|
{
|
|
public PageTurn Turn { get; } = turn;
|
|
|
|
public TaskCompletionSource Ready { get; } = ready;
|
|
|
|
public LinkedListNode<Waiter>? Node { get; set; }
|
|
}
|
|
}
|
|
|
|
/// <summary>A turn on the page; disposing it gives the page back (to the next in the queue).</summary>
|
|
public sealed class PageTurn : IAsyncDisposable, IDisposable
|
|
{
|
|
private readonly PageTurns _owner;
|
|
|
|
internal PageTurn(PageTurns owner, PageUser user)
|
|
{
|
|
_owner = owner;
|
|
User = user;
|
|
}
|
|
|
|
public PageUser User { get; }
|
|
|
|
public void Dispose() => _owner.Release(this);
|
|
|
|
public ValueTask DisposeAsync()
|
|
{
|
|
_owner.Release(this);
|
|
return ValueTask.CompletedTask;
|
|
}
|
|
}
|