dappsnation / akita-ng-fire

Akita ❤️ Angular 🔥 Firebase
MIT License
131 stars 27 forks source link

Store is not getting Reset with Sync Option #167

Closed randallmeeker closed 3 years ago

randallmeeker commented 3 years ago

I have a guard setup to reset collection on sync, however restet does not appear to be working. The store does not reset when the sync starts and I have all the old records from the previous sync. Is this setup correctly to remove all the current records when starting a new sync?

v. 3.1.6

Store

@Injectable({ providedIn: 'root' })
@StoreConfig({ name: 'site', resettable: true })
export class SiteStore extends EntityStore<SiteState> {
  constructor() {
    super();
  }
}

Service

@Injectable({ providedIn: 'root' })
@CollectionConfig({ path: 'orgs/:orgId/projects/:projectId/sites' })
export class SiteService extends CollectionService<SiteState> {
  constructor(store: SiteStore, protected routerQuery: RouterQuery) {
    super(store);
  }

  sync = syncWithRouter.bind(this, this.routerQuery);
}

Guard

@Injectable({ providedIn: 'root' })
export class SitesGuard extends CollectionGuard<SiteState> {
  constructor(service: SiteService) {
    super(service);
  }

  // Sync and set active
  sync(next: ActivatedRouteSnapshot): Observable<any> {
    return this.service.syncCollection({
      params: {
        orgId: next.params.orgId,
        projectId: next.params.projectId,
      },
      reset: true,
    });
  }
}
randallmeeker commented 3 years ago

As far as I can tell the code does not actually reset the store when reset: true is set, but attempts to update the store with merging an empty array with the current state object???.

https://github.com/dappsnation/akita-ng-fire/blob/d1c33f8362667bd639da81e036b992100651c9c3/projects/akita-ng-fire/src/lib/utils/sync-from-action.ts#L18

Instead I would think that we should be running the akita's resetStore action?

This is based on staring at the code for like 30 minutes so I easily may have misjudged something.

Pinging @Benny739 as they authored the reset code and my offer some insight?

I have updated my guard to reset manually for now.

@Injectable({ providedIn: 'root' })
export class SitesGuard extends CollectionGuard<SiteState> {
  constructor(service: SiteService, private store: SiteStore) {
    super(service);
  }

  // Sync and set active
  sync(next: ActivatedRouteSnapshot): Observable<any> {
    this.store.reset(); // <== HAVE TO ADD THIS HERE
    return this.service.syncCollection({
      params: {
        orgId: next.params.orgId,
        projectId: next.params.projectId,
      },
      reset: true, <=== THIS DOES NOTHING
    });
  }
}
fritzschoff commented 3 years ago

Hey, thanks for reporting. There was a reason why we patched the store with an empty array rather then resetting it. But I can't remember why and obviously it doesn't work. I did a PR which now uses the reset func from akita

fritzschoff commented 3 years ago

https://github.com/dappsnation/akita-ng-fire/pull/168 you can try it out with version v4.0.1 . The critical change for version 4 is that you need to use firebase v8. If that is a problem for you let me know, I can publish a different v3 for you.

randallmeeker commented 3 years ago

I appreciate this. Currently I am stuck on V3 as you suspected.

fritzschoff commented 3 years ago

I pushed version 3.1.8 . Let me know if it worked for you