Kernel-Announcements.st 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. Smalltalk createPackage: 'Kernel-Announcements'!
  2. Object subclass: #AnnouncementSubscription
  3. instanceVariableNames: 'valuable announcementClass'
  4. package: 'Kernel-Announcements'!
  5. !AnnouncementSubscription commentStamp!
  6. I am 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 globals at: self announcementClass name)
  42. ifNil: [ ^ false ]
  43. ifNotNil: [ :class |
  44. (Smalltalk globals at: anAnnouncement class theNonMetaClass name) includesBehavior: class ]
  45. ! !
  46. Object subclass: #Announcer
  47. instanceVariableNames: 'registry subscriptions'
  48. package: 'Kernel-Announcements'!
  49. !Announcer commentStamp!
  50. I hold annoncement subscriptions (instances of `AnnouncementSubscription`) in a private registry.
  51. I announce (trigger) announces, which are then dispatched to all subscriptions.
  52. The code is based on the announcements as [described by Vassili Bykov](http://www.cincomsmalltalk.com/userblogs/vbykov/blogView?searchCategory=Announcements%20Framework).
  53. ## API
  54. Use `#announce:` to trigger an announcement.
  55. Use `#on:do:` or `#on:send:to:` to register subscriptions.
  56. When using `#on:send:to:`, unregistration can be done with `#unregister:`.
  57. ## Usage example:
  58. SystemAnnouncer current
  59. on: ClassAdded
  60. do: [ :ann | window alert: ann theClass name, ' added' ].!
  61. !Announcer methodsFor: 'announcing'!
  62. announce: anAnnouncement
  63. subscriptions do: [ :each |
  64. each deliver: anAnnouncement ]
  65. ! !
  66. !Announcer methodsFor: 'initialization'!
  67. initialize
  68. super initialize.
  69. subscriptions := OrderedCollection new
  70. ! !
  71. !Announcer methodsFor: 'subscribing'!
  72. on: aClass do: aBlock
  73. subscriptions add: (AnnouncementSubscription new
  74. valuable: aBlock;
  75. announcementClass: aClass;
  76. yourself)
  77. !
  78. on: aClass doOnce: aBlock
  79. | subscription |
  80. subscription := AnnouncementSubscription new
  81. announcementClass: aClass;
  82. yourself.
  83. subscription valuable: [ :ann |
  84. subscriptions remove: subscription.
  85. aBlock value: ann ].
  86. subscriptions add: subscription
  87. !
  88. on: aClass send: aSelector to: anObject
  89. subscriptions add: (AnnouncementSubscription new
  90. valuable: (MessageSend new
  91. receiver: anObject;
  92. selector: aSelector;
  93. yourself);
  94. announcementClass: aClass;
  95. yourself)
  96. !
  97. unsubscribe: anObject
  98. subscriptions := subscriptions reject: [ :each |
  99. each receiver = anObject ]
  100. ! !
  101. Announcer subclass: #SystemAnnouncer
  102. instanceVariableNames: ''
  103. package: 'Kernel-Announcements'!
  104. !SystemAnnouncer commentStamp!
  105. My unique instance is the global announcer handling all Amber system-related announces.
  106. ## API
  107. Access to the unique instance is done via `#current`!
  108. SystemAnnouncer class instanceVariableNames: 'current'!
  109. !SystemAnnouncer class methodsFor: 'accessing'!
  110. current
  111. ^ current ifNil: [ current := super new ]
  112. ! !
  113. !SystemAnnouncer class methodsFor: 'instance creation'!
  114. new
  115. self shouldNotImplement
  116. ! !
  117. Object subclass: #SystemAnnouncement
  118. instanceVariableNames: ''
  119. package: 'Kernel-Announcements'!
  120. !SystemAnnouncement commentStamp!
  121. I am the superclass of all system announcements!
  122. !SystemAnnouncement class methodsFor: 'helios'!
  123. heliosClass
  124. ^ 'announcement'
  125. ! !
  126. SystemAnnouncement subclass: #ClassAnnouncement
  127. instanceVariableNames: 'theClass'
  128. package: 'Kernel-Announcements'!
  129. !ClassAnnouncement commentStamp!
  130. I am the abstract superclass of class-related announcements.!
  131. !ClassAnnouncement methodsFor: 'accessing'!
  132. theClass
  133. ^ theClass
  134. !
  135. theClass: aClass
  136. theClass := aClass
  137. ! !
  138. ClassAnnouncement subclass: #ClassAdded
  139. instanceVariableNames: ''
  140. package: 'Kernel-Announcements'!
  141. !ClassAdded commentStamp!
  142. I am emitted when a class is added to the system.
  143. See ClassBuilder >> #addSubclassOf:... methods!
  144. ClassAnnouncement subclass: #ClassCommentChanged
  145. instanceVariableNames: ''
  146. package: 'Kernel-Announcements'!
  147. !ClassCommentChanged commentStamp!
  148. I am emitted when the comment of a class changes. (Behavior >> #comment)!
  149. ClassAnnouncement subclass: #ClassDefinitionChanged
  150. instanceVariableNames: ''
  151. package: 'Kernel-Announcements'!
  152. !ClassDefinitionChanged commentStamp!
  153. I am emitted when the definition of a class changes.
  154. See ClassBuilder >> #class:instanceVariableNames:!
  155. ClassAnnouncement subclass: #ClassMigrated
  156. instanceVariableNames: 'oldClass'
  157. package: 'Kernel-Announcements'!
  158. !ClassMigrated commentStamp!
  159. I am emitted when a class is migrated.!
  160. !ClassMigrated methodsFor: 'accessing'!
  161. oldClass
  162. ^ oldClass
  163. !
  164. oldClass: aClass
  165. oldClass := aClass
  166. ! !
  167. ClassAnnouncement subclass: #ClassMoved
  168. instanceVariableNames: 'oldPackage'
  169. package: 'Kernel-Announcements'!
  170. !ClassMoved commentStamp!
  171. I am emitted when a class is moved from one package to another.!
  172. !ClassMoved methodsFor: 'accessing'!
  173. oldPackage
  174. ^ oldPackage
  175. !
  176. oldPackage: aPackage
  177. oldPackage := aPackage
  178. ! !
  179. ClassAnnouncement subclass: #ClassRemoved
  180. instanceVariableNames: ''
  181. package: 'Kernel-Announcements'!
  182. !ClassRemoved commentStamp!
  183. I am emitted when a class is removed.
  184. See Smalltalk >> #removeClass:!
  185. ClassAnnouncement subclass: #ClassRenamed
  186. instanceVariableNames: ''
  187. package: 'Kernel-Announcements'!
  188. !ClassRenamed commentStamp!
  189. I am emitted when a class is renamed.
  190. See ClassBuilder >> #renameClass:to:!
  191. SystemAnnouncement subclass: #MethodAnnouncement
  192. instanceVariableNames: 'method'
  193. package: 'Kernel-Announcements'!
  194. !MethodAnnouncement commentStamp!
  195. I am the abstract superclass of method-related announcements.!
  196. !MethodAnnouncement methodsFor: 'accessing'!
  197. method
  198. ^ method
  199. !
  200. method: aCompiledMethod
  201. method := aCompiledMethod
  202. ! !
  203. MethodAnnouncement subclass: #MethodAdded
  204. instanceVariableNames: ''
  205. package: 'Kernel-Announcements'!
  206. !MethodAdded commentStamp!
  207. I am emitted when a `CompiledMethod` is added to a class.!
  208. MethodAnnouncement subclass: #MethodModified
  209. instanceVariableNames: 'oldMethod'
  210. package: 'Kernel-Announcements'!
  211. !MethodModified commentStamp!
  212. I am emitted when a `CompiledMethod` is modified (a new method is installed). I hold a reference to the old method being replaced.!
  213. !MethodModified methodsFor: 'accessing'!
  214. oldMethod
  215. ^ oldMethod
  216. !
  217. oldMethod: aMethod
  218. oldMethod := aMethod
  219. ! !
  220. MethodAnnouncement subclass: #MethodMoved
  221. instanceVariableNames: 'oldProtocol'
  222. package: 'Kernel-Announcements'!
  223. !MethodMoved commentStamp!
  224. I am emitted when a `CompiledMethod` is moved to another protocol. I hold a refernce to the old protocol of the method.!
  225. !MethodMoved methodsFor: 'accessing'!
  226. oldProtocol
  227. ^ oldProtocol
  228. !
  229. oldProtocol: aString
  230. oldProtocol := aString
  231. ! !
  232. MethodAnnouncement subclass: #MethodRemoved
  233. instanceVariableNames: ''
  234. package: 'Kernel-Announcements'!
  235. !MethodRemoved commentStamp!
  236. I am emitted when a `CompiledMethod` is removed from a class.!
  237. SystemAnnouncement subclass: #PackageAnnouncement
  238. instanceVariableNames: 'package'
  239. package: 'Kernel-Announcements'!
  240. !PackageAnnouncement commentStamp!
  241. I am the abstract superclass of package-related announcements.!
  242. !PackageAnnouncement methodsFor: 'accessing'!
  243. package
  244. ^ package
  245. !
  246. package: aPackage
  247. package := aPackage
  248. ! !
  249. PackageAnnouncement subclass: #PackageAdded
  250. instanceVariableNames: ''
  251. package: 'Kernel-Announcements'!
  252. !PackageAdded commentStamp!
  253. I am emitted when a `Package` is added to the system.!
  254. PackageAnnouncement subclass: #PackageRemoved
  255. instanceVariableNames: ''
  256. package: 'Kernel-Announcements'!
  257. !PackageRemoved commentStamp!
  258. I am emitted when a `Package` is removed from the system.!
  259. SystemAnnouncement subclass: #ProtocolAnnouncement
  260. instanceVariableNames: 'theClass protocol'
  261. package: 'Kernel-Announcements'!
  262. !ProtocolAnnouncement commentStamp!
  263. I am the abstract superclass of protocol-related announcements.!
  264. !ProtocolAnnouncement methodsFor: 'accessing'!
  265. protocol
  266. ^ protocol
  267. !
  268. protocol: aString
  269. protocol := aString
  270. !
  271. theClass
  272. ^ theClass
  273. !
  274. theClass: aClass
  275. theClass := aClass
  276. ! !
  277. ProtocolAnnouncement subclass: #ProtocolAdded
  278. instanceVariableNames: ''
  279. package: 'Kernel-Announcements'!
  280. !ProtocolAdded commentStamp!
  281. I am emitted when a protocol is added to a class.!
  282. ProtocolAnnouncement subclass: #ProtocolRemoved
  283. instanceVariableNames: ''
  284. package: 'Kernel-Announcements'!
  285. !ProtocolRemoved commentStamp!
  286. I am emitted when a protocol is removed from a class.!