phillipcurl / ngx-warehouse

:package: An offline storage solution for Angular apps https://phillipcurl.github.io/ngx-warehouse
MIT License
10 stars 1 forks source link

first initialization fail #1

Open htrex opened 7 years ago

htrex commented 7 years ago

I'm trying the following sample code in the constructor of an Injectable service in my app (Injectable services don't allow to use onInit):

        let testObject: any = {
            anArray: [
                'first string',
                'another string',
                'and even one more'
            ],
            aNumber: 2,
            anObject: {
                nestedValue: {
                    key: 'Woah this is nested!'
                }
            }
        };

        this.warehouse.set('test', testObject);

        this.warehouse.get('test').subscribe(data => {
            console.log('data',data);  // <-- returns null at first execution
        }
        );

At the absolute first execution, or at every first execution after I delete the 'ngx-warehouse' IndexedDB from the Chrome web browser, the get is returning null data.

phillipcurl commented 7 years ago

Hi @htrex - do you mind sharing what your NgxWarehouseModule import from your root AppModule looks like? If you you're able to point me to a repo or a working plunkr, that will also help me track down what might be going on here. Thanks!

htrex commented 7 years ago

Hi @phillipcurl and thanks for sharing ngx-warehouse, it's cool! Apart this glitch I haven't found any other problem so far. My root appmodule is here https://gist.github.com/htrex/75df09fb7c839b41041aa35974aaef01 but unhopefully I can't share the app as it's for a customer.

I'm using a workaround in the constructor of my injectable "APICommonService" where I'm injecting ngx-warehouse and that does the trick for me

@Injectable()
export class APICommonService {
    public config: any;
    public configFn: any;

// ...

    private _allData:any = {};
    private _allData$:any = {};
    public dataEnum:any = {};

    private currentStorageVersion: string = '19';
    private storageVersion: string = localStorage.getItem('storageVersion');

 // ... 

    constructor( private http: Http,
                 public warehouse: Warehouse,
                 config: AppConfig
    ) {
        this.config = config.getConfig();
        this.configFn = config;

// ...

        this.warehouse.set('apiServiceLastInitTime', Date.now() ); // <-- I'm using it just for log and it does the trick !!!
    }

// ...
phillipcurl commented 7 years ago

@htrex No problem at all - I'm glad you're enjoying using it! Do you mind giving the custom config option a try in your AppModule? As per the docs/README, it should look something like this:

import { NgxWarehouseModule, WarehouseConfig, DRIVER_TYPE } from 'ngx-warehouse';

const config: WarehouseConfig = {
  driver: DRIVER_TYPE.DEFAULT,
  name: 'Your App',
  version: 1.0,
  storeName: 'key_value_pairs', // Should be alphanumeric, with underscores.
  description: 'A description of your app'
};

@NgModule({
  declarations: [...],
  imports: [
    ...
    NgxWarehouseModule.configureWarehouse(config),
    ...
  ],
  bootstrap: [...]
})

I haven't encountered this issue myself, but I'm thinking it might be caused by using the default config option. Using the NgxWarehouseModule.configureWarehouse method won't have a negative effect on performance, so you really shouldn't see any changes on your end.

In short, I think you're attempting to use the Warehouse service before it has been fully initialized. Using the configureWarehouse option should force that initialization and mitigate the issue. If this ends up being the case, I'll address the issue with the default config option to ensure this doesn't happen in the future.

phillipcurl commented 7 years ago

Hi @htrex, I just wanted to check in and see if you're still experiencing this issue. Please let me know if you have any additional details.

htrex commented 7 years ago

@phillipcurl sorry for the delay, I've a few deadlines nearing, should be able to check and report before next week I hope.

htrex commented 7 years ago

Sorry for the delay. I can confirm the issue even when using the suggested custom config on my App root module.

My test code is the following, placed in the constructor of "apiService" an Injectable service.

        this.warehouse.set('apiServiceLastInitTime', Date.now() );

        this.warehouse.get('apiServiceLastInitTime').subscribe(data => {
            console.log('apiServiceLastInitTime', data);  // <-- returns null at first execution
        });

I'm using a Chrome extension to delete the indexeddb database https://chrome.google.com/webstore/detail/clientmyadmin/mpfdmkiipkikcdbjlhgpjblpapkfcbjj?utm_source=chrome-app-launcher-info-dialog

After a database reset I'm always seeing a null value.