Last active
May 11, 2022 17:35
-
-
Save okay-type/0922f38ce7268cda58bf915cdfb89b00 to your computer and use it in GitHub Desktop.
testing event coalescing in robofont subscriber ... this one tries to wait until all button presses are done before firing
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
from mojo.subscriber import Subscriber, registerRoboFontSubscriber | |
from mojo.subscriber import registerSubscriberEvent, roboFontSubscriberEventRegistry | |
from mojo.subscriber import WindowController | |
from mojo.events import postEvent | |
from vanilla import Window, Button | |
coalescingTestKey = 'okay.coalescingTest' | |
class coalescingTest(Subscriber, WindowController): | |
debug = True | |
incrementCache = [] | |
def build(self): | |
self.i = 0 | |
self.w = Window((0, 0, 150, 30), 'Blank') | |
self.w.button = Button((0, 0, -0, -0), 'Test '+str(self.i), callback=self.test) | |
self.w.open() | |
def test(self, sender): | |
postEvent(f'{coalescingTestKey}.testHold') | |
postEvent(f'{coalescingTestKey}.testDidIncrement', increment=self.i) | |
print('event posted', self.i) | |
self.i += 1 | |
self.w.button.setTitle('Test '+str(self.i)) | |
self.hold = True | |
# print('self.hold = True') | |
testHoldDelay = 0.5 | |
def testHold(self, sender): | |
self.hold = False | |
postEvent(f'{coalescingTestKey}.testDidIncrement', increment=None) | |
# print('self.hold = False') | |
def testDidIncrement(self, notification): | |
for event in notification['lowLevelEvents']: | |
i = event['increment'] | |
if i is not None: | |
self.incrementCache.append(i) | |
if self.hold is False: | |
print('testDidIncrement event occured', self.incrementCache) | |
self.incrementCache = [] | |
else: | |
print('testDidIncrement is holding') | |
if f'{coalescingTestKey}.testDidIncrement' not in roboFontSubscriberEventRegistry: | |
registerSubscriberEvent( | |
subscriberEventName=f'{coalescingTestKey}.testDidIncrement', | |
methodName='testDidIncrement', | |
lowLevelEventNames=[f'{coalescingTestKey}.testDidIncrement'], | |
dispatcher='roboFont', | |
delay=2, | |
) | |
if f'{coalescingTestKey}.testHold' not in roboFontSubscriberEventRegistry: | |
registerSubscriberEvent( | |
subscriberEventName=f'{coalescingTestKey}.testHold', | |
methodName='testHold', | |
lowLevelEventNames=[f'{coalescingTestKey}.testHold'], | |
dispatcher='roboFont', | |
delay=0.5, | |
) | |
if __name__ == '__main__': | |
registerRoboFontSubscriber(coalescingTest) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment