Socket
Book a DemoInstallSign in
Socket

slots

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

slots

Slot manager for event emitters!

latest
Source
npmnpm
Version
0.0.1
Version published
Maintainers
1
Created
Source

Slots — An event emitter with slots!

Slots does just that, it provides slots for your event emitter bonds! There is also a SlotManager for combining your slots from different emitters, giving you a single interface for wrangling all your bonds.

A slot is an object that holds the bond between a callback, and an event emitter. This is convenient because you can just tell the slot to disconnect, without having to remember both parts of the bond.

Documentation

Examples

With a state graph

With socket.io, one usually only wants a user to be able to perform an action in a certain state. Such as being able to kick other players if they are the host of the game. With Slots— you can simply remove the event callback for players who aren't host, as opposed to writing logic instead the callback to verify the player's state.

A great way to handle 'state' is with a state graph. It keeps your code simple, and your state isolated. Below is some pseudo-code to illustrate my example of hosts only being able to click players:

function Player(socket, game, isHost) {
    var graph = new StateGraph();

    graph.state('host', function (state) {
        // When the player enters the host state, bind the kick event
        state.slot = socket.on('kick', function (player) {
            game.kick(player);
        });
    }).on('end', function (state) {
        // Disconnect the kick event when the player is no longer a host
        state.slot.remove();
    });

    graph.state('player', function () {
        // Poor players... they can't do anything!
    });

    if (isHost) {
        graph.go('host');
    } else {
        graph.go('player');
    }

    return {
        host: function () {
            graph.go('host');
        },
        player: function () {
            graph.go('player');
        }
    };
}

FAQs

Package last updated on 15 May 2012

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts