Skip to content

Commit

Permalink
added testing and finished ObservableProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
srinivasayush committed Mar 27, 2021
1 parent 6407bcc commit c20a1d6
Show file tree
Hide file tree
Showing 11 changed files with 45,847 additions and 10,115 deletions.
149 changes: 53 additions & 96 deletions react-providerx/package-lock.json

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

20 changes: 12 additions & 8 deletions react-providerx/package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
{
"name": "react-providerx",
"version": "1.0.0",
"description": "",
"main": "index.js",
"version": "0.0.1",
"description": "A state management library built for React, using RxJS and Observables",
"main": "lib/index.js",
"types": "lib",
"homepage": "https://github.com/DudeBro249/providerx",
"scripts": {
"build": "tsc -p ."
"build": "npx tsc -p ."
},
"keywords": [],
"author": "",
"license": "ISC",
"author": "DudeBro249",
"license": "Apache-2.0",
"dependencies": {
"rxjs": "^6.6.6",
"rxjs-hooks": "^0.6.2"
"rxjs": "^6.6.6"
},
"devDependencies": {
"@types/react": "^17.0.3"
}
}
37 changes: 22 additions & 15 deletions react-providerx/src/hooks/useProvider.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import { useObservable } from "rxjs-hooks"
import { ObservableProvider } from "../providers/observableProvider"
import { ObservableProvider } from '../providers/observableProvider'
import { useState, useEffect } from 'react'

type UseProviderValues<T> = {
isLoading: boolean
data: T
}

export const useProvider = <T>(provider: ObservableProvider<T>) => {
try {
const value = useObservable(() => provider.behaviorSubject$.asObservable())
return {
isLoading: value === null,
data: value,
error: null
const [currentValue, setCurrentValue] = useState<T | null>(null)

useEffect(() => {
const handleSubscriptionValue = (value: any) => {
setCurrentValue(value)
}
}
catch (error) {
return {
isLoading: false,
data: null,
error: error,
const subscription = (provider.behaviorSubject$.asObservable() as any).subscribe(handleSubscriptionValue)
return () => {
subscription.unsubscribe()
}
}
}, [provider.behaviorSubject$])

return {
isLoading: currentValue === null || currentValue === undefined,
data: currentValue,
error: null
} as UseProviderValues<T>
}
24 changes: 17 additions & 7 deletions react-providerx/src/providers/observableProvider.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import { BehaviorSubject, Observable } from 'rxjs'
import { BehaviorSubject, from, Observable } from 'rxjs'

export class ObservableProvider<T> {
behaviorSubject$: BehaviorSubject<T> | BehaviorSubject<null>
observable$: Observable<T>
constructor(observableCreator: () => Observable<T>) {
this.observable$ = observableCreator()
observableCreator: () => (Observable<T> | null)
observable$?: (Observable<T> | null)
constructor(observableCreator: () => (Observable<T> | null)) {
this.observableCreator = observableCreator
this.behaviorSubject$ = new BehaviorSubject(null)
if(this.observable$ === null) {
throw 'observableCreator cannot return null. It must return an instance of Observable'
}
this._compute()
}

get value() {
return this.behaviorSubject$.value
}

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((val: T) => {
this.behaviorSubject$.next(val as any)
})
Expand Down
10 changes: 0 additions & 10 deletions tests/node_modules/.yarn-integrity

This file was deleted.

Loading

0 comments on commit c20a1d6

Please sign in to comment.