Trapped-Common.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. smalltalk.addPackage('Trapped-Common', {});
  2. smalltalk.addClass('TrappedDispatcher', smalltalk.Object, [], 'Trapped-Common');
  3. smalltalk.TrappedDispatcher.comment="I am base class for change event dispatchers.\x0aI manage changed path - action block subscriptions.\x0aThese subscription must be three-element arrays\x0a\x09{ dirty. path. block }\x0a\x0aMy subclasses need to provide implementation for:\x0a\x09add:\x0a do:\x0a (optionally) run\x0a"
  4. smalltalk.addMethod(
  5. "_changed_",
  6. smalltalk.method({
  7. selector: "changed:",
  8. category: 'action',
  9. fn: function (path){
  10. var self=this;
  11. var $1;
  12. var needsToRun;
  13. needsToRun=false;
  14. smalltalk.send(self,"_do_",[(function(each){
  15. var aPath;
  16. var lesser;
  17. aPath=smalltalk.send(each,"_second",[]);
  18. aPath;
  19. lesser=smalltalk.send(smalltalk.send(aPath,"_size",[]),"_min_",[smalltalk.send(path,"_size",[])]);
  20. lesser;
  21. $1=smalltalk.send(smalltalk.send(path,"_copyFrom_to_",[(1),lesser]),"__eq",[smalltalk.send(aPath,"_copyFrom_to_",[(1),lesser])]);
  22. if(smalltalk.assert($1)){
  23. smalltalk.send(each,"_at_put_",[(1),true]);
  24. needsToRun=true;
  25. return needsToRun;
  26. };
  27. })]);
  28. smalltalk.send(self,"_dirty_",[needsToRun]);
  29. return self},
  30. args: ["path"],
  31. source: "changed: path\x0a\x09| needsToRun |\x0a needsToRun := false.\x0a\x09self do: [ :each |\x0a\x09\x09| aPath lesser |\x0a\x09\x09aPath := each second.\x0a\x09\x09lesser := aPath size min: path size.\x0a\x09\x09(path copyFrom: 1 to: lesser) = (aPath copyFrom: 1 to: lesser) ifTrue: [\x0a\x09\x09\x09each at: 1 put: true.\x0a needsToRun := true.\x0a\x09\x09]\x0a\x09].\x0a\x09self dirty: needsToRun",
  32. messageSends: ["do:", "second", "min:", "size", "ifTrue:", "at:put:", "=", "copyFrom:to:", "dirty:"],
  33. referencedClasses: []
  34. }),
  35. smalltalk.TrappedDispatcher);
  36. smalltalk.addMethod(
  37. "_dirty_",
  38. smalltalk.method({
  39. selector: "dirty:",
  40. category: 'action',
  41. fn: function (aBoolean){
  42. var self=this;
  43. if(smalltalk.assert(aBoolean)){
  44. smalltalk.send((function(){
  45. return smalltalk.send(self,"_run",[]);
  46. }),"_fork",[]);
  47. };
  48. return self},
  49. args: ["aBoolean"],
  50. source: "dirty: aBoolean\x0a\x09aBoolean ifTrue: [[ self run ] fork]",
  51. messageSends: ["ifTrue:", "fork", "run"],
  52. referencedClasses: []
  53. }),
  54. smalltalk.TrappedDispatcher);
  55. smalltalk.addMethod(
  56. "_run",
  57. smalltalk.method({
  58. selector: "run",
  59. category: 'action',
  60. fn: function (){
  61. var self=this;
  62. var $1;
  63. smalltalk.send(self,"_do_",[(function(each){
  64. $1=smalltalk.send(each,"_first",[]);
  65. if(smalltalk.assert($1)){
  66. return smalltalk.send((function(){
  67. return smalltalk.send(smalltalk.send(each,"_third",[]),"_value",[]);
  68. }),"_ensure_",[(function(){
  69. return smalltalk.send(each,"_at_put_",[(1),false]);
  70. })]);
  71. };
  72. })]);
  73. return self},
  74. args: [],
  75. source: "run\x0a\x09self do: [ :each |\x0a\x09\x09each first ifTrue: [[ each third value ] ensure: [ each at: 1 put: false ]]\x0a\x09]",
  76. messageSends: ["do:", "ifTrue:", "ensure:", "at:put:", "value", "third", "first"],
  77. referencedClasses: []
  78. }),
  79. smalltalk.TrappedDispatcher);
  80. smalltalk.addClass('TrappedModelWrapper', smalltalk.Object, ['dispatcher', 'payload'], 'Trapped-Common');
  81. smalltalk.TrappedModelWrapper.comment="I am base class for model wrappers.\x0aI wrap a model which can be any object.\x0a\x0aMy subclasses need to provide implementation for:\x0a\x09read:do:\x0a\x09(optionally) name\x0a\x0aand must initialize:\x0a\x09payload\x0a\x09dispatcher\x0a"
  82. smalltalk.addMethod(
  83. "_dispatcher",
  84. smalltalk.method({
  85. selector: "dispatcher",
  86. category: 'accessing',
  87. fn: function (){
  88. var self=this;
  89. return self["@dispatcher"];
  90. },
  91. args: [],
  92. source: "dispatcher\x0a\x09^dispatcher",
  93. messageSends: [],
  94. referencedClasses: []
  95. }),
  96. smalltalk.TrappedModelWrapper);
  97. smalltalk.addMethod(
  98. "_dispatcher_",
  99. smalltalk.method({
  100. selector: "dispatcher:",
  101. category: 'accessing',
  102. fn: function (aDispatcher){
  103. var self=this;
  104. self["@dispatcher"]=aDispatcher;
  105. return self},
  106. args: ["aDispatcher"],
  107. source: "dispatcher: aDispatcher\x0a\x09dispatcher := aDispatcher",
  108. messageSends: [],
  109. referencedClasses: []
  110. }),
  111. smalltalk.TrappedModelWrapper);
  112. smalltalk.addMethod(
  113. "_name",
  114. smalltalk.method({
  115. selector: "name",
  116. category: 'accessing',
  117. fn: function (){
  118. var self=this;
  119. var $1;
  120. $1=smalltalk.send(smalltalk.send(self,"_class",[]),"_name",[]);
  121. return $1;
  122. },
  123. args: [],
  124. source: "name\x0a\x09^ self class name",
  125. messageSends: ["name", "class"],
  126. referencedClasses: []
  127. }),
  128. smalltalk.TrappedModelWrapper);
  129. smalltalk.addMethod(
  130. "_payload",
  131. smalltalk.method({
  132. selector: "payload",
  133. category: 'accessing',
  134. fn: function (){
  135. var self=this;
  136. return self["@payload"];
  137. },
  138. args: [],
  139. source: "payload\x0a\x09^payload",
  140. messageSends: [],
  141. referencedClasses: []
  142. }),
  143. smalltalk.TrappedModelWrapper);
  144. smalltalk.addMethod(
  145. "_payload_",
  146. smalltalk.method({
  147. selector: "payload:",
  148. category: 'accessing',
  149. fn: function (anObject){
  150. var self=this;
  151. self["@payload"]=anObject;
  152. return self},
  153. args: ["anObject"],
  154. source: "payload: anObject\x0a\x09payload := anObject",
  155. messageSends: [],
  156. referencedClasses: []
  157. }),
  158. smalltalk.TrappedModelWrapper);
  159. smalltalk.addMethod(
  160. "_start",
  161. smalltalk.method({
  162. selector: "start",
  163. category: 'action',
  164. fn: function (){
  165. var self=this;
  166. smalltalk.send(smalltalk.send((smalltalk.Trapped || Trapped),"_current",[]),"_register_name_",[self,smalltalk.send(self,"_name",[])]);
  167. return self},
  168. args: [],
  169. source: "start\x0a\x09Trapped current register: self name: self name",
  170. messageSends: ["register:name:", "name", "current"],
  171. referencedClasses: ["Trapped"]
  172. }),
  173. smalltalk.TrappedModelWrapper);
  174. smalltalk.addMethod(
  175. "_watch_do_",
  176. smalltalk.method({
  177. selector: "watch:do:",
  178. category: 'action',
  179. fn: function (path,aBlock){
  180. var self=this;
  181. smalltalk.send(smalltalk.send(self,"_dispatcher",[]),"_add_",[[true,path,(function(){
  182. return smalltalk.send(self,"_read_do_",[path,aBlock]);
  183. })]]);
  184. smalltalk.send((function(){
  185. return smalltalk.send(smalltalk.send(self,"_dispatcher",[]),"_run",[]);
  186. }),"_fork",[]);
  187. return self},
  188. args: ["path", "aBlock"],
  189. source: "watch: path do: aBlock\x0a\x09self dispatcher add: { true. path. [ self read: path do: aBlock ] }.\x0a [ self dispatcher run ] fork",
  190. messageSends: ["add:", "read:do:", "dispatcher", "fork", "run"],
  191. referencedClasses: []
  192. }),
  193. smalltalk.TrappedModelWrapper);
  194. smalltalk.addMethod(
  195. "_start",
  196. smalltalk.method({
  197. selector: "start",
  198. category: 'action',
  199. fn: function (){
  200. var self=this;
  201. smalltalk.send(smalltalk.send(self,"_new",[]),"_start",[]);
  202. return self},
  203. args: [],
  204. source: "start\x0a\x09self new start",
  205. messageSends: ["start", "new"],
  206. referencedClasses: []
  207. }),
  208. smalltalk.TrappedModelWrapper.klass);