reyesoft / ngx-jsonapi

JSON API client library for Angular 5+ 👌 :: Production Ready 🚀
https://ngx-jsonapi.reyesoft.com/
MIT License
101 stars 52 forks source link

Disabling ALL Caching for service objects #174

Closed iamchrismiller closed 4 years ago

iamchrismiller commented 5 years ago

Hi,

This one bit me this morning and wasted some time figuring out what was going on. (I Tried with version 2.0.2 and 2.0.0-rc.4) which is another hard to follow paradigm for this package, not sure what is latest. There are conflicting versions in NPM and the Github release page.

Anyways, Here's my scenario:

 NgxJsonapiModule.forRoot({
    url: '/',
    cache_prerequests: false,
    cachestore_support: false
  })

I have a Service object that is used within an Angular resolver.

I call service.get(<id>) within the resolver which in turn adds the resource to the cachememory object and doesn't respect the jsonapi configuration specifying cachestore_support false. <-- Not sure if cachestore / cachememory are mutually exclusive.

I then update a nested relationship on that resource and call .save() and redirect to another page which uses the same resolver.

That resolver doesn't resolve the newly fetched object from the api but does resolve the cached copy.

    Service.prototype.getOrCreateResource = function (id) {
        /** @type {?} */
        var service = Converter.getService(this.type);
        if (service.cachememory && id in service.cachememory.resources) {
            return /** @type {?} */ (service.cachememory.resources[id]);
        }
        else {
            /** @type {?} */
            var resource = service.new();
            resource.id = id;
            service.cachememory.setResource(resource, false);
            return /** @type {?} */ (resource);
        }
    };

if (service.cachememory && id in service.cachememory.resources) { return (service.cachememory.resources[id]); }

I tried a few different approaches for overriding the cachememory object within the service directly but the register method constructs a new one for us on construct.

    Service.prototype.register = function () {
        if (Core.me === null) {
            throw new Error('Error: you are trying register `' + this.type + '` before inject JsonapiCore somewhere, almost one time.');
        }
        // only when service is registered, not cloned object
        this.cachememory = new CacheMemory();
        this.cachestore = new CacheStore();
        return Core.me.registerService(this);
    };

this.cachememory = new CacheMemory();

I have a hacky fix that seems to work in the resolver but would rather remove this hack and have the library handle caching of resources correctly.

   const { cachememory } = this.learnerClassesService;
   if (cachememory.resources[route.params.classId]) {
     cachememory.removeResource(route.params.classId);
   }

This solves the issue I was experiencing but feels like a round about way to remove caching and could lead to further confusion within the engineering team.

pablorsk commented 4 years ago

Can you check last version >=2.1.74: LocalForage is droped and we work with data-providers.