Skip to content

Commit

Permalink
refactor: generalize repository layer (DAP-4759) (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
alsakhaev authored Sep 19, 2024
1 parent 4c87505 commit e575a2a
Show file tree
Hide file tree
Showing 47 changed files with 1,693 additions and 2,177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,21 @@ const CloseIcon = () => (
</svg>
)

const createEmptyMutation = (accountId: string): Mutation => ({
id: `${accountId}/mutation/Untitled-${generateRandomHex(6)}`,
apps: [],
metadata: {
name: '',
},
targets: [
{
namespace: 'engine',
contextType: 'website',
if: { id: { in: [window.location.hostname] } },
const createEmptyMutation = (accountId: string): Mutation =>
Mutation.create({
id: `${accountId}/mutation/Untitled-${generateRandomHex(6)}`,
apps: [],
metadata: {
name: '',
},
],
})
targets: [
{
namespace: 'engine',
contextType: 'website',
if: { id: { in: [window.location.hostname] } },
},
],
})

export interface Props {
apps: AppMetadata[]
Expand Down
18 changes: 9 additions & 9 deletions libs/core/src/core.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { PureTreeBuilder } from './tree/pure-tree/pure-tree-builder'
import { IAdapter } from './adapters/interface'
import { DynamicHtmlAdapter } from './adapters/dynamic-html-adapter'
import { JsonParser } from './parsers/json-parser'
import { BosParser } from './parsers/bos-parser'
import { JsonParser, JsonParserConfig } from './parsers/json-parser'
import { BosParser, BosParserConfig } from './parsers/bos-parser'
import { MutableWebParser } from './parsers/mweb-parser'
import { LinkParser } from './parsers/link-parser'
import { AdapterType, ParserConfig } from './types'
import { ParserType, ParserConfig } from './types'

export class Core {
private _treeBuilder: PureTreeBuilder
Expand Down Expand Up @@ -70,31 +70,31 @@ export class Core {
}

switch (config.parserType) {
case AdapterType.Json:
case ParserType.Json:
return new DynamicHtmlAdapter(
document.documentElement,
this._treeBuilder,
config.id,
new JsonParser(config) // ToDo: add try catch because config can be invalid
new JsonParser(config.contexts as JsonParserConfig) // ToDo: add try catch because config can be invalid
)

case AdapterType.Bos:
case ParserType.Bos:
return new DynamicHtmlAdapter(
document.documentElement,
this._treeBuilder,
config.id,
new BosParser(config)
new BosParser(config.contexts as BosParserConfig)
)

case AdapterType.MWeb:
case ParserType.MWeb:
return new DynamicHtmlAdapter(
document.body,
this._treeBuilder,
config.id,
new MutableWebParser()
)

case AdapterType.Link:
case ParserType.Link:
return new DynamicHtmlAdapter(document.body, this._treeBuilder, config.id, new LinkParser())

default:
Expand Down
2 changes: 1 addition & 1 deletion libs/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export { BosParser, BosParserConfig } from './parsers/bos-parser'
export { MutableWebParser } from './parsers/mweb-parser'
export { PureContextNode } from './tree/pure-tree/pure-context-node'
export { IContextNode, ITreeBuilder, ContextLevel } from './tree/types'
export { AdapterType, ParserConfig } from './types'
export { ParserType, ParserConfig } from './types'
export { Subscription } from './event-emitter'
export { InsertionPointWithElement } from './tree/types'
export { isDeepEqual } from './utils'
Expand Down
40 changes: 19 additions & 21 deletions libs/core/src/parsers/bos-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@ const CompAttr = 'data-component'
const PropsAttr = 'data-props'

export type BosParserConfig = {
contexts: {
[name: string]: {
component?: string
props?: {
[prop: string]: string
}
insertionPoints?: {
[insPointName: string]: {
component?: string
bosLayoutManager?: string
insertionType?: InsertionType
}
[name: string]: {
component?: string
props?: {
[prop: string]: string
}
insertionPoints?: {
[insPointName: string]: {
component?: string
bosLayoutManager?: string
insertionType?: InsertionType
}
children?: string[]
}
children?: string[]
}
}

Expand All @@ -30,18 +28,18 @@ export class BosParser implements IParser {
// ToDo: validate config
this.config = config

if (!this.config.contexts['root']) {
this.config.contexts['root'] = {
if (!this.config['root']) {
this.config['root'] = {
props: {
id: 'root',
},
children: Object.keys(this.config.contexts), // ToDo:
children: Object.keys(this.config), // ToDo:
}
}
}

parseContext(element: HTMLElement, contextName: string) {
const contextProperties = this.config.contexts[contextName].props
const contextProperties = this.config[contextName].props
if (!contextProperties) return {}

const parsed: any = {}
Expand All @@ -56,14 +54,14 @@ export class BosParser implements IParser {
}

findChildElements(element: HTMLElement, contextName: string) {
const contextConfig = this.config.contexts[contextName]
const contextConfig = this.config[contextName]
if (!contextConfig.children?.length) return []

const result: { element: HTMLElement; contextName: string }[] = []

// ToDo: maybe querySelectorAll('.foo:not(.foo .foo)') is faster?
for (const childContextName of contextConfig.children ?? []) {
const childConfig = this.config.contexts[childContextName]
const childConfig = this.config[childContextName]
if (!childConfig.component) continue

const childElements = Array.from(
Expand All @@ -83,7 +81,7 @@ export class BosParser implements IParser {
contextName: string,
insertionPoint: string
): HTMLElement | null {
const contextConfig = this.config.contexts[contextName]
const contextConfig = this.config[contextName]
const insPointConfig = contextConfig.insertionPoints?.[insertionPoint]

if (insPointConfig?.component) {
Expand All @@ -97,7 +95,7 @@ export class BosParser implements IParser {
}

getInsertionPoints(_: HTMLElement, contextName: string): InsertionPoint[] {
const contextConfig = this.config.contexts[contextName]
const contextConfig = this.config[contextName]
if (!contextConfig.insertionPoints) return []

return Object.entries(contextConfig.insertionPoints).map(([name, selectorOrObject]) => ({
Expand Down
40 changes: 19 additions & 21 deletions libs/core/src/parsers/json-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@ import { InsertionType } from '../adapters/interface'
import { IParser, InsertionPoint } from './interface'

export type JsonParserConfig = {
contexts: {
[name: string]: {
selector?: string
props?: {
[prop: string]: string
}
insertionPoints?: {
[insPointName: string]:
| string
| {
selector?: string
bosLayoutManager?: string
insertionType?: InsertionType
}
}
children?: string[]
[name: string]: {
selector?: string
props?: {
[prop: string]: string
}
insertionPoints?: {
[insPointName: string]:
| string
| {
selector?: string
bosLayoutManager?: string
insertionType?: InsertionType
}
}
children?: string[]
}
}

Expand Down Expand Up @@ -58,7 +56,7 @@ export class JsonParser implements IParser {
}

parseContext(element: HTMLElement, contextName: string) {
const contextProperties = this.config.contexts[contextName].props
const contextProperties = this.config[contextName].props
if (!contextProperties) return {}

const parsed: any = {}
Expand All @@ -75,13 +73,13 @@ export class JsonParser implements IParser {
element: HTMLElement,
contextName: string
): { element: HTMLElement; contextName: string }[] {
const contextConfig = this.config.contexts[contextName]
const contextConfig = this.config[contextName]
if (!contextConfig.children?.length) return []

const result: { element: HTMLElement; contextName: string }[] = []

for (const childContextName of contextConfig.children ?? []) {
const childConfig = this.config.contexts[childContextName]
const childConfig = this.config[childContextName]
if (!childConfig.selector) continue

const childElements = Array.from(element.querySelectorAll<HTMLElement>(childConfig.selector))
Expand All @@ -99,7 +97,7 @@ export class JsonParser implements IParser {
contextName: string,
insertionPoint: string
): HTMLElement | null {
const contextConfig = this.config.contexts[contextName]
const contextConfig = this.config[contextName]
const selectorOrObject = contextConfig.insertionPoints?.[insertionPoint]

if (typeof selectorOrObject === 'string') {
Expand All @@ -114,7 +112,7 @@ export class JsonParser implements IParser {
}

getInsertionPoints(_: HTMLElement, contextName: string): InsertionPoint[] {
const contextConfig = this.config.contexts[contextName]
const contextConfig = this.config[contextName]
if (!contextConfig.insertionPoints) return []

return Object.entries(contextConfig.insertionPoints).map(([name, selectorOrObject]) => ({
Expand Down
13 changes: 7 additions & 6 deletions libs/core/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { BosParserConfig } from './parsers/bos-parser'
import { JsonParserConfig } from './parsers/json-parser'

export enum AdapterType {
export enum ParserType {
Bos = 'bos',
Microdata = 'microdata',
Json = 'json',
MWeb = 'mweb',
Link = 'link',
Unknown = 'unknown',
}

export type ParserConfig =
| ({ parserType: AdapterType.Json; id: string } & JsonParserConfig)
| ({ parserType: AdapterType.Bos; id: string } & BosParserConfig)
| { parserType: AdapterType.MWeb; id: string }
| { parserType: AdapterType.Link; id: string }
export type ParserConfig = {
id: string
parserType: ParserType
contexts?: JsonParserConfig | BosParserConfig | null
}
3 changes: 2 additions & 1 deletion libs/engine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"caching-decorator": "^1.0.3",
"ethereum-blockies-base64": "^1.0.2",
"js-sha256": "^0.11.0",
"json-stringify-deterministic": "^1.0.12"
"json-stringify-deterministic": "^1.0.12",
"reflect-metadata": "^0.2.2"
}
}
Loading

0 comments on commit e575a2a

Please sign in to comment.