Skip to content

Commit

Permalink
Added babel parser cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ackava committed Jul 11, 2023
1 parent 9ee80b4 commit 42982b8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/common/cache/TimedCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ export default class TimedCache<TKey = any, T = any> {
this.map.delete(key);
}

getOrCreate(key: TKey, factory: (k: TKey) => T, ttl: number = 15000) {
getOrCreate<TP>(key: TKey, p1: TP, factory: (k: TKey,p: TP) => T, ttl: number = 15000) {
let item = this.map.get(key);
if (!item) {
item = { value: factory(key), ttl, expire: Date.now() + ttl };
item = { value: factory(key, p1), ttl, expire: Date.now() + ttl };
this.map.set(key, item);
} else {
item.expire = Date.now() + ttl;
Expand Down
5 changes: 1 addition & 4 deletions src/model/EntitySource.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import type EntityContext from "./EntityContext.js";
import type EntityType from "../entity-query/EntityType.js";
import type { IEntityQuery, IFilterExpression } from "./IFilterWithParameter.js";
import { Expression, ExpressionAs, QuotedLiteral, SelectStatement } from "../query/ast/Expressions.js";
import { Expression } from "../query/ast/Expressions.js";
import EntityQuery from "./EntityQuery.js";
import TimedCache from "../common/cache/TimedCache.js";
import { contextSymbol, modelSymbol } from "../common/symbols/symbols.js";

const modelCache = new TimedCache<any, SelectStatement>();

export class EntitySource<T = any> {

get [modelSymbol]() {
Expand Down
16 changes: 11 additions & 5 deletions src/query/parser/ArrowToExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@ import { parseExpression } from "@babel/parser";
import { ArrowFunctionExpression, BinaryExpression, CallExpression, CoalesceExpression, ConditionalExpression, Constant, Expression, ExpressionAs, Identifier, MemberExpression, NewObjectExpression, NullExpression, NumberLiteral, ParameterExpression, QuotedLiteral, StringLiteral, TemplateLiteral } from "../ast/Expressions.js";
import { BabelVisitor } from "./BabelVisitor.js";
import * as bpe from "@babel/types";
import EntityType from "../../entity-query/EntityType.js";
import { EntitySource } from "../../model/EntitySource.js";
import Restructure from "./Restructure.js";
import { NotSupportedError } from "./NotSupportedError.js";
import TimedCache from "../../common/cache/TimedCache.js";

type IQueryFragment = string | { name?: string, value?: any };
type IQueryFragments = IQueryFragment[];

const parsedCache = new TimedCache<string, bpe.Node>();

export default class ArrowToExpression extends BabelVisitor<Expression> {

public static transform(fx: (p: any) => (x: any) => any, target?: ParameterExpression) {
const key = fx.toString();
const node = parsedCache.getOrCreate(key, fx, (k, f) => {
const rs = new Restructure();
return rs.visit(parseExpression(f.toString()));
});
return this.transformUncached(node, target);
}

const rs = new Restructure();
private static transformUncached(node: bpe.Node, target?: ParameterExpression) {

const node = rs.visit(parseExpression(fx.toString()));
if (node.type !== "ArrowFunctionExpression") {
throw new Error("Expecting an arrow function");
}
Expand Down

0 comments on commit 42982b8

Please sign in to comment.