Skip to content

Commit

Permalink
work work
Browse files Browse the repository at this point in the history
  • Loading branch information
dsl400 committed Nov 30, 2023
1 parent 170d8bc commit e1fce83
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 231 deletions.
62 changes: 62 additions & 0 deletions samples/drivers/Files.RxHub.Driver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { from, of, switchMap } from "rxjs";
import { RxHubRequest, RxHubSet } from "../../src";
import { RxHubDriver } from "../../src/RxHub.Driver";
import { promises as fs } from 'fs'




export class FileSystemRxHubDriver extends RxHubDriver {

private base = 'Test'


private streams = {
'Documents.Test.get': import('../streams/Documents.Test.get'),
'Documents.Test.set': import('../streams/Documents.Test.set'),
'Documents.Test.list': import('../streams/Documents.Test.list'),
'Documents.Test.update': import('../streams/Documents.Test.update'),
}

docId(){
return '';
}

get(request: RxHubRequest) {
return from(fs.readFile(request.path))
}

set(request: RxHubRequest) {
return of().pipe(request.stream,switchMap((x:RxHubSet) => from(fs.writeFile(request.path,x.set))))
}


update(request: RxHubRequest) {
return of().pipe(request.stream,switchMap((x:RxHubSet) => from(fs.writeFile(request.path,x.set))))
}


list(request: RxHubRequest) {


}



arrayUnion(element: any) {
// return arrayUnion(element)
}

arrayRemove(element: any) {
// return arrayRemove(element)
}









}
83 changes: 23 additions & 60 deletions samples/drivers/FirestoreClient.RxHub.Driver.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,59 @@
import { Subject, from, map, of, switchMap } from 'rxjs'
import { RxHubGet, RxHubRequest, RxHubSet, RxHubUpdate } from '../../src'
import { RxHubRequest, RxHubSet, RxHubUpdate } from '../../src'
import { RxHubDriver } from '../../src/RxHub.Driver'
import { serverTimestamp, arrayUnion, arrayRemove, doc, Firestore, getDoc, onSnapshot, DocumentSnapshot, setDoc, updateDoc } from 'firebase/firestore'
import { serverTimestamp, arrayUnion, arrayRemove, doc, Firestore, onSnapshot, DocumentSnapshot, setDoc, updateDoc, collection } from 'firebase/firestore'
// import { RxHubTransfer } from '../../src/types'


