Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API aspects - WIP #168

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,23 @@ export interface APILayer {
name: string
}

export type FunctionInterceptor = (name: string, original: Function) => Function
export type PropertyInterceptor = (name: string, original: any) => any

export interface APIAspect {
readonly name: string
readonly interceptFunction?: FunctionInterceptor
readonly interceptProperty?: PropertyInterceptor
readonly descendNestedLevel?: number
}

export type APIAspectFactory = (apiKey: AnySlotKey, normalizedName: string) => APIAspect

export interface AppHostOptions {
readonly logger?: HostLogger
readonly monitoring: MonitoringOptions
readonly layers?: APILayer[] | APILayer[][]
readonly apiAspects?: APIAspectFactory[]
readonly disableLayersValidation?: boolean
readonly disableCheckCircularDependencies?: boolean
readonly enableStickyErrorBoundaries?: boolean
Expand Down
35 changes: 26 additions & 9 deletions src/appHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { ConsoleHostLogger, createShellLogger } from './loggers'
import { monitorAPI } from './monitorAPI'
import { Graph, Tarjan } from './tarjanGraph'
import { setupDebugInfo } from './repluggableAppDebug'
import { interceptAnyObject } from './interceptAnyObject'

const isMultiArray = <T>(v: T[] | T[][]): v is T[][] => _.every(v, _.isArray)
const castMultiArray = <T>(v: T[] | T[][]): T[][] => {
Expand Down Expand Up @@ -881,14 +882,30 @@ miss: ${memoizedWithMissHit.miss}
)
}

const applyAPIAspects = (inner: TAPI): TAPI => {
const normalizedName = normalizeApiName(slotKeyToName(key))

let result = monitorAPI(shell, options, normalizedName, api /*, trace, memoizedArr*/, apiOptions)

const aspectFactories = options.apiAspects

if (aspectFactories) {
for (const aspectFactory of aspectFactories) {
const aspect = aspectFactory(key, normalizedName)
result = interceptAnyObject(
result,
aspect.interceptFunction,
aspect.interceptProperty,
aspect.descendNestedLevel
)
}
}

return result
}

const api = factory()
const monitoredAPI = monitorAPI(
shell,
options,
normalizeApiName(slotKeyToName(key)),
api /*, trace, memoizedArr*/,
apiOptions
)
const apiWithAspects = applyAPIAspects(api)
const apiSlot = declareSlot<TAPI>(key)

APILayers.set(
Expand All @@ -900,7 +917,7 @@ miss: ${memoizedWithMissHit.miss}
.value()
: undefined
)
apiSlot.contribute(shell, monitoredAPI)
apiSlot.contribute(shell, apiWithAspects)

readyAPIs.add(key)

Expand All @@ -910,7 +927,7 @@ miss: ${memoizedWithMissHit.miss}
setInstalledShellNames(_.difference(shellNames, _.map(unReadyEntryPointsStore.get(), 'name')))
}

return monitoredAPI
return apiWithAspects
},

contributeState<TState, TAction extends AnyAction = AnyAction>(
Expand Down
3 changes: 1 addition & 2 deletions src/interceptAnyObject.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import _ from 'lodash'
import { FunctionInterceptor, PropertyInterceptor } from './API'

export interface AnyObject {
[key: string]: any
}
export type FunctionInterceptor = (name: string, original: Function) => Function
export type PropertyInterceptor = (name: string, original: any) => any

export function interceptAnyObject<T extends AnyObject>(
inner: T,
Expand Down
3 changes: 2 additions & 1 deletion test/interceptAnyObject.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _ from 'lodash'
import { interceptAnyObject, FunctionInterceptor, PropertyInterceptor } from '../src/interceptAnyObject'
import { FunctionInterceptor, PropertyInterceptor } from '../src/API'
import { interceptAnyObject } from '../src/interceptAnyObject'

type LogSpy = jest.Mock<void, string[]>

Expand Down