Skip to content

Commit

Permalink
migrate onChange and dynamic add/remove to the main class constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
danphilibin committed Dec 7, 2022
1 parent 8954b91 commit 6c2132f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 95 deletions.
45 changes: 44 additions & 1 deletion src/classes/Routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ import { z } from 'zod'
import fetch from 'cross-fetch'
import * as superjson from 'superjson'
import Logger from './Logger'
import Interval, { IntervalError, QueuedAction } from '..'
import Interval, {
IntervalActionDefinition,
IntervalError,
Page,
QueuedAction,
} from '..'
import { ENQUEUE_ACTION, DEQUEUE_ACTION } from '../internalRpcSchema'
import { Ctx } from 'evt'

/**
* This is effectively a namespace inside of Interval with a little bit of its own state.
Expand All @@ -13,8 +19,10 @@ export default class Routes {
#logger: Logger
#apiKey?: string
#endpoint: string
#groupChangeCtx: Ctx<void>

constructor(
ctx: Ctx<void>,
interval: Interval,
endpoint: string,
logger: Logger,
Expand All @@ -24,6 +32,7 @@ export default class Routes {
this.#apiKey = apiKey
this.#logger = logger
this.#endpoint = endpoint + '/api/actions'
this.#groupChangeCtx = ctx
}

#getAddress(path: string): string {
Expand Down Expand Up @@ -125,4 +134,38 @@ export default class Routes {
params,
}
}

add(slug: string, route: IntervalActionDefinition | Page) {
if (!this.interval.config.routes) {
this.interval.config.routes = {}
}

if (route instanceof Page) {
route.onChange.attach(this.#groupChangeCtx, () => {
this.interval.client?.handleActionsChange(this.interval.config)
})
}

this.interval.config.routes[slug] = route
this.interval.client?.handleActionsChange(this.interval.config)
}

remove(slug: string) {
for (const key of ['routes', 'actions', 'groups'] as const) {
const routes = this.interval.config[key]

if (!routes) continue
const route = routes[slug]
if (!route) continue

if (route instanceof Page) {
route.onChange.detach(this.#groupChangeCtx)
}

delete routes[slug]

this.interval.client?.handleActionsChange(this.interval.config)
return
}
}
}
94 changes: 0 additions & 94 deletions src/experimental.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { z } from 'zod'
import { Evt, Ctx } from 'evt'
import fetch from 'cross-fetch'
import type { IncomingMessage, ServerResponse } from 'http'
import Interval, { io, ctx, InternalConfig, IntervalError } from '.'
Expand All @@ -13,56 +12,12 @@ import {
LambdaRequestPayload,
LambdaResponse,
} from './utils/http'
import Routes from './classes/Routes'
import Logger from './classes/Logger'
import Action from './classes/Action'
import { IntervalActionDefinition } from './types'
import { BasicLayout } from './classes/Layout'

class ExperimentalInterval extends Interval {
#groupChangeCtx = Evt.newCtx()
routes: ExperimentalRoutes

constructor(config: InternalConfig) {
super(config)
this.routes = new ExperimentalRoutes(
this.#groupChangeCtx,
this,
this.httpEndpoint,
this.log,
this.apiKey
)

const routes = {
...this.config.actions,
...this.config.groups,
...this.config.routes,
}

if (routes) {
for (const group of Object.values(routes)) {
if (group instanceof Page) {
group.onChange.attach(this.#groupChangeCtx, () => {
this.client?.handleActionsChange(this.config)
})
}
}
}
}

// TODO: Mark as deprecated soon, remove soon afterward
get actions(): ExperimentalRoutes {
return this.routes
}

// TODO: Mark as deprecated soon, remove soon afterward
addGroup(slug: string, group: Page) {
return this.routes.add(slug, group)
}

// TODO: Mark as deprecated soon, remove soon afterward
removeGroup(slug: string) {
return this.routes.remove(slug)
}

/*
Expand Down Expand Up @@ -255,55 +210,6 @@ class ExperimentalInterval extends Interval {
}
}

export class ExperimentalRoutes extends Routes {
#groupChangeCtx: Ctx<void>

constructor(
ctx: Ctx<void>,
interval: Interval,
endpoint: string,
logger: Logger,
apiKey?: string
) {
super(interval, endpoint, logger, apiKey)
this.#groupChangeCtx = ctx
}

add(slug: string, route: IntervalActionDefinition | Page) {
if (!this.interval.config.routes) {
this.interval.config.routes = {}
}

if (route instanceof Page) {
route.onChange.attach(this.#groupChangeCtx, () => {
this.interval.client?.handleActionsChange(this.interval.config)
})
}

this.interval.config.routes[slug] = route
this.interval.client?.handleActionsChange(this.interval.config)
}

remove(slug: string) {
for (const key of ['routes', 'actions', 'groups'] as const) {
const routes = this.interval.config[key]

if (!routes) continue
const route = routes[slug]
if (!route) continue

if (route instanceof Page) {
route.onChange.detach(this.#groupChangeCtx)
}

delete routes[slug]

this.interval.client?.handleActionsChange(this.interval.config)
return
}
}
}

export {
Page,
// TODO: Mark as deprecated soon, remove soon afterward
Expand Down
20 changes: 20 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import IntervalClient, {
} from './classes/IntervalClient'
import Action from './classes/Action'
import { BasicLayout } from './classes/Layout'
import { Evt } from 'evt'

export type {
ActionCtx,
Expand Down Expand Up @@ -146,6 +147,7 @@ export default class Interval {
#client: IntervalClient | undefined
#apiKey: string | undefined
#httpEndpoint: string
#groupChangeCtx = Evt.newCtx()
routes: Routes

constructor(config: InternalConfig) {
Expand All @@ -156,12 +158,30 @@ export default class Interval {
this.#httpEndpoint = getHttpEndpoint(
config.endpoint ?? DEFAULT_WEBSOCKET_ENDPOINT
)

this.routes = new Routes(
this.#groupChangeCtx,
this,
this.#httpEndpoint,
this.#logger,
this.#apiKey
)

const routes = {
...this.config.actions,
...this.config.groups,
...this.config.routes,
}

if (routes) {
for (const group of Object.values(routes)) {
if (group instanceof Page) {
group.onChange.attach(this.#groupChangeCtx, () => {
this.client?.handleActionsChange(this.config)
})
}
}
}
}

// TODO: Mark as deprecated soon, remove soon afterward
Expand Down

0 comments on commit 6c2132f

Please sign in to comment.