Ver código fonte

interestFactory: off, on:hook: => addInterest:

Herbert Vojčík 9 anos atrás
pai
commit
92216d97d0
4 arquivos alterados com 87 adições e 99 exclusões
  1. 29 45
      src/Axon.js
  2. 7 15
      src/Axon.st
  3. 42 33
      src/Trapped-Backend.js
  4. 9 6
      src/Trapped-Backend.st

+ 29 - 45
src/Axon.js

@@ -6,8 +6,36 @@ $core.packages["Axon"].transport = {"type":"amd","amdNamespace":"axon"};
 
 $core.addClass('AxonBase', $globals.Object, ['factory'], 'Axon');
 //>>excludeStart("ide", pragmas.excludeIdeData);
-$globals.AxonBase.comment="I represent a pub-sub based on a key (called 'aspect').\x0aI manage aspect-block subscriptions (called 'interests') as well as run blocks of dirtied interests.\x0aThe interest objects are responsible of decision if the change of an aspect is relevant for them.\x0aInterest object must be subclasses of `AxonInterestBase`.\x0a\x0aMy subclasses must provide implementation for:\x0a\x0a - add:\x0a - do:\x0a - clean\x0a - (optionally) run\x0a\x0aand issue this call before actual use:\x0a\x0a\x09interestFactory: [ :description :block | ... factory that creates appropriate AxonInterest ... ]";
+$globals.AxonBase.comment="I represent a pub-sub based on a key (called 'aspect').\x0aI manage aspect-block subscriptions (called 'interests') as well as run blocks of dirtied interests.\x0aThe interest objects are responsible of decision if the change of an aspect is relevant for them.\x0aInterest object must be subclasses of `AxonInterestBase`.\x0a\x0aMy subclasses must provide implementation for:\x0a\x0a - add:\x0a - do:\x0a - clean";
 //>>excludeEnd("ide");
+$core.addMethod(
+$core.method({
+selector: "addInterest:",
+protocol: 'action',
+fn: function (anInterest){
+var self=this;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+var $1,$2;
+$recv(anInterest)._flag();
+$1=$recv(anInterest)._yourself();
+self._add_($1);
+$2=self._dirty_(true);
+return self;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"addInterest:",{anInterest:anInterest},$globals.AxonBase)});
+//>>excludeEnd("ctx");
+},
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: ["anInterest"],
+source: "addInterest: anInterest\x0a\x09self\x0a\x09\x09add: (anInterest flag; yourself);\x0a\x09\x09dirty: true",
+referencedClasses: [],
+//>>excludeEnd("ide");
+messageSends: ["add:", "flag", "yourself", "dirty:"]
+}),
+$globals.AxonBase);
+
 $core.addMethod(
 $core.method({
 selector: "changed:",
@@ -83,50 +111,6 @@ messageSends: ["ifTrue:", "fork", "run"]
 }),
 $globals.AxonBase);
 