export class FirestoreClientRxHubDriver extends RxHubDriver {

private appNode = 'Test'
private base = 'Test'

private streams = {
'Documents.Test.get': import('../streams/Documents.Test.get'),
'Documents.Test.set': import('../streams/Documents.Test.set'),
'Documents.Test.list': import('../streams/Documents.Test.list'),
'Documents.Test.update': import('../streams/Documents.Test.update'),
streams = {
// 'Documents.Test.get': import('../streams/Documents.Test.get'),
// 'Documents.Test.set': import('../streams/Documents.Test.set'),
// 'Documents.Test.list': import('../streams/Documents.Test.list'),
// 'Documents.Test.update': import('../streams/Documents.Test.update'),
}

constructor(private firestore: Firestore) {
super()
}



docId() {
return ''
return doc(collection(this.firestore,'')).id
}


get(request: RxHubRequest) {
const docRef = doc(this.firestore, `${this.appNode}/${request.ref}`)
const subject = new Subject<any>()
onSnapshot(docRef,subject)
return subject.pipe(map((x:DocumentSnapshot)=>x.data()),request.stream)

// return docRef.valueChanges()
const docRef = doc(this.firestore, `${this.base}/${request.ref}`)
const subject = new Subject<any>();
onSnapshot(docRef,subject);
// console.log(request.stream)
return subject.pipe(map((x:DocumentSnapshot)=>x.data()))
.pipe(request.stream)
}



set(request: RxHubRequest) {
const docRef = doc(this.firestore, `${this.appNode}/${request.ref}`)
const docRef = doc(this.firestore, `${this.base}/${request.ref}`)
return of(request.set).pipe(
request.stream,
switchMap((x:RxHubSet)=>from(setDoc(docRef,x)))
switchMap((x:RxHubSet)=>from(setDoc(docRef,x.set)))
)
}

update(request: RxHubRequest) {
const docRef = doc(this.firestore, `${this.appNode}/${request.ref}`)
const docRef = doc(this.firestore, `${this.base}/${request.ref}`)
return of(request.set).pipe(
request.stream,
switchMap((x:RxHubSet)=>from(updateDoc(docRef,x)))
switchMap((x:RxHubUpdate)=>from(updateDoc(docRef,x.update)))
)
}

list() {
list(request: RxHubRequest) {

}

Expand All @@ -74,45 +75,7 @@ export class FirestoreClientRxHubDriver extends RxHubDriver {
}


// /**
// *
// * @param t
// * @param driver
// * @returns
// */
// private setDocAttributes(t: any, driver = this.defaultDriver) {
// if (!t.user) return;
// const now = this.timeStamp();
// if (t.set._attr) {
// t.set._attr.edited = now;
// t.set._attr.editor = t.user.name;
// t.set._attr.editorId = t.user.user_id;
// t.set._attr.uv = this.appVersion;
// t.set._attr.prev = this.docId(driver);
// t.set._attr.updated = this.serverTimestamp(driver);
// return;
// }

// t.set._attr = this.newDocAttributes(t, t.docId);
// }


// /**
// *
// * @param t
// * @param driver
// * @returns
// */
// private updateDocAttributes(t: any, driver = this.defaultDriver) {
// if (!this.user) return;
// t.update['_attr.edited'] = this.timeStamp();
// t.update['_attr.editor'] = this.user.name;
// t.update['_attr.editorId'] = this.user.user_id;
// t.update['_attr.uv'] = this.appVersion;
// t.update['_attr.prev'] = this.docId(driver);
// t.update['_attr.updated'] = this.serverTimestamp(driver);
// return;
// }




Expand Down
6 changes: 6 additions & 0 deletions src/RxHub.Auth.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@


export abstract class RxHubAuth{

private _user

constructor(){}

get user(){
return {...this._user || {}}
}

abstract logIn(email:string, password: string)

abstract logOut()
Expand Down
128 changes: 67 additions & 61 deletions src/RxHub.Driver.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,73 @@
import { RxHubGet, RxHubSet, RxHubTransfer, RxHubUpdate } from ".";
import { Observable, pipe } from "rxjs";
import { RxHubGet, RxHubRequest, RxHubSet, RxHubTransfer, RxHubUpdate, RxHubUser } from ".";
import { Observable, from, map, of, pipe, switchMap, tap } from "rxjs";

export abstract class RxHubDriver {


abstract streams: { [key: string]: Promise<any> }

constructor(options: any = {}) { }

getStream(transfer: RxHubTransfer) {

console.log(transfer)

// const pathSplit = docPath.split('/')
// const moduleName = pathSplit.join('.')

// return (force ? of(1) : this.user$).pipe(switchMap(() => {
// // console.log('reeeeeeeeeeeeeeeeeeeeady')
// let stream: any = null
// let roles = this.user?.claims?.roles || [];
// if (!Array.isArray(roles)) roles = [];
// const paths: string[] = [];
// for (let r of roles) {
// if (docId) paths.push(`${moduleName}.${docId}.${action}.${r}`)
// paths.push(`${moduleName}.${action}.${r}`)
// }
// if (docId) paths.push(`${moduleName}.${docId}.${action}`)
// paths.push(`${moduleName}.${action}`)
// paths.sort((a, b) => b.split('.').length - a.split('.').length)
// for (let p of paths) {
// // console.log(p)
// stream = getModule(p)
// if (stream) break;
// }

// //default set
// if (!stream && action == 'set') {
// console.warn(docPath, docId, action, 'DEFAULTS SET')
// stream = [pipe(map((x: any) => ({
// path: x.req.ref,
// set: x.req.set,
// merge: x.req.merge
// })))]
// }

// //default set
// if (!stream && action == 'update') {
// console.warn(docPath, docId, action, 'DEFAULTS UPDATE')
// stream = [pipe(map((x: any) => ({
// path: x.req.ref,
// update: x.req.update,
// })))]
// }

// //default get || list
// if (!stream) {
// console.warn(docPath, docId, action, 'DEFAULT GET || LIST')
// stream = [pipe(map((x: any) => x.doc || x.data))]
// }

// return from(stream).pipe(map((x: any) => {
// return { user: this.user, stream: x.default ? x.default : x }
// }));
// }))



return pipe
// getStream(user$: Observable<RxHubUser>, transfer: RxHubTransfer):<T>(source: Observable<T>) => Observable<T> {
getStream(user: RxHubUser, r: RxHubRequest): any {

return pipe(map(x=>x))

let roles = user.roles || [];

let stream = null;

const moduleName = r.ref.replace('/', '.')
const paths: string[] = [];
for (let role of roles) {
if (r.docId) paths.push(`${moduleName}.${r.docId}.${r.action}.${role}`)
paths.push(`${moduleName}.${r.action}.${role}`)
}
if (r.docId) paths.push(`${moduleName}.${r.docId}.${r.action}`)
paths.push(`${moduleName}.${r.action}`)
paths.sort((a, b) => b.split('.').length - a.split('.').length)
for (let p of paths) {
// console.log(p)
stream = this.streams[p]
if (stream) {
stream = stream()
break;
}

//default set
if (!stream && r.action == 'set') {
console.warn(r.ref, 'DEFAULTS SET')
stream = [pipe(map((x: any) => ({
path: x.req.ref,
set: x.req.set,
merge: x.req.merge
})))]
}

//default set
if (!stream && r.action == 'update') {
console.warn(r.ref, 'DEFAULTS UPDATE')
stream = [pipe(map((x: any) => ({
path: x.req.ref,
update: x.req.update,
})))]
}

//default get || list
if (!stream) {
console.warn(r.ref, 'DEFAULT GET || LIST')
stream = [pipe(map((x: any) => x.doc || x.data))]
}

}
// return of(pipe(map(x => ({ 111111111: x }))))
return from(stream()).pipe(
tap(x => console.log(1111111111111, x)),
map((x: any) => {
return { stream: x.default ? x.default : x }
}),
map(x => x)
);

}

Expand All @@ -73,6 +77,8 @@ export abstract class RxHubDriver {

abstract update(request: RxHubUpdate)

abstract list(request: RxHubUpdate)

// abstract serverTimestamp(): any

abstract docId(table?: string): string
Expand Down
Loading

0 comments on commit e1fce83

Please sign in to comment.