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

Explicit Resource Management #60

Open
freshgum-bubbles opened this issue Jul 25, 2023 · 0 comments
Open

Explicit Resource Management #60

freshgum-bubbles opened this issue Jul 25, 2023 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@freshgum-bubbles
Copy link
Owner

Currently, the API is rather unsafe, in that a service could be disposed as another service is using it.
We could combat this using an explicit resource management framework, wherein services are attained
via references, and those references can be released later. This provides guarantees to the calling code
that a service won't dispose itself while it's in-use.

We could accomplish the above using a sort of garbage collector, with a simplified implementation of
the reference counting algorithm, which is a fairly universal concept.

The advantage of this algorithm is that it gives us more information as to how the caller is using the provided
symbol, so we can make more informed decisions later.

The current APIs should not be affected by this change. In the case of a reference counter, Calls to get
should not be reference-counted to ensure backwards compatibility. Additionally, we should ensure that the
usual behaviour of disposing services (after a .get call) is still satisfied.

Find a basic implementation of this below:

const reference = Container.refer(MyService);
const myService = reference.current;

// This won't do anything, as a reference to MyService is currently held:
Container.remove(MyService);

await myService.runAsyncCode();
reference.release();

// The identifier is now removed.

A way to use this in services could be:

@Service([
  Refer(MyService)
])
class AnotherService {
  constructor (private myServiceRef: Refer<MyService>) { }
}

Questions

  • In the case of Container.remove, it would probably be best to immediately remove the identifier from the map, to ensure compatibility with code that would expect the older behaviour.
  • One aspect of interest is calling reset when a reference is held: when that reference is released, should the identifier be immediately discarded? I personally think this would be wise.
@freshgum-bubbles freshgum-bubbles added the enhancement New feature or request label Jul 25, 2023
@freshgum-bubbles freshgum-bubbles self-assigned this Jul 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant