devour-js/devour-client

[question] Use cache across all models/relationships.

aars opened this issue · 3 comments

aars commented

Currently the cache is cleared after processing each resource in the deserializer. This results in duplicate objects of the same type and id, which should actually be the same object, right?

For example:

group = jsonApi.define('Group', {
  members: { jsonApi: 'hasMany', type: 'people' },
  admin: { jsonApi: 'hasOne', type: 'people' },
});

Admin will be part of the members as well, but the actual JS objects group.admin and group.member[idxForAdmin] will not be the same object. This makes it impossible to use devour's models as reliable access to your data structure, since you're accessing different objects, depending on the models you access it through.

I can't seem to figure out the business logic behind where cache.clear() is called. If I simply comment out that line, I have a full working cache across all my models and instances. Of course there is now no way to clear this cache, and no logic for when this should happen.

Before I translate this question/problem into a proposed solution/PR I wanted to ask: Is there anything I am overlooking in the cache implementation?

Cheers!

Howdy,

Thanks for pointing this out. I found the original issue that resulted in the addition of the cache.clear() – #47

I'm a little out of the loop with this stuff, but maybe read through this PR and see if you can figure out it was added. If we don't need it anymore, or you have an alternative solution to the initial issue I'd be happy to merge 👍

aars commented

I'll read up on how/why it was added and see if I can suggest an improvement.

For now, I replaced the res-deserialize middleware with a copy of the original, using a copy of the original _deserialize without the cache cleared, which works for our current setup, but will probably break once the server starts pushing/updating changes to models.

Thanks for getting back to me.

I have a similar issue. Because the cache is cleared I get a RangeError: Maximum call stack size exceeded error. By removing the cache.clear() it works fine,
@aars or @Emerson did someone already change this?