Browse Source

guard processor

Herbert Vojčík 11 years ago
parent
commit
3471f5e0f8
4 changed files with 147 additions and 15 deletions
  1. 6 13
      js/Trapped-Demo.js
  2. 97 0
      js/Trapped-Frontend.js
  3. 2 2
      st/Trapped-Demo.st
  4. 42 0
      st/Trapped-Frontend.st

+ 6 - 13
js/Trapped-Demo.js

@@ -239,23 +239,16 @@ selector: "renderOn:",
 category: 'rendering',
 fn: function (html){
 var self=this;
-function $TemporaryWidget(){return smalltalk.TemporaryWidget||(typeof TemporaryWidget=="undefined"?nil:TemporaryWidget)}
-function $TemporaryWidget2(){return smalltalk.TemporaryWidget2||(typeof TemporaryWidget2=="undefined"?nil:TemporaryWidget2)}
 return smalltalk.withContext(function($ctx1) { 
-var $1,$2,$3;
 _st(_st(html)._h2())._trap_([["title"]]);
-$1=_st(html)._div();
-$2=[["todos"], ["notNil"]];
-$3=_st($TemporaryWidget())._new();
-$ctx1.sendIdx["new"]=1;
-_st($1)._trapGuard_contents_($2,$3);
-$ctx1.sendIdx["trapGuard:contents:"]=1;
-_st(_st(html)._p())._trapGuard_contents_([["todos"], ["isNil"]],_st($TemporaryWidget2())._new());
+_st(_st(html)._div())._trap_processors_([],[["guard", [["todos"], ["notNil"]]], ["widget", "TemporaryWidget"]]);
+$ctx1.sendIdx["trap:processors:"]=1;
+_st(_st(html)._p())._trap_processors_([],[["guard", [["todos"], ["isNil"]]], ["widget", "TemporaryWidget2"]]);
 return self}, function($ctx1) {$ctx1.fill(self,"renderOn:",{html:html},smalltalk.AppView)})},
 args: ["html"],
-source: "renderOn: html\x0a\x09html h2 trap: #((title)).\x0a    html div trapGuard: #((todos) (notNil)) contents: TemporaryWidget new.\x0a\x09html p trapGuard: #((todos) (isNil)) contents: TemporaryWidget2 new",
-messageSends: ["trap:", "h2", "trapGuard:contents:", "div", "new", "p"],
-referencedClasses: ["TemporaryWidget", "TemporaryWidget2"]
+source: "renderOn: html\x0a\x09html h2 trap: #((title)).\x0a    html div trap: #() processors: #((guard ((todos) (notNil))) (widget TemporaryWidget)).\x0a\x09html p trap: #() processors: #((guard ((todos) (isNil))) (widget TemporaryWidget2))",
+messageSends: ["trap:", "h2", "trap:processors:", "div", "p"],
+referencedClasses: []
 }),
 smalltalk.AppView);
 

+ 97 - 0
js/Trapped-Frontend.js

@@ -597,6 +597,25 @@ referencedClasses: ["TrappedProcessorContents"]
 }),
 smalltalk.TrappedProcessor.klass);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "guard:",
+category: 'factory',
+fn: function (aString){
+var self=this;
+function $TrappedProcessorGuard(){return smalltalk.TrappedProcessorGuard||(typeof TrappedProcessorGuard=="undefined"?nil:TrappedProcessorGuard)}
+return smalltalk.withContext(function($ctx1) { 
+var $1;
+$1=_st($TrappedProcessorGuard())._new_(aString);
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"guard:",{aString:aString},smalltalk.TrappedProcessor.klass)})},
+args: ["aString"],
+source: "guard: aString\x0a\x09^TrappedProcessorGuard new: aString",
+messageSends: ["new:"],
+referencedClasses: ["TrappedProcessorGuard"]
+}),
+smalltalk.TrappedProcessor.klass);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "inputChecked",
@@ -898,6 +917,84 @@ smalltalk.TrappedProcessorBlackboard);
 
 
 
