Kernel-Announcements.st 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. Smalltalk current createPackage: 'Kernel-Announcements' properties: #{}!
  2. Object subclass: #Announcer
  3. instanceVariableNames: 'registry subscriptions'
  4. category: 'Kernel-Announcements'!
  5. !Announcer methodsFor: 'announcing'!
  6. announce: anAnnouncement
  7. subscriptions do: [:each |
  8. each deliver: anAnnouncement]
  9. ! !
  10. !Announcer methodsFor: 'initialization'!
  11. initialize
  12. super initialize.
  13. subscriptions := OrderedCollection new
  14. ! !
  15. !Announcer methodsFor: 'subscribing'!
  16. on: aClass do: aBlock
  17. subscriptions add: (AnnouncementSubscription new
  18. block: aBlock;
  19. announcementClass: aClass;
  20. yourself)
  21. ! !
  22. Object subclass: #AnnouncementSubscription
  23. instanceVariableNames: 'block announcementClass'
  24. category: 'Kernel-Announcements'!
  25. !AnnouncementSubscription methodsFor: 'accessing'!
  26. announcementClass
  27. ^announcementClass
  28. !
  29. announcementClass: aClass
  30. announcementClass := aClass
  31. !
  32. block
  33. ^block
  34. !
  35. block: aBlock
  36. block := aBlock
  37. ! !
  38. !AnnouncementSubscription methodsFor: 'announcing'!
  39. deliver: anAnnouncement
  40. (self handlesAnnouncement: anAnnouncement)
  41. ifTrue: [self block value: anAnnouncement]
  42. !
  43. handlesAnnouncement: anAnnouncement
  44. ^anAnnouncement isKindOf: self announcementClass
  45. ! !