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

[2308] Refactor Adaptive store to use DI #2312

Merged
merged 7 commits into from
Aug 13, 2021

Conversation

BobrImperator
Copy link
Collaborator

@BobrImperator BobrImperator commented Jul 13, 2021

  • registers session-stores in Ember's DI system.
  • refactors AdaptiveStore to use registered CookieStore and LocalStorageStore.
  • modifies test to use setupTest so there's access to ember's container
  • the side effect of setupTest is that it seems to require the test to be asynchronous, so all test functions are marked as async so they return a promise implicitly
  • changes how CookieStore is initialized by implementing _initialize method that handles setting of properties that would normally be provided during creation of CookieStore instance.
  • changed e.g cookieName property to _cookieName in some tests because using 'non private' property would override the underlying computed property which acts as a proxy, because of this behaviour, the private 'default' values are changed and not the public ones.

closes #2308

@BobrImperator BobrImperator force-pushed the 2308-adaptive-store-DI branch 6 times, most recently from 56c6516 to d753b58 Compare July 13, 2021 22:29
@BobrImperator BobrImperator changed the title [DRAFT] 2308 adaptive store di [2308] Refactor Adaptive store to use DI Jul 13, 2021
@BobrImperator BobrImperator requested a review from a team July 15, 2021 17:12
Copy link
Member

@marcoow marcoow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great work – this is one of the messiest parts of the codebase 🙌

@@ -53,6 +57,8 @@ export default Base.extend({
*/
localStorageKey: 'ember_simple_auth-session',

localStorage: injectStore('local-storage'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be set in init instead?

this.localStorage = getOwner(this).lookup(`session-store:local-storage`);

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, yes 👍

_cookies: service('cookies'),
cookie: injectStore('cookie'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see above


store = this._createStore(Cookie, options);
store._initialize(options);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does

Suggested change
store._initialize(options);
store.setProperties(options);

not work here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could check that out, although _initalize currently needs to treat the cookieName property in a special way.

The proxy properties that were receiving some default values on initialization weren't causing any issues before, but right now since we can't really pass any properties during init, but we set them after instead - its causing e.g cookieName to be cached with a wrong value.

sinon.spy(cookieService, 'read');
sinon.spy(cookieService, 'write');
store = createAdaptiveStore(cookieService, {
_isLocal: false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to exist anywhere actually? 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's true, I will remove it, also noticed that some of the _isFastBoot properties were miswritten, we have _isFastBoot and _isFastboot currently somewhere i believe.

@@ -311,7 +302,8 @@ export default function(options) {
);
});

it('clears cached expiration times when setting expiration to null', function() {
// eslint-disable-next-line require-await
it('clears cached expiration times when setting expiration to null', async function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this async in fact necessary?

Copy link
Collaborator Author

@BobrImperator BobrImperator Jul 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, otherwise the mocha test is failing, it's throwing some error saying done() wasn't called after the test finished and it's failing because of that. The test works correctly, the expectations inside in fact are working, though for some reason I wasn't able to debunk why the test has to be async.

It started happening after I've used setupTest, so I believe that it's the cause.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds like we're working around something – this test itself has nothing asynchronous. We should look into this a bit closer – happy to pair on it next week.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK 👍
I was able to remove it from one of the places, I figured that it was caused by beforeEach calling an async method on store.
Though can't find the cause for the other one so far, although I wouldn't be surprised if it actually be caused by mocha setupTest which expects the hook callback to be thenable https://github.com/emberjs/ember-mocha/blob/master/addon-test-support/ember-mocha/setup-test.js#L21

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, let's figure out how we can fix this one as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, for now I've removed the eslint disable line, so it fails PR until it's fixed.

});

it('restores expiration time from cookie', function() {
// eslint-disable-next-line require-await
it('restores expiration time from cookie', async function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this async necessary? There's nothing asynchronous going on in the test?

localStorage.setProperties(options);

store = localStorage;
this.localStorage = localStorage;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used anywhere?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I was under the impression that we want to expose the services as properties as well.

I'll remove that next time.

store = this._createStore(Cookie, options);
this.set('cookieExpirationTime', store.get('cookieExpirationTime'));
store = cookieStorage;
this.cookieStorage = cookieStorage;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used anywhere?

@@ -311,7 +302,8 @@ export default function(options) {
);
});

it('clears cached expiration times when setting expiration to null', function() {
// eslint-disable-next-line require-await
it('clears cached expiration times when setting expiration to null', async function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, let's figure out how we can fix this one as well.

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

Successfully merging this pull request may close these issues.

AdaptiveStore should use DI instead of initializing other stores by itself
2 participants