Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resource Reform #112

Merged
merged 50 commits into from
Sep 22, 2023
Merged

Resource Reform #112

merged 50 commits into from
Sep 22, 2023

Conversation

wycats
Copy link
Member

@wycats wycats commented Aug 8, 2023

This is an in-progress pull request that reforms the resource implementation.

Its primary purpose is to separate out the setup phase (the code that runs on initial render that sets up the resource's state) from the synchronization phase (the code that runs outside of the render phase and synchronizes external state with reactive state).

It creates a new low-level primitive: SyncTo. A SyncTo represents code that runs in the sync phase, which corresponds to a framework's structured phase for synchronizing reactive state with external stores. For example, it would correspond to the code in useEffect in React.

This pull request also formalizes the separation between the setup phase (which runs once per component lifetime), the render phase (which runs once per top-level render, including re-render), and the sync phase (which runs during external store synchronization).

Finally, this pull request also retools the lifetime system to be more aligned with JavaScript's Stage 3 Explicit Resource Management API. It's a rather significant internal change, but from a user-facing perspective, it creates the concept of an active Finalization Scope that supplements the existing concept of an active Tracking Frame.

At the primitive level, it's now possible to register a finalization handler with the current finalization scope, and it's also possible to link an object with the current finalization scope. It is also possible to push a new finalization scope, as well as mount an existing finalization scope to collect additional finalizers.

It's still possible to explicitly link objects together and register finalization handlers onto existing objects, but a lot of explicit linking is handled through finalization scopes.

Before this PR is ready, I'll update this text with some additional information about the fact that automatic linking occurs during the setup phase, and how the updated resource API makes structuring code in this way idiomatic.

Current status:

  • Update lifetime infrastructure
    • Move lifetime primitives into @starbeam/shared
  • Lifetime tests
  • Sync primitive
  • Higher level SyncTo
  • Child synchronization
  • Sync testing
  • Resource implementation
  • Child resources
  • Resource testing
  • ResourceList
  • Update Service for new Resource infrastructure
  • Integration with existing renderers
    • React
    • Preact
    • Vue
  • Garbage collect old resource implementation
  • Restore Description infrastructure
  • All tests pass (currently 5 4 failing tests)

Follow-ups:

  • Good error messages if reactive values are used incorrectly during the resource phases.
  • Minimize the surface area of the lifetime infrastructure in @starbeam/shared

🚧 This description, like the PR itself, is under construction. It will be updated as the PR nears completion.

@stackblitz
Copy link

stackblitz bot commented Aug 8, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@wycats wycats marked this pull request as draft August 8, 2023 20:16
wycats and others added 14 commits September 5, 2023 16:45
Child resources is the last bit. To be continued!
Since we're going to use Sync as the primitive under Resource, this
commit beefs up and thoroughly tests child Syncs.
This is a first stab at the implementation. I think that using primitive
sync features would make the code more straightforward, so I plan to do that
before finalizing this PR.
getServiceFormula is a low-level API that renderers can use to
synchronize all services together with the root component (or
equivalent).
@NullVoxPopuli NullVoxPopuli force-pushed the feature/resources-refresh branch from 8bd259a to 921ceb7 Compare September 5, 2023 20:46
wycats and others added 21 commits September 17, 2023 19:28
This is a first stab at the implementation. I think that using primitive
sync features would make the code more straightforward, so I plan to do that
before finalizing this PR.
getServiceFormula is a low-level API that renderers can use to
synchronize all services together with the root component (or
equivalent).
React testing library assumes that `act` is available in testing, but
it's not available in prod.

For now, disable the React prod tests to land resource reform.

We'll hopefully re-enable the tests soon, but it will require finding a
strategy for writing generic acceptance tests in React that work both in
development and production mode.
@wycats wycats force-pushed the feature/resources-refresh branch from 26ec114 to dc3844f Compare September 22, 2023 20:06
This is an old test in @starbeam/runtime that has been moved to
@starbeam/debug *and* updated to guard against production mode (because
description APIs are intended to produce `undefined` in production
builds).
@NullVoxPopuli NullVoxPopuli marked this pull request as ready for review September 22, 2023 21:55
@NullVoxPopuli NullVoxPopuli merged commit d03d514 into main Sep 22, 2023
7 checks passed
@NullVoxPopuli NullVoxPopuli deleted the feature/resources-refresh branch September 22, 2023 22:06
@github-actions github-actions bot mentioned this pull request Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants