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 translations #12

Closed
wants to merge 7 commits into from
Closed
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
14 changes: 13 additions & 1 deletion src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type ScopedStore<S> = Pick<Redux.Store<S>, 'dispatch' | 'getState' | 'sub
export type ReactComponentContributor = () => React.ReactNode
export type ReducersMapObjectContributor<TState = {}> = () => Redux.ReducersMapObject<TState>
export type ContributionPredicate = () => boolean
export type LazyEntryPointFactory = () => Promise<EntryPoint>
export type LazyEntryPointFactory = () => Promise<EntryPoint> //TODO: get rid of these
export type ShellsChangedCallback = (shellNames: string[]) => void
export interface LazyEntryPointDescriptor {
readonly name: string
Expand All @@ -32,6 +32,9 @@ export interface EntryPoint {

export type AnyEntryPoint = EntryPoint | LazyEntryPointDescriptor
export type EntryPointOrPackage = AnyEntryPoint | AnyEntryPoint[]
export interface EntryPointOrPackagesMap {
[name: string]: EntryPointOrPackage
}

export type ExtensionItemFilter<T> = (extensionItem: ExtensionItem<T>) => boolean
export interface ExtensionSlot<T> {
Expand Down Expand Up @@ -94,6 +97,15 @@ export interface EntryPointsInfo {
readonly attached: boolean
}

export interface EntryPointInterceptor {
interceptName?(innerName: string): string
interceptGetDependencyAPIs?(innerGetDependencyAPIs?: EntryPoint['getDependencyAPIs']): EntryPoint['getDependencyAPIs']
interceptDeclareAPIs?(innerDeclareAPIs?: EntryPoint['declareAPIs']): EntryPoint['declareAPIs']
interceptAttach?(innerAttach?: EntryPoint['attach']): EntryPoint['attach']
interceptDetach?(innerDetach?: EntryPoint['detach']): EntryPoint['detach']
interceptExtend?(innerExtend?: EntryPoint['extend']): EntryPoint['extend']
}

// TODO: define logging abstraction
/*
export type LogSeverity = 'debug' | 'info' | 'warning' | 'error';
Expand Down
1 change: 1 addition & 0 deletions src/appHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ function createAppHostImpl(): AppHost {

return host

//TODO: get rid of LazyEntryPointDescriptor
function isLazyEntryPointDescriptor(value: AnyEntryPoint): value is LazyEntryPointDescriptor {
return typeof (value as LazyEntryPointDescriptor).factory === 'function'
}
Expand Down
24 changes: 12 additions & 12 deletions src/appHostUtils.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { AnyEntryPoint, AnySlotKey } from './API';
import { AnyEntryPoint, AnySlotKey } from './API'

import _ from 'lodash';
import _ from 'lodash'

export const dependentAPIs = (entryPoint: AnyEntryPoint): AnySlotKey[] => {
return _.chain(entryPoint)
.invoke('getDependencyAPIs')
.defaultTo([])
.value();
};
return _.chain(entryPoint)
.invoke('getDependencyAPIs')
.defaultTo([])
.value()
}

export const declaredAPIs = (entryPoint: AnyEntryPoint): AnySlotKey[] => {
return _.chain(entryPoint)
.invoke('declareAPIs')
.defaultTo([])
.value();
};
return _.chain(entryPoint)
.invoke('declareAPIs')
.defaultTo([])
.value()
}
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export {
AnySlotKey,
SlotKey,
ReactComponentContributor,
ReducersMapObjectContributor
ReducersMapObjectContributor,
EntryPointInterceptor
} from './API'

export { AppMainView } from './appMainView'
Expand All @@ -19,3 +20,4 @@ export { renderSlotComponents, SlotRenderer, ShellRenderer } from './renderSlotC

export * from './connectWithShell'
export { ErrorBoundary } from './errorBoundary'
export { interceptEntryPoints, interceptEntryPointsMap } from './interceptEntryPoints'
35 changes: 35 additions & 0 deletions src/interceptEntryPoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import _ from 'lodash'
import { EntryPoint, EntryPointOrPackage, EntryPointInterceptor, EntryPointOrPackagesMap } from './API'

// function isArray(entryPoints: EntryPointOrPackage[] | Object): entryPoints is EntryPointOrPackage[] {
// return (typeof (entryPoints as EntryPointOrPackage[]).length === 'number')
// }

export function interceptEntryPoints(entryPoints: EntryPointOrPackage, interceptor: EntryPointInterceptor): EntryPoint[] {
return (_.flatten([entryPoints]) as EntryPoint[]).map(ep => applyInterceptor(ep, interceptor))
}

export function interceptEntryPointsMap(entryPointsMap: EntryPointOrPackagesMap, interceptor: EntryPointInterceptor) {
const result: EntryPointOrPackagesMap = {}

for (const key in entryPointsMap) {
if (entryPointsMap.hasOwnProperty(key)) {
result[key] = interceptEntryPoints(entryPointsMap[key], interceptor)
}
}

return result
}

function applyInterceptor(inner: EntryPoint, interceptor: EntryPointInterceptor): EntryPoint {
return {
name: interceptor.interceptName ? interceptor.interceptName(inner.name) : inner.name,
getDependencyAPIs: interceptor.interceptGetDependencyAPIs
? interceptor.interceptGetDependencyAPIs(inner.getDependencyAPIs)
: inner.getDependencyAPIs,
declareAPIs: interceptor.interceptDeclareAPIs ? interceptor.interceptDeclareAPIs(inner.declareAPIs) : inner.declareAPIs,
attach: interceptor.interceptAttach ? interceptor.interceptAttach(inner.attach) : inner.attach,
detach: interceptor.interceptDetach ? interceptor.interceptDetach(inner.detach) : inner.detach,
extend: interceptor.interceptExtend ? interceptor.interceptExtend(inner.extend) : inner.extend
}
}
184 changes: 184 additions & 0 deletions test/interceptEntryPoints.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
import _ from 'lodash'
import { EntryPoint, EntryPointInterceptor } from '../src/API'
import { interceptEntryPoints, interceptEntryPointsMap } from '../src/interceptEntryPoints'

describe('interceptEntryPoints', () => {
it('should intercept name', () => {
const log: string[] = []

const entryPoints = createTestEntryPoints(2, log)
const interceptor = createTestInterceptor(log, { interceptName: true })
const intercepted = interceptEntryPoints(entryPoints, interceptor)

expect(intercepted[0].name).toBe('INTERCEPTED!EP-0')
expect(intercepted[1].name).toBe('INTERCEPTED!EP-1')
})
it('should intercept attach', () => {
const log: string[] = []

const entryPoints = createTestEntryPoints(1, log)
const interceptor = createTestInterceptor(log, { interceptAttach: true })
const intercepted = interceptEntryPoints(entryPoints, interceptor)

intercepted[0].attach && intercepted[0].attach({} as any)

expect(log).toEqual(['INTERCEPTED:attach', 'EP-0:attach'])
})
it('should intercept extend', () => {
const log: string[] = []

const entryPoints = createTestEntryPoints(1, log)
const interceptor = createTestInterceptor(log, { interceptExtend: true })
const intercepted = interceptEntryPoints(entryPoints, interceptor)

intercepted[0].extend && intercepted[0].extend({} as any)

expect(log).toEqual(['INTERCEPTED:extend', 'EP-0:extend'])
})
it('should intercept detach', () => {
const log: string[] = []

const entryPoints = createTestEntryPoints(1, log)
const interceptor = createTestInterceptor(log, { interceptDetach: true })
const intercepted = interceptEntryPoints(entryPoints, interceptor)

intercepted[0].detach && intercepted[0].detach({} as any)

expect(log).toEqual(['INTERCEPTED:detach', 'EP-0:detach'])
})
it('should intercept declareAPIs', () => {
const log: string[] = []

const entryPoints = createTestEntryPoints(1, log)
const interceptor = createTestInterceptor(log, { interceptDeclareAPIs: true })
const intercepted = interceptEntryPoints(entryPoints, interceptor)

intercepted[0].declareAPIs && intercepted[0].declareAPIs()

expect(log).toEqual(['INTERCEPTED:declareAPIs', 'EP-0:declareAPIs'])
})
it('should intercept getDependencyAPIs', () => {
const log: string[] = []

const entryPoints = createTestEntryPoints(1, log)
const interceptor = createTestInterceptor(log, { interceptGetDependencyAPIs: true })
const intercepted = interceptEntryPoints(entryPoints, interceptor)

intercepted[0].getDependencyAPIs && intercepted[0].getDependencyAPIs()

expect(log).toEqual(['INTERCEPTED:getDependencyAPIs', 'EP-0:getDependencyAPIs'])
})
it('should handle single entry point', () => {
const log: string[] = []

const entryPoints = createTestEntryPoints(3, log)
const interceptor = createTestInterceptor(log, { interceptAttach: true })
const intercepted = interceptEntryPoints(entryPoints[0], interceptor)

intercepted[0].attach && intercepted[0].attach({} as any)

expect(log).toEqual(['INTERCEPTED:attach', 'EP-0:attach'])
})

it('should recognize maps of entry points', () => {
const log: string[] = []

const entryPoints = createTestEntryPoints(3, log)
const packagesMap = {
one: entryPoints[0],
two: [entryPoints[1], entryPoints[2]]
}
const interceptor = createTestInterceptor(log, { interceptAttach: true })
const interceptedMap = interceptEntryPointsMap(packagesMap, interceptor)
const intercepted = [
(interceptedMap.one as EntryPoint[])[0],
(interceptedMap.two as EntryPoint[])[0],
(interceptedMap.two as EntryPoint[])[1]
]

intercepted[0].attach && intercepted[0].attach({} as any)
intercepted[1].attach && intercepted[1].attach({} as any)
intercepted[2].attach && intercepted[2].attach({} as any)

expect(typeof interceptedMap).toBe('object')
expect(log).toEqual(['INTERCEPTED:attach', 'EP-0:attach', 'INTERCEPTED:attach', 'EP-1:attach', 'INTERCEPTED:attach', 'EP-2:attach'])
})
})

type TestInterceptorFlags = { [P in keyof EntryPointInterceptor]?: boolean }

function createTestEntryPoints(count: number, log: string[]): EntryPoint[] {
return _.range(count).map<EntryPoint>(index => ({
name: `EP-${index}`,
getDependencyAPIs() {
log.push(`EP-${index}:getDependencyAPIs`)
return []
},
declareAPIs() {
log.push(`EP-${index}:declareAPIs`)
return []
},
attach() {
log.push(`EP-${index}:attach`)
return []
},
extend() {
log.push(`EP-${index}:extend`)
return []
},
detach() {
log.push(`EP-${index}:detach`)
return []
}
}))
}

function createTestInterceptor(log: string[], flags: TestInterceptorFlags): EntryPointInterceptor {
return {
interceptName: flags.interceptName
? name => {
return `INTERCEPTED!${name}`
}
: undefined,
interceptDeclareAPIs: flags.interceptDeclareAPIs
? innerDeclareAPIs => {
return () => {
log.push(`INTERCEPTED:declareAPIs`)
return (innerDeclareAPIs && innerDeclareAPIs()) || []
}
}
: undefined,
interceptGetDependencyAPIs: flags.interceptGetDependencyAPIs
? innerGetDependencyAPIs => {
return () => {
log.push(`INTERCEPTED:getDependencyAPIs`)
return (innerGetDependencyAPIs && innerGetDependencyAPIs()) || []
}
}
: undefined,
interceptAttach: flags.interceptAttach
? innerAttach => {
return shell => {
log.push(`INTERCEPTED:attach`)
innerAttach && innerAttach(shell)
}
}
: undefined,
interceptExtend: flags.interceptExtend
? innerExtend => {
return shell => {
log.push(`INTERCEPTED:extend`)
innerExtend && innerExtend(shell)
}
}
: undefined,
interceptDetach: flags.interceptDetach
? innerDetach => {
return shell => {
log.push(`INTERCEPTED:detach`)
innerDetach && innerDetach(shell)
}
}
: undefined
}
}
7 changes: 6 additions & 1 deletion testKit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { mount, ReactWrapper } from 'enzyme'
import _ from 'lodash'
import React, { Component, ReactElement } from 'react'
import { Provider } from 'react-redux'
import { EntryPoint, PrivateShell } from '../src/API'
import { EntryPoint, PrivateShell, LocaleDictionary, TranslationFunc } from '../src/API'
import { AnySlotKey, AppHost, AppMainView, createAppHost, EntryPointOrPackage, Shell, SlotKey } from '../src/index'
import { ShellRenderer } from '../src/renderSlotComponents'

Expand Down Expand Up @@ -150,6 +150,11 @@ function createShell(host: AppHost): PrivateShell {
const API: any = {}
return API
},
contributeTranslations(dictionary: LocaleDictionary): void {},
useTranslationFunction(func: TranslationFunc): void {},
translate(key: string, params?: { [name: string]: any }): string {
return key
},
contributeState: _.noop,
contributeMainView: _.noop
}
Expand Down