Kernel-Announcements.st 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. Smalltalk current createPackage: 'Kernel-Announcements'!
  2. Object subclass: #AnnouncementSubscription
  3. instanceVariableNames: 'valuable announcementClass'
  4. package: 'Kernel-Announcements'!
  5. !AnnouncementSubscription commentStamp!
  6. The subscription is a single entry in a subscription registry of an `Announcer`.
  7. Several subscriptions by the same object is possible.!
  8. !AnnouncementSubscription methodsFor: 'accessing'!
  9. announcementClass
  10. ^ announcementClass
  11. !
  12. announcementClass: aClass
  13. announcementClass := aClass
  14. !
  15. block
  16. "Use #valuable instead"
  17. self deprecatedAPI.
  18. ^ self valuable
  19. !
  20. block: aValuable
  21. "Use #valuable instead"
  22. self deprecatedAPI.
  23. self valuable: aValuable
  24. !
  25. receiver
  26. ^ self valuable receiver
  27. !
  28. valuable
  29. ^ valuable
  30. !
  31. valuable: aValuable
  32. valuable := aValuable
  33. ! !
  34. !AnnouncementSubscription methodsFor: 'announcing'!
  35. deliver: anAnnouncement
  36. (self handlesAnnouncement: anAnnouncement)
  37. ifTrue: [self valuable value: anAnnouncement]
  38. !
  39. handlesAnnouncement: anAnnouncement
  40. "anAnnouncement might be announced from within another Amber environment"
  41. ^ (Smalltalk current at: self announcementClass name)
  42. ifNil: [ ^ false ]
  43. ifNotNil: [ :class |
  44. class includesBehavior: (Smalltalk current at: anAnnouncement class theNonMetaClass name) ]
  45. ! !
  46. Object subclass: #Announcer
  47. instanceVariableNames: 'registry subscriptions'
  48. package: 'Kernel-Announcements'!
  49. !Announcer commentStamp!
  50. The code is based on the announcements as [described by Vassili Bykov](http://www.cincomsmalltalk.com/userblogs/vbykov/blogView?searchCategory=Announcements%20Framework).
  51. The Announcer holds annoncement subscriptions (`AnnouncementSubscription`) in a private registry.
  52. Use `#on:do:` to register subscriptions.!
  53. !Announcer methodsFor: 'announcing'!
  54. announce: anAnnouncement
  55. subscriptions do: [ :each |
  56. each deliver: anAnnouncement ]
  57. ! !
  58. !Announcer methodsFor: 'initialization'!
  59. initialize
  60. super initialize.
  61. subscriptions := OrderedCollection new
  62. ! !
  63. !Announcer methodsFor: 'subscribing'!
  64. on: aClass do: aBlock
  65. subscriptions add: (AnnouncementSubscription new
  66. valuable: aBlock;
  67. announcementClass: aClass;
  68. yourself)
  69. !
  70. on: aClass send: aSelector to: anObject
  71. subscriptions add: (AnnouncementSubscription new
  72. valuable: (MessageSend new
  73. receiver: anObject;
  74. selector: aSelector;
  75. yourself);
  76. announcementClass: aClass;
  77. yourself)
  78. !
  79. unsubscribe: anObject
  80. subscriptions := subscriptions reject: [ :each |
  81. each receiver = anObject ]
  82. ! !
  83. Announcer subclass: #SystemAnnouncer
  84. instanceVariableNames: ''
  85. package: 'Kernel-Announcements'!
  86. !SystemAnnouncer commentStamp!
  87. My unique instance #current is the global announcer handling all Amber system-related announces!
  88. SystemAnnouncer class instanceVariableNames: 'current'!
  89. !SystemAnnouncer class methodsFor: 'accessing'!
  90. current
  91. ^ current ifNil: [ current := super new ]
  92. ! !
  93. !SystemAnnouncer class methodsFor: 'instance creation'!
  94. new
  95. self shouldNotImplement
  96. ! !
  97. Object subclass: #SystemAnnouncement
  98. instanceVariableNames: 'theClass'
  99. package: 'Kernel-Announcements'!
  100. !SystemAnnouncement commentStamp!
  101. I am the superclass of all system announcements!
  102. !SystemAnnouncement methodsFor: 'accessing'!
  103. theClass
  104. ^ theClass
  105. !
  106. theClass: aClass
  107. theClass := aClass
  108. ! !
  109. SystemAnnouncement subclass: #ClassAnnouncement
  110. instanceVariableNames: 'theClass'
  111. package: 'Kernel-Announcements'!
  112. !ClassAnnouncement methodsFor: 'accessing'!
  113. theClass
  114. ^ theClass
  115. !
  116. theClass: aClass
  117. theClass := aClass
  118. ! !
  119. ClassAnnouncement subclass: #ClassAdded
  120. instanceVariableNames: ''
  121. package: 'Kernel-Announcements'!
  122. !ClassAdded commentStamp!
  123. I am emitted when a class is added to the system.
  124. See ClassBuilder >> #addSubclassOf:... methods!
  125. ClassAnnouncement subclass: #ClassCommentChanged
  126. instanceVariableNames: ''
  127. package: 'Kernel-Announcements'!
  128. !ClassCommentChanged commentStamp!
  129. I am emitted when the comment of a class changes. (Behavior >> #comment)!
  130. ClassAnnouncement subclass: #ClassDefinitionChanged
  131. instanceVariableNames: ''
  132. package: 'Kernel-Announcements'!
  133. !ClassDefinitionChanged commentStamp!
  134. I am emitted when the defintion of a class changes.
  135. See ClassBuilder >> #class:instanceVariableNames:!
  136. ClassAnnouncement subclass: #ClassMigrated
  137. instanceVariableNames: 'oldClass'
  138. package: 'Kernel-Announcements'!
  139. !ClassMigrated methodsFor: 'accessing'!
  140. oldClass
  141. ^ oldClass
  142. !
  143. oldClass: aClass
  144. oldClass := aClass
  145. ! !
  146. ClassAnnouncement subclass: #ClassMoved
  147. instanceVariableNames: 'oldPackage'
  148. package: 'Kernel-Announcements'!
  149. !ClassMoved methodsFor: 'accessing'!
  150. oldPackage
  151. ^ oldPackage
  152. !
  153. oldPackage: aPackage
  154. oldPackage := aPackage
  155. ! !
  156. ClassAnnouncement subclass: #ClassRemoved
  157. instanceVariableNames: ''
  158. package: 'Kernel-Announcements'!
  159. !ClassRemoved commentStamp!
  160. I am emitted when a class is removed.
  161. See Smalltalk >> #removeClass:!
  162. ClassAnnouncement subclass: #ClassRenamed
  163. instanceVariableNames: ''
  164. package: 'Kernel-Announcements'!
  165. !ClassRenamed commentStamp!
  166. I am emitted when a class is renamed.
  167. See ClassBuilder >> #renameClass:to:!
  168. SystemAnnouncement subclass: #MethodAnnouncement
  169. instanceVariableNames: 'method'
  170. package: 'Kernel-Announcements'!
  171. !MethodAnnouncement methodsFor: 'accessing'!
  172. method
  173. ^ method
  174. !
  175. method: aCompiledMethod
  176. method := aCompiledMethod
  177. ! !
  178. MethodAnnouncement subclass: #MethodAdded
  179. instanceVariableNames: ''
  180. package: 'Kernel-Announcements'!
  181. MethodAnnouncement subclass: #MethodModified
  182. instanceVariableNames: 'oldMethod'
  183. package: 'Kernel-Announcements'!
  184. !MethodModified methodsFor: 'accessing'!
  185. oldMethod
  186. ^ oldMethod
  187. !
  188. oldMethod: aMethod
  189. oldMethod := aMethod
  190. ! !
  191. MethodAnnouncement subclass: #MethodMoved
  192. instanceVariableNames: 'oldProtocol'
  193. package: 'Kernel-Announcements'!
  194. !MethodMoved methodsFor: 'accessing'!
  195. oldProtocol
  196. ^ oldProtocol
  197. !
  198. oldProtocol: aString
  199. oldProtocol := aString
  200. ! !
  201. MethodAnnouncement subclass: #MethodRemoved
  202. instanceVariableNames: ''
  203. package: 'Kernel-Announcements'!
  204. SystemAnnouncement subclass: #PackageAnnouncement
  205. instanceVariableNames: 'package'
  206. package: 'Kernel-Announcements'!
  207. !PackageAnnouncement methodsFor: 'accessing'!
  208. package
  209. ^ package
  210. !
  211. package: aPackage
  212. package := aPackage
  213. ! !
  214. PackageAnnouncement subclass: #PackageAdded
  215. instanceVariableNames: ''
  216. package: 'Kernel-Announcements'!
  217. PackageAnnouncement subclass: #PackageRemoved
  218. instanceVariableNames: ''
  219. package: 'Kernel-Announcements'!
  220. SystemAnnouncement subclass: #ProtocolAnnouncement
  221. instanceVariableNames: 'theClass protocol'
  222. package: 'Kernel-Announcements'!
  223. !ProtocolAnnouncement methodsFor: 'accessing'!
  224. protocol
  225. ^ protocol
  226. !
  227. protocol: aString
  228. protocol := aString
  229. !
  230. theClass
  231. ^ theClass
  232. !
  233. theClass: aClass
  234. theClass := aClass
  235. ! !
  236. ProtocolAnnouncement subclass: #ProtocolAdded
  237. instanceVariableNames: ''
  238. package: 'Kernel-Announcements'!
  239. ProtocolAnnouncement subclass: #ProtocolRemoved
  240. instanceVariableNames: ''
  241. package: 'Kernel-Announcements'!