Browse Source

Announcer: add >>on: aClass doOnce: aBlock

This addition was proposed by Sebastian Sastre (@sebastianconcept)
on the mailinglist.
Manfred Kroehnert 11 years ago
parent
commit
7368590bb8
2 changed files with 41 additions and 0 deletions
  1. 28 0
      js/Kernel-Announcements.js
  2. 13 0
      st/Kernel-Announcements.st

+ 28 - 0
js/Kernel-Announcements.js

@@ -240,6 +240,34 @@ referencedClasses: ["AnnouncementSubscription"]
 }),
 }),
 smalltalk.Announcer);
 smalltalk.Announcer);
 
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "on:doOnce:",
+category: 'subscribing',
+fn: function (aClass,aBlock){
+var self=this;
+var subscription;
+function $AnnouncementSubscription(){return smalltalk.AnnouncementSubscription||(typeof AnnouncementSubscription=="undefined"?nil:AnnouncementSubscription)}
+return smalltalk.withContext(function($ctx1) { 
+var $1,$2;
+$1=_st($AnnouncementSubscription())._new();
+_st($1)._announcementClass_(aClass);
+$2=_st($1)._yourself();
+subscription=$2;
+_st(subscription)._block_((function(ann){
+return smalltalk.withContext(function($ctx2) {
+_st(self["@subscriptions"])._remove_(subscription);
+return _st(aBlock)._value_(ann);
+}, function($ctx2) {$ctx2.fillBlock({ann:ann},$ctx1,1)})}));
+_st(self["@subscriptions"])._add_(subscription);
+return self}, function($ctx1) {$ctx1.fill(self,"on:doOnce:",{aClass:aClass,aBlock:aBlock,subscription:subscription},smalltalk.Announcer)})},
+args: ["aClass", "aBlock"],
+source: "on: aClass doOnce: aBlock\x0a\x09| subscription |\x0a\x09\x0a\x09subscription := AnnouncementSubscription new\x0a\x09\x09announcementClass: aClass;\x0a\x09\x09yourself.\x0a\x09subscription block: [ :ann |\x0a\x09\x09subscriptions remove: subscription.\x0a\x09\x09aBlock value: ann ].\x0a\x0a\x09subscriptions add: subscription",
+messageSends: ["announcementClass:", "new", "yourself", "block:", "remove:", "value:", "add:"],
+referencedClasses: ["AnnouncementSubscription"]
+}),
+smalltalk.Announcer);
+
 smalltalk.addMethod(
 smalltalk.addMethod(
 smalltalk.method({
 smalltalk.method({
 selector: "on:send:to:",
 selector: "on:send:to:",

+ 13 - 0
st/Kernel-Announcements.st

@@ -104,6 +104,19 @@ on: aClass do: aBlock
 		yourself)
 		yourself)
 !
 !
 
 
+on: aClass doOnce: aBlock
+	| subscription |
+	
+	subscription := AnnouncementSubscription new
+		announcementClass: aClass;
+		yourself.
+	subscription block: [ :ann |
+		subscriptions remove: subscription.
+		aBlock value: ann ].
+
+	subscriptions add: subscription
+!
+
 on: aClass send: aSelector to: anObject
 on: aClass send: aSelector to: anObject
 	subscriptions add: (AnnouncementSubscription new
 	subscriptions add: (AnnouncementSubscription new
 		valuable: (MessageSend new
 		valuable: (MessageSend new