Skip to content

Commit

Permalink
Merge pull request #2 from DudeBro249/docs
Browse files Browse the repository at this point in the history
Cleaned up documentation - finished most concepts
  • Loading branch information
srinivasayush authored Mar 31, 2021
2 parents 4e32e14 + c30e141 commit 99ccc7d
Show file tree
Hide file tree
Showing 25 changed files with 38,973 additions and 21,318 deletions.
26 changes: 14 additions & 12 deletions react-providerx/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions react-providerx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
],
"author": "DudeBro249",
"license": "Apache-2.0",
"dependencies": {
"rxjs": "^6.6.6"
},
"devDependencies": {
"@types/node": "^14.14.37",
"@types/react": "^17.0.3"
"@types/react": "^17.0.3",
"rxjs": "^6.6.7"
}
}
2 changes: 1 addition & 1 deletion react-providerx/src/hooks/useProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const useProvider = <T>(provider: BaseObservableProvider<T>) => {
}, [provider])

return {
isLoading: (currentValue === null || currentValue === undefined) && (currentError === null || currentError === undefined),
isLoading: currentValue === undefined && currentError === undefined,
data: currentValue,
error: currentError,
} as UseProviderValues<T>
Expand Down
12 changes: 6 additions & 6 deletions react-providerx/src/observableProvider/autoDispose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { ProviderReference } from "../models/providerReference";
import { BaseObservableProvider } from "./base";

export class AutoDisposeObservableProvider<T> extends BaseObservableProvider<T> {
_valueSubject$: BehaviorSubject<T> | BehaviorSubject<null>
_errorSubject$: BehaviorSubject<T> | BehaviorSubject<null>
_valueSubject$: BehaviorSubject<T> | BehaviorSubject<undefined>
_errorSubject$: BehaviorSubject<T> | BehaviorSubject<undefined>
observableCreator
ref: ProviderReference
_observable$: Observable<T>
Expand All @@ -17,8 +17,8 @@ export class AutoDisposeObservableProvider<T> extends BaseObservableProvider<T>
maintainState: true
}
this._observable$ = new Observable()
this._valueSubject$ = new BehaviorSubject(null)
this._errorSubject$ = new BehaviorSubject(null)
this._valueSubject$ = new BehaviorSubject(undefined)
this._errorSubject$ = new BehaviorSubject(undefined)
}

static fromPromise<S>(promise: () => Promise<S>): AutoDisposeObservableProvider<S> {
Expand Down Expand Up @@ -50,8 +50,8 @@ export class AutoDisposeObservableProvider<T> extends BaseObservableProvider<T>
if(this._internalSubscription !== undefined) {
this._internalSubscription.unsubscribe()
}
this._valueSubject$ = new BehaviorSubject(null)
this._errorSubject$ = new BehaviorSubject(null)
this._valueSubject$ = new BehaviorSubject(undefined)
this._errorSubject$ = new BehaviorSubject(undefined)
}

registerUnsubscribe() {
Expand Down
16 changes: 10 additions & 6 deletions react-providerx/src/observableProvider/base.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import { BehaviorSubject, Observable, Subscription } from 'rxjs'

export abstract class BaseObservableProvider<T> {
_valueSubject$: BehaviorSubject<T> | BehaviorSubject<null>
_errorSubject$: BehaviorSubject<T> | BehaviorSubject<null>
_valueSubject$: BehaviorSubject<T> | BehaviorSubject<undefined>
_errorSubject$: BehaviorSubject<T> | BehaviorSubject<undefined>
observableCreator
_observable$: Observable<T>
_internalSubscription?: Subscription

constructor(observableCreator: any) {
this.observableCreator = observableCreator
this._observable$ = new Observable()
this._valueSubject$ = new BehaviorSubject(null)
this._errorSubject$ = new BehaviorSubject(null)
this._valueSubject$ = new BehaviorSubject(undefined)
this._errorSubject$ = new BehaviorSubject(undefined)
}

public get valueObservable() {
public get observable() {
if(this._internalSubscription === undefined) {
this._compute()
}
return this._valueSubject$.asObservable() as Observable<T>
}

Expand All @@ -23,7 +27,7 @@ export abstract class BaseObservableProvider<T> {

subscribe(dataCallback: (value: T) => void, errorCallback: (error: any) => void): Subscription[] {
this._compute()
return [this.valueObservable.subscribe(dataCallback), this.errorObservable.subscribe(errorCallback)]
return [this.observable.subscribe(dataCallback), this.errorObservable.subscribe(errorCallback)]
}

abstract _compute(): void
Expand Down
17 changes: 7 additions & 10 deletions react-providerx/src/observableProvider/index.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
import { BehaviorSubject, from, Observable } from 'rxjs'
import { BehaviorSubject, Observable, Subscription } from 'rxjs'
import { ProviderReference } from '../models/providerReference'
import { AutoDisposeObservableProvider } from './autoDispose'
import { BaseObservableProvider } from './base'


export class ObservableProvider<T> extends BaseObservableProvider<T> {
_valueSubject$: BehaviorSubject<T> | BehaviorSubject<null>
_errorSubject$: BehaviorSubject<T> | BehaviorSubject<null>
_valueSubject$: BehaviorSubject<T> | BehaviorSubject<undefined>
_errorSubject$: BehaviorSubject<T> | BehaviorSubject<undefined>
observableCreator
_observable$: Observable<T>
_internalSubscription?: Subscription

constructor(observableCreator: () => Observable<T>) {
super(observableCreator)
this.observableCreator = observableCreator
this._observable$ = new Observable()
this._valueSubject$ = new BehaviorSubject(null)
this._errorSubject$ = new BehaviorSubject(null)
this._valueSubject$ = new BehaviorSubject(undefined)
this._errorSubject$ = new BehaviorSubject(undefined)
}

static autoDispose<S>(observableCreator: (ref: ProviderReference) => Observable<S>) {
return new AutoDisposeObservableProvider<S>(observableCreator);
}

static fromPromise<S>(promise: () => Promise<S>): ObservableProvider<S> {
return new ObservableProvider<S>(() => (from(promise()) as any))
}

_compute() {
this._observable$ = this.observableCreator()
if(this._observable$ === null) {
throw 'observableCreator cannot return null. It must return an instance of Observable'
}
this._observable$.subscribe(
this._internalSubscription = this._observable$.subscribe(
(val: T) => {
this._valueSubject$.next(val as any)
this._errorSubject$.next(null as any)
Expand Down
Loading

0 comments on commit 99ccc7d

Please sign in to comment.