-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #291 from Vonage/iss-290-init-core-revision
feat: issue #290: core services init and config resolver
- Loading branch information
Showing
11 changed files
with
133 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,30 @@ | ||
import { init as coreInit } from '@vonage/vvd-core'; | ||
import { coreReady } from '@vonage/vvd-core'; | ||
import { style } from './vvd-context.css'; | ||
|
||
/** | ||
* Vivid context initialiser | ||
* this API is a 'customer facing' one - meant to be used by a consuming application to initialise the Vivid context, services and resource for the application as a whole | ||
* | ||
* @param services map of services to initialise; keys are a service names, values are an options to pass to those services upon initialisation | ||
* @returns a compound Promise of all initialisation Promises of the participating parties (services, resources etc) | ||
*/ | ||
async function init(services: Record<string, unknown>): Promise<void[]> { | ||
injectGlobalStyle(); | ||
return await coreInit(services); | ||
let | ||
initResolver: (value?: unknown) => void | PromiseLike<void>, | ||
initRejector: (reason?: unknown) => void | PromiseLike<void>; | ||
|
||
export const contextReady = new Promise((resolve, reject) => { | ||
initResolver = resolve; | ||
initRejector = reject; | ||
}); | ||
|
||
init(); | ||
|
||
async function init(): Promise<void> { | ||
try { | ||
injectGlobalStyle(); | ||
await coreReady; | ||
initResolver(); | ||
} catch (e) { | ||
initRejector(e); | ||
} | ||
} | ||
|
||
function injectGlobalStyle() { | ||
const globalStyleSheet = document.createElement('style'); | ||
globalStyleSheet.type = 'text/css'; | ||
globalStyleSheet.innerHTML = style.cssText; | ||
document.head.appendChild(globalStyleSheet); | ||
} | ||
|
||
export default Object.freeze({ | ||
init | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { SchemeOption } from '@vonage/vvd-scheme'; | ||
|
||
const | ||
VVD_CONTEXT_ATTRIBUTE = 'data-vvd-context', | ||
defaultConfig = {} as Record<string, unknown>; | ||
|
||
let tmpConfig: Record<string, unknown> | null = null; | ||
|
||
tmpConfig = updateByHtmlAttribute(); | ||
|
||
if (!tmpConfig) { | ||
tmpConfig = defaultConfig; | ||
} | ||
|
||
export interface Configuration { | ||
scheme?: SchemeOption | ||
} | ||
const effectiveConfig: Configuration = tmpConfig; | ||
export default effectiveConfig; | ||
|
||
function updateByHtmlAttribute(): Record<string, unknown> | null { | ||
let result = null; | ||
const htmlContextAttribute = document.documentElement.getAttribute(VVD_CONTEXT_ATTRIBUTE); | ||
if (htmlContextAttribute) { | ||
result = {}; | ||
} | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,25 @@ | ||
import configuration, { Configuration } from './config-resolver.js'; | ||
import fonts from '@vonage/vvd-fonts/vvd-fonts.js'; | ||
import scheme from '@vonage/vvd-scheme'; | ||
import schemeService from '@vonage/vvd-scheme'; | ||
|
||
interface ServiceEntry { | ||
defaultInitParam?: unknown, | ||
initialiser(initParam: unknown): Promise<void> | ||
} | ||
let | ||
initResolver: (value?: unknown) => void | PromiseLike<void>, | ||
initRejector: (reason?: unknown) => void | PromiseLike<void>; | ||
|
||
const servicesRegistry: Record<string, ServiceEntry> = { | ||
'fonts': { | ||
defaultInitParam: null, | ||
initialiser: fonts.init | ||
}, | ||
'scheme': { | ||
defaultInitParam: null, | ||
initialiser: scheme.init | ||
} | ||
}; | ||
export const coreReady = new Promise((resolve, reject) => { | ||
initResolver = resolve; | ||
initRejector = reject; | ||
}); | ||
|
||
export function validateInitParameters(services: Record<string, unknown>): void { | ||
const knownServices: string[] = Object.keys(servicesRegistry); | ||
const unknownService = Object.keys(services).find(serviceKey => !knownServices.includes(serviceKey)); | ||
if (unknownService) { | ||
throw new Error(`unknown service key '${unknownService}' specified for init API`); | ||
} | ||
} | ||
console.debug('effective config', JSON.stringify(configuration)); | ||
init(configuration); | ||
|
||
/** | ||
* Internal initialiser | ||
* this API is an internal initialiser of the core services | ||
* | ||
* @param services a map of the services requested to be initialised, where the key is the name of the service and the value is the parameter/s to be passed to the service initialisation function | ||
* @returns compound Promise of all inidividual service initialisation Promises | ||
*/ | ||
export async function init(services?: Record<string, unknown>): Promise<void[]> { | ||
if (services) { | ||
validateInitParameters(services); | ||
} else { | ||
services = servicesRegistry; | ||
} | ||
|
||
const serviceInitPromises = Object | ||
.entries(services) | ||
.map(([serviceKey, initParams]) => servicesRegistry[serviceKey].initialiser(initParams)); | ||
|
||
return await Promise.all(serviceInitPromises); | ||
async function init({ scheme }: Configuration): Promise<void> { | ||
Promise | ||
.all([ | ||
fonts.init(), | ||
schemeService.set(scheme) | ||
]) | ||
.then(initResolver) | ||
.catch(initRejector); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,15 @@ | ||
import { init as coreInit } from '@vonage/vvd-core'; | ||
import '@vonage/vvd-core'; | ||
import { customElement } from 'lit-element'; | ||
import { TopAppBarFixed as MWCTopAppBarFixed } from '@material/mwc-top-app-bar-fixed'; | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
'vwc-top-app-bar-fixed': VWCTopAppBarFixed; | ||
} | ||
interface HTMLElementTagNameMap { | ||
'vwc-top-app-bar-fixed': VWCTopAppBarFixed; | ||
} | ||
} | ||
|
||
coreInit(); | ||
|
||
/** | ||
* This component is an extension of [<mwc-top-app-bar-fixed>](https://github.com/material-components/material-components-web-components/tree/master/packages/top-app-bar-fixed) | ||
*/ | ||
@customElement('vwc-top-app-bar-fixed') | ||
export class VWCTopAppBarFixed extends MWCTopAppBarFixed {} | ||
export class VWCTopAppBarFixed extends MWCTopAppBarFixed { } |
Oops, something went wrong.