Code Monkey home page Code Monkey logo

Comments (6)

Zhuinden avatar Zhuinden commented on June 24, 2024

Curiously, this is the one thing I've been wondering about ever since creating backstack.lookupService<T>(), I specifically remember thinking about this exact scenario about 4 years ago, but it somehow just never came up.

What I do know is that this is also why I added lookupFromScope() in case something goes wrong.

In fact, to my surprise, this is what the changelog says:

-Simple Stack 1.13.1 (2018-11-25)

...

ADDED: lookupFromScope() and canFindFromScope() methods that begin the lookup from the specified scope, instead of the active-most one. This is to allow safer look-ups when the same service tag is used in different scopes.

So apparently I knew about this 4.5 years ago, but somehow it never came up in my usecases.

Knowing that, the following works:

            val backstack = LocalBackstack.current
            val key = LocalComposeKey.current as FirstKey

            val eventHandler = rememberService<ActionHandler>()
            val commonSharedService = remember {
                backstack.lookupFromScope<CommonSharedService>(
                    key.scopeTag,
                    CommonSharedService::class.java.name
                )
            }

Not exactly optimal, there's clearly no reified helper function for it in simple-stack-extensions at this time (hence the manual ::class.java.name) because we never ended up running into this problem, but this is the theoreticaly fix for this, as you do look up the nearest service by that name, and scopes are only evicted when the state changes fully execute.

I'm not sure what to do about it as this is happening according to lookupService's design, but the solution really is to either use lookupFromScope or use a specific service tag rather than T::class.java.name if there are multiple instances of the same class. 🤔

from simple-stack.

matejdro avatar matejdro commented on June 24, 2024

So, something like this would be a solution?

matejdro/simple-stack-compose-integration@fffbc52

Maybe documentation should emphasize lookupFromScope over lookupService, since there is this gotcha with lookupService?

from simple-stack.

Zhuinden avatar Zhuinden commented on June 24, 2024

I've been using by lazy { backstack.lookup<T>() } for so long, I'll need to think of a good plan for this one

from simple-stack.

Zhuinden avatar Zhuinden commented on June 24, 2024

I added a rememberServiceFrom() although I feel like I will still need to add documentation and think a bit more about this.

This generally hasn't come up as I usually have 1 specific service type for screen so that you can utilize lookup across screens via implicit parents.

from simple-stack.

Zhuinden avatar Zhuinden commented on June 24, 2024

I think this is "resolved" by Zhuinden/simple-stack-extensions@9c7d00f and Zhuinden/simple-stack-extensions@5471b83 + reusing service name is not technically "disallowed", disallowing it would be a breaking change.

It could be hypothetically possible to add a "strict mode" where this case throws but not sure if that really helps.

from simple-stack.

matejdro avatar matejdro commented on June 24, 2024

Yes, this has been resolved. Sorry, I forgot to close it.

from simple-stack.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.