"use strict";

var assert = require('assert');
var Rx = require('rx');
var EventEmitter = require('events').EventEmitter;

var eventEmitter = new EventEmitter();
var source = Rx.Observable.fromEvent(eventEmitter,'signalChange');

var Wire = function(name) {
    
    var signal = false;

    var observers = [];

    this.getSignal = function() {
	return signal;
    };

    this.setSignal = function(s) {
	signal = !!s;
	eventEmitter.emit('signalChange', this, signal);
    };

    return this;
};

var Inverter = function(input,output) {

    source.subscribe(function(data) {
	if (data === input) 
	    output.setSignal(!input.getSignal());
    });

    input.setSignal(input.getSignal());

    return this;
};

var And = function(input1,input2,output) {
    input1.onChange(function(s) {
	output.setSignal(s && input2.getSignal());
    });

    input2.onChange(function(s) {
	output.setSignal(s && input1.getSignal());
    });
};

var Or = function(input1,input2,output) {
    input1.onChange(function(s) {
	output.setSignal(s || input2.getSignal());
    });

    input2.onChange(function(s) {
	output.setSignal(s || input1.getSignal());
    });
};

var makeWire = function() {
    return new Wire();
};

var makeInverter = function(input,output) {
    return new Inverter(input,output);
};

var makeAnd = function(input1,input2,output) {
    return new And(input1,input2,output);
};

var makeOr = function(input1,input2,output) {
    return new Or(input1,input2,output);
};

exports.makeWire = makeWire;
exports.makeInverter = makeInverter;
exports.makeAnd = makeAnd;
exports.makeOr = makeOr;

@@ -205,16 +205,17 @@
 Emitter,
+ 
 'signalC
@@ -458,16 +458,8 @@
 this
-, signal
 );%0A 
@@ -574,16 +574,21 @@
 if (data
+.wire
  === inp
@@ -615,33 +615,26 @@
 Signal(!
-input.getSignal()
+data.value
 );%0A    %7D

use an event emitter to subscribe to sequences