[TVChannel] Implement channel listener
authorPrzemyslaw Ciezkowski <p.ciezkowski@samsung.com>
Mon, 15 Dec 2014 10:42:21 +0000 (11:42 +0100)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Mon, 2 Feb 2015 11:05:00 +0000 (20:05 +0900)
Change-Id: Ifdfa529f475d8b07bc380def645c71a53d7ab0ca
Signed-off-by: Przemyslaw Ciezkowski <p.ciezkowski@samsung.com>
src/tvchannel/tvchannel_api.js

index 31099c1..821f984 100644 (file)
@@ -15,7 +15,43 @@ var WindowType = {
   MAIN: 'MAIN'
 };
 
-// TVChannelManager interface
+function ListenerManager(native, listenerName) {
+  this.listeners = {};
+  this.nextId = 1;
+  native.addListener(listenerName, this.onListenerCalled.bind(this));
+}
+
+ListenerManager.prototype.onListenerCalled = function(msg) {
+  for (var key in this.listeners) {
+    if (this.listeners.hasOwnProperty(key)) {
+      this.listeners[key](msg.channel);
+    }
+  }
+};
+
+ListenerManager.prototype.addListener = function(callback) {
+  var id = this.nextId;
+  this.listeners[id] = callback;
+  ++this.nextId;
+  return id;
+};
+
+ListenerManager.prototype.removeListener = function(watchId) {
+  if (this.listeners.hasOwnProperty(watchId)) {
+    delete this.listeners[watchId];
+  }
+};
+
+
+/**
+ * @const
+ * @type {string}
+ */
+var CHANNEL_CHANGE_LISTENER = 'ChannelChanged';
+
+var channelListener = new ListenerManager(native, CHANNEL_CHANGE_LISTENER);
+
+//TVChannelManager interface
 function TVChannelManager() {
   if (!(this instanceof TVChannelManager)) {
     throw new TypeError;
@@ -88,12 +124,26 @@ TVChannelManager.prototype.getCurrentProgram = function(windowType) {
   return native.getResultObject(ret);
 };
 
-TVChannelManager.prototype.addChannelChangeListener = function(successCallback, windowType) {
-  return undefined;
+TVChannelManager.prototype.addChannelChangeListener = function(callback, windowType) {
+  var args = validator.validateArgs(arguments, [
+    {
+      name: 'callback',
+      type: validator.Types.FUNCTION
+    },
+    {
+      name: 'windowType',
+      optional: true,
+      nullable: true,
+      type: validator.Types.ENUM,
+      values: validatorType.getValues(WindowType)
+    }
+  ]);
+
+  return channelListener.addListener(args.callback);
 };
 
 TVChannelManager.prototype.removeChannelChangeListener = function(listenerId) {
-  return undefined;
+  channelListener.removeListener(listenerId);
 };
 
 TVChannelManager.prototype.addProgramChangeListener = function(successCallback, windowType) {