Transient Channels

Transient Queues have failed. I wrote a simple demo the other day of a little pixar-like lamp guy who moves his joints when you push the arrow keys. I had to make him move them smoothly, lest he explode due to the integration error. I could have used a transient queue to do exactly what I wanted, but I didn’t. Why not? Let’s look at the transformation of some code to using them:

    use Class::Closure;
    sub CLASS {
        my $x = 0;
        Glop->input->Keyboard->register(
            left => sub { $x = -1; }
        );
    }

When I want to smoothly move $x to -1, it turns into:

    use Class::Closure;
    sub CLASS {
        my $x = 0;
        my $queue = Glop::TransientQueue->new;
        Glop->input->Keyboard->register(
            left => sub { $queue->push(Timed Glop::Transient (1, sub { $x -= STEP }) }
        );
        method step => sub { $queue->run; }
    }

That’s not really that much more is it? Only three lines to type. But one must realize that my vision of Glop is not to reduce typing, it’s to reduce brain strain. If I were worried about the poor programmer’s fingers, I wouldn’t have them typing Glop all over the place (except to advertize for my cult). But three lines are a lot compared to, say, Glop->input->Keyboard->register, which is just “register some callbacks.” In order to use a TransientQueue you have to 1) create a TransientQueue object in your class, 2) Push on a transient, and 3) add a step function to call the queue every frame. That’s a lot of conceptual load for a simple task. And it’s a one-time load, but it was enough to encourage me not to use it, and I wrote the damn software.

So, I’m killing them. They are no longer part of Glop. What I’m adding in their place are transient channels. They’re the exact same thing, except that you don’t have to create them or call run on them. They’re integrated into the Glop kernel. Here is the change using channels instead of queues:

    use Class::Closure;
    sub CLASS {
        my $x = 0;
        Glop->input->Keyboard->register(
            left => sub { Timed Glop::Transient (1, sub { $x -= STEP }) }
        );
    }

You hardly have to know what’s going on. They just do their job, and one second from now, they’re done and they’re gone. If you need the queue’s queueing behavior, then you do still have to create an object. But you don’t have to call run on it yourself; that’s done automatically. So even when you need the more sophistocated behavior, one conceptual strain point is gone. But most of the time, it’s all gone, and significantly easier than doing it yourself.

About these ads

2 thoughts on “Transient Channels

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s