Skip to content

Refactor/audit reduce global state#5530

Open
vanshika2720 wants to merge 5 commits intosugarlabs:masterfrom
vanshika2720:refactor/audit-reduce-global-state
Open

Refactor/audit reduce global state#5530
vanshika2720 wants to merge 5 commits intosugarlabs:masterfrom
vanshika2720:refactor/audit-reduce-global-state

Conversation

@vanshika2720
Copy link
Contributor

@vanshika2720 vanshika2720 commented Feb 4, 2026

Architecture Pilot: Audit & Reduce Implicit Global State (Logo Subsystem)

Summary

This PR introduces a pilot architectural improvement to audit and reduce implicit global state usage in Music Blocks by focusing on the Logo subsystem. It establishes a repeatable, backward-compatible pattern for making dependencies explicit without changing runtime behavior.

This work builds directly on the goals of #2767 and is intentionally scoped as a non-breaking, reference implementation rather than a full refactor.


What’s Included

1. Global State Audit

  • Audited the Logo subsystem for implicit global dependencies accessed via this.activity.*
  • Identified and categorized 150+ implicit dependencies (Blocks, Turtles, Stage, UI, Config, etc.)
  • Added detailed documentation for future refactoring efforts

2. Explicit Dependency Interface

  • Introduced a LogoDependencies class defining clear dependency contracts
  • Documented required vs optional dependencies
  • Added a fromActivity() factory for backward compatibility with existing architecture

3. Backward-Compatible Constructor Refactor

  • Updated Logo to accept either:
    • the legacy Activity object (unchanged behavior), or
    • a LogoDependencies instance (new explicit pattern)
  • Preserves all existing code paths and behavior

4. Testability Demonstration

  • Added documentation-style tests showing how Logo can now be instantiated with minimal mocks
  • Demonstrates improved isolation and testability without requiring full Activity setup

5. Implementation Documentation

  • Added a concise summary of motivation, approach, benefits, and recommended next steps

Why This Matters

  • Explicit Contracts: Makes hidden global dependencies visible and documented
  • Improved Testability: Enables dependency injection and lightweight mocking
  • Backward Compatible: No breaking changes; existing behavior preserved
  • Foundation for Future Work: Establishes a safe, repeatable pattern for reducing global state
  • Low Risk: Architectural clarity without runtime or UI changes

Verification

  • All existing Logo tests pass (logo.test.js)
  • No behavioral or UI changes observed
  • Backward compatibility confirmed

Scope (Intentional)

Included:

  • Audit and documentation of Logo dependencies
  • Explicit dependency interface
  • Constructor refactor with full backward compatibility

Not included (future work):

  • Internal refactor of this.activity.* usages
  • Changes to other subsystems
  • ES module migration or bundling changes

Related Issues

@github-actions
Copy link
Contributor

github-actions bot commented Feb 4, 2026

✅ All Jest tests passed! This PR is ready to merge.

1 similar comment
@github-actions
Copy link
Contributor

github-actions bot commented Feb 4, 2026

✅ All Jest tests passed! This PR is ready to merge.

@kartikktripathi
Copy link
Contributor

Please address the Lint Errors for logo.js. Thank you.

@vanshika2720 vanshika2720 force-pushed the refactor/audit-reduce-global-state branch from e51b96c to 1f1ccfe Compare February 4, 2026 17:11
@github-actions
Copy link
Contributor

github-actions bot commented Feb 4, 2026

✅ All Jest tests passed! This PR is ready to merge.

@vanshika2720 vanshika2720 force-pushed the refactor/audit-reduce-global-state branch from 1f1ccfe to fc76345 Compare February 4, 2026 17:14
@github-actions
Copy link
Contributor

github-actions bot commented Feb 4, 2026

✅ All Jest tests passed! This PR is ready to merge.

@vanshika2720 vanshika2720 force-pushed the refactor/audit-reduce-global-state branch from fc76345 to 515ee2e Compare February 4, 2026 17:42
@github-actions
Copy link
Contributor

github-actions bot commented Feb 4, 2026

✅ All Jest tests passed! This PR is ready to merge.

@vanshika2720
Copy link
Contributor Author

@walterbender please have a review on this

@walterbender
Copy link
Member

I think this is a nice approach. As per the question I raised in the PhraseMaker widget refactoring, do we want to go with this._deps.... everywhere (Phase 4 as described in your md file) or just define the relevant consts so as to not require major changes.

@walterbender walterbender added the WF3-Needs design review Assign this label after Design has been completed. label Feb 5, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Feb 6, 2026

