Skip to content

Commit

Permalink
v1.0.2
Browse files Browse the repository at this point in the history
Better TypeScript types for `getSignal()` and `SignalOptions`.
  • Loading branch information
e-oz committed Nov 15, 2023
1 parent d0cb632 commit 3437fb3
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### v1.0.2
Better TypeScript types for `getSignal()` and `SignalOptions`.

### v1.0.1
Method `getSignal()` now accepts `SignalOptions` as the second argument. This allows you to set the initial value and the equality check function.

Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-reactive-storage/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-reactive-storage",
"version": "1.0.1",
"version": "1.0.2",
"license": "MIT",
"private": false,
"author": {
Expand Down
4 changes: 2 additions & 2 deletions projects/ngx-reactive-storage/src/lib/idb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ export class RxStorage implements ReactiveStorage {
return obs;
}

getSignal<T>(key: string, options?: SignalOptions): Signal<T | undefined> {
const s = this.observer.getSignal<T>(key, options?.initialValue, options?.equal);
getSignal<T, N = undefined>(key: string, options?: SignalOptions<T, N>): Signal<T | N> {
const s = this.observer.getSignal<T, N>(key, options?.initialValue, options?.equal);
this.get(key).catch();
return s;
}
Expand Down
4 changes: 2 additions & 2 deletions projects/ngx-reactive-storage/src/lib/local-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class RxLocalStorage implements ReactiveStorage {
return this.observer.getObservable<T>(key, value);
}

public getSignal<T>(key: string, options?: SignalOptions): Signal<T | undefined> {
public getSignal<T, N = undefined>(key: string, options?: SignalOptions<T, N>): Signal<T | N> {
const str = localStorage.getItem(this.prefixed(key));
let value: T | undefined;
if (str !== null) {
Expand All @@ -57,7 +57,7 @@ export class RxLocalStorage implements ReactiveStorage {
}
}
this.startListening(key);
return this.observer.getSignal<T>(key, value ?? options?.initialValue, options?.equal);
return this.observer.getSignal<T, N>(key, value ?? options?.initialValue, options?.equal);
}

public get<T = string>(key: string): Promise<T | null | undefined> {
Expand Down
6 changes: 3 additions & 3 deletions projects/ngx-reactive-storage/src/lib/observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ export class Observer {
* The key becomes "observed" and future modifications will be
* written to the returned signal.
*/
public getSignal<T>(key: string, initialValue: unknown, equal?: ValueEqualityFn<T | undefined>): Signal<T | undefined> {
public getSignal<T, N = undefined>(key: string, initialValue: T | N | undefined, equal?: ValueEqualityFn<T | N | undefined>): Signal<T | N> {
let s = this.signals.get(key);
if (!s) {
s = signal((initialValue as T) ?? undefined, { equal });
s = signal<T | N | undefined>(initialValue, { equal });
this.signals.set(key, s);
}
s.set(initialValue ?? undefined);
return s.asReadonly() as Signal<T | undefined>;
return s.asReadonly() as Signal<T | N>;
}

public dispose() {
Expand Down
8 changes: 4 additions & 4 deletions projects/ngx-reactive-storage/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export type ReactiveStorage = {
*
* If localStorage is being used as the storage, the value will be pushed synchronously.
*/
getSignal<T>(key: string, options?: SignalOptions): Signal<T | undefined>;
getSignal<T, N = undefined>(key: string, options?: SignalOptions<T, N>): Signal<T | N>;

/**
* Set a key-value pair
Expand Down Expand Up @@ -51,7 +51,7 @@ export type ReactiveStorage = {
dispose(): void;
}

export type SignalOptions<T = unknown> = {
initialValue?: unknown;
equal: ValueEqualityFn<T>;
export type SignalOptions<T = unknown, N = T> = {
initialValue?: N;
equal?: ValueEqualityFn<T | N | undefined>;
};

0 comments on commit 3437fb3

Please sign in to comment.