+smalltalk.addClass('TrappedProcessorGuard', smalltalk.TrappedProcessor, ['guardPath'], 'Trapped-Frontend');
+smalltalk.TrappedProcessorGuard.comment="I am used to guard contants of the brush I am installed on.\x0a\x0aI observe guard expression in the model,\x0aand when it changes to nil or false, I delete the brush contents;\x0aon the other hand, when it changes to non-nil and non-false,\x0aI run the rest on the chain, which should be one-time\x0athat sets up the contents,";
+smalltalk.addMethod(
+smalltalk.method({
+selector: "guardPath:",
+category: 'accessing',
+fn: function (anArray){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+self["@guardPath"]=anArray;
+return self}, function($ctx1) {$ctx1.fill(self,"guardPath:",{anArray:anArray},smalltalk.TrappedProcessorGuard)})},
+args: ["anArray"],
+source: "guardPath: anArray\x0a\x09guardPath := anArray",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.TrappedProcessorGuard);
+
+smalltalk.addMethod(
+smalltalk.method({
+selector: "toModel:",
+category: 'data transformation',
+fn: function (aDataCarrier){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+return self}, function($ctx1) {$ctx1.fill(self,"toModel:",{aDataCarrier:aDataCarrier},smalltalk.TrappedProcessorGuard)})},
+args: ["aDataCarrier"],
+source: "toModel: aDataCarrier\x0a\x09\x22stop\x22",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.TrappedProcessorGuard);
+
+smalltalk.addMethod(
+smalltalk.method({
+selector: "toView:",
+category: 'data transformation',
+fn: function (aDataCarrier){
+var self=this;
+var frozen;
+return smalltalk.withContext(function($ctx1) { 
+frozen=_st(aDataCarrier)._copy();
+$ctx1.sendIdx["copy"]=1;
+_st(_st(frozen)._target())._trapGuard_contents_(self["@guardPath"],(function(){
+return smalltalk.withContext(function($ctx2) {
+return _st(_st(frozen)._copy())._proceed();
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1,1)})}));
+return self}, function($ctx1) {$ctx1.fill(self,"toView:",{aDataCarrier:aDataCarrier,frozen:frozen},smalltalk.TrappedProcessorGuard)})},
+args: ["aDataCarrier"],
+source: "toView: aDataCarrier\x0a\x09| frozen |\x0a\x09frozen := aDataCarrier copy.\x0a\x09frozen target trapGuard: guardPath contents: [ frozen copy proceed ]",
+messageSends: ["copy", "trapGuard:contents:", "target", "proceed"],
+referencedClasses: []
+}),
+smalltalk.TrappedProcessorGuard);
+
+
+smalltalk.addMethod(
+smalltalk.method({
+selector: "new:",
+category: 'instance creation',
+fn: function (anArray){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+var $2,$3,$1;
+$2=self._new();
+_st($2)._guardPath_(anArray);
+$3=_st($2)._yourself();
+$1=$3;
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"new:",{anArray:anArray},smalltalk.TrappedProcessorGuard.klass)})},
+args: ["anArray"],
+source: "new: anArray\x0a\x09^ self new\x0a\x09\x09guardPath: anArray;\x0a\x09\x09yourself",
+messageSends: ["guardPath:", "new", "yourself"],
+referencedClasses: []
+}),
+smalltalk.TrappedProcessorGuard.klass);
+
+
 smalltalk.addClass('TrappedProcessorSignal', smalltalk.TrappedProcessor, ['selector'], 'Trapped-Frontend');
 smalltalk.TrappedProcessorSignal.comment="Instead of writing data directly to model,\x0aI instead modify it by sending a message specified when instantiating me.";
 smalltalk.addMethod(

+ 2 - 2
st/Trapped-Demo.st

@@ -149,8 +149,8 @@ Widget subclass: #AppView
 
 renderOn: html
 	html h2 trap: #((title)).
-    html div trapGuard: #((todos) (notNil)) contents: TemporaryWidget new.
-	html p trapGuard: #((todos) (isNil)) contents: TemporaryWidget2 new
+    html div trap: #() processors: #((guard ((todos) (notNil))) (widget TemporaryWidget)).
+	html p trap: #() processors: #((guard ((todos) (isNil))) (widget TemporaryWidget2))
 ! !
 
 Widget subclass: #TemporaryWidget

+ 42 - 0
st/Trapped-Frontend.st

@@ -202,6 +202,10 @@ contents
 	^TrappedProcessorContents new
 !
 
+guard: aString
+	^TrappedProcessorGuard new: aString
+!
+
 inputChecked
 	^TrappedProcessorInputChecked new
 !
@@ -324,6 +328,44 @@ installToView: aDataCarrier toModel: anotherDataCarrier
 	aDataCarrier value: false
 ! !
 
+TrappedProcessor subclass: #TrappedProcessorGuard
+	instanceVariableNames: 'guardPath'
+	package: 'Trapped-Frontend'!
+!TrappedProcessorGuard commentStamp!
+I am used to guard contants of the brush I am installed on.
+
+I observe guard expression in the model,
+and when it changes to nil or false, I delete the brush contents;
+on the other hand, when it changes to non-nil and non-false,
+I run the rest on the chain, which should be one-time
+that sets up the contents,!
+
+!TrappedProcessorGuard methodsFor: 'accessing'!
+
+guardPath: anArray
+	guardPath := anArray
+! !
+
+!TrappedProcessorGuard methodsFor: 'data transformation'!
+
+toModel: aDataCarrier
+	"stop"
+!
+
+toView: aDataCarrier
+	| frozen |
+	frozen := aDataCarrier copy.
+	frozen target trapGuard: guardPath contents: [ frozen copy proceed ]
+! !
+
+!TrappedProcessorGuard class methodsFor: 'instance creation'!
+
+new: anArray
+	^ self new
+		guardPath: anArray;
+		yourself
+! !
+
 TrappedProcessor subclass: #TrappedProcessorSignal
 	instanceVariableNames: 'selector'
 	package: 'Trapped-Frontend'!