Skip to content

Commit 0c67b9e

Browse files
authored
feat: remove xSubscribe() methods (#96)
1 parent 33006b5 commit 0c67b9e

File tree

2 files changed

+60
-92
lines changed

2 files changed

+60
-92
lines changed

docs/MIGRATION_TO_V8.md

Lines changed: 60 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,7 @@ npm install @ngx-pwa/local-storage@next
2929
2. Start your project: problems will be seen at compilation.
3030
Or you could search for `getItem` as most breaking changes are about its options.
3131

32-
## The bad part: breaking changes
33-
34-
**The following changes may require action from you**.
35-
36-
The main change is to add `type` to all your JSON schemas.
37-
38-
### New `indexedDB` store
32+
## New `indexedDB` store
3933

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

45+
## The bad part: breaking changes
46+
47+
**The following changes may require action from you**.
48+
49+
The main change is to add `type` to all your JSON schemas.
50+
5151
### Validation of arrays
5252

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

127127
Also, `JSONSchemaEnum` interface is removed.
128128

129+
### `JSONSchemaNull` removed
130+
131+
`JSONSchemaNull` is removed (it was useless, as if the data is `null`, validation doesn't occur).
132+
129133
### Additional properties in schemas
130134

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

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

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

142-
### Prefix and collision
143-
144-
If you were using a prefix because you have multiple apps on the same subdomain,
145-
configuration has changed to allow interoperability.
146-
The old one still works for now but is deprecated and will be removed in v9.
147-
148-
Before v8:
149-
```typescript
150-
import { localStorageProviders, LOCAL_STORAGE_PREFIX } from '@ngx-pwa/local-storage';
151-
152-
@NgModule({
153-
providers: [
154-
localStorageProviders({ prefix: 'myapp' }),
155-
// or
156-
{ provide: LOCAL_STORAGE_PREFIX, useValue: 'myapp' },
157-
]
158-
})
159-
export class AppModule {}
160-
```
161-
162-
Since v8:
163-
```typescript
164-
import { localStorageProviders } from '@ngx-pwa/local-storage';
147+
### `xSubscribe()` methods removed
165148

166-
@NgModule({
167-
providers: [
168-
localStorageProviders({
169-
LSPrefix: 'myapp_', // Note the underscore
170-
IDBDBName: 'myapp_ngStorage',
171-
}),
172-
]
173-
})
174-
export class AppModule {}
175-
```
149+
Auto-subscription methods were added for simplicity, but they were risky shortcuts because:
150+
- potential errors are not managed,
151+
- it can cause concurrency issues, especially given the average RxJS knowledge.
176152

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

179155
## The good part: simplication changes
180156

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

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

201+
## Deprecations
202+
203+
Version 8 is a fresh new start, where everything has been cleaned. But as the previous changes already require quite some work,
204+
**the following features still work in v8 but are deprecated**. They will be removed in v9.
205+
So there's no hurry, but as soon as you have some time, do the following changes too.
206+
225207
### Use the generic `JSONSchema`
226208

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

231213
`JSONSchemaNumeric` still works but is deprecated in favor of `JSONSchemaNumber` or `JSONSchemaInteger`
232-
(but again, just use `JSONSchema`). Will be removed in v9.
214+
(but again, just use `JSONSchema`).
215+
216+
### Prefix and collision
217+
218+
If you were using a prefix because you have multiple apps on the same subdomain,
219+
configuration has changed to allow interoperability.
220+
221+
Before v8:
222+
```typescript
223+
import { localStorageProviders, LOCAL_STORAGE_PREFIX } from '@ngx-pwa/local-storage';
224+
225+
@NgModule({
226+
providers: [
227+
localStorageProviders({ prefix: 'myapp' }),
228+
// or
229+
{ provide: LOCAL_STORAGE_PREFIX, useValue: 'myapp' },
230+
]
231+
})
232+
export class AppModule {}
233+
```
233234

234-
Also, `JSONSchemaNull` is removed (it was useless, as if the data is `null`, validation doesn't occur).
235+
Since v8:
236+
```typescript
237+
import { localStorageProviders } from '@ngx-pwa/local-storage';
238+
239+
@NgModule({
240+
providers: [
241+
localStorageProviders({
242+
LSPrefix: 'myapp_', // Note the underscore
243+
IDBDBName: 'myapp_ngStorage',
244+
}),
245+
]
246+
})
247+
export class AppModule {}
248+
```
249+
250+
**Be very careful while changing this, as an error could mean the loss of all previously stored data.**
235251

236252
## More documentation
237253

projects/ngx-pwa/local-storage/src/lib/lib.service.ts

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -182,54 +182,6 @@ export class LocalStorage {
182182

183183
}
184184

185-
/**
186-
* Set an item in storage, and auto-subscribe
187-
* @param key The item's key
188-
* @param data The item's value
189-
* **WARNING: should be avoided in most cases, use this method only if these conditions are fulfilled:**
190-
* - you don't need to manage the error callback (errors will silently fail),
191-
* - you don't need to wait the operation to finish before the next one (remember, it's asynchronous).
192-
*/
193-
setItemSubscribe(key: string, data: string | number | boolean | object): void {
194-
195-
this.setItem(key, data).subscribe({
196-
next: () => {},
197-
error: () => {},
198-
});
199-
200-
}
201-
202-
/**
203-
* Delete an item in storage, and auto-subscribe
204-
* @param key The item's key
205-
* **WARNING: should be avoided in most cases, use this method only if these conditions are fulfilled:**
206-
* - you don't need to manage the error callback (errors will silently fail),
207-
* - you don't need to wait the operation to finish before the next one (remember, it's asynchronous).
208-
*/
209-
removeItemSubscribe(key: string): void {
210-
211-
this.removeItem(key).subscribe({
212-
next: () => {},
213-
error: () => {},
214-
});
215-
216-
}
217-
218-
/**
219-
* Delete all items in storage, and auto-subscribe
220-
* **WARNING: should be avoided in most cases, use this method only if these conditions are fulfilled:**
221-
* - you don't need to manage the error callback (errors will silently fail),
222-
* - you don't need to wait the operation to finish before the next one (remember, it's asynchronous).
223-
*/
224-
clearSubscribe(): void {
225-
226-
this.clear().subscribe({
227-
next: () => {},
228-
error: () => {},
229-
});
230-
231-
}
232-
233185
/**
234186
* RxJS operator to catch if `indexedDB` is broken
235187
* @param operationCallback Callback with the operation to redo

0 commit comments

Comments
 (0)