NAME

Chandra::Bind - JavaScript to Perl function binding

SYNOPSIS

use Chandra::Bind;

my $bind = Chandra::Bind->new(app => $app);

# Register a Perl sub callable from JavaScript
$bind->bind('greet', sub {
    my ($name) = @_;
    return "Hello, $name!";
});

# Check and list bindings
if ($bind->is_bound('greet')) { ... }
my @names = $bind->list;

# Remove a binding
$bind->unbind('greet');

# In JavaScript:
# const result = await window.chandra.invoke('greet', ['World']);

DESCRIPTION

Chandra::Bind manages the registry of Perl functions that can be called from JavaScript. It handles JSON serialization, dispatching, and error capture via Chandra::Error.

CONSTRUCTOR

new

my $bind = Chandra::Bind->new(app => $app);

Takes the Chandra::App instance that owns this binding registry.

METHODS

bind

$bind->bind($name, sub { ... });

Register a Perl subroutine as callable from JavaScript. $name is the function name used in window.chandra.invoke($name, \@args).

unbind

$bind->unbind($name);

Remove a previously bound function.

is_bound

my $bool = $bind->is_bound($name);

Returns true if a function with the given name is bound.

list

my @names = $bind->list;

Returns the names of all bound functions.

register_handler

Chandra::Bind->register_handler($id, sub { ... });

Class method. Register a handler by a specific ID. Used internally by Chandra::Element for event handler wiring.

dispatch

$bind->dispatch($json_string);

Dispatch a call from JavaScript. The JSON string contains name, args, and seq fields. Called internally by the XS callback. Errors are captured via "capture" in Chandra::Error.

encode_result

my $json = $bind->encode_result($result);

Encode a Perl value to JSON for returning to JavaScript.

js_resolve

$bind->js_resolve($seq, $result, $error);

Generate and dispatch JavaScript to resolve or reject the promise identified by $seq.

SEE ALSO

Chandra::App, Chandra::Bridge, Chandra::Element, Chandra::Error