-$core.addMethod(
-$core.method({
-selector: "interestFactory:",
-protocol: 'action',
-fn: function (aBlock){
-var self=this;
-self["@factory"]=aBlock;
-return self;
-
-},
-//>>excludeStart("ide", pragmas.excludeIdeData);
-args: ["aBlock"],
-source: "interestFactory: aBlock\x0a    factory := aBlock",
-referencedClasses: [],
-//>>excludeEnd("ide");
-messageSends: []
-}),
-$globals.AxonBase);
-
-$core.addMethod(
-$core.method({
-selector: "on:hook:",
-protocol: 'action',
-fn: function (description,aBlock){
-var self=this;
-//>>excludeStart("ctx", pragmas.excludeDebugContexts);
-return $core.withContext(function($ctx1) {
-//>>excludeEnd("ctx");
-self._add_($recv($recv(self["@factory"])._value_value_(description,aBlock))._flag());
-self._dirty_(true);
-return self;
-//>>excludeStart("ctx", pragmas.excludeDebugContexts);
-}, function($ctx1) {$ctx1.fill(self,"on:hook:",{description:description,aBlock:aBlock},$globals.AxonBase)});
-//>>excludeEnd("ctx");
-},
-//>>excludeStart("ide", pragmas.excludeIdeData);
-args: ["description", "aBlock"],
-source: "on: description hook: aBlock\x0a\x09self add: (factory value: description value: aBlock) flag.\x0a   \x09self dirty: true",
-referencedClasses: [],
-//>>excludeEnd("ide");
-messageSends: ["add:", "flag", "value:value:", "dirty:"]
-}),
-$globals.AxonBase);
-
 $core.addMethod(
 $core.method({
 selector: "run",

+ 7 - 15
src/Axon.st

@@ -12,15 +12,16 @@ My subclasses must provide implementation for:
 
  - add:
  - do:
- - clean
- - (optionally) run
-
-and issue this call before actual use:
-
-	interestFactory: [ :description :block | ... factory that creates appropriate AxonInterest ... ]!
+ - clean!
 
 !AxonBase methodsFor: 'action'!
 
+addInterest: anInterest
+	self
+		add: (anInterest flag; yourself);
+		dirty: true
+!
+
 changed: anAspect
 	| needsToRun |
     needsToRun := false.
@@ -37,15 +38,6 @@ dirty: aBoolean
 	aBoolean ifTrue: [[ self run ] fork]
 !
 
-interestFactory: aBlock
-    factory := aBlock
-!
-
-on: description hook: aBlock
-	self add: (factory value: description value: aBlock) flag.
-   	self dirty: true
-!
-
 run
 	[
 		| needsClean |

+ 42 - 33
src/Trapped-Backend.js

@@ -371,53 +371,62 @@ $globals.Trapper.comment="A portmanteau of 'Trapped wrapper', I am base class fo
 //>>excludeEnd("ide");
 $core.addMethod(
 $core.method({
-selector: "model:",
-protocol: 'accessing',
-fn: function (anObject){
+selector: "interestOn:block:",
+protocol: 'action',
+fn: function (anAspect,aBlock){
 var self=this;
 function $InterestedInTrapPathSubtree(){return $globals.InterestedInTrapPathSubtree||(typeof InterestedInTrapPathSubtree=="undefined"?nil:InterestedInTrapPathSubtree)}
 function $InterestedInTrapPath(){return $globals.InterestedInTrapPath||(typeof InterestedInTrapPath=="undefined"?nil:InterestedInTrapPath)}
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx1) {
 //>>excludeEnd("ctx");
-var $1,$2,$3,$4,$5;
-$recv(self._axon())._interestFactory_((function(description,block){
+var $1,$3,$2,$4;
+$1=$recv($recv(anAspect)._notEmpty())._and_((function(){
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx2) {
 //>>excludeEnd("ctx");
-$1=$recv($recv(description)._notEmpty())._and_((function(){
+return $recv($recv(anAspect)._last())._isNil();
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
-return $core.withContext(function($ctx3) {
-//>>excludeEnd("ctx");
-return $recv($recv(description)._last())._isNil();
-//>>excludeStart("ctx", pragmas.excludeDebugContexts);
-}, function($ctx3) {$ctx3.fillBlock({},$ctx2,2)});
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1,1)});
 //>>excludeEnd("ctx");
 }));
 if($core.assert($1)){
-$2=$recv($InterestedInTrapPathSubtree())._new();
+$3=$recv($InterestedInTrapPathSubtree())._new();
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
-$ctx2.sendIdx["new"]=1;
+$ctx1.sendIdx["new"]=1;
 //>>excludeEnd("ctx");
-$recv($2)._aspect_block_($recv(description)._allButLast(),block);
+$2=$recv($3)._aspect_block_($recv(anAspect)._allButLast(),aBlock);
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
-$ctx2.sendIdx["aspect:block:"]=1;
+$ctx1.sendIdx["aspect:block:"]=1;
 //>>excludeEnd("ctx");
-$3=$recv($2)._yourself();
-//>>excludeStart("ctx", pragmas.excludeDebugContexts);
-$ctx2.sendIdx["yourself"]=1;
-//>>excludeEnd("ctx");
-return $3;
+return $2;
 } else {
-$4=$recv($InterestedInTrapPath())._new();
-$recv($4)._aspect_block_(description,block);
-$5=$recv($4)._yourself();
-return $5;
+$4=$recv($recv($InterestedInTrapPath())._new())._aspect_block_(anAspect,aBlock);
+return $4;
 };
+return self;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
-}, function($ctx2) {$ctx2.fillBlock({description:description,block:block},$ctx1,1)});
+}, function($ctx1) {$ctx1.fill(self,"interestOn:block:",{anAspect:anAspect,aBlock:aBlock},$globals.Trapper)});
+//>>excludeEnd("ctx");
+},
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: ["anAspect", "aBlock"],
+source: "interestOn: anAspect block: aBlock\x0a\x09(anAspect notEmpty and: [ anAspect last isNil ])\x0a\x09\x09ifTrue: [ ^ InterestedInTrapPathSubtree new aspect: anAspect allButLast block: aBlock ]\x0a\x09\x09ifFalse: [ ^ InterestedInTrapPath new aspect: anAspect block: aBlock ]",
+referencedClasses: ["InterestedInTrapPathSubtree", "InterestedInTrapPath"],
+//>>excludeEnd("ide");
+messageSends: ["ifTrue:ifFalse:", "and:", "notEmpty", "isNil", "last", "aspect:block:", "new", "allButLast"]
+}),
+$globals.Trapper);
+
+$core.addMethod(
+$core.method({
+selector: "model:",
+protocol: 'accessing',
+fn: function (anObject){
+var self=this;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
 //>>excludeEnd("ctx");
-}));
 self["@payload"]=anObject;
 self._changed_([]);
 return self;
@@ -427,10 +436,10 @@ return self;
 },
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: ["anObject"],
-source: "model: anObject\x0a\x09self axon\x0a\x09\x09interestFactory: [ :description :block |\x0a\x09\x09\x09(description notEmpty and: [ description last isNil ])\x0a\x09\x09\x09\x09ifTrue: [ InterestedInTrapPathSubtree new aspect: description allButLast block: block; yourself ]\x0a\x09\x09\x09\x09ifFalse: [ InterestedInTrapPath new aspect: description block: block; yourself ]].\x0a\x09payload := anObject.\x0a    self changed: #()",
-referencedClasses: ["InterestedInTrapPathSubtree", "InterestedInTrapPath"],
+source: "model: anObject\x0a\x09payload := anObject.\x0a    self changed: #()",
+referencedClasses: [],
 //>>excludeEnd("ide");
-messageSends: ["interestFactory:", "axon", "ifTrue:ifFalse:", "and:", "notEmpty", "isNil", "last", "aspect:block:", "new", "allButLast", "yourself", "changed:"]
+messageSends: ["changed:"]
 }),
 $globals.Trapper);
 
@@ -491,7 +500,7 @@ var self=this;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx1) {
 //>>excludeEnd("ctx");
-$recv(self._axon())._on_hook_(path,(function(){
+$recv(self._axon())._addInterest_(self._interestOn_block_(path,(function(){
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx2) {
 //>>excludeEnd("ctx");
@@ -499,7 +508,7 @@ return self._read_do_(path,aBlock);
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 }, function($ctx2) {$ctx2.fillBlock({},$ctx1,1)});
 //>>excludeEnd("ctx");
-}));
+})));
 return self;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 }, function($ctx1) {$ctx1.fill(self,"watch:do:",{path:path,aBlock:aBlock},$globals.Trapper)});
