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

onReady callback causes infinite loop in template.autorun #71

Open
cormip opened this issue Jun 14, 2022 · 4 comments
Open

onReady callback causes infinite loop in template.autorun #71

cormip opened this issue Jun 14, 2022 · 4 comments

Comments

@cormip
Copy link

cormip commented Jun 14, 2022

Sample code below with onReady callback goes into an infinite AUTORUN loop:

Template.users.onCreated(function() {
    const template = this
    template.usersReady = new ReactiveVar(false)
    template.userLimit = new ReactiveVar(10)
    template.autorun(function() {
        console.log('AUTORUN')
        const subUsers = subsCache.subscribe('users', template.userLimit.get(), {
            onReady: function () {
                template.usersReady.set(true)
            }
        })
    })
})

The code below does not:

Template.users.onCreated(function() {
    const template = this
    template.usersReady = new ReactiveVar(false)
    template.userLimit = new ReactiveVar(10)
    template.autorun(function() {
        console.log('AUTORUN')
        const subUsers = subsCache.subscribe('users', template.userLimit.get())
        if (subUsers.ready()) {
            template.usersReady.set(true)
        }
    })
})
@jankapunkt
Copy link
Contributor

Onready makes the autorun useless so just call it outside.

@cormip
Copy link
Author

cormip commented Jun 14, 2022

If I don't use autorun, then the subscription doesn't rerun when a parameter the subscription depends on gets updated. In the example above:

template.userLimit.get()

@jankapunkt
Copy link
Contributor

Okay I overlooked this. In such case, would it work if you omit assigning the subscription value to a variable that is declared inside autorun? Furthermore, I would also try to play with Tracker.nonReactive for those vars that are not supposed to affect the current autorun computation:

Template.users.onCreated(function() {
    const template = this
    template.usersReady = new ReactiveVar(false)
    template.userLimit = new ReactiveVar(10)
    template.autorun(function() {
        console.log('AUTORUN')
        // avoid introducting a new reactive data source:
        subsCache.subscribe('users', template.userLimit.get(), {
            onReady: function () {
                 // avoid deps.changed call by using:
                Tracker.nonReactive(() => template.usersReady.set(true))
            }
        })
    })
})

Does that help? I am just asking this much to see, if the issue is rather a Blaze/Tracker issue or if it's really caused by this package.

@cormip
Copy link
Author

cormip commented Jun 16, 2022

Tried your code sample. It still goes into an infinite loop. Not assigning the variable also wouldn't be ideal, because further manipulation of that cached subscription would then not be possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants