Trapped-Common.st 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. ! !
  78. !Object methodsFor: '*Trapped-Common'!
  79. reverseTrapAt: anObject
  80. ^nil
  81. !
  82. reverseTrapAt: anObject put: value
  83. self error: 'Trapped cannot put at ', self class name, ' type key.'
  84. ! !
  85. !String methodsFor: '*Trapped-Common'!
  86. reverseTrapAt: anObject
  87. ^anObject at: self ifAbsent: [nil]
  88. !
  89. reverseTrapAt: anObject put: value
  90. ^anObject at: self put: value
  91. ! !
  92. !Symbol methodsFor: '*Trapped-Common'!
  93. reverseTrapAt: anObject
  94. ^[anObject perform: self] on: MessageNotUnderstood do: [^nil]
  95. !
  96. reverseTrapAt: anObject put: value
  97. ^anObject perform: (self, ':') asSymbol withArguments: { value }
  98. ! !
  99. !Number methodsFor: '*Trapped-Common'!
  100. reverseTrapAt: anObject
  101. ^anObject at: self ifAbsent: [nil]
  102. !
  103. reverseTrapAt: anObject put: value
  104. ^anObject at: self put: value
  105. ! !