✅ All Jest tests passed! This PR is ready to merge.

@vanshika2720 vanshika2720 force-pushed the refactor/audit-reduce-global-state branch from 933e4fc to ad66917 Compare February 6, 2026 13:54
@github-actions
Copy link
Contributor

github-actions bot commented Feb 6, 2026

✅ All Jest tests passed! This PR is ready to merge.

3 similar comments
@github-actions
Copy link
Contributor

github-actions bot commented Feb 6, 2026

✅ All Jest tests passed! This PR is ready to merge.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 6, 2026

✅ All Jest tests passed! This PR is ready to merge.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 6, 2026

✅ All Jest tests passed! This PR is ready to merge.

This commit introduces explicit dependency injection for the Logo subsystem to reduce reliance on the global Activity facade. Included is an audit of current dependencies and unit tests demonstrating the new pattern.
This refactor follows the dependency injection pattern established for the
Logo subsystem, making PhraseMaker's external dependencies explicit and
testable.

## Changes Made

✅ Introduced explicit dependency injection via constructor
✅ Updated instantiation site in WidgetBlocks.js to pass dependencies
✅ Replaced all global function calls with this._deps.* pattern (289 refs)
✅ Verified runtime behavior with Jest (all tests passing)
✅ Created a clean, auditable isolation boundary

## Design Decision: this._deps.* vs Local Constants

This implementation uses `this._deps.*` throughout rather than extracting
local constants (e.g., `const _ = this._deps._`) for the following reasons:

1. **Explicit Dependency Tracking**: Every `this._deps.*` call makes it
   immediately clear that we're using an injected dependency, not a global.
   This aids in auditing and understanding data flow.

2. **Grep-ability**: Searching for `this._deps.` instantly reveals all
   external dependencies used in any method, making refactoring safer.

3. **Consistency with Logo Pattern**: Matches the approach documented in
   GLOBAL_STATE_AUDIT.md Phase 4, establishing a uniform pattern across
   the codebase.

4. **Future-Proof**: If we later need to swap implementations or add
   instrumentation, having explicit `this._deps.*` calls makes it easier
   to intercept or modify behavior.

5. **No Hidden Globals**: Local constants like `const _ = this._deps._`
   could be mistaken for module-level imports, reducing clarity.

The trade-off is more verbose code, but the architectural benefits of
explicit, traceable dependencies outweigh the verbosity cost.

## Testing

All 2430 Jest tests pass. No behavioral changes.

Addresses sugarlabs#5529 (Audit and Reduce Implicit Global State Usage)
Automated formatting fixes to comply with Prettier code style.
No functional changes.
Apply local binding pattern to Logo and PhraseMaker to improve code
readability while maintaining explicit dependency injection.

## Changes Made

✅ Logo: Bind blocks, turtles, stage as local properties
✅ PhraseMaker: Bind platformColor, docById, _, wheelnav, slicePath locally
✅ Replace this._deps.* with this.* for bound dependencies (161 refs in PhraseMaker)
✅ Update GLOBAL_STATE_AUDIT.md Phase 4 to document this pattern
✅ All tests passing (2430 tests)

## Pattern Benefits

1. **Reduced Verbosity**: `this.blocks` instead of `this.deps.blocks`
2. **Maintained Auditability**: All dependencies declared in constructor
3. **Grep-able**: Easy to search for `this.blocks` to find usages
4. **No Behavior Change**: Functionally equivalent to `this.deps.*`
5. **Explicit Contracts**: Dependencies still injected, not global

This addresses reviewer feedback about excessive `this._deps.*` usage
while preserving the architectural benefits of dependency injection.

Addresses sugarlabs#5529 (Audit and Reduce Implicit Global State Usage)
@vanshika2720 vanshika2720 force-pushed the refactor/audit-reduce-global-state branch from 7b3607b to 14fe9ae Compare February 6, 2026 16:35
@github-actions
Copy link
Contributor

github-actions bot commented Feb 6, 2026

✅ All Jest tests passed! This PR is ready to merge.

@vanshika2720
Copy link
Contributor Author

vanshika2720 commented Feb 6, 2026

@walterbender I’ve simplified the usage without changing the architecture:

  • Dependency injection remains unchanged
  • Bound commonly used deps once in the constructor
  • Replaced repeated this._deps.* with local properties for readability
  • No behavior or architectural changes (all tests passing)
  • This keeps isolation and auditability intact while reducing verbosity.
    Please have a review on this

@walterbender
Copy link
Member

This looks good to me. Should we close the PhraseMaker PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

WF3-Needs design review Assign this label after Design has been completed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants