Skip to content

loopback.getCurrentContext() not working in a non-http context #982

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

Closed
BerkeleyTrue opened this issue Jan 9, 2015 · 8 comments
Closed

Comments

@BerkeleyTrue
Copy link
Contributor

I have a need to get the current user during change.rectify() calls after a client calls bulkUpdate.

I have limited understanding of how domains/cls work but it seems that since the juggler is not binded to the current domain/ns anywhere as the req/res objects are, the context is not preserved when the DAO emit's events.

Am I on the right track?

Using loopback 2.8.8
loopback-datasource-juggler 2.12.0

@bajtos bajtos added this to the #Epic: Offline Sync V1 milestone Jan 13, 2015
@bajtos
Copy link
Member

bajtos commented Jan 14, 2015

@BerkeleyTrue could you please provide a test case to reproduce the problem? Ideally using a memory connector.

@bajtos bajtos changed the title Get currentContext working in change.rectify. Get currentContext working outside REST Feb 20, 2015
@bajtos
Copy link
Member

bajtos commented Feb 20, 2015

This is a problem not specific to bulkUpdate only, it is also a problem when calling model methods from Mocha unit tests (mailing list thread) and strong-mq message listeners (gitter: subscribing to events does not involve a req object, and thus the middleware to initialize the context is not executed. Can you think of any way to read/write the context from within event subscribe (non web context)? Or perhaps there's another construct aside from req context that allows for local storage?)

@bajtos bajtos changed the title Get currentContext working outside REST loopback.getCurrentContext() not working in a non-http context Feb 20, 2015
@bajtos bajtos added the bug label Feb 20, 2015
@BerkeleyTrue
Copy link
Contributor Author

I ended up changing the logic to avoid this issue. It would be nice to be able to do this in the future.

@raymondfeng
Copy link
Member

LGTM

@bajtos
Copy link
Member

bajtos commented Mar 27, 2015

@raymondfeng @ritch what's your opinion on getCurrentContext in browser? At the moment, it always returns null. Should I implement a shim that will always return the same context object instead?

Related: othiym23/node-continuation-local-storage#36

@BerkeleyTrue
Copy link
Contributor Author

@bajtos Why not just use localStorage in the browser?

@bajtos
Copy link
Member

bajtos commented Mar 30, 2015

Why not just use localStorage in the browser?

I don't understand. Why do you want to persist context in the local storage? How would you provide the default value?

Here is what I am thinking about:

// browser client
loopback.runInContext(function(context) {
  context.set('currentUser', /* data */ });
  // start the HTML5 application
});

However, since we don't have continuation-local-storage now, we can make runInContext optional and simply change loopback.getCurrentContext to return e.g .window.__loopbackCurrentContext.

// browser client
loopback.getCurrentContext().set('currentUser', /* data */);
// start the HTML5 application

Users can implement whatever persistent mechanism on top of this, for example:

var currentUser = localStorage.currentUser ? new User(JSON.parse(localStorage.currentUser)) : null;
loopback.getCurrentContext().set('currentUser', currentUser);
// start the HTML5 app

@bajtos
Copy link
Member

bajtos commented Mar 30, 2015

We agreed with @raymondfeng and @ritch to keep returning null in the browser, as we don't have a reliable way how to provide local context in the browser.

@bajtos bajtos closed this as completed Mar 30, 2015
@bajtos bajtos removed the #wip label Mar 30, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants