Observer.lua 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. --[[
  2. * @author PureMVC Lua Native Port by Sean
  3. * @author Copyright(c) 2006-2012 Futurescale, Inc., Some rights reserved.
  4. *
  5. * @class puremvc.Observer
  6. *
  7. * A base Observer implementation.
  8. *
  9. * An Observer is an object that encapsulates information
  10. * about an interested object with a method that should
  11. * be called when a particular Notification is broadcast.
  12. *
  13. * In PureMVC, the Observer class assumes these responsibilities:
  14. *
  15. * - Encapsulate the notification (callback) method of the interested object.
  16. * - Encapsulate the notification context (this) of the interested object.
  17. * - Provide methods for setting the notification method and context.
  18. * - Provide a method for notifying the interested object.
  19. *
  20. *
  21. * The notification method on the interested object should take
  22. * one parameter of type Notification.
  23. *
  24. *
  25. * @param {Function} notifyMethod
  26. * the notification method of the interested object
  27. * @param {Object} notifyContext
  28. * the notification context of the interested object
  29. * @constructor
  30. ]]
  31. local Observer = class('Observer')
  32. function Observer:ctor(notifyMethod, notifyContext)
  33. self:setNotifyMethod(notifyMethod)
  34. self:setNotifyContext(notifyContext)
  35. end
  36. --[[
  37. * Set the Observers notification method.
  38. *
  39. * The notification method should take one parameter of type Notification
  40. * @param {Function} notifyMethod
  41. * the notification (callback) method of the interested object.
  42. * @return {void}
  43. ]]
  44. function Observer:setNotifyMethod(notifyMethod)
  45. self.notify = notifyMethod
  46. end
  47. --[[
  48. * Set the Observers notification context.
  49. *
  50. * @param {Object} notifyContext
  51. * the notification context (this) of the interested object.
  52. *
  53. * @return {void}
  54. ]]
  55. function Observer:setNotifyContext(notifyContext)
  56. self.context = notifyContext
  57. end
  58. --[[
  59. * Get the Function that this Observer will invoke when it is notified.
  60. *
  61. * @private
  62. * @return {Function}
  63. ]]
  64. function Observer:getNotifyMethod()
  65. return self.notify
  66. end
  67. --[[
  68. * Get the Object that will serve as the Observers callback execution context
  69. *
  70. * @private
  71. * @return {Object}
  72. ]]
  73. function Observer:getNotifyContext()
  74. return self.context
  75. end
  76. --[[
  77. * Notify the interested object.
  78. *
  79. * @param {puremvc.Notification} notification
  80. * The Notification to pass to the interested objects notification method
  81. * @return {void}
  82. ]]
  83. function Observer:notifyObserver(notification)
  84. self.notify(self.context, notification)
  85. end
  86. --[[
  87. * Compare an object to this Observers notification context.
  88. *
  89. * @param {Object} object
  90. *
  91. * @return {boolean}
  92. ]]
  93. function Observer:compareNotifyContext(object)
  94. return object == self.context
  95. end
  96. return Observer