package com.phidgets
{
	import com.phidgets.PhidgetDictionary;
	import com.phidgets.events.PhidgetDictionaryEvent;
	import flash.events.EventDispatcher;
	
	public class PhidgetDictionaryKeyListener extends EventDispatcher
	{
		private var _pattern:String;
		private var _dict:PhidgetDictionary;
		private var _lid:int = com.phidgets.Constants.PUNK_INT;
		
		public function PhidgetDictionaryKeyListener(dict:PhidgetDictionary, pattern:String) {
			_dict = dict;
			_pattern = pattern;
		}
		
		private function onDictData(key:String, val:String, reason:int):void
		{
			if(reason == Constants.ENTRY_REMOVING)
				dispatchEvent(new PhidgetDictionaryEvent(PhidgetDictionaryEvent.KEY_REMOVAL, this._dict, key, val));
			else
				dispatchEvent(new PhidgetDictionaryEvent(PhidgetDictionaryEvent.KEY_CHANGE, this._dict, key, val));
		}
		
		public function start():void {
			_lid = _dict._phidgetSocket.setListener(_pattern, onDictData);
		}
		public function stop():void {
			if(_lid != com.phidgets.Constants.PUNK_INT){
				_dict._phidgetSocket.removeListener(_lid);
				_lid = com.phidgets.Constants.PUNK_INT;
			}
		}
	}
}