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

refactor: runtime structure #63

Merged
merged 28 commits into from
May 11, 2022
Merged
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
3482953
refactor: runtime structure
SoloJiang Apr 25, 2022
42aa51b
chore: lint
SoloJiang Apr 25, 2022
1949423
feat: support element template diff
SoloJiang Apr 26, 2022
b38fee5
chore: lint
SoloJiang Apr 26, 2022
9c687a5
feat(compiler): update structure in get template method (#62)
ChrisCindy Apr 26, 2022
7c7a6af
chore: resolve conflict
SoloJiang Apr 26, 2022
271da66
feat: html method support new structure
SoloJiang Apr 26, 2022
5dae056
feat: html method support new structure
SoloJiang Apr 26, 2022
57a434b
fix(compiler): inject wrong value in runtime compiler
ChrisCindy Apr 26, 2022
739ce9e
refactor: reactive node
SoloJiang May 6, 2022
83eb345
chore: test case
SoloJiang May 6, 2022
00d3192
fix(compiler): replace values with templateData in template result
ChrisCindy May 6, 2022
c30711b
chore: test case
SoloJiang May 6, 2022
3c04a3d
chore: add test case
SoloJiang May 6, 2022
3d77430
refactor: base render
SoloJiang May 7, 2022
a7a4316
chore: revert code
SoloJiang May 7, 2022
086d62f
chore: test case
SoloJiang May 9, 2022
32bec4f
chore: resolve conflict
SoloJiang May 9, 2022
32cabcd
chore: lint
SoloJiang May 9, 2022
24bb23b
chore: ts type
SoloJiang May 9, 2022
7d78381
chore: test case
SoloJiang May 9, 2022
56614ee
chore: add comment
SoloJiang May 9, 2022
39398dc
chore: remove useless comment
SoloJiang May 9, 2022
e7e4c36
chore: add test case
SoloJiang May 9, 2022
13cea0b
chore: optimize code
SoloJiang May 9, 2022
b2be016
chore: optimize code
SoloJiang May 10, 2022
44f9f5b
chore: link
SoloJiang May 10, 2022
87202f1
chore: optimize code
SoloJiang May 10, 2022
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
Prev Previous commit
Next Next commit
feat: html method support new structure
  • Loading branch information
SoloJiang committed Apr 26, 2022
commit 271da66b7fb40b2af64527740716c854d13085ef
32 changes: 2 additions & 30 deletions examples/list/main.js
Original file line number Diff line number Diff line change
@@ -3,41 +3,13 @@ import { reactive, customElement, html } from 'pwc';
@customElement('custom-element')
class CustomElement extends HTMLElement {
@reactive
accessor #title;
accessor #title = 'default title';

onClick() {
this.#title = '123';
}

get template() {
return {
templateString: '<!--?pwc_p--><div>map: <!--?pwc_t--><!--?pwc_t--></div>',
templateData: [
[
{
name: 'onclick',
handler: this.onClick
},
],
{
templateString: 'title is <!--?pwc_t-->',
templateData: [this.#title],
template: true
},
[
{
templateString: 'title is <!--?pwc_t-->',
templateData: [this.#title],
template: true
},
{
templateString: 'title is <!--?pwc_t-->',
templateData: [this.#title],
template: true
}
]
],
template: true
};
return html`<div @click=${this.onClick}>title is ${this.#title}</div>`;
}
}
4 changes: 2 additions & 2 deletions packages/pwc-compiler/src/compileScript.ts
Original file line number Diff line number Diff line change
@@ -17,10 +17,10 @@ export function compileScript(descriptor: SFCDescriptor): SFCScriptCompileResult
// With template block
const hasTemplate = !!descriptor.template;
if (hasTemplate) {
const { templateString, values } = compileTemplate(descriptor);
const { templateString, templateData } = compileTemplate(descriptor);
transformScript(ast, {
templateString,
values,
templateData,
});
} else {
transformScript(ast, { templateString: null });
6 changes: 3 additions & 3 deletions packages/pwc-compiler/src/compileTemplate.ts
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ export type ValueDescriptor = Array<string | Array<AttributeDescriptor>>;

export interface CompileTemplateResult {
templateString?: string;
values?: ValueDescriptor;
templateData?: ValueDescriptor;
}

const TEXT_COMMENT_DATA = '?pwc_t';
@@ -151,7 +151,7 @@ export function compileTemplate(descriptor: SFCDescriptor): CompileTemplateResul

export function compileTemplateAST(ast: ElementNode): CompileTemplateResult {
const nodes = dfs(ast);
const values = transformTemplateAst(nodes);
const templateData = transformTemplateAst(nodes);
const templateString = genTemplateString(ast);
return { templateString, values };
return { templateString, templateData };
}
19 changes: 12 additions & 7 deletions packages/pwc-compiler/src/compileTemplateInRuntime.ts
Original file line number Diff line number Diff line change
@@ -3,8 +3,9 @@ import { compileTemplateAST } from './compileTemplate';
import type { CompileTemplateResult } from './compileTemplate';

interface CompileTemplateInRuntimeResult {
[0]: CompileTemplateResult['templateString'];
[1]: CompileTemplateResult['values'];
templateString: CompileTemplateResult['templateString'];
templateData: CompileTemplateResult['templateData'];
template: true;
}

/**
@@ -13,7 +14,7 @@ interface CompileTemplateInRuntimeResult {
* originalValues: [[{name: 'class', value: ''}, {name: 'onclick', value: '', capture: false}], '']
* result: [[{name: 'class', value: 'className'}, {name: 'onclick', value: function, capture: false}], 'title']
*/
function injectRuntimeValue(bindings, originalValues): CompileTemplateResult['values'] {
function injectRuntimeValue(bindings, originalValues): CompileTemplateResult['templateData'] {
let bindingIndex = 0;
return originalValues.map(val => {
if (typeof val === 'string') {
@@ -24,7 +25,7 @@ function injectRuntimeValue(bindings, originalValues): CompileTemplateResult['va
});
return val;
}
}) as CompileTemplateResult['values'];
}) as CompileTemplateResult['templateData'];
}

export function compileTemplateInRuntime(strings: Array<string>, ...runtimeValues): CompileTemplateInRuntimeResult {
@@ -36,9 +37,13 @@ export function compileTemplateInRuntime(strings: Array<string>, ...runtimeValue
} catch (err) {
throw new Error(`template compile error: ${err}`);
}
const { templateString, values: originalValues } = compileTemplateAST(dom);
const { templateString, templateData: originalTemplateData } = compileTemplateAST(dom);
// bindings in originalValues is empty and actual values is in runtimeValues
// injectRuntimeValue will inject corresponding runtime values into originalValues
const values = injectRuntimeValue(runtimeValues, originalValues);
return [templateString, values];
const templateData = injectRuntimeValue(runtimeValues, originalTemplateData);
return {
templateString,
templateData,
template: true,
};
}
8 changes: 4 additions & 4 deletions packages/pwc-compiler/src/transform/index.ts
Original file line number Diff line number Diff line change
@@ -9,19 +9,19 @@ import type { CompileTemplateResult } from '../compileTemplate';

export interface TransformScriptOptions {
templateString: string | null;
values?: CompileTemplateResult['values'];
templateData?: CompileTemplateResult['templateData'];
}

export default function transformScript(ast: File, {
templateString,
values,
templateData,
}: TransformScriptOptions): void {
autoAddCustomElementDecorator(ast);
let shouldImportReactive = false;
if (templateString !== null) {
shouldImportReactive = autoAddReactiveDecorator(ast, values);
shouldImportReactive = autoAddReactiveDecorator(ast, templateData);
autoAddAccessor(ast);
genGetTemplateMethod(ast, { templateString, values });
genGetTemplateMethod(ast, { templateString, templateData });
}
autoInjectImportPWC(ast, shouldImportReactive);
}
4 changes: 2 additions & 2 deletions packages/pwc/src/elements/reactiveElementFactory.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { ElementTemplate, PWCElement, PWCElementTemplate, ReflectProperties, RootElement } from '../type';
import { Reactive } from '../reactivity/reactive';
import { ReactiveNode } from './reactiveNode';
import type { ReactiveNode } from './reactiveNode';
import { generateUid } from '../utils';
import { enqueueJob, nextTick } from './sheduler';
import { initRenderTemplate } from './initRenderTemplate';
import { getTemplateInfo } from './getTemplateInfo';
import { validateElementTemplate } from './validateElementTemplate';
import { updateView } from './updateView';
import { updateView } from './reactiveNode';

export default (Definition: PWCElement) => {
return class extends Definition {
32 changes: 30 additions & 2 deletions packages/pwc/src/elements/reactiveNode.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { commitAttributes } from './commitAttributes';
import type { Attributes, PWCElementTemplate, PWCElement, TemplateNodeValue } from '../type';
import { isTemplate } from '../utils';
import { updateView } from './updateView';
import { isTemplate, shallowEqual } from '../utils';

export interface ReactiveNode {
commitValue: (value: any) => void;
@@ -66,3 +65,32 @@ export class TemplateNode implements ReactiveNode {
}
}
}

export function updateView(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个也是

oldElementTemplate: PWCElementTemplate,
newElementTemplate: PWCElementTemplate,
reactiveNodes: ReactiveNode[],
) {
const {
templateString: oldTemplateString,
templateData: oldTemplateData,
} = oldElementTemplate;
const {
templateString,
templateData,
} = newElementTemplate;
// While template strings is constant with prev ones,
// it should just update node values and attributes
if (oldTemplateString === templateString) {
for (let index = 0; index < oldTemplateData.length; index++) {
const reactiveNode = reactiveNodes[index];
// Avoid html fragment effect
if (reactiveNode instanceof TemplateNode) {
// TODO more diff
reactiveNode.commitValue([oldTemplateData[index], templateData[index]] as TemplateNodeValue);
} else if (!shallowEqual(oldTemplateData[index], templateData[index])) {
reactiveNode.commitValue(templateData[index]);
}
}
}
}
32 changes: 0 additions & 32 deletions packages/pwc/src/elements/updateView.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/pwc/src/elements/validateElementTemplate.ts
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ import { ElementTemplate } from '../type';

export function validateElementTemplate(elementTemplate: ElementTemplate) {
if (typeof elementTemplate === 'string') return;
console.log(elementTemplate)

if (elementTemplate.template !== true) {
throwError('Invalid template, it should return type field.');
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.