Trapped-Common.st 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. modify:do:
  46. (optionally) name
  47. and must initialize:
  48. payload
  49. dispatcher!
  50. !TrappedModelWrapper methodsFor: 'accessing'!
  51. dispatcher
  52. ^dispatcher
  53. !
  54. dispatcher: aDispatcher
  55. dispatcher := aDispatcher
  56. !
  57. name
  58. ^ self class name
  59. !
  60. payload
  61. ^payload
  62. !
  63. payload: anObject
  64. payload := anObject
  65. ! !
  66. !TrappedModelWrapper methodsFor: 'action'!
  67. start
  68. Trapped current register: self name: self name
  69. !
  70. watch: path do: aBlock
  71. self dispatcher add: { true. path. [ self read: path do: aBlock ] }.
  72. self dispatcher dirty: true
  73. ! !
  74. !TrappedModelWrapper class methodsFor: 'action'!
  75. start
  76. self new start
  77. ! !