Skip to content

Commit

Permalink
⚡️ No more double instantiation of objects
Browse files Browse the repository at this point in the history
Objects that are in common between bin/ and lib/ are handed off from former to latter in dependency injection.

This also preserves state in any handoff objects.
  • Loading branch information
mkarlesky committed May 2, 2024
1 parent bc7f6b4 commit 02e3e1f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
15 changes: 15 additions & 0 deletions bin/ceedling
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ begin
objects = {} # Empty initial hash to be redefined (fingers crossed)
objects = DIY::Context.from_yaml( File.read( bin_objects_filepath ) )
objects.build_everything()

# Extract objects shared between bootloader and application
# This prevents double instantiation and preserves object state in handoff
handoff_objects = {}
handoff = [
:loginator,
:file_wrapper,
:yaml_wrapper,
:config_walkinator,
:stream_wrapper,
:system_wrapper,
:verbosinator
]
CEEDLING_HANDOFF_OBJECTS = handoff_objects
handoff.each {|name| handoff_objects[name] = objects[name] }

# Remove all load paths we've relied on (main application will set up load paths again)
$LOAD_PATH.delete( ceedling_bin_path )
Expand Down
11 changes: 9 additions & 2 deletions bin/objects.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
# SPDX-License-Identifier: MIT
# =========================================================================

# Loaded from ceedling/lib
#
# Loaded from lib/
# ----------------
#

file_wrapper:

yaml_wrapper:
Expand All @@ -25,7 +29,10 @@ loginator:
- system_wrapper
- stream_wrapper

# Loaded from bin (here)
#
# Loaded from bin/
# ----------------
#

actions_wrapper:

Expand Down
9 changes: 9 additions & 0 deletions lib/ceedling/rakefile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,18 @@ def log_runtime(run, start_time_s, end_time_s, enabled)
# 3. Remove full path from $LOAD_PATH
$LOAD_PATH.unshift( CEEDLING_APPCFG[:ceedling_lib_path] )
objects_filepath = File.join( CEEDLING_APPCFG[:ceedling_lib_path], 'objects.yml' )

# Create object hash and dependency injection context
@ceedling = {} # Empty hash to be redefined if all goes well
@ceedling = DIY::Context.from_yaml( File.read( objects_filepath ) )

# Inject objects already insatantiated from bin/ bootloader before building the rest
CEEDLING_HANDOFF_OBJECTS.each_pair {|name,obj| @ceedling.set_object( name.to_s, obj )}

# Build Ceedling application's objects
@ceedling.build_everything()

# Simplify load path after construction
$LOAD_PATH.delete( CEEDLING_APPCFG[:ceedling_lib_path] )

# One-stop shopping for all our setup and such after construction
Expand Down

0 comments on commit 02e3e1f

Please sign in to comment.