-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
124 additions
and
10 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
src/GToolkit-Utility-Basic/GtAnnouncingTimeCachedObject.class.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
" | ||
GtAnnouncingTimeCachedObject extends GtTimeCachedObject to update values in the background and announce when they are available. | ||
If `retrievingValue` is nil, the last known value is used, otherwise the `retrievingValue` is answered. | ||
" | ||
Class { | ||
#name : #GtAnnouncingTimeCachedObject, | ||
#superclass : #GtTimeCachedObject, | ||
#instVars : [ | ||
'announcer', | ||
'retrievingMessage' | ||
], | ||
#category : #'GToolkit-Utility-Basic' | ||
} | ||
|
||
{ #category : #'instance creation' } | ||
GtAnnouncingTimeCachedObject class >> get: aBlock timeout: aDuration [ | ||
|
||
^ self new initialize: aBlock timeout: aDuration | ||
] | ||
|
||
{ #category : #'instance creation' } | ||
GtAnnouncingTimeCachedObject class >> get: aBlock timeout: aDuration announcer: anAnnouncer [ | ||
|
||
^ (self new initialize: aBlock timeout: aDuration) | ||
announcer: anAnnouncer | ||
] | ||
|
||
{ #category : #'instance creation' } | ||
GtAnnouncingTimeCachedObject class >> get: aBlock timeout: aDuration announcer: anAnnouncer retrievingMessage: aString [ | ||
|
||
^ (self new initialize: aBlock timeout: aDuration) | ||
announcer: anAnnouncer; | ||
retrievingMessage: aString | ||
] | ||
|
||
{ #category : #accessing } | ||
GtAnnouncingTimeCachedObject >> announcer [ | ||
^ announcer | ||
] | ||
|
||
{ #category : #accessing } | ||
GtAnnouncingTimeCachedObject >> announcer: anObject [ | ||
announcer := anObject | ||
] | ||
|
||
{ #category : #accessing } | ||
GtAnnouncingTimeCachedObject >> retrievingMessage [ | ||
^ retrievingMessage | ||
] | ||
|
||
{ #category : #accessing } | ||
GtAnnouncingTimeCachedObject >> retrievingMessage: anObject [ | ||
retrievingMessage := anObject | ||
] | ||
|
||
{ #category : #private } | ||
GtAnnouncingTimeCachedObject >> updateAndAnnounce [ | ||
|
||
[ self newValue ] asAsyncPromise | ||
then: [ :newValue | | ||
[ announcer announce: (GtTimeCachedObjectAnnouncement | ||
newValue: newValue cache: self) ] | ||
on: Error | ||
fork: [ :ex | self error: 'Unable to notify: ', ex errorMessage ] ] | ||
otherwise: [ :ex | | ||
self error: 'Unable to update: ', ex errorMessage ]. | ||
] | ||
|
||
{ #category : #accessing } | ||
GtAnnouncingTimeCachedObject >> valueWithin: aDuration [ | ||
"Answer the receiver's value, updating it if the value is more than aDuration old" | ||
|
||
DateAndTime now <= self cacheTimeout ifTrue: [ ^ value ]. | ||
self updateAndAnnounce. | ||
^ retrievingMessage ifNil: [ value ]. | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/GToolkit-Utility-Basic/GtTimeCachedObjectAnnouncement.class.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
Class { | ||
#name : #GtTimeCachedObjectAnnouncement, | ||
#superclass : #Announcement, | ||
#instVars : [ | ||
'newValue', | ||
'cache' | ||
], | ||
#category : #'GToolkit-Utility-Basic' | ||
} | ||
|
||
{ #category : #'instance creation' } | ||
GtTimeCachedObjectAnnouncement class >> newValue: anObject cache: aGtTimeCachedObject [ | ||
|
||
^ self new initializeValue: anObject from: aGtTimeCachedObject | ||
] | ||
|
||
{ #category : #'as yet unclassified' } | ||
GtTimeCachedObjectAnnouncement class >> value [ | ||
^ self | ||
] | ||
|
||
{ #category : #accessing } | ||
GtTimeCachedObjectAnnouncement >> cache [ | ||
^ cache | ||
] | ||
|
||
{ #category : #initialization } | ||
GtTimeCachedObjectAnnouncement >> initializeValue: anObject from: aGtTimeCachedObject [ | ||
|
||
newValue := anObject. | ||
cache := aGtTimeCachedObject. | ||
] | ||
|
||
{ #category : #accessing } | ||
GtTimeCachedObjectAnnouncement >> newValue [ | ||
^ newValue | ||
] |