Skip to content

Commit 1a170fd

Browse files
committed
🎉 feat: compile thing
1 parent 54bf6ac commit 1a170fd

File tree

4 files changed

+35
-44
lines changed

4 files changed

+35
-44
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Improvement:
33
- ValidationError: add `messageValue` as an alias of `errorValue`
44
- ValidationError.detail now accept optional 2nd parameter `allowUnsafeValidatorDetails`
55
- macro: add `introspect`
6+
- prevent redundant route compilation
67

78
Bug fix:
89
- [#1537](https://github.com/elysiajs/elysia/issues/1537) websocket: ping/pong not being called

example/a.ts

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,19 @@
1-
import { Elysia } from '../src'
1+
import { InternalSymbolName } from 'typescript'
2+
import { Elysia, t } from '../src'
23
import { req } from '../test/utils'
34

4-
const app = new Elysia()
5-
.macro('a', {
6-
introspect(option) {
7-
console.log('a', option)
8-
},
9-
beforeHandle() {
10-
console.log('before handle a')
11-
}
12-
})
13-
.macro({
14-
b: {
15-
introspect(option) {
16-
console.log('b', option)
17-
},
18-
beforeHandle() {
19-
console.log('before handle a')
20-
}
21-
}
22-
})
23-
.get('/', () => 'hello world', {
24-
a: true,
25-
b: true,
26-
detail: {
27-
description: 'a'
28-
}
29-
})
5+
const app = new Elysia().get('/', () => 'ok').compile()
6+
for (const route of app.routes) route.compile()
307

31-
app.handle(req('/'))
8+
console.log(app.fetch.toString())
9+
console.log(app.routes[0].compile().toString())
10+
11+
// Bun.sleepSync(7)
12+
// console.log('Slept')
13+
14+
const res = app
15+
.handle(req('/'))
16+
.then((x) => x.text())
17+
.then(console.log)
18+
19+
// process.exit(0)

src/adapter/web-standard/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,8 @@ export const WebStandardAdapter: ElysiaAdapter = {
9393
fnLiteral +=
9494
`const u=r.url,` +
9595
`s=u.indexOf('/',${standardHostname ? 11 : 7}),` +
96-
`qi=u.indexOf('?',s+1)\n` +
97-
`let p\n` +
98-
`if(qi===-1)p=u.substring(s)\n` +
99-
`else p=u.substring(s, qi)\n`
96+
`qi=u.indexOf('?',s+1),` +
97+
`p=u.substring(s,qi===-1?undefined:qi)\n`
10098

10199
if (hasTrace) fnLiteral += `const id=randomId()\n`
102100

src/index.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -909,11 +909,8 @@ export default class Elysia<
909909

910910
addResponsePath(path)
911911

912-
let _compiled: ComposedHandler
913912
const compile = () => {
914-
if (_compiled) return _compiled
915-
916-
return (_compiled = composeHandler({
913+
const compiled = composeHandler({
917914
app: this,
918915
path,
919916
method,
@@ -926,7 +923,12 @@ export default class Elysia<
926923
: handle,
927924
allowMeta,
928925
inference: this.inference
929-
}))
926+
})
927+
928+
if (this.router.history[index])
929+
this.router.history[index].composed = compiled
930+
931+
return compiled
930932
}
931933

932934
let oldIndex: number | undefined
@@ -941,22 +943,22 @@ export default class Elysia<
941943
else this.routeTree[`${method}_${path}`] = this.router.history.length
942944

943945
const index = oldIndex ?? this.router.history.length
946+
const route = this.router.history
944947

945948
const mainHandler = shouldPrecompile
946949
? compile()
947950
: (ctx: Context) =>
948-
(
949-
(this.router.history[index].composed =
950-
compile!()) as ComposedHandler
951-
)(ctx)
951+
((route[index].composed = compile!()) as ComposedHandler)(
952+
ctx
953+
)
952954

953955
if (oldIndex !== undefined)
954956
this.router.history[oldIndex] = Object.assign(
955957
{
956958
method,
957959
path,
958960
composed: mainHandler,
959-
compile: compile!,
961+
compile,
960962
handler: handle,
961963
hooks
962964
},
@@ -976,7 +978,7 @@ export default class Elysia<
976978
method,
977979
path,
978980
composed: mainHandler,
979-
compile: compile!,
981+
compile,
980982
handler: handle,
981983
hooks
982984
},
@@ -987,7 +989,9 @@ export default class Elysia<
987989
)
988990

989991
const handler = {
990-
handler: shouldPrecompile ? mainHandler : undefined,
992+
handler: shouldPrecompile
993+
? (route[index].composed as ComposedHandler)
994+
: undefined,
991995
compile() {
992996
return (this.handler = compile!())
993997
}

0 commit comments

Comments
 (0)