-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from providerx/firebase-hosting
Fixed api and hosted to firebase
- Loading branch information
Showing
15 changed files
with
420 additions
and
332 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,53 @@ | ||
## React-ProviderX | ||
|
||
A React state management library built on top of RxJS and Observables | ||
|
||
## Usage: | ||
|
||
The library will cache data within your ObservableProvider, so you can grab values from a provider with the `useProvider` | ||
hook - without re-fetching data. | ||
|
||
```tsx | ||
import React from 'react' | ||
import { useProvider, ObservableProvider, refresh } from 'react-providerx' | ||
import { from } from 'rxjs' | ||
import { tap } from 'rxjs/operators' | ||
|
||
export const userResponseProvider$ = ObservableProvider.autoDispose((ref) => { | ||
const fetchErrorApi = async () => { | ||
const response = await fetch('https://jsonplaceholder.typicode.com/users/1') | ||
const json = await response.json() | ||
return json | ||
} | ||
|
||
return from(fetchErrorApi()).pipe( | ||
catchError((error: Error) => { | ||
console.log('there was an error in fetching the api') | ||
ref.maintainState = false | ||
return throwError(error) | ||
}), | ||
) | ||
}) | ||
const fetchErrorApi = async () => { | ||
const response = await fetch('https://jsonplaceholder.typicode.com/users/1') | ||
const json = await response.json() | ||
return json | ||
} | ||
|
||
return from(fetchErrorApi()).pipe( | ||
catchError((error: any) => { | ||
ref.maintainState = false | ||
return ref.error(error) | ||
}) | ||
) | ||
}) | ||
|
||
const Component: React.FC = () => { | ||
const { isLoading, data, error } = useProvider(userResponseProvider$) | ||
if(isLoading) { | ||
return ( | ||
<div> | ||
Waiting For Data... | ||
</div> | ||
) | ||
} | ||
return ( | ||
<div> | ||
{data} | ||
<button onClick={() => refresh(userResponseProvider$)}>Click to refresh</button> | ||
</div> | ||
) | ||
const { isLoading, data, error } = useProvider(userResponseProvider$) | ||
if (isLoading) { | ||
return <div>Waiting For Data...</div> | ||
} | ||
return ( | ||
<div> | ||
{data} | ||
<button onClick={() => refresh(userResponseProvider$)}> | ||
Click to refresh | ||
</button> | ||
</div> | ||
) | ||
} | ||
``` | ||
|
||
## Supporters | ||
[![Stargazers repo roster for @DudeBro249/providerx](https://reporoster.com/stars/DudeBro249/providerx)](https://github.com/DudeBro249/providerx/stargazers) | ||
|
||
[![Stargazers repo roster for @DudeBro249/providerx](https://reporoster.com/stars/DudeBro249/providerx)](https://github.com/providerx/providerx-js/stargazers) | ||
|
||
## License | ||
|
||
[Apache License 2.0](https://choosealicense.com/licenses/apache-2.0/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
import { throwError } from "rxjs" | ||
import { throwError } from 'rxjs' | ||
|
||
export class ProviderReference { | ||
executionError?: any | ||
error(error: any) { | ||
this.executionError = error | ||
return throwError(error) | ||
} | ||
executionError?: any | ||
|
||
error(error: any) { | ||
this.executionError = error | ||
return throwError(error) | ||
} | ||
} | ||
|
||
export class AutoDisposeProviderReference extends ProviderReference { | ||
maintainState: boolean | ||
constructor(maintainState?: boolean) { | ||
super() | ||
this.maintainState = maintainState ?? true | ||
} | ||
maintainState: boolean | ||
constructor(maintainState?: boolean) { | ||
super() | ||
this.maintainState = maintainState ?? true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,67 @@ | ||
import { BehaviorSubject, from, Observable, Subscription } from "rxjs"; | ||
import { AutoDisposeProviderReference } from "../models/providerReference"; | ||
import { BaseObservableProvider } from "./base"; | ||
import { BehaviorSubject, from, Observable, Subscription } from 'rxjs' | ||
import { AutoDisposeProviderReference } from '../models/providerReference' | ||
import { BaseObservableProvider } from './base' | ||
|
||
export class AutoDisposeObservableProvider<T> extends BaseObservableProvider<T> { | ||
_valueSubject$: BehaviorSubject<T> | BehaviorSubject<undefined> | ||
_errorSubject$: BehaviorSubject<T> | BehaviorSubject<undefined> | ||
observableCreator | ||
ref: AutoDisposeProviderReference | ||
_observable$: Observable<T> | ||
_internalSubscription?: Subscription | ||
export class AutoDisposeObservableProvider< | ||
T | ||
> extends BaseObservableProvider<T> { | ||
observableCreator | ||
ref: AutoDisposeProviderReference | ||
_observable$: Observable<T> | ||
_internalSubscription?: Subscription | ||
|
||
constructor(observableCreator: (ref: AutoDisposeProviderReference) => Observable<T>) { | ||
super(observableCreator) | ||
this.observableCreator = observableCreator | ||
this.ref = new AutoDisposeProviderReference(false) | ||
this._observable$ = new Observable() | ||
this._valueSubject$ = new BehaviorSubject(undefined) | ||
this._errorSubject$ = new BehaviorSubject(undefined) | ||
constructor( | ||
observableCreator: (ref: AutoDisposeProviderReference) => Observable<T> | ||
) { | ||
super(observableCreator) | ||
this.observableCreator = observableCreator | ||
this.ref = new AutoDisposeProviderReference(false) | ||
this._observable$ = new Observable() | ||
} | ||
|
||
static fromPromise<S>( | ||
promise: () => Promise<S> | ||
): AutoDisposeObservableProvider<S> { | ||
return new AutoDisposeObservableProvider<S>(() => from(promise()) as any) | ||
} | ||
|
||
_reset() { | ||
if (this._internalSubscription !== undefined) { | ||
this._internalSubscription.unsubscribe() | ||
} | ||
|
||
static fromPromise<S>(promise: () => Promise<S>): AutoDisposeObservableProvider<S> { | ||
return new AutoDisposeObservableProvider<S>(() => (from(promise()) as any)) | ||
this._valueSubject$ = new BehaviorSubject<T | undefined>(undefined) | ||
this._errorSubject$ = new BehaviorSubject<T | undefined>(undefined) | ||
} | ||
|
||
_compute() { | ||
this.ref = new AutoDisposeProviderReference(false) | ||
this._reset() | ||
this._observable$ = this.observableCreator(this.ref) | ||
if (this._observable$ === null) { | ||
throw 'observableCreator cannot return null. It must return an instance of Observable' | ||
} | ||
|
||
_compute() { | ||
this.ref = new AutoDisposeProviderReference(false) | ||
this._observable$ = this.observableCreator(this.ref) | ||
if(this._observable$ === null) { | ||
throw 'observableCreator cannot return null. It must return an instance of Observable' | ||
this._internalSubscription = this._observable$.subscribe( | ||
(val: T) => { | ||
if (this.ref.executionError !== undefined) { | ||
this._advanceError(this.ref.executionError) | ||
return | ||
} | ||
this._internalSubscription = this._observable$.subscribe( | ||
(val: T) => { | ||
if(this.ref.executionError !== undefined) { | ||
this._advanceError(this.ref.executionError) | ||
return | ||
} | ||
this._advanceValue(val) | ||
}, | ||
(error: any) => { | ||
this._advanceError(error) | ||
} | ||
) | ||
} | ||
this._advanceValue(val) | ||
}, | ||
(error: any) => { | ||
this._advanceError(error) | ||
} | ||
) | ||
} | ||
|
||
_reset() { | ||
if(this._internalSubscription !== undefined) { | ||
this._internalSubscription.unsubscribe() | ||
} | ||
this._valueSubject$ = new BehaviorSubject(undefined) | ||
this._errorSubject$ = new BehaviorSubject(undefined) | ||
registerUnsubscribe() { | ||
if (this.ref.maintainState === true) { | ||
return | ||
} | ||
|
||
registerUnsubscribe() { | ||
if(this.ref.maintainState === true) { | ||
return | ||
} | ||
const valueObservers = this._valueSubject$.observers | ||
const errorObservers = this._errorSubject$.observers | ||
if(valueObservers.length < 1 && errorObservers.length < 1) { | ||
this._reset() | ||
} | ||
const valueObservers = this._valueSubject$.observers | ||
const errorObservers = this._errorSubject$.observers | ||
if (valueObservers.length < 1 && errorObservers.length < 1) { | ||
this._reset() | ||
} | ||
} | ||
} |
Oops, something went wrong.