Skip to content

Commit

Permalink
Let syncState be offline if license expired
Browse files Browse the repository at this point in the history
Don't connect websocket if license expired
  • Loading branch information
dfahlander committed Sep 27, 2023
1 parent a500d07 commit 85cc39f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion addons/dexie-cloud/src/authentication/authenticate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export async function loadAccessToken(
await db.table('$logins').update(claims.sub, {
accessToken: refreshedLogin.accessToken,
accessTokenExpiration: refreshedLogin.accessTokenExpiration,
license: refreshedLogin.license
license: refreshedLogin.license,
});
return refreshedLogin;
}
Expand Down
12 changes: 11 additions & 1 deletion addons/dexie-cloud/src/computeSyncState.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { combineLatest, Observable, of } from 'rxjs';
import { debounceTime, map, startWith, switchMap } from 'rxjs/operators';
import { getCurrentUserEmitter } from './currentUserEmitter';
import { DexieCloudDB, SyncStateChangedEventData } from './db/DexieCloudDB';
import { isOnline } from './sync/isOnline';
import { SyncState } from './types/SyncState';
Expand Down Expand Up @@ -35,9 +36,17 @@ export function computeSyncState(db: DexieCloudDB): Observable<SyncState> {
return combineLatest([
lazyWebSocketStatus,
db.syncStateChangedEvent.pipe(startWith({ phase: 'initial' } as SyncStateChangedEventData)),
getCurrentUserEmitter(db.dx._novip),
userIsReallyActive
]).pipe(
map(([status, syncState, userIsActive]) => {
map(([status, syncState, user, userIsActive]) => {
if (user.license?.status && user.license.status !== 'ok') {
return {
phase: 'offline',
status: 'offline',
license: user.license.status
} satisfies SyncState;
}
let { phase, error, progress } = syncState;
let adjustedStatus = status;
if (phase === 'error') {
Expand Down Expand Up @@ -72,6 +81,7 @@ export function computeSyncState(db: DexieCloudDB): Observable<SyncState> {
error,
progress,
status: isOnline ? adjustedStatus : 'offline',
license: 'ok'
};

return retState;
Expand Down
8 changes: 8 additions & 0 deletions addons/dexie-cloud/src/sync/InvalidLicenseError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { UserLogin } from "../db/entities/UserLogin";

export class InvalidLicenseError extends Error {
name = "InvalidLicenseError";
constructor() {
super(`Invalid license`);
}
}
18 changes: 13 additions & 5 deletions addons/dexie-cloud/src/sync/connectWebSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
WSConnectionMsg,
WSObservable,
} from '../WSObservable';
import { InvalidLicenseError } from './InvalidLicenseError';

function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
Expand Down Expand Up @@ -68,11 +69,14 @@ export function connectWebSocket(db: DexieCloudDB) {
map((userLogin) => [userLogin, syncState] as const)
)
),
switchMap(([userLogin, syncState]) =>
userIsReallyActive.pipe(
switchMap(([userLogin, syncState]) => {
if (userLogin.license?.status && userLogin.license.status !== 'ok') {
throw new InvalidLicenseError();
}
return userIsReallyActive.pipe(
map((isActive) => [isActive ? userLogin : null, syncState] as const)
)
),
);
}),
switchMap(([userLogin, syncState]) => {
if (userLogin?.isLoggedIn && !syncState?.realms.includes(userLogin.userId!)) {
// We're in an in-between state when user is logged in but the user's realms are not yet synced.
Expand Down Expand Up @@ -131,11 +135,15 @@ export function connectWebSocket(db: DexieCloudDB) {
switchMap(() => createObservable())
);
} else {
return throwError(error);
return throwError(()=>error);
}
}),
catchError((error) => {
db.cloud.webSocketStatus.next("error");
if (error instanceof InvalidLicenseError) {
// Don't retry. Just throw and don't try connect again.
return throwError(() => error);
}
return from(waitAndReconnectWhenUserDoesSomething(error)).pipe(
switchMap(() => createObservable())
);
Expand Down
1 change: 1 addition & 0 deletions addons/dexie-cloud/src/types/SyncState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export interface SyncState {
phase: SyncStatePhase;
progress?: number; // 0..100
error?: Error; // If phase === "error"
license?: 'ok' | 'expired' | 'deactivated';
}

0 comments on commit 85cc39f

Please sign in to comment.