Browse Source

Extracted ad-hoc subscription array into TrappedSubscription class.

Herbert Vojčík 12 years ago
parent
commit
bdfb73b155
6 changed files with 399 additions and 74 deletions
  1. 133 20
      js/Trapped-Backend.deploy.js
  2. 186 28
      js/Trapped-Backend.js
  3. 4 4
      js/Trapped-Demo.deploy.js
  4. 9 9
      js/Trapped-Demo.js
  5. 63 9
      st/Trapped-Backend.st
  6. 4 4
      st/Trapped-Demo.st

+ 133 - 20
js/Trapped-Backend.deploy.js

@@ -140,15 +140,9 @@ var $1;
 var needsToRun;
 needsToRun=false;
 smalltalk.send(self,"_do_",[(function(each){
-var aPath;
-var lesser;
-aPath=smalltalk.send(each,"_second",[]);
-aPath;
-lesser=smalltalk.send(smalltalk.send(aPath,"_size",[]),"_min_",[smalltalk.send(path,"_size",[])]);
-lesser;
-$1=smalltalk.send(smalltalk.send(path,"_copyFrom_to_",[(1),lesser]),"__eq",[smalltalk.send(aPath,"_copyFrom_to_",[(1),lesser])]);
+$1=smalltalk.send(each,"_accepts_",[path]);
 if(smalltalk.assert($1)){
-smalltalk.send(each,"_at_put_",[(1),true]);
+smalltalk.send(each,"_flag",[]);
 needsToRun=true;
 return needsToRun;
 };
@@ -179,7 +173,7 @@ smalltalk.method({
 selector: "on:hook:",
 fn: function (path,aBlock){
 var self=this;
-smalltalk.send(self,"_add_",[[true,path,aBlock]]);
+smalltalk.send(self,"_add_",[smalltalk.send(smalltalk.send((smalltalk.TrappedSubscription || TrappedSubscription),"_path_action_",[path,aBlock]),"_flag",[])]);
 smalltalk.send(self,"_dirty_",[true]);
 return self}
 }),
@@ -191,23 +185,18 @@ smalltalk.method({
 selector: "run",
 fn: function (){
 var self=this;
-var $1;
+var $1,$2;
 var needsClean;
 needsClean=false;
 smalltalk.send(self,"_do_",[(function(each){
-$1=smalltalk.send(each,"_first",[]);
+$1=smalltalk.send(each,"_isFlagged",[]);
 if(smalltalk.assert($1)){
-return smalltalk.send((function(){
-return smalltalk.send((function(){
-return smalltalk.send(smalltalk.send(each,"_third",[]),"_value",[]);
-}),"_ensure_",[(function(){
-return smalltalk.send(each,"_at_put_",[(1),false]);
-})]);
-}),"_on_do_",[(smalltalk.TrappedUnwatch || TrappedUnwatch),(function(){
-smalltalk.send(each,"_at_put_",[(3),nil]);
+smalltalk.send(each,"_run",[]);
+$2=smalltalk.send(each,"_isEnabled",[]);
+if(! smalltalk.assert($2)){
 needsClean=true;
 return needsClean;
-})]);
+};
 };
 })]);
 if(smalltalk.assert(needsClean)){
@@ -219,6 +208,130 @@ smalltalk.TrappedDispatcher);
 
 
 
+smalltalk.addClass('TrappedSubscription', smalltalk.Object, ['path', 'actionBlock', 'flagged'], 'Trapped-Backend');
+smalltalk.addMethod(
+"_accepts_",
+smalltalk.method({
+selector: "accepts:",
+fn: function (aPath){
+var self=this;
+var $1;
+var lesser;
+lesser=smalltalk.send(smalltalk.send(self["@path"],"_size",[]),"_min_",[smalltalk.send(aPath,"_size",[])]);
+$1=smalltalk.send(smalltalk.send(aPath,"_copyFrom_to_",[(1),lesser]),"__eq",[smalltalk.send(self["@path"],"_copyFrom_to_",[(1),lesser])]);
+return $1;
+}
+}),
+smalltalk.TrappedSubscription);
+
+smalltalk.addMethod(
+"_flag",
+smalltalk.method({
+selector: "flag",
+fn: function (){
+var self=this;
+self["@flagged"]=true;
+return self}
+}),
+smalltalk.TrappedSubscription);
+
+smalltalk.addMethod(
+"_initialize",
+smalltalk.method({
+selector: "initialize",
+fn: function (){
+var self=this;
+smalltalk.send(self,"_initialize",[],smalltalk.Object);
+self["@path"]=nil;
+self["@actionBlock"]=nil;
+self["@flagged"]=false;
+return self}
+}),
+smalltalk.TrappedSubscription);
+
+smalltalk.addMethod(
+"_isEnabled",
+smalltalk.method({
+selector: "isEnabled",
+fn: function (){
+var self=this;
+var $1;
+$1=smalltalk.send(self["@actionBlock"],"_notNil",[]);
+return $1;
+}
+}),
+smalltalk.TrappedSubscription);
+
+smalltalk.addMethod(
+"_isFlagged",
+smalltalk.method({
+selector: "isFlagged",
+fn: function (){
+var self=this;
+return self["@flagged"];
+}
+}),
+smalltalk.TrappedSubscription);
+
+smalltalk.addMethod(
+"_path_actionBlock_",
+smalltalk.method({
+selector: "path:actionBlock:",
+fn: function (anArray,aBlock){
+var self=this;
+self["@path"]=anArray;
+self["@actionBlock"]=aBlock;
+return self}
+}),
+smalltalk.TrappedSubscription);
+
+smalltalk.addMethod(
+"_run",
+smalltalk.method({
+selector: "run",
+fn: function (){
+var self=this;
+smalltalk.send((function(){
+return smalltalk.send((function(){
+return smalltalk.send(self["@actionBlock"],"_value",[]);
+}),"_ensure_",[(function(){
+self["@flagged"]=false;
+return self["@flagged"];
+})]);
+}),"_on_do_",[(smalltalk.TrappedUnwatch || TrappedUnwatch),(function(){
+self["@actionBlock"]=nil;
+return self["@actionBlock"];
+})]);
+return self}
+}),
+smalltalk.TrappedSubscription);
+
+
+smalltalk.addMethod(
+"_new",
+smalltalk.method({
+selector: "new",
+fn: function (){
+var self=this;
+smalltalk.send(self,"_shouldNotImplement",[]);
+return self}
+}),
+smalltalk.TrappedSubscription.klass);
+
+smalltalk.addMethod(
+"_path_action_",
+smalltalk.method({
+selector: "path:action:",
+fn: function (anArray,aBlock){
+var self=this;
+var $1;
+$1=smalltalk.send(smalltalk.send(self,"_new",[],smalltalk.Object.klass),"_path_actionBlock_",[anArray,aBlock]);
+return $1;
+}
+}),
+smalltalk.TrappedSubscription.klass);
+
+
 smalltalk.addClass('TrappedUnwatch', smalltalk.Error, [], 'Trapped-Backend');
 
 

+ 186 - 28
js/Trapped-Backend.js

@@ -193,15 +193,9 @@ var $1;
 var needsToRun;
 needsToRun=false;
 smalltalk.send(self,"_do_",[(function(each){
-var aPath;
-var lesser;
-aPath=smalltalk.send(each,"_second",[]);
-aPath;
-lesser=smalltalk.send(smalltalk.send(aPath,"_size",[]),"_min_",[smalltalk.send(path,"_size",[])]);
-lesser;
-$1=smalltalk.send(smalltalk.send(path,"_copyFrom_to_",[(1),lesser]),"__eq",[smalltalk.send(aPath,"_copyFrom_to_",[(1),lesser])]);
+$1=smalltalk.send(each,"_accepts_",[path]);
 if(smalltalk.assert($1)){
-smalltalk.send(each,"_at_put_",[(1),true]);
+smalltalk.send(each,"_flag",[]);
 needsToRun=true;
 return needsToRun;
 };
@@ -209,8 +203,8 @@ return needsToRun;
 smalltalk.send(self,"_dirty_",[needsToRun]);
 return self},
 args: ["path"],
-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",
-messageSends: ["do:", "second", "min:", "size", "ifTrue:", "at:put:", "=", "copyFrom:to:", "dirty:"],
+source: "changed: path\x0a\x09| needsToRun |\x0a    needsToRun := false.\x0a\x09self do: [ :each |\x0a\x09\x09(each accepts: path) ifTrue: [\x0a\x09\x09\x09each flag.\x0a            needsToRun := true.\x0a\x09\x09]\x0a\x09].\x0a\x09self dirty: needsToRun",
+messageSends: ["do:", "ifTrue:", "flag", "accepts:", "dirty:"],
 referencedClasses: []
 }),
 smalltalk.TrappedDispatcher);
@@ -242,13 +236,13 @@ selector: "on:hook:",
 category: 'action',
 fn: function (path,aBlock){
 var self=this;
-smalltalk.send(self,"_add_",[[true,path,aBlock]]);
+smalltalk.send(self,"_add_",[smalltalk.send(smalltalk.send((smalltalk.TrappedSubscription || TrappedSubscription),"_path_action_",[path,aBlock]),"_flag",[])]);
 smalltalk.send(self,"_dirty_",[true]);
 return self},
 args: ["path", "aBlock"],
-source: "on: path hook: aBlock\x0a\x09self add: { true. path. aBlock }.\x0a   \x09self dirty: true",
-messageSends: ["add:", "dirty:"],
-referencedClasses: []
+source: "on: path hook: aBlock\x0a\x09self add: (TrappedSubscription path: path action: aBlock) flag.\x0a   \x09self dirty: true",
+messageSends: ["add:", "flag", "path:action:", "dirty:"],
+referencedClasses: ["TrappedSubscription"]
 }),
 smalltalk.TrappedDispatcher);
 
@@ -259,23 +253,18 @@ selector: "run",
 category: 'action',
 fn: function (){
 var self=this;
-var $1;
+var $1,$2;
 var needsClean;
 needsClean=false;
 smalltalk.send(self,"_do_",[(function(each){
-$1=smalltalk.send(each,"_first",[]);
+$1=smalltalk.send(each,"_isFlagged",[]);
 if(smalltalk.assert($1)){
-return smalltalk.send((function(){
-return smalltalk.send((function(){
-return smalltalk.send(smalltalk.send(each,"_third",[]),"_value",[]);
-}),"_ensure_",[(function(){
-return smalltalk.send(each,"_at_put_",[(1),false]);
-})]);
-}),"_on_do_",[(smalltalk.TrappedUnwatch || TrappedUnwatch),(function(){
-smalltalk.send(each,"_at_put_",[(3),nil]);
+smalltalk.send(each,"_run",[]);
+$2=smalltalk.send(each,"_isEnabled",[]);
+if(! smalltalk.assert($2)){
 needsClean=true;
 return needsClean;
-})]);
+};
 };
 })]);
 if(smalltalk.assert(needsClean)){
@@ -283,14 +272,183 @@ smalltalk.send(self,"_clean",[]);
 };
 return self},
 args: [],
-source: "run\x0a\x09| needsClean |\x0a    needsClean := false.\x0a\x09self do: [ :each |\x0a\x09\x09each first ifTrue: [\x0a            [[ each third value ] ensure: [ each at: 1 put: false ]]\x0a            on: TrappedUnwatch do: [ each at: 3 put: nil. needsClean := true ]\x0a        ]\x0a\x09].\x0a    needsClean ifTrue: [ self clean ]",
-messageSends: ["do:", "ifTrue:", "on:do:", "at:put:", "ensure:", "value", "third", "first", "clean"],
-referencedClasses: ["TrappedUnwatch"]
+source: "run\x0a\x09| needsClean |\x0a    needsClean := false.\x0a\x09self do: [ :each |\x0a\x09\x09each isFlagged ifTrue: [\x0a            each run.\x0a            each isEnabled ifFalse: [ needsClean := true ]\x0a        ]\x0a\x09].\x0a    needsClean ifTrue: [ self clean ]",
+messageSends: ["do:", "ifTrue:", "run", "ifFalse:", "isEnabled", "isFlagged", "clean"],
+referencedClasses: []
 }),
 smalltalk.TrappedDispatcher);
 
 
 
+smalltalk.addClass('TrappedSubscription', smalltalk.Object, ['path', 'actionBlock', 'flagged'], 'Trapped-Backend');
+smalltalk.addMethod(
+"_accepts_",
+smalltalk.method({
+selector: "accepts:",
+category: 'testing',
+fn: function (aPath){
+var self=this;
+var $1;
+var lesser;
+lesser=smalltalk.send(smalltalk.send(self["@path"],"_size",[]),"_min_",[smalltalk.send(aPath,"_size",[])]);
+$1=smalltalk.send(smalltalk.send(aPath,"_copyFrom_to_",[(1),lesser]),"__eq",[smalltalk.send(self["@path"],"_copyFrom_to_",[(1),lesser])]);
+return $1;
+},
+args: ["aPath"],
+source: "accepts: aPath\x0a    | lesser |\x0a    lesser := path size min: aPath size.\x0a    ^(aPath copyFrom: 1 to: lesser) = (path copyFrom: 1 to: lesser)",
+messageSends: ["min:", "size", "=", "copyFrom:to:"],
+referencedClasses: []
+}),
+smalltalk.TrappedSubscription);
+
+smalltalk.addMethod(
+"_flag",
+smalltalk.method({
+selector: "flag",
+category: 'accessing',
+fn: function (){
+var self=this;
+self["@flagged"]=true;
+return self},
+args: [],
+source: "flag\x0a\x09flagged := true",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.TrappedSubscription);
+
+smalltalk.addMethod(
+"_initialize",
+smalltalk.method({
+selector: "initialize",
+category: 'initialization',
+fn: function (){
+var self=this;
+smalltalk.send(self,"_initialize",[],smalltalk.Object);
+self["@path"]=nil;
+self["@actionBlock"]=nil;
+self["@flagged"]=false;
+return self},
+args: [],
+source: "initialize\x0a\x09super initialize.\x0a    path := nil.\x0a    actionBlock := nil.\x0a    flagged := false.",
+messageSends: ["initialize"],
+referencedClasses: []
+}),
+smalltalk.TrappedSubscription);
+
+smalltalk.addMethod(
+"_isEnabled",
+smalltalk.method({
+selector: "isEnabled",
+category: 'testing',
+fn: function (){
+var self=this;
+var $1;
+$1=smalltalk.send(self["@actionBlock"],"_notNil",[]);
+return $1;
+},
+args: [],
+source: "isEnabled\x0a\x09^actionBlock notNil",
+messageSends: ["notNil"],
+referencedClasses: []
+}),
+smalltalk.TrappedSubscription);
+
+smalltalk.addMethod(
+"_isFlagged",
+smalltalk.method({
+selector: "isFlagged",
+category: 'testing',
+fn: function (){
+var self=this;
+return self["@flagged"];
+},
+args: [],
+source: "isFlagged\x0a\x09^flagged",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.TrappedSubscription);
+
+smalltalk.addMethod(
+"_path_actionBlock_",
+smalltalk.method({
+selector: "path:actionBlock:",
+category: 'accessing',
+fn: function (anArray,aBlock){
+var self=this;
+self["@path"]=anArray;
+self["@actionBlock"]=aBlock;
+return self},
+args: ["anArray", "aBlock"],
+source: "path: anArray actionBlock: aBlock\x0a\x09path := anArray.\x0a    actionBlock := aBlock",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.TrappedSubscription);
+
+smalltalk.addMethod(
+"_run",
+smalltalk.method({
+selector: "run",
+category: 'action',
+fn: function (){
+var self=this;
+smalltalk.send((function(){
+return smalltalk.send((function(){
+return smalltalk.send(self["@actionBlock"],"_value",[]);
+}),"_ensure_",[(function(){
+self["@flagged"]=false;
+return self["@flagged"];
+})]);
+}),"_on_do_",[(smalltalk.TrappedUnwatch || TrappedUnwatch),(function(){
+self["@actionBlock"]=nil;
+return self["@actionBlock"];
+})]);
+return self},
+args: [],
+source: "run\x0a\x09[[ actionBlock value ] ensure: [ flagged := false ]]\x0a    on: TrappedUnwatch do: [ actionBlock := nil ]",
+messageSends: ["on:do:", "ensure:", "value"],
+referencedClasses: ["TrappedUnwatch"]
+}),
+smalltalk.TrappedSubscription);
+
+
+smalltalk.addMethod(
+"_new",
+smalltalk.method({
+selector: "new",
+category: 'instance creation',
+fn: function (){
+var self=this;
+smalltalk.send(self,"_shouldNotImplement",[]);
+return self},
+args: [],
+source: "new\x0a\x09self shouldNotImplement",
+messageSends: ["shouldNotImplement"],
+referencedClasses: []
+}),
+smalltalk.TrappedSubscription.klass);
+
+smalltalk.addMethod(
+"_path_action_",
+smalltalk.method({
+selector: "path:action:",
+category: 'instance creation',
+fn: function (anArray,aBlock){
+var self=this;
+var $1;
+$1=smalltalk.send(smalltalk.send(self,"_new",[],smalltalk.Object.klass),"_path_actionBlock_",[anArray,aBlock]);
+return $1;
+},
+args: ["anArray", "aBlock"],
+source: "path: anArray action: aBlock\x0a\x09^super new path: anArray actionBlock: aBlock",
+messageSends: ["path:actionBlock:", "new"],
+referencedClasses: []
+}),
+smalltalk.TrappedSubscription.klass);
+
+
 smalltalk.addClass('TrappedUnwatch', smalltalk.Error, [], 'Trapped-Backend');
 smalltalk.TrappedUnwatch.comment="SIgnal me from the watch: block to unwatch it."
 

+ 4 - 4
js/Trapped-Demo.deploy.js

@@ -36,10 +36,10 @@ smalltalk.addMethod(
 "_add_",
 smalltalk.method({
 selector: "add:",
-fn: function (aTriplet){
+fn: function (aSubscription){
 var self=this;
-smalltalk.send(self["@queue"],"_add_",[aTriplet]);
-smalltalk.send(self,"_dirty_",[smalltalk.send(aTriplet,"_first",[])]);
+smalltalk.send(self["@queue"],"_add_",[aSubscription]);
+smalltalk.send(self,"_dirty_",[smalltalk.send(aSubscription,"_isFlagged",[])]);
 return self}
 }),
 smalltalk.TrappedDumbDispatcher);
@@ -51,7 +51,7 @@ selector: "clean",
 fn: function (){
 var self=this;
 self["@queue"]=smalltalk.send(self["@queue"],"_select_",[(function(each){
-return smalltalk.send(smalltalk.send(each,"_third",[]),"_notNil",[]);
+return smalltalk.send(each,"_isEnabled",[]);
 })]);
 return self}
 }),

+ 9 - 9
js/Trapped-Demo.js

@@ -42,14 +42,14 @@ smalltalk.addMethod(
 smalltalk.method({
 selector: "add:",
 category: 'accessing',
-fn: function (aTriplet){
+fn: function (aSubscription){
 var self=this;
-smalltalk.send(self["@queue"],"_add_",[aTriplet]);
-smalltalk.send(self,"_dirty_",[smalltalk.send(aTriplet,"_first",[])]);
+smalltalk.send(self["@queue"],"_add_",[aSubscription]);
+smalltalk.send(self,"_dirty_",[smalltalk.send(aSubscription,"_isFlagged",[])]);
 return self},
-args: ["aTriplet"],
-source: "add: aTriplet\x0a\x09queue add: aTriplet.\x0a    self dirty: aTriplet first\x0a\x09",
-messageSends: ["add:", "dirty:", "first"],
+args: ["aSubscription"],
+source: "add: aSubscription\x0a\x09queue add: aSubscription.\x0a    self dirty: aSubscription isFlagged\x0a\x09",
+messageSends: ["add:", "dirty:", "isFlagged"],
 referencedClasses: []
 }),
 smalltalk.TrappedDumbDispatcher);
@@ -62,12 +62,12 @@ category: 'bookkeeping',
 fn: function (){
 var self=this;
 self["@queue"]=smalltalk.send(self["@queue"],"_select_",[(function(each){
-return smalltalk.send(smalltalk.send(each,"_third",[]),"_notNil",[]);
+return smalltalk.send(each,"_isEnabled",[]);
 })]);
 return self},
 args: [],
-source: "clean\x0a\x09queue := queue select: [ :each | each third notNil ]",
-messageSends: ["select:", "notNil", "third"],
+source: "clean\x0a\x09queue := queue select: [ :each | each isEnabled ]",
+messageSends: ["select:", "isEnabled"],
 referencedClasses: []
 }),
 smalltalk.TrappedDumbDispatcher);

+ 63 - 9
st/Trapped-Backend.st

@@ -94,11 +94,8 @@ changed: path
 	| needsToRun |
     needsToRun := false.
 	self do: [ :each |
-		| aPath lesser |
-		aPath := each second.
-		lesser := aPath size min: path size.
-		(path copyFrom: 1 to: lesser) = (aPath copyFrom: 1 to: lesser) ifTrue: [
-			each at: 1 put: true.
+		(each accepts: path) ifTrue: [
+			each flag.
             needsToRun := true.
 		]
 	].
@@ -110,7 +107,7 @@ dirty: aBoolean
 !
 
 on: path hook: aBlock
-	self add: { true. path. aBlock }.
+	self add: (TrappedSubscription path: path action: aBlock) flag.
    	self dirty: true
 !
 
@@ -118,14 +115,71 @@ run
 	| needsClean |
     needsClean := false.
 	self do: [ :each |
-		each first ifTrue: [
-            [[ each third value ] ensure: [ each at: 1 put: false ]]
-            on: TrappedUnwatch do: [ each at: 3 put: nil. needsClean := true ]
+		each isFlagged ifTrue: [
+            each run.
+            each isEnabled ifFalse: [ needsClean := true ]
         ]
 	].
     needsClean ifTrue: [ self clean ]
 ! !
 
+Object subclass: #TrappedSubscription
+	instanceVariableNames: 'path actionBlock flagged'
+	package: 'Trapped-Backend'!
+
+!TrappedSubscription methodsFor: 'accessing'!
+
+flag
+	flagged := true
+!
+
+path: anArray actionBlock: aBlock
+	path := anArray.
+    actionBlock := aBlock
+! !
+
+!TrappedSubscription methodsFor: 'action'!
+
+run
+	[[ actionBlock value ] ensure: [ flagged := false ]]
+    on: TrappedUnwatch do: [ actionBlock := nil ]
+! !
+
+!TrappedSubscription methodsFor: 'initialization'!
+
+initialize
+	super initialize.
+    path := nil.
+    actionBlock := nil.
+    flagged := false.
+! !
+
+!TrappedSubscription methodsFor: 'testing'!
+
+accepts: aPath
+    | lesser |
+    lesser := path size min: aPath size.
+    ^(aPath copyFrom: 1 to: lesser) = (path copyFrom: 1 to: lesser)
+!
+
+isEnabled
+	^actionBlock notNil
+!
+
+isFlagged
+	^flagged
+! !
+
+!TrappedSubscription class methodsFor: 'instance creation'!
+
+new
+	self shouldNotImplement
+!
+
+path: anArray action: aBlock
+	^super new path: anArray actionBlock: aBlock
+! !
+
 Error subclass: #TrappedUnwatch
 	instanceVariableNames: ''
 	package: 'Trapped-Backend'!

+ 4 - 4
st/Trapped-Demo.st

@@ -24,15 +24,15 @@ TrappedDispatcher subclass: #TrappedDumbDispatcher
 
 !TrappedDumbDispatcher methodsFor: 'accessing'!
 
-add: aTriplet
-	queue add: aTriplet.
-    self dirty: aTriplet first
+add: aSubscription
+	queue add: aSubscription.
+    self dirty: aSubscription isFlagged
 ! !
 
 !TrappedDumbDispatcher methodsFor: 'bookkeeping'!
 
 clean
-	queue := queue select: [ :each | each third notNil ]
+	queue := queue select: [ :each | each isEnabled ]
 ! !
 
 !TrappedDumbDispatcher methodsFor: 'enumeration'!