/*
 * Licensed to Cloudkick, Inc ('Cloudkick') under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * Cloudkick licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

var log = require('./log');

var pending = {};

exports.CONFIG_DONE = "dislocate.config.done";

exports.pub = function(path, data)
{
	//log.debug("pub:", path, data);
	if (pending[path] !== undefined) {
		var arr = pending[path];
		for (var i = 0; i != arr.length; i++) {
			arr[i](data);
		}
	}
};

exports.sub = function(path, cb)
{
	if (pending[path] === undefined) {
		pending[path] = [];
	}
	pending[path].push(cb);
	//log.debug("sub:", path, cb);
};

/*
 * Licensed to Cloudkick, Inc ('Cloudkick') under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * Cloudkick licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

var log = require('./log');

var pending = {};

exports.CONFIG_DONE = "dislocate.config.done";
exports.STATE_START ='dislocate.state.start';
exports.STATE_STOP ='dislocate.state.stop';
exports.STATE_EXIT ='dislocate.state.exit';

exports.pub = function(path, data)
{
  if (path === undefined) {
    throw "pubsub: path must be defined, did you forget to add a new event type?";
  }

	//log.debug("pub:", path, data);
	if (pending[path] !== undefined) {
		var arr = pending[path];
		pending[path] = arr.filter(function(i) {
			i.cb(data);
		  if (i.once == true) {
		    return false;
		  }
		  return true;
		});
	}
};

exports.sub = function(path, cb, once)
{
  if (path === undefined) {
    throw "pubsub: path must be defined, did you forget to add a new event type?";
  }

  if (once === undefined) {
    once = false;
  }

	if (pending[path] === undefined) {
		pending[path] = [];
	}

	//log.debug("sub:", path, cb);
	pending[path].push({'cb': cb, 'once': once});
};

Add support for one time subs, and add more event types