Created
June 3, 2021 12:07
-
-
Save iax7/0e170084e0d59046b78e83bb40b9af0f to your computer and use it in GitHub Desktop.
Wisper Gem Usage basic example
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
require 'wisper' | |
SLEEP = 0.5 | |
class Sync | |
include Wisper::Publisher | |
def start(user_id) | |
broadcast :start, user_id | |
sleep SLEEP | |
broadcast :matching, user_id | |
sleep SLEEP | |
broadcast :categories, user_id | |
sleep SLEEP | |
broadcast :finish, user_id | |
nested = NestedSync.new | |
nested.on(:nested) { |param| broadcast :nested, param } | |
nested.start_nested | |
nested.start_nested_block { |param| broadcast :nested, param } | |
end | |
end | |
class NestedSync | |
include Wisper::Publisher | |
def start_nested | |
broadcast :nested, 'nested' | |
end | |
def start_nested_block | |
yield({current: 10, total: 300}) | |
end | |
end | |
class SyncNotifier | |
def start(id) puts "Starting to sync #{id}" end | |
def matching(id) puts "Matching products for #{id}" end | |
define_method(:categories) {|id| puts "Matching categories #{id}" } | |
def finish(id) puts "Finished syncing for #{id}" end | |
end | |
#s = Sync.new | |
#s.subscribe(SyncNotifier.new) | |
#s.start(124) | |
puts | |
s = Sync.new | |
s.on(:start) { |id| puts "started #{id}" } | |
.on(:matching) { |id| puts "matching #{id}" } | |
.on(:categories) { |id| puts "categories #{id}" } | |
.on(:finish) { |id| puts "finish #{id}" } | |
.on(:nested) { |txt| puts txt } | |
s.start(99) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment