Skip to content

Commit e575a2a

Browse files
authored
refactor: generalize repository layer (DAP-4759) (#21)
1 parent 4c87505 commit e575a2a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1693
-2177
lines changed

apps/extension/src/contentscript/multitable-panel/components/mutation-editor-modal.tsx

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -147,20 +147,21 @@ const CloseIcon = () => (
147147
</svg>
148148
)
149149

150-
const createEmptyMutation = (accountId: string): Mutation => ({
151-
id: `${accountId}/mutation/Untitled-${generateRandomHex(6)}`,
152-
apps: [],
153-
metadata: {
154-
name: '',
155-
},
156-
targets: [
157-
{
158-
namespace: 'engine',
159-
contextType: 'website',
160-
if: { id: { in: [window.location.hostname] } },
150+
const createEmptyMutation = (accountId: string): Mutation =>
151+
Mutation.create({
152+
id: `${accountId}/mutation/Untitled-${generateRandomHex(6)}`,
153+
apps: [],
154+
metadata: {
155+
name: '',
161156
},
162-
],
163-
})
157+
targets: [
158+
{
159+
namespace: 'engine',
160+
contextType: 'website',
161+
if: { id: { in: [window.location.hostname] } },
162+
},
163+
],
164+
})
164165

165166
export interface Props {
166167
apps: AppMetadata[]

libs/core/src/core.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { PureTreeBuilder } from './tree/pure-tree/pure-tree-builder'
22
import { IAdapter } from './adapters/interface'
33
import { DynamicHtmlAdapter } from './adapters/dynamic-html-adapter'
4-
import { JsonParser } from './parsers/json-parser'
5-
import { BosParser } from './parsers/bos-parser'
4+
import { JsonParser, JsonParserConfig } from './parsers/json-parser'
5+
import { BosParser, BosParserConfig } from './parsers/bos-parser'
66
import { MutableWebParser } from './parsers/mweb-parser'
77
import { LinkParser } from './parsers/link-parser'
8-
import { AdapterType, ParserConfig } from './types'
8+
import { ParserType, ParserConfig } from './types'
99

1010
export class Core {
1111
private _treeBuilder: PureTreeBuilder
@@ -70,31 +70,31 @@ export class Core {
7070
}
7171

7272
switch (config.parserType) {
73-
case AdapterType.Json:
73+
case ParserType.Json:
7474
return new DynamicHtmlAdapter(
7575
document.documentElement,
7676
this._treeBuilder,
7777
config.id,
78-
new JsonParser(config) // ToDo: add try catch because config can be invalid
78+
new JsonParser(config.contexts as JsonParserConfig) // ToDo: add try catch because config can be invalid
7979
)
8080

81-
case AdapterType.Bos:
81+
case ParserType.Bos:
8282
return new DynamicHtmlAdapter(
8383
document.documentElement,
8484
this._treeBuilder,
8585
config.id,
86-
new BosParser(config)
86+
new BosParser(config.contexts as BosParserConfig)
8787
)
8888

89-
case AdapterType.MWeb:
89+
case ParserType.MWeb:
9090
return new DynamicHtmlAdapter(
9191
document.body,
9292
this._treeBuilder,
9393
config.id,
9494
new MutableWebParser()
9595
)
9696

97-
case AdapterType.Link:
97+
case ParserType.Link:
9898
return new DynamicHtmlAdapter(document.body, this._treeBuilder, config.id, new LinkParser())
9999

100100
default:

libs/core/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export { BosParser, BosParserConfig } from './parsers/bos-parser'
66
export { MutableWebParser } from './parsers/mweb-parser'
77
export { PureContextNode } from './tree/pure-tree/pure-context-node'
88
export { IContextNode, ITreeBuilder, ContextLevel } from './tree/types'
9-
export { AdapterType, ParserConfig } from './types'
9+
export { ParserType, ParserConfig } from './types'
1010
export { Subscription } from './event-emitter'
1111
export { InsertionPointWithElement } from './tree/types'
1212
export { isDeepEqual } from './utils'

libs/core/src/parsers/bos-parser.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,19 @@ const CompAttr = 'data-component'
55
const PropsAttr = 'data-props'
66

77
export type BosParserConfig = {
8-
contexts: {
9-
[name: string]: {
10-
component?: string
11-
props?: {
12-
[prop: string]: string
13-
}
14-
insertionPoints?: {
15-
[insPointName: string]: {
16-
component?: string
17-
bosLayoutManager?: string
18-
insertionType?: InsertionType
19-
}
8+
[name: string]: {
9+
component?: string
10+
props?: {
11+
[prop: string]: string
12+
}
13+
insertionPoints?: {
14+
[insPointName: string]: {
15+
component?: string
16+
bosLayoutManager?: string
17+
insertionType?: InsertionType
2018
}
21-
children?: string[]
2219
}
20+
children?: string[]
2321
}
2422
}
2523

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

33-
if (!this.config.contexts['root']) {
34-
this.config.contexts['root'] = {
31+
if (!this.config['root']) {
32+
this.config['root'] = {
3533
props: {
3634
id: 'root',
3735
},
38-
children: Object.keys(this.config.contexts), // ToDo:
36+
children: Object.keys(this.config), // ToDo:
3937
}
4038
}
4139
}
4240

4341
parseContext(element: HTMLElement, contextName: string) {
44-
const contextProperties = this.config.contexts[contextName].props
42+
const contextProperties = this.config[contextName].props
4543
if (!contextProperties) return {}
4644

4745
const parsed: any = {}
@@ -56,14 +54,14 @@ export class BosParser implements IParser {
5654
}
5755

5856
findChildElements(element: HTMLElement, contextName: string) {
59-
const contextConfig = this.config.contexts[contextName]
57+
const contextConfig = this.config[contextName]
6058
if (!contextConfig.children?.length) return []
6159

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

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

6967
const childElements = Array.from(
@@ -83,7 +81,7 @@ export class BosParser implements IParser {
8381
contextName: string,
8482
insertionPoint: string
8583
): HTMLElement | null {
86-
const contextConfig = this.config.contexts[contextName]
84+
const contextConfig = this.config[contextName]
8785
const insPointConfig = contextConfig.insertionPoints?.[insertionPoint]
8886

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

9997
getInsertionPoints(_: HTMLElement, contextName: string): InsertionPoint[] {
100-
const contextConfig = this.config.contexts[contextName]
98+
const contextConfig = this.config[contextName]
10199
if (!contextConfig.insertionPoints) return []
102100

103101
return Object.entries(contextConfig.insertionPoints).map(([name, selectorOrObject]) => ({

libs/core/src/parsers/json-parser.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,21 @@ import { InsertionType } from '../adapters/interface'
22
import { IParser, InsertionPoint } from './interface'
33

44
export type JsonParserConfig = {
5-
contexts: {
6-
[name: string]: {
7-
selector?: string
8-
props?: {
9-
[prop: string]: string
10-
}
11-
insertionPoints?: {
12-
[insPointName: string]:
13-
| string
14-
| {
15-
selector?: string
16-
bosLayoutManager?: string
17-
insertionType?: InsertionType
18-
}
19-
}
20-
children?: string[]
5+
[name: string]: {
6+
selector?: string
7+
props?: {
8+
[prop: string]: string
9+
}
10+
insertionPoints?: {
11+
[insPointName: string]:
12+
| string
13+
| {
14+
selector?: string
15+
bosLayoutManager?: string
16+
insertionType?: InsertionType
17+
}
2118
}
19+
children?: string[]
2220
}
2321
}
2422

@@ -58,7 +56,7 @@ export class JsonParser implements IParser {
5856
}
5957

6058
parseContext(element: HTMLElement, contextName: string) {
61-
const contextProperties = this.config.contexts[contextName].props
59+
const contextProperties = this.config[contextName].props
6260
if (!contextProperties) return {}
6361

6462
const parsed: any = {}
@@ -75,13 +73,13 @@ export class JsonParser implements IParser {
7573
element: HTMLElement,
7674
contextName: string
7775
): { element: HTMLElement; contextName: string }[] {
78-
const contextConfig = this.config.contexts[contextName]
76+
const contextConfig = this.config[contextName]
7977
if (!contextConfig.children?.length) return []
8078

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

8381
for (const childContextName of contextConfig.children ?? []) {
84-
const childConfig = this.config.contexts[childContextName]
82+
const childConfig = this.config[childContextName]
8583
if (!childConfig.selector) continue
8684

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

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

116114
getInsertionPoints(_: HTMLElement, contextName: string): InsertionPoint[] {
117-
const contextConfig = this.config.contexts[contextName]
115+
const contextConfig = this.config[contextName]
118116
if (!contextConfig.insertionPoints) return []
119117

120118
return Object.entries(contextConfig.insertionPoints).map(([name, selectorOrObject]) => ({

libs/core/src/types.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { BosParserConfig } from './parsers/bos-parser'
22
import { JsonParserConfig } from './parsers/json-parser'
33

4-
export enum AdapterType {
4+
export enum ParserType {
55
Bos = 'bos',
66
Microdata = 'microdata',
77
Json = 'json',
88
MWeb = 'mweb',
99
Link = 'link',
10+
Unknown = 'unknown',
1011
}
1112

12-
export type ParserConfig =
13-
| ({ parserType: AdapterType.Json; id: string } & JsonParserConfig)
14-
| ({ parserType: AdapterType.Bos; id: string } & BosParserConfig)
15-
| { parserType: AdapterType.MWeb; id: string }
16-
| { parserType: AdapterType.Link; id: string }
13+
export type ParserConfig = {
14+
id: string
15+
parserType: ParserType
16+
contexts?: JsonParserConfig | BosParserConfig | null
17+
}

libs/engine/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"caching-decorator": "^1.0.3",
4646
"ethereum-blockies-base64": "^1.0.2",
4747
"js-sha256": "^0.11.0",
48-
"json-stringify-deterministic": "^1.0.12"
48+
"json-stringify-deterministic": "^1.0.12",
49+
"reflect-metadata": "^0.2.2"
4950
}
5051
}

0 commit comments

Comments
 (0)