Skip to content

Commit

Permalink
New features :3
Browse files Browse the repository at this point in the history
  • Loading branch information
aquapi committed Jul 13, 2024
1 parent 1dcce4c commit 5b59527
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bit-js/byte",
"version": "2.0.0-alpha.4",
"version": "2.0.0-alpha.5",
"module": "index.js",
"devDependencies": {
"@types/bun": "latest",
Expand Down
12 changes: 5 additions & 7 deletions src/core/server/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class Route<
for (let i = 0, lI = actions.length; i < lI; ++i) {
const list = actions[i];

for (let j = 0, lJ = list.length; j < lJ; ++j) {
for (let j = 0, lJ = list.length; j < lJ; ++j, ++idx) {
const action = list[j];

const fn = action[1];
Expand All @@ -117,7 +117,6 @@ export class Route<
const fnNoContext = fn.length === 0;
noContext &&= fnNoContext;

++idx;
switch (action[0]) {
case 1:
statements.push(`${fnAsync ? 'await ' : ''}${fnKey}(${noContext ? '' : 'c'})`);
Expand Down Expand Up @@ -153,13 +152,13 @@ export class Route<
const fnAsync = isAsync(handler);
hasAsync ||= fnAsync;

// Hold a ref to the context
statements.push(`const r=${fnAsync ? 'await ' : ''}$(${handlerNoContext ? '' : 'c'})`);
// Hold a ref to the response
statements.push(`let r=${fnAsync ? 'await ' : ''}$(${handlerNoContext ? '' : 'c'})`);

for (let i = 0, { length } = defers; i < length; ++i) {
const list = defers[i];

for (let i = list.length - 1; i > -1; --i) {
for (let i = list.length - 1; i > -1; --i, ++idx) {
const fn = list[i];
const fnKey = `f${idx}`;

Expand All @@ -172,8 +171,7 @@ export class Route<
const fnNoContext = fn.length < 2;
noContext &&= fnNoContext;

statements.push(`const c${idx}=${fnAsync ? 'await ' : ''}${fnKey}(${fn.length === 0 ? '' : noContext ? 'r' : 'r,c'});if(c${idx} instanceof Response)return c${idx};`);
++idx;
statements.push(`const c${idx}=${fnAsync ? 'await ' : ''}${fnKey}(${fn.length === 0 ? '' : noContext ? 'r' : 'r,c'});if(c${idx} instanceof Response)r=c${idx}`);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/server/types/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ export type BaseContext = Context<any>;
export type BaseHandler<Path extends string, Set> = (c: Context<Params<Path>> & Set) => any;

export type Fn<T = any> = (c: BaseContext & T) => any;
export type DeferFn<T = any> = (res: Response, c: BaseContext & T) => any;
export type DeferFn<T = any> = (res: any, c: BaseContext & T) => any;
4 changes: 2 additions & 2 deletions tests/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export const apiWithCsrf = new Byte()
// Defers
export const apiWithDefers = new Byte()
.use((ctx) => console.time(ctx.path))
.defer((ctx) => {
.defer((res, ctx) => {
// You should change the response here
console.log(ctx.res.ok);
console.log(res.ok);
console.timeEnd(ctx.path);
})
.get('/', send.body('Hi'));
Expand Down

0 comments on commit 5b59527

Please sign in to comment.