This commit is contained in:
Peter Maquiran
2024-10-08 13:16:01 +01:00
parent 0dd4fd665b
commit 9c8ecc182f
22 changed files with 121 additions and 60 deletions
+3 -3
View File
@@ -20,7 +20,7 @@ export abstract class IDexieRepository<T, R, I = EntityTable<any, any>> {
abstract find(filter: Partial<T>): Promise<RepositoryResult<R[], T[]>>
abstract findOne(filter: Partial<T>): Promise<RepositoryResult<T & ILocalModel | undefined, T>>
abstract findOne(filter: Partial<T>): Promise<RepositoryResult<T & ILocalModel<T, R, I> | undefined, T>>
abstract findAll(): Promise<RepositoryResult<T[], T>>
@@ -29,7 +29,7 @@ export abstract class IDexieRepository<T, R, I = EntityTable<any, any>> {
abstract clear(): Promise<Result<any, any>>
}
export abstract class ILocalModel{
abstract save()
export abstract class ILocalModel<T, R, I = EntityTable<any, any>>{
abstract save() : Promise<RepositoryResult<number, T>>
abstract delete()
}
@@ -218,9 +218,14 @@ export class DexieRepository<T, R, I = EntityTable<any, any>> implements IDexieR
}
}
async findOne(filter: Partial<T>): Promise<RepositoryResult<T & ILocalModel | undefined, T>> {
async findOne(filter: Partial<T>): Promise<RepositoryResult<T & ILocalModel<T, R, I> | undefined, T>> {
try {
const document = await this.table.where(filter).first();
if(document) {
return ok(Object.assign(new LocalModel(this.table, this, this.db), document));
}
return ok(document);
} catch (_error) {
const error: IDexieError = _error
@@ -232,7 +237,7 @@ export class DexieRepository<T, R, I = EntityTable<any, any>> implements IDexieR
}
}
async findAll(): Promise<RepositoryResult<(T & ILocalModel)[], T>> {
async findAll(): Promise<RepositoryResult<(T & ILocalModel<T, R, I>)[], T>> {
try {
const documents = await this.table.toArray()
return ok(documents);
@@ -270,7 +275,7 @@ export class DexieRepository<T, R, I = EntityTable<any, any>> implements IDexieR
}
}
export class LocalModel<T, R, I = EntityTable<any, any>> implements ILocalModel {
export class LocalModel<T, R, I> implements ILocalModel<T, R, I> {
constructor(
private table: EntityTable<any, any>,
@@ -279,13 +284,15 @@ export class LocalModel<T, R, I = EntityTable<any, any>> implements ILocalModel
) {}
save() {
const primaryKey = this.db[this.table.name].schema.primKey.name
return this.repository.update(primaryKey, this as any)
const primaryKey = this.table.schema.primKey.name
const idValue = this[primaryKey]
return this.repository.update(idValue, this as any)
}
delete() {
const primaryKey = this.db[this.table.name].schema.primKey.name
return this.repository.delete(primaryKey)
const primaryKey = this.table.schema.primKey.name
const idValue = this[primaryKey]
return this.repository.delete(idValue)
}
}
@@ -6,7 +6,7 @@ import { Plugins } from '@capacitor/core';
import { switchMap } from 'rxjs/operators';
import { err, Result } from 'neverthrow';
import { HubConnection } from '@microsoft/signalr';
import { ISignalRInput } from '../type';
import { ISignalRInput, ISignalROutput } from '../type';
const { App } = Plugins;
@@ -16,7 +16,7 @@ const { App } = Plugins;
export class SignalRService {
private connection!: SignalRConnection;
private connectingSubject: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(null);
private sendDataSubject: BehaviorSubject<{method: string, data: any}> = new BehaviorSubject<{method: string, data: any}>(null);
private sendDataSubject: Subject<ISignalROutput> = new Subject<ISignalROutput>();
private deadConnectionBackGround: Subject<any>;
@@ -94,7 +94,7 @@ export class SignalRService {
}
getData<T>() {
return this.sendDataSubject.asObservable() as BehaviorSubject<{method: string, data: T}>
return this.sendDataSubject.asObservable()
}
public getConnectionState(): Observable<boolean> {
+10 -5
View File
@@ -4,12 +4,13 @@ import { ok, Result, err } from 'neverthrow';
import { SessionStore } from 'src/app/store/session.service';
import { filter, first, map } from 'rxjs/operators';
import { v4 as uuidv4 } from 'uuid'
import { ISignalRInput } from '../type';
import { ISignalRInput, ISignalROutput } from '../type';
import { MessageOutPutDataDTO } from 'src/app/core/chat/repository/dto/messageOutputDTO';
import { Subject } from 'rxjs';
export interface SocketMessage<T> {
export interface SocketMessage<T, I = any> {
method: string,
data: T
data: T,
payload: I
}
export enum EnumSocketError {
@@ -26,10 +27,11 @@ export class SignalRConnection {
private sendLaterSubject: BehaviorSubject<Object> = new BehaviorSubject<Object>(false);
private reconnect = true
private sendDataSubject: BehaviorSubject<{method: string, data: any}> = new BehaviorSubject<{method: string, data: any}>(null);
private sendDataSubject: Subject<ISignalROutput> = new Subject<ISignalROutput>();
private pendingRequests: Map<string, { resolve: Function; reject: Function }> = new Map();
url: string
private hasConnectOnce = false
private payload = {}
constructor({url}) {
this.url = url
@@ -125,6 +127,8 @@ export class SignalRConnection {
this.hubConnection.invoke(input.method, input.data)
this.payload[input.data.requestId] = input.data
this.sendDataSubject.pipe(
filter((message) => {
return input.data.requestId == message?.data.requestId ||
@@ -163,7 +167,8 @@ export class SignalRConnection {
this.hubConnection.on(method, (message: any) => {
this.sendDataSubject.next({
method: method,
data: message
data: message,
payload: this.payload[message?.requestId]
})
});
}
+20
View File
@@ -8,3 +8,23 @@ const SignalRInputSchema = z.object({
})
export type ISignalRInput = z.infer<typeof SignalRInputSchema>;
const SignalROutOutSchema = z.object({
method: z.string(),
data: z.object({
requestId: z.string(),
}).catchall(z.unknown()), // Allows any additional properties with unknown values
payload: z.object({
// requestId: z.string(),
}).catchall(z.unknown()),
})
export interface ISignalROutput<T = any> {
method: string;
data: T;
payload: {
[key: string]: unknown; // Allows any additional properties with unknown values
};
}