@@ -507,10 +516,10 @@ return self;
 },
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: ["path", "aBlock"],
-source: "watch: path do: aBlock\x0a\x09self axon on: path hook: [ self read: path do: aBlock ]",
+source: "watch: path do: aBlock\x0a\x09self axon addInterest: (self\x0a\x09\x09interestOn: path\x0a\x09\x09block: [ self read: path do: aBlock ])",
 referencedClasses: [],
 //>>excludeEnd("ide");
-messageSends: ["on:hook:", "axon", "read:do:"]
+messageSends: ["addInterest:", "axon", "interestOn:block:", "read:do:"]
 }),
 $globals.Trapper);
 

+ 9 - 6
src/Trapped-Backend.st

@@ -122,17 +122,18 @@ and must issue these calls when initializing:
 !Trapper methodsFor: 'accessing'!
 
 model: anObject
-	self axon
-		interestFactory: [ :description :block |
-			(description notEmpty and: [ description last isNil ])
-				ifTrue: [ InterestedInTrapPathSubtree new aspect: description allButLast block: block; yourself ]
-				ifFalse: [ InterestedInTrapPath new aspect: description block: block; yourself ]].
 	payload := anObject.
     self changed: #()
 ! !
 
 !Trapper methodsFor: 'action'!
 
+interestOn: anAspect block: aBlock
+	(anAspect notEmpty and: [ anAspect last isNil ])
+		ifTrue: [ ^ InterestedInTrapPathSubtree new aspect: anAspect allButLast block: aBlock ]
+		ifFalse: [ ^ InterestedInTrapPath new aspect: anAspect block: aBlock ]
+!
+
 modify: path do: aBlock
 	self subclassResponsibility
 !
@@ -142,7 +143,9 @@ read: path do: aBlock
 !
 
 watch: path do: aBlock
-	self axon on: path hook: [ self read: path do: aBlock ]
+	self axon addInterest: (self
+		interestOn: path
+		block: [ self read: path do: aBlock ])
 ! !
 
 Trapper subclass: #DirectTrapper