Trapped-Demo.st 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. Smalltalk current createPackage: 'Trapped-Demo' properties: #{}!
  2. Widget subclass: #AppView
  3. instanceVariableNames: ''
  4. package: 'Trapped-Demo'!
  5. !AppView methodsFor: 'rendering'!
  6. renderOn: html
  7. html h2 trapShow: #('title').
  8. html div trap: #('items') toggle: [
  9. html p with: [ html span trapShow: #(#size). html with: ' item(s).' ].
  10. html p trapShow: #()
  11. ] ifNotPresent: [ html with: 'Loading ...' ]
  12. ! !
  13. TrappedDispatcher subclass: #TrappedDumbDispatcher
  14. instanceVariableNames: 'queue'
  15. package: 'Trapped-Demo'!
  16. !TrappedDumbDispatcher methodsFor: 'accessing'!
  17. add: aTriplet
  18. queue add: aTriplet.
  19. self dirty: aTriplet first
  20. ! !
  21. !TrappedDumbDispatcher methodsFor: 'bookkeeping'!
  22. clean
  23. queue := queue select: [ :each | each third notNil ]
  24. ! !
  25. !TrappedDumbDispatcher methodsFor: 'enumeration'!
  26. do: aBlock
  27. queue do: aBlock
  28. ! !
  29. !TrappedDumbDispatcher methodsFor: 'initialization'!
  30. initialize
  31. queue := OrderedCollection new
  32. ! !
  33. TrappedModelWrapper subclass: #TrappedPlainModel
  34. instanceVariableNames: ''
  35. package: 'Trapped-Demo'!
  36. !TrappedPlainModel methodsFor: 'action'!
  37. modify: path do: aBlock
  38. | newValue eavModel |
  39. eavModel := path asEavModel.
  40. newValue := aBlock value: (eavModel on: self payload).
  41. [ eavModel on: self payload put: newValue ] ensure: [ self dispatcher changed: path ]
  42. !
  43. read: path do: aBlock
  44. | eavModel |
  45. eavModel := path asEavModel.
  46. aBlock value: (eavModel on: self payload)
  47. ! !
  48. !TrappedPlainModel methodsFor: 'initialization'!
  49. initialize
  50. super initialize.
  51. self dispatcher: TrappedDumbDispatcher new
  52. ! !
  53. TrappedPlainModel subclass: #App
  54. instanceVariableNames: ''
  55. package: 'Trapped-Demo'!
  56. !App methodsFor: 'initialization'!
  57. initialize
  58. super initialize.
  59. self payload: #{'title' -> 'To-Do List'}.
  60. [ self payload at: 'items' put: #('hello' 'world'). self payload: self payload ] valueWithTimeout: 2000
  61. ! !