Skip to content

feat: remove xSubscribe() methods #96

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

Merged
merged 1 commit into from
Mar 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 60 additions & 44 deletions docs/MIGRATION_TO_V8.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,7 @@ npm install @ngx-pwa/local-storage@next
2. Start your project: problems will be seen at compilation.
Or you could search for `getItem` as most breaking changes are about its options.

## The bad part: breaking changes

**The following changes may require action from you**.

The main change is to add `type` to all your JSON schemas.

### New `indexedDB` store
## New `indexedDB` store

To allow interoperability, the internal `indexedDB` storing system has changed.
It is not a breaking change as the lib do it in a backward-compatible way:
Expand All @@ -48,6 +42,12 @@ So it should not concern you, but as it is very sensitive change, we recommend
It's internal stuff, but it also means there is a transition phase where some of the users of your app
will be on the new storage, and others will be on the old one.

## The bad part: breaking changes

**The following changes may require action from you**.

The main change is to add `type` to all your JSON schemas.

### Validation of arrays

**`type` option is now required.**
Expand Down Expand Up @@ -126,55 +126,31 @@ It also means enums of different types are no longer possible (and it's better f

Also, `JSONSchemaEnum` interface is removed.

### `JSONSchemaNull` removed

`JSONSchemaNull` is removed (it was useless, as if the data is `null`, validation doesn't occur).

### Additional properties in schemas

The `schema` option of `getItem()` now only accepts our own `JSONSchema` interface,
which is a subset of the JSON Schema standard.

This lib has always discarded some features of the standard, as it uses the schemas for a specific purpose (validation).
But before v8, extra properties in the schema were accepted even if not supported. It is no longer possible since v8.
But before v8, extra properties in the schema were accepted even if not supported.
It is no longer possible since v8 due to TypeScript limitations.

While not recommended, you can still force it:
```typescript
this.localStorage.getItem('test', schema as any)
```

### Prefix and collision

If you were using a prefix because you have multiple apps on the same subdomain,
configuration has changed to allow interoperability.
The old one still works for now but is deprecated and will be removed in v9.

Before v8:
```typescript
import { localStorageProviders, LOCAL_STORAGE_PREFIX } from '@ngx-pwa/local-storage';

@NgModule({
providers: [
localStorageProviders({ prefix: 'myapp' }),
// or
{ provide: LOCAL_STORAGE_PREFIX, useValue: 'myapp' },
]
})
export class AppModule {}
```

Since v8:
```typescript
import { localStorageProviders } from '@ngx-pwa/local-storage';
### `xSubscribe()` methods removed

@NgModule({
providers: [
localStorageProviders({
LSPrefix: 'myapp_', // Note the underscore
IDBDBName: 'myapp_ngStorage',
}),
]
})
export class AppModule {}
```
Auto-subscription methods were added for simplicity, but they were risky shortcuts because:
- potential errors are not managed,
- it can cause concurrency issues, especially given the average RxJS knowledge.

**Be very careful while changing this, as an error could mean the loss of all previously stored data.**
So `setItemSubscribe()`, `removeItemSubscribe()` and `clearSubscribe()` are removed: subscribe manually.

## The good part: simplication changes

Expand Down Expand Up @@ -222,16 +198,56 @@ this.localStorage.getItem('test', { type: 'string' }).subscribe((data) => {

Cast is still needed for objects. Follow the new [validation guide](./VALIDATION.md).

## Deprecations

Version 8 is a fresh new start, where everything has been cleaned. But as the previous changes already require quite some work,
**the following features still work in v8 but are deprecated**. They will be removed in v9.
So there's no hurry, but as soon as you have some time, do the following changes too.

### Use the generic `JSONSchema`

Now the `JSONSchema` interface has been refactored, just use this one.
IntelliSense will adjust itself based on the `type` option.
The specific interfaces (`JSONSchemaString`, `JSONSchemaBoolean` and so on) are still there but are useless.

`JSONSchemaNumeric` still works but is deprecated in favor of `JSONSchemaNumber` or `JSONSchemaInteger`
(but again, just use `JSONSchema`). Will be removed in v9.
(but again, just use `JSONSchema`).

### Prefix and collision

If you were using a prefix because you have multiple apps on the same subdomain,
configuration has changed to allow interoperability.

Before v8:
```typescript
import { localStorageProviders, LOCAL_STORAGE_PREFIX } from '@ngx-pwa/local-storage';

@NgModule({
providers: [
localStorageProviders({ prefix: 'myapp' }),
// or
{ provide: LOCAL_STORAGE_PREFIX, useValue: 'myapp' },
]
})
export class AppModule {}
```

Also, `JSONSchemaNull` is removed (it was useless, as if the data is `null`, validation doesn't occur).
Since v8:
```typescript
import { localStorageProviders } from '@ngx-pwa/local-storage';

@NgModule({
providers: [
localStorageProviders({
LSPrefix: 'myapp_', // Note the underscore
IDBDBName: 'myapp_ngStorage',
}),
]
})
export class AppModule {}
```

**Be very careful while changing this, as an error could mean the loss of all previously stored data.**

## More documentation

Expand Down
48 changes: 0 additions & 48 deletions projects/ngx-pwa/local-storage/src/lib/lib.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,54 +182,6 @@ export class LocalStorage {

}

/**
* Set an item in storage, and auto-subscribe
* @param key The item's key
* @param data The item's value
* **WARNING: should be avoided in most cases, use this method only if these conditions are fulfilled:**
* - you don't need to manage the error callback (errors will silently fail),
* - you don't need to wait the operation to finish before the next one (remember, it's asynchronous).
*/
setItemSubscribe(key: string, data: string | number | boolean | object): void {

this.setItem(key, data).subscribe({
next: () => {},
error: () => {},
});

}

/**
* Delete an item in storage, and auto-subscribe
* @param key The item's key
* **WARNING: should be avoided in most cases, use this method only if these conditions are fulfilled:**
* - you don't need to manage the error callback (errors will silently fail),
* - you don't need to wait the operation to finish before the next one (remember, it's asynchronous).
*/
removeItemSubscribe(key: string): void {

this.removeItem(key).subscribe({
next: () => {},
error: () => {},
});

}

/**
* Delete all items in storage, and auto-subscribe
* **WARNING: should be avoided in most cases, use this method only if these conditions are fulfilled:**
* - you don't need to manage the error callback (errors will silently fail),
* - you don't need to wait the operation to finish before the next one (remember, it's asynchronous).
*/
clearSubscribe(): void {

this.clear().subscribe({
next: () => {},
error: () => {},
});

}

/**
* RxJS operator to catch if `indexedDB` is broken
* @param operationCallback Callback with the operation to redo
Expand Down