Kernel-Announcements.st 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. Smalltalk current 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 current at: self announcementClass name)
  42. ifNil: [ ^ false ]
  43. ifNotNil: [ :class |
  44. (Smalltalk current 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 send: aSelector to: anObject
  79. subscriptions add: (AnnouncementSubscription new
  80. valuable: (MessageSend new
  81. receiver: anObject;
  82. selector: aSelector;
  83. yourself);
  84. announcementClass: aClass;
  85. yourself)
  86. !
  87. unsubscribe: anObject
  88. subscriptions := subscriptions reject: [ :each |
  89. each receiver = anObject ]
  90. ! !
  91. Announcer subclass: #SystemAnnouncer
  92. instanceVariableNames: ''
  93. package: 'Kernel-Announcements'!
  94. !SystemAnnouncer commentStamp!
  95. My unique instance is the global announcer handling all Amber system-related announces.
  96. ## API
  97. Access to the unique instance is done via `#current`!
  98. SystemAnnouncer class instanceVariableNames: 'current'!
  99. !SystemAnnouncer class methodsFor: 'accessing'!
  100. current
  101. ^ current ifNil: [ current := super new ]
  102. ! !
  103. !SystemAnnouncer class methodsFor: 'instance creation'!
  104. new
  105. self shouldNotImplement
  106. ! !
  107. Object subclass: #SystemAnnouncement
  108. instanceVariableNames: 'theClass'
  109. package: 'Kernel-Announcements'!
  110. !SystemAnnouncement commentStamp!
  111. I am the superclass of all system announcements!
  112. !SystemAnnouncement methodsFor: 'accessing'!
  113. theClass
  114. ^ theClass
  115. !
  116. theClass: aClass
  117. theClass := aClass
  118. ! !
  119. !SystemAnnouncement class methodsFor: 'helios'!
  120. heliosClass
  121. ^ 'announcement'
  122. ! !
  123. SystemAnnouncement subclass: #ClassAnnouncement
  124. instanceVariableNames: 'theClass'
  125. package: 'Kernel-Announcements'!
  126. !ClassAnnouncement commentStamp!
  127. I am the abstract superclass of class-related announcements.!
  128. !ClassAnnouncement methodsFor: 'accessing'!
  129. theClass
  130. ^ theClass
  131. !
  132. theClass: aClass
  133. theClass := aClass
  134. ! !
  135. ClassAnnouncement subclass: #ClassAdded
  136. instanceVariableNames: ''
  137. package: 'Kernel-Announcements'!
  138. !ClassAdded commentStamp!
  139. I am emitted when a class is added to the system.
  140. See ClassBuilder >> #addSubclassOf:... methods!
  141. ClassAnnouncement subclass: #ClassCommentChanged
  142. instanceVariableNames: ''
  143. package: 'Kernel-Announcements'!
  144. !ClassCommentChanged commentStamp!
  145. I am emitted when the comment of a class changes. (Behavior >> #comment)!
  146. ClassAnnouncement subclass: #ClassDefinitionChanged
  147. instanceVariableNames: ''
  148. package: 'Kernel-Announcements'!
  149. !ClassDefinitionChanged commentStamp!
  150. I am emitted when the definition of a class changes.
  151. See ClassBuilder >> #class:instanceVariableNames:!
  152. ClassAnnouncement subclass: #ClassMigrated
  153. instanceVariableNames: 'oldClass'
  154. package: 'Kernel-Announcements'!
  155. !ClassMigrated commentStamp!
  156. I am emitted when a class is migrated.!
  157. !ClassMigrated methodsFor: 'accessing'!
  158. oldClass
  159. ^ oldClass
  160. !
  161. oldClass: aClass
  162. oldClass := aClass
  163. ! !
  164. ClassAnnouncement subclass: #ClassMoved
  165. instanceVariableNames: 'oldPackage'
  166. package: 'Kernel-Announcements'!
  167. !ClassMoved commentStamp!
  168. I am emitted when a class is moved from one package to another.!
  169. !ClassMoved methodsFor: 'accessing'!
  170. oldPackage
  171. ^ oldPackage
  172. !
  173. oldPackage: aPackage
  174. oldPackage := aPackage
  175. ! !
  176. ClassAnnouncement subclass: #ClassRemoved
  177. instanceVariableNames: ''
  178. package: 'Kernel-Announcements'!
  179. !ClassRemoved commentStamp!
  180. I am emitted when a class is removed.
  181. See Smalltalk >> #removeClass:!
  182. ClassAnnouncement subclass: #ClassRenamed
  183. instanceVariableNames: ''
  184. package: 'Kernel-Announcements'!
  185. !ClassRenamed commentStamp!
  186. I am emitted when a class is renamed.
  187. See ClassBuilder >> #renameClass:to:!
  188. SystemAnnouncement subclass: #MethodAnnouncement
  189. instanceVariableNames: 'method'
  190. package: 'Kernel-Announcements'!
  191. !MethodAnnouncement commentStamp!
  192. I am the abstract superclass of method-related announcements.!
  193. !MethodAnnouncement methodsFor: 'accessing'!
  194. method
  195. ^ method
  196. !
  197. method: aCompiledMethod
  198. method := aCompiledMethod
  199. ! !
  200. MethodAnnouncement subclass: #MethodAdded
  201. instanceVariableNames: ''
  202. package: 'Kernel-Announcements'!
  203. !MethodAdded commentStamp!
  204. I am emitted when a `CompiledMethod` is added to a class.!
  205. MethodAnnouncement subclass: #MethodModified
  206. instanceVariableNames: 'oldMethod'
  207. package: 'Kernel-Announcements'!
  208. !MethodModified commentStamp!
  209. I am emitted when a `CompiledMethod` is modified (a new method is installed). I hold a reference to the old method being replaced.!
  210. !MethodModified methodsFor: 'accessing'!
  211. oldMethod
  212. ^ oldMethod
  213. !
  214. oldMethod: aMethod
  215. oldMethod := aMethod
  216. ! !
  217. MethodAnnouncement subclass: #MethodMoved
  218. instanceVariableNames: 'oldProtocol'
  219. package: 'Kernel-Announcements'!
  220. !MethodMoved commentStamp!
  221. I am emitted when a `CompiledMethod` is moved to another protocol. I hold a refernce to the old protocol of the method.!
  222. !MethodMoved methodsFor: 'accessing'!
  223. oldProtocol
  224. ^ oldProtocol
  225. !
  226. oldProtocol: aString
  227. oldProtocol := aString
  228. ! !
  229. MethodAnnouncement subclass: #MethodRemoved
  230. instanceVariableNames: ''
  231. package: 'Kernel-Announcements'!
  232. !MethodRemoved commentStamp!
  233. I am emitted when a `CompiledMethod` is removed from a class.!
  234. SystemAnnouncement subclass: #PackageAnnouncement
  235. instanceVariableNames: 'package'
  236. package: 'Kernel-Announcements'!
  237. !PackageAnnouncement commentStamp!
  238. I am the abstract superclass of package-related announcements.!
  239. !PackageAnnouncement methodsFor: 'accessing'!
  240. package
  241. ^ package
  242. !
  243. package: aPackage
  244. package := aPackage
  245. ! !
  246. PackageAnnouncement subclass: #PackageAdded
  247. instanceVariableNames: ''
  248. package: 'Kernel-Announcements'!
  249. !PackageAdded commentStamp!
  250. I am emitted when a `Package` is added to the system.!
  251. PackageAnnouncement subclass: #PackageRemoved
  252. instanceVariableNames: ''
  253. package: 'Kernel-Announcements'!
  254. !PackageRemoved commentStamp!
  255. I am emitted when a `Package` is removed from the system.!
  256. SystemAnnouncement subclass: #ProtocolAnnouncement
  257. instanceVariableNames: 'theClass protocol'
  258. package: 'Kernel-Announcements'!
  259. !ProtocolAnnouncement commentStamp!
  260. I am the abstract superclass of protocol-related announcements.!
  261. !ProtocolAnnouncement methodsFor: 'accessing'!
  262. protocol
  263. ^ protocol
  264. !
  265. protocol: aString
  266. protocol := aString
  267. !
  268. theClass
  269. ^ theClass
  270. !
  271. theClass: aClass
  272. theClass := aClass
  273. ! !
  274. ProtocolAnnouncement subclass: #ProtocolAdded
  275. instanceVariableNames: ''
  276. package: 'Kernel-Announcements'!
  277. !ProtocolAdded commentStamp!
  278. I am emitted when a protocol is added to a class.!
  279. ProtocolAnnouncement subclass: #ProtocolRemoved
  280. instanceVariableNames: ''
  281. package: 'Kernel-Announcements'!
  282. !ProtocolRemoved commentStamp!
  283. I am emitted when a protocol is removed from a class.!