Skip to content

Instantly share code, notes, and snippets.

@Xliff
Last active January 15, 2020 23:41
Show Gist options
  • Save Xliff/ba271400c195e8412736b04738a9a628 to your computer and use it in GitHub Desktop.
Save Xliff/ba271400c195e8412736b04738a9a628 to your computer and use it in GitHub Desktop.
TappedSignals

Given a supplier in $s, this allows a consumer to know if $signal has been tapped by setting
the appropriate entry in %sigs

sub create-signal-supply (%sigs, $signal, $s) {
  my $supply = $s.Supply;
  $supply.^can('tap')[0].wrap: -> |c {
    %sigs{$signal} = True;
    nextsame;
  });
  $supply;
}

This should work, however getting the following...

Attempt to return outside of immediately-enclosing Routine (i.e. `return` execution is outside the dynamic scope of the Routine where `return` was used)
  in block  at /home/cbwood/Projects/p6-GLib/lib/GLib/Raw/Subs.pm6 (GLib::Raw::Subs) line 233
  in block  at /home/cbwood/Projects/p6-GLib/lib/GLib/Raw/Subs.pm6 (GLib::Raw::Subs) line 233
  in block  at /home/cbwood/Projects/p6-GLib/lib/GLib/Raw/Subs.pm6 (GLib::Raw::Subs) line 233
  in submethod BUILD at /home/cbwood/Projects/p6-GtkPlus/lib/GTK/Application.pm6 (GTK::Application) line 67
  in method new at /home/cbwood/Projects/p6-GtkPlus/lib/GTK/Application.pm6 (GTK::Application) line 152
  in block <unit> at t/0010-widget.t line 11

Hah! This is the fixed version!

sub create-signal-supply (%sigs, $signal, $s) {
  my $supply = $s.Supply;
  $supply.^lookup('tap').wrap: my method (|c) {
    %sigs{$signal} = True;
    nextsame;
  };
  $supply;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment