Trapped-Common.st 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. Smalltalk current createPackage: 'Trapped-Common' properties: #{}!
  2. Object subclass: #TrappedDispatcher
  3. instanceVariableNames: ''
  4. package: 'Trapped-Common'!
  5. !TrappedDispatcher commentStamp!
  6. I am base class for change event dispatchers.
  7. I manage changed path - action block subscriptions.
  8. These subscription must be three-element arrays
  9. { dirty. path. block }
  10. My subclasses need to provide implementation for:
  11. add:
  12. do:
  13. (optionally) run!
  14. !TrappedDispatcher methodsFor: 'action'!
  15. changed: path
  16. | needsToRun |
  17. needsToRun := false.
  18. self do: [ :each |
  19. | aPath lesser |
  20. aPath := each second.
  21. lesser := aPath size min: path size.
  22. (path copyFrom: 1 to: lesser) = (aPath copyFrom: 1 to: lesser) ifTrue: [
  23. each at: 1 put: true.
  24. needsToRun := true.
  25. ]
  26. ].
  27. self dirty: needsToRun
  28. !
  29. dirty: aBoolean
  30. aBoolean ifTrue: [[ self run ] fork]
  31. !
  32. run
  33. self do: [ :each |
  34. each first ifTrue: [[ each third value ] ensure: [ each at: 1 put: false ]]
  35. ]
  36. ! !
  37. Object subclass: #TrappedModelWrapper
  38. instanceVariableNames: 'dispatcher payload'
  39. package: 'Trapped-Common'!
  40. !TrappedModelWrapper commentStamp!
  41. I am base class for model wrappers.
  42. I wrap a model which can be any object.
  43. My subclasses need to provide implementation for:
  44. read:do:
  45. (optionally) name
  46. and must initialize:
  47. payload
  48. dispatcher!
  49. !TrappedModelWrapper methodsFor: 'accessing'!
  50. dispatcher
  51. ^dispatcher
  52. !
  53. dispatcher: aDispatcher
  54. dispatcher := aDispatcher
  55. !
  56. name
  57. ^ self class name
  58. !
  59. payload
  60. ^payload
  61. !
  62. payload: anObject
  63. payload := anObject
  64. ! !
  65. !TrappedModelWrapper methodsFor: 'action'!
  66. start
  67. Trapped current register: self name: self name
  68. !
  69. watch: path do: aBlock
  70. self dispatcher add: { true. path. [ self read: path do: aBlock ] }.
  71. [ self dispatcher run ] fork
  72. ! !
  73. !TrappedModelWrapper class methodsFor: 'action'!
  74. start
  75. self new start
  76. ! !