hmlongco / Resolver

Swift Ultralight Dependency Injection / Service Locator framework
MIT License
2.14k stars 188 forks source link

`Reset` not resetting Application-scope objects #86

Closed corban123 closed 3 years ago

corban123 commented 3 years ago

Issue: Upon calling Resolver.reset(), registerAllServices() is called, however, upon checking the data of an Application-scope object, the data has not been reset

Repro steps:

  1. Register class as Application scope
  2. Set bool in class to some new value
  3. Call Resolver.reset()
  4. Force registerAllServices() to get called again
  5. Check data in application-scope class

Expectation: Bool is set back to original value

Actual: Bool has not changed.

Unsure if this expectation is correct but would appreciate some sort of resolution

corban123 commented 3 years ago

Is the expectation that if you want to be able to clear it, you'll want to use the Cache scope?

hmlongco commented 3 years ago

Basically. All of the scopes are currently static let values and can't be replaced. The application scope exists for objects whose lifetime equals the entire lifetime of the application.

If you want to cache a set of objects for a certain period of time and then clear them out, use the built-in cache scope or you can also define your own scope on Resolver.

extension Resolver {
    public static let session = ResolverScopeCache()
}

Then at some point in the process call Resolver.session.reset().

corban123 commented 3 years ago

Makes sense, works for me. Thanks for clarifying!

hmlongco commented 3 years ago

That said... I'm not sure it makes sense to have a global reset that doesn't reset everything... so fixed in 1.3.