Skip to content

Commit

Permalink
Merge child async (#2)
Browse files Browse the repository at this point in the history
* merge child async

* lint

* Update changeset.rbi

* misc

* add ruby 3.2

* bumping version

* Update changelog.md
  • Loading branch information
apneadiving authored Aug 1, 2024
1 parent c391dd0 commit 7dcde58
Show file tree
Hide file tree
Showing 15 changed files with 451 additions and 87 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ on:

jobs:
tests:
name: Sorbet
name: all
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: ['3.1', '3.0', '2.7']
ruby-version: ['3.2', '3.1', '3.0']
steps:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ gemspec

# To use a debugger
# gem 'byebug', group: [:development, :test]
gem "sorbet-runtime", "0.5.10473"
gem "sorbet-runtime", "0.5.9204"

group :development, :test do
gem "sorbet", "0.5.10473"
gem "sorbet", "0.5.9204"
gem "tapioca", require: false
gem "standard"
end
Expand Down
101 changes: 58 additions & 43 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
changeset (0.1.1)
changeset (0.1.3)
zeitwerk

GEM
Expand All @@ -11,22 +11,27 @@ GEM
byebug (11.1.3)
coderay (1.1.3)
diff-lcs (1.5.0)
json (2.7.2)
language_server-protocol (3.17.0.3)
lint_roller (1.1.0)
method_source (1.0.0)
netrc (0.11.0)
parallel (1.22.1)
parser (3.1.2.1)
parallel (1.25.1)
parser (3.3.4.0)
ast (~> 2.4.1)
racc
pry (0.14.1)
coderay (~> 1.1)
method_source (~> 1.0)
rainbow (3.0.0)
rbi (0.0.15)
racc (1.8.1)
rainbow (3.1.1)
rbi (0.0.17)
ast
parser (>= 2.6.4.0)
parser (>= 3.0.0)
sorbet-runtime (>= 0.5.9204)
unparser
regexp_parser (2.2.1)
rexml (3.2.5)
unparser (>= 0.5.6)
regexp_parser (2.9.2)
rexml (3.3.4)
strscan
rspec (3.11.0)
rspec-core (~> 3.11.0)
rspec-expectations (~> 3.11.0)
Expand All @@ -40,49 +45,58 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-support (3.11.1)
rubocop (1.22.3)
rubocop (1.64.1)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.0.0.0)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml
rubocop-ast (>= 1.12.0, < 2.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.31.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.13.0)
parser (>= 3.0.1.1)
rubocop-performance (1.11.5)
rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
ruby-progressbar (1.11.0)
sorbet (0.5.10473)
sorbet-static (= 0.5.10473)
sorbet-runtime (0.5.10473)
sorbet-static (0.5.10473-universal-darwin-19)
sorbet-static (0.5.10473-universal-darwin-20)
sorbet-static (0.5.10473-x86_64-linux)
sorbet-static-and-runtime (0.5.10473)
sorbet (= 0.5.10473)
sorbet-runtime (= 0.5.10473)
spoom (1.1.12)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.31.3)
parser (>= 3.3.1.0)
rubocop-performance (1.21.1)
rubocop (>= 1.48.1, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
ruby-progressbar (1.13.0)
sorbet (0.5.9204)
sorbet-static (= 0.5.9204)
sorbet-runtime (0.5.9204)
sorbet-static (0.5.9204-universal-darwin-19)
sorbet-static (0.5.9204-universal-darwin-20)
sorbet-static (0.5.9204-universal-darwin-21)
sorbet-static (0.5.9204-x86_64-linux)
spoom (1.1.13)
sorbet (>= 0.5.9204)
sorbet-runtime (>= 0.5.9204)
thor (>= 0.19.2)
standard (1.4.0)
rubocop (= 1.22.3)
rubocop-performance (= 1.11.5)
tapioca (0.10.2)
standard (1.39.2)
language_server-protocol (~> 3.17.0.2)
lint_roller (~> 1.0)
rubocop (~> 1.64.0)
standard-custom (~> 1.0.0)
standard-performance (~> 1.4)
standard-custom (1.0.2)
lint_roller (~> 1.0)
rubocop (~> 1.50)
standard-performance (1.4.0)
lint_roller (~> 1.1)
rubocop-performance (~> 1.21.0)
strscan (3.1.0)
tapioca (0.7.3)
bundler (>= 1.17.3)
netrc (>= 0.11.0)
parallel (>= 1.21.0)
pry (>= 0.12.2)
rbi (~> 0.0.0, >= 0.0.14)
sorbet-static-and-runtime (>= 0.5.9204)
sorbet-runtime (>= 0.5.9204)
sorbet-static (>= 0.5.9204)
spoom (~> 1.1.0, >= 1.1.11)
thor (>= 1.2.0)
yard-sorbet
thor (1.2.1)
unicode-display_width (2.1.0)
unicode-display_width (2.5.0)
unparser (0.6.5)
diff-lcs (~> 1.3)
parser (>= 3.1.0)
Expand All @@ -92,9 +106,10 @@ GEM
yard-sorbet (0.7.0)
sorbet-runtime (>= 0.5)
yard (>= 0.9)
zeitwerk (2.6.1)
zeitwerk (2.6.6)

PLATFORMS
arm64-darwin-21
x86_64-darwin-19
x86_64-darwin-20
x86_64-linux
Expand All @@ -103,10 +118,10 @@ DEPENDENCIES
byebug
changeset!
rspec (= 3.11.0)
sorbet (= 0.5.10473)
sorbet-runtime (= 0.5.10473)
sorbet (= 0.5.9204)
sorbet-runtime (= 0.5.9204)
standard
tapioca

BUNDLED WITH
2.3.21
2.5.9
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 0.1.3
- Add `merge_child_async` for legacy code concerns

# 0.1.2
- Breaking change: `add_event` signature without keyword args

Expand Down
15 changes: 10 additions & 5 deletions lib/changeset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@
class Changeset
def initialize(events_catalog = Changeset::NullEventCatalog.new)
@events_collection = Changeset::EventCollection.new
@db_operations = []
@db_operations = ::Changeset::DbOperationCollection.new
@events_catalog = events_catalog
end

def merge_child(change_set)
events_collection.merge_child(change_set.events_collection)
db_operations.concat(change_set.db_operations)
db_operations.merge_child(change_set.db_operations)
self
end

def merge_child_async(&changeset_wrapped_in_proc)
async_change_set = ::Changeset::AsyncChangeset.new(changeset_wrapped_in_proc)
events_collection.merge_child_async(async_change_set)
db_operations.merge_child_async(async_change_set)
self
end

Expand All @@ -30,7 +37,7 @@ def add_db_operations(*persistence_handlers)
end

def add_db_operation(persistence_handler)
db_operations.push(persistence_handler)
db_operations.add(persistence_handler)
self
end

Expand Down Expand Up @@ -60,8 +67,6 @@ def self.configure(&block)
private

def commit_db_operations
# should we move the transaction to also wrap the events?
# in other words: should we still commit to db if events fail to dispatch?
Changeset.configuration.db_transaction_wrapper.call do
db_operations.each(&:call)
end
Expand Down
28 changes: 28 additions & 0 deletions lib/changeset/async_changeset.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# typed: true

class Changeset
class AsyncChangeset
class InconsistencyError < StandardError; end

def initialize(changeset_wrapped_in_proc)
@changeset_wrapped_in_proc = changeset_wrapped_in_proc
@called = false
end

def db_operations
changeset.send(:db_operations)
end

def events_collection
changeset.send(:events_collection)
end

private

def changeset
@changeset ||= @changeset_wrapped_in_proc.call.tap do |result|
raise InconsistencyError unless result.is_a?(::Changeset)
end
end
end
end
45 changes: 45 additions & 0 deletions lib/changeset/db_operation_collection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# typed: true

class Changeset
class DbOperationCollection
def initialize
@collection = []
end

def add(persistence_handler)
collection.push(persistence_handler)
end

def merge_child(db_operations)
db_operations.collection.each do |db_operation|
add(db_operation)
end
end

def merge_child_async(async_change_set)
add(async_change_set)
self
end

def each(&block)
collection.each do |element|
case element
when Changeset::AsyncChangeset
element.db_operations.each do |operation|
yield(operation)
end
else
yield(element)
end
end
end

def ==(other)
collection == other.collection
end

protected

attr_reader :collection
end
end
1 change: 1 addition & 0 deletions lib/changeset/event_catalog_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def dispatch(event)
end

def known_event?(event_name)
false
end

def class
Expand Down
20 changes: 19 additions & 1 deletion lib/changeset/event_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Changeset
class EventCollection
def initialize
@grouped_events = {}
@async_change_sets = []
end

def add(name:, raw_payload:, events_catalog:)
Expand All @@ -19,19 +20,29 @@ def merge_child(event_collection)
event_collection.all_events.each do |event|
add_event(event)
end
event_collection.async_change_sets.each do |async_change_set|
async_change_sets.push(async_change_set)
end
end

def merge_child_async(async_change_set)
async_change_sets.push(async_change_set)
self
end

# standard:disable Style/ArgumentsForwarding
def each(&block)
uniq_events.each(&block)
end
# standard:enable Style/ArgumentsForwarding

def ==(other)
uniq_events == other.uniq_events
end

protected

attr_reader :grouped_events
attr_reader :grouped_events, :async_change_sets

# only used for merge
def all_events
Expand All @@ -42,7 +53,14 @@ def all_events
end
end

# called after push through #each
def uniq_events
async_change_sets.each do |async_change_set|
async_change_set.events_collection.each do |event|
add_event(event)
end
end

[].tap do |collection|
grouped_events.each_value do |events|
collection.concat(events.uniq { |event| event.unicity_key })
Expand Down
2 changes: 1 addition & 1 deletion lib/changeset/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# typed: strict

class Changeset
VERSION = "0.1.2"
VERSION = "0.1.3"
end
Loading

0 comments on commit 7dcde58

Please sign in to comment.