Skip to content

Commit

Permalink
0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
bahrus committed Dec 10, 2023
1 parent 34aaf76 commit 2dfe502
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ Advantages of using script element -- less issues with characters that cause pro

Since the expression starts with open parenthesis, wrapping is more lightweight. Just adds export const default.

## Example 1g [TODO]
## Example 1g

Specify export symbol

Expand Down
4 changes: 2 additions & 2 deletions be-computed.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class BeComputed extends BE {
async hydrate(self) {
const { fromStatements, enhancedElement } = self;
for (const fromStatement of fromStatements) {
let { attrContainingExpression, args, previousElementScriptElement, onloadOrPreviousElementScriptElement } = fromStatement;
let { attrContainingExpression, args, previousElementScriptElement, onloadOrPreviousElementScriptElement, importName } = fromStatement;
if (args === undefined)
throw 'NI';
let scriptText = null;
Expand Down Expand Up @@ -66,7 +66,7 @@ export class BeComputed extends BE {
}
const rewritten = rewrite(scriptText, args.map(x => x.remoteProp));
const parsedJavaScript = await parse(rewritten);
const expr = parsedJavaScript['expr'];
const expr = parsedJavaScript[importName || 'expr'];
this.#computationObservers.push(new ComputationObserver(expr, fromStatement, args, enhancedElement, self));
}
return {
Expand Down
7 changes: 5 additions & 2 deletions be-computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ export class BeComputed extends BE<AP, Actions> implements Actions{
async hydrate(self: this){
const {fromStatements, enhancedElement} = self;
for(const fromStatement of fromStatements!){
let {attrContainingExpression, args, previousElementScriptElement, onloadOrPreviousElementScriptElement} = fromStatement;
let {
attrContainingExpression, args, previousElementScriptElement,
onloadOrPreviousElementScriptElement, importName
} = fromStatement;
if(args === undefined) throw 'NI';
let scriptText: string | null = null;
if(attrContainingExpression === undefined && !previousElementScriptElement){
Expand Down Expand Up @@ -73,7 +76,7 @@ export class BeComputed extends BE<AP, Actions> implements Actions{

const rewritten = rewrite(scriptText!, args.map(x => x.remoteProp!));
const parsedJavaScript = await parse(rewritten);
const expr = parsedJavaScript['expr'];
const expr = parsedJavaScript[importName || 'expr'];
this.#computationObservers.push(
new ComputationObserver(expr, fromStatement, args, enhancedElement, self
));
Expand Down
7 changes: 5 additions & 2 deletions rewrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@
// }
export function rewrite(script, names) {
const trimmedScript = script.trim();
if (trimmedScript.includes('export const')) {
return trimmedScript;
}
if (trimmedScript[0] === '(') {
const newScript2 = `export const expr = async ${trimmedScript}`;
return newScript2;
}
const newScript = `
const newScript3 = `
export const expr = async ({${names.join(', ')}}) => {
return ${trimmedScript};
}
`;
return newScript;
return newScript3;
}
7 changes: 5 additions & 2 deletions rewrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ import {AP} from './types';

export function rewrite(script: string, names: Array<string>): string{
const trimmedScript = script.trim();
if(trimmedScript.includes('export const')){
return trimmedScript;
}
if(trimmedScript[0] === '('){
const newScript2 = `export const expr = async ${trimmedScript}`;
return newScript2;
}
const newScript = `
const newScript3 = `
export const expr = async ({${names.join(', ')}}) => {
return ${trimmedScript};
}
`;
return newScript;
return newScript3;
}

0 comments on commit 2dfe502

Please sign in to comment.