'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
var Emitter = require('./Emitter');
var each = require('./each');
var some = require('./some');
var toArr = require('./toArr');
exports = Emitter.extend({
    className: 'State',
    initialize: function(initial, events) {
        this.callSuper(Emitter, 'initialize');
        this.current = initial;
        var self = this;
        each(events, function(event, key) {
            self[key] = buildEvent(key, event);
        });
    },
    is: function(state) {
        return this.current === state;
    }
});
function buildEvent(name, event) {
    var from = toArr(event.from);
    var to = event.to;
    return function() {
        var args = toArr(arguments);
        args.unshift(name);
        var hasEvent = some(
            from,
            function(val) {
                return this.current === val;
            },
            this
        );
        if (hasEvent) {
            this.current = to;
            this.emit.apply(this, args);
        } else {
            this.emit(
                'error',
                new Error(this.current + ' => ' + to + ' error'),
                name
            );
        }
    };
}
 
module.exports = exports;