Skip to content

Commit

Permalink
Add GtAnnounceingTimeCachedObject
Browse files Browse the repository at this point in the history
  • Loading branch information
akgrant43 committed Dec 6, 2024
1 parent 585a319 commit 0f25fa1
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 10 deletions.
77 changes: 77 additions & 0 deletions src/GToolkit-Utility-Basic/GtAnnouncingTimeCachedObject.class.st
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 ].
]
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ GtInheritedMethodsSortFunction >> compareMethodNameOf: aMethodOrName1 withMethod
ifFalse: [ methodName2 isBinary threeWayCompareTo: methodName1 isBinary ]
]

{ #category : #comparing }
GtInheritedMethodsSortFunction >> hash [
^ self class hash
]

{ #category : #'private - comparison' }
GtInheritedMethodsSortFunction >> hasMethodNameOf: aMethodOrName [
<return: #Symbol>
Expand All @@ -116,11 +121,6 @@ GtInheritedMethodsSortFunction >> hasMethodNameOf: aMethodOrName [
^ [ aMethodOrName selector notNil ] on: Error do: [ :ex | ex return: false ]
]

{ #category : #comparing }
GtInheritedMethodsSortFunction >> hash [
^ self class hash
]

{ #category : #'private - comparison' }
GtInheritedMethodsSortFunction >> methodNameOf: aMethodOrName [
<return: #Symbol>
Expand Down
10 changes: 5 additions & 5 deletions src/GToolkit-Utility-Basic/GtMethodsSortFunction.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ GtMethodsSortFunction >> compareMethodNameOf: aMethodOrName1 withMethodNameOf: a
ifFalse: [ methodName2 isBinary threeWayCompareTo: methodName1 isBinary ]
]

{ #category : #comparing }
GtMethodsSortFunction >> hash [
^ self class hash
]

{ #category : #'private - comparison' }
GtMethodsSortFunction >> hasMethodNameOf: aMethodOrName [
<return: #Symbol>
Expand All @@ -127,11 +132,6 @@ GtMethodsSortFunction >> hasMethodNameOf: aMethodOrName [
^ [ aMethodOrName selector notNil ] on: Error do: [ :ex | ex return: false ]
]

{ #category : #comparing }
GtMethodsSortFunction >> hash [
^ self class hash
]

{ #category : #'private - comparison' }
GtMethodsSortFunction >> methodNameOf: aMethodOrName [
<return: #Symbol>
Expand Down
37 changes: 37 additions & 0 deletions src/GToolkit-Utility-Basic/GtTimeCachedObjectAnnouncement.class.st
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
]

0 comments on commit 0f25fa1

Please sign in to comment.