chore: dev mode refactor

This commit is contained in:
Mo
2023-07-03 06:00:32 -05:00
parent 932695631e
commit da974e1b64
4 changed files with 53 additions and 53 deletions

View File

@@ -30,6 +30,7 @@ import { DeinitMode } from './DeinitMode'
import { DeinitSource } from './DeinitSource'
import { UserClientInterface } from '../User/UserClientInterface'
import { SessionsClientInterface } from '../Session/SessionsClientInterface'
import { User } from '@standardnotes/responses'
export interface ApplicationInterface {
deinit(mode: DeinitMode, source: DeinitSource): void
@@ -57,6 +58,8 @@ export interface ApplicationInterface {
contentType: ContentType | ContentType[],
stream: ItemStream<I>,
): () => void
getUser(): User | undefined
hasAccount(): boolean
importData(data: BackupFile, awaitSync?: boolean): Promise<ImportDataReturnType>

View File

@@ -1,8 +1,43 @@
import { InternalFeature, InternalFeatureService } from '@standardnotes/snjs'
import { WebApplicationInterface } from '@standardnotes/ui-services'
export class DevModeHook {
load(_application: WebApplicationInterface) {
export class DevMode {
constructor(private application: WebApplicationInterface) {
InternalFeatureService.get().enableFeature(InternalFeature.Vaults)
}
/** Valid only when running a mock event publisher on port 3124 */
async purchaseMockSubscription() {
const subscriptionId = 2000
const email = this.application.getUser()?.email
const response = await fetch('http://localhost:3124/events', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
eventType: 'SUBSCRIPTION_PURCHASED',
eventPayload: {
userEmail: email,
subscriptionId: subscriptionId,
subscriptionName: 'PRO_PLAN',
subscriptionExpiresAt: (new Date().getTime() + 3_600_000) * 1_000,
timestamp: Date.now(),
offline: false,
discountCode: null,
limitedDiscountPurchased: false,
newSubscriber: true,
totalActiveSubscriptionsCount: 1,
userRegisteredAt: 1,
billingFrequency: 12,
payAmount: 59.0,
},
}),
})
if (!response.ok) {
console.error(`Failed to publish mocked event: ${response.status} ${response.statusText}`)
}
}
}

View File

@@ -51,19 +51,21 @@ import { FeatureName } from '@/Controllers/FeatureName'
import { ItemGroupController } from '@/Components/NoteView/Controller/ItemGroupController'
import { VisibilityObserver } from './VisibilityObserver'
import { MomentsService } from '@/Controllers/Moments/MomentsService'
import { purchaseMockSubscription } from '@/Utils/Dev/PurchaseMockSubscription'
import { DevModeHook } from './DevMode'
import { DevMode } from './DevMode'
export type WebEventObserver = (event: WebAppEvent, data?: unknown) => void
export class WebApplication extends SNApplication implements WebApplicationInterface {
private webServices!: WebServices
private webEventObservers: WebEventObserver[] = []
public itemControllerGroup: ItemGroupController
private mobileWebReceiver?: MobileWebReceiver
private androidBackHandler?: AndroidBackHandler
public readonly itemControllerGroup: ItemGroupController
public readonly routeService: RouteServiceInterface
private visibilityObserver?: VisibilityObserver
private readonly webServices!: WebServices
private readonly webEventObservers: WebEventObserver[] = []
private readonly mobileWebReceiver?: MobileWebReceiver
private readonly androidBackHandler?: AndroidBackHandler
private readonly visibilityObserver?: VisibilityObserver
public readonly devMode?: DevMode
constructor(
deviceInterface: WebOrDesktopDevice,
@@ -93,7 +95,7 @@ export class WebApplication extends SNApplication implements WebApplicationInter
})
if (isDev) {
new DevModeHook().load(this)
this.devMode = new DevMode(this)
}
makeObservable(this, {
@@ -157,7 +159,7 @@ export class WebApplication extends SNApplication implements WebApplicationInter
;(service as { application?: WebApplication }).application = undefined
}
this.webServices = {} as WebServices
;(this.webServices as unknown) = undefined
this.itemControllerGroup.deinit()
;(this.itemControllerGroup as unknown) = undefined
@@ -170,7 +172,7 @@ export class WebApplication extends SNApplication implements WebApplicationInter
if (this.visibilityObserver) {
this.visibilityObserver.deinit()
this.visibilityObserver = undefined
;(this.visibilityObserver as unknown) = undefined
}
} catch (error) {
console.error('Error while deiniting application', error)
@@ -463,12 +465,4 @@ export class WebApplication extends SNApplication implements WebApplicationInter
generateUUID(): string {
return this.options.crypto.generateUUID()
}
dev__purchaseMockSubscription() {
if (!isDev) {
throw new Error('This method is only available in dev mode')
}
void purchaseMockSubscription(this.getUser()?.email as string, 2000)
}
}

View File

@@ -1,32 +0,0 @@
/** Valid only when running a mock event publisher on port 3124 */
export async function purchaseMockSubscription(email: string, subscriptionId: number) {
const response = await fetch('http://localhost:3124/events', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
eventType: 'SUBSCRIPTION_PURCHASED',
eventPayload: {
userEmail: email,
subscriptionId: subscriptionId,
subscriptionName: 'PRO_PLAN',
subscriptionExpiresAt: (new Date().getTime() + 3_600_000) * 1_000,
timestamp: Date.now(),
offline: false,
discountCode: null,
limitedDiscountPurchased: false,
newSubscriber: true,
totalActiveSubscriptionsCount: 1,
userRegisteredAt: 1,
billingFrequency: 12,
payAmount: 59.0,
},
}),
})
if (!response.ok) {
console.error(`Failed to publish mocked event: ${response.status} ${response.statusText}`)
}
}