Skip to content

Commit

Permalink
Combine bound "use cache" closure args into a single parameter (#72587
Browse files Browse the repository at this point in the history
)

This is a pure refactoring without any behavioral changes. It will allow us to more easily incorporate the presence of the bound args param into the server reference information byte.
  • Loading branch information
unstubbable authored Nov 12, 2024
1 parent b1f1328 commit 883c337
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 66 deletions.
96 changes: 34 additions & 62 deletions crates/next-custom-transforms/src/transforms/server_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,26 @@ impl<C: Comments> ServerActions<C> {
id
}

fn create_bound_action_args_array_pat(&mut self, arg_len: usize) -> Pat {
Pat::Array(ArrayPat {
span: DUMMY_SP,
elems: (0..arg_len)
.map(|i| {
Some(Pat::Ident(
Ident::new(
format!("$$ACTION_ARG_{i}").into(),
DUMMY_SP,
self.private_ctxt,
)
.into(),
))
})
.collect(),
optional: false,
type_ann: None,
})
}

// Check if the function or arrow function is an action or cache function
fn get_body_info(&mut self, maybe_body: Option<&mut BlockStmt>) -> (bool, Option<String>) {
let mut is_action_fn = false;
Expand Down Expand Up @@ -343,29 +363,13 @@ impl<C: Comments> ServerActions<C> {
// Also prepend the decryption decl into the body.
// var [arg1, arg2, arg3] = await decryptActionBoundArgs(actionId,
// $$ACTION_CLOSURE_BOUND)
let mut pats = vec![];
for i in 0..ids_from_closure.len() {
pats.push(Some(Pat::Ident(
Ident::new(
format!("$$ACTION_ARG_{i}").into(),
DUMMY_SP,
self.private_ctxt,
)
.into(),
)));
}
let decryption_decl = VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Var,
declare: false,
decls: vec![VarDeclarator {
span: DUMMY_SP,
name: Pat::Array(ArrayPat {
span: DUMMY_SP,
elems: pats,
optional: false,
type_ann: None,
}),
name: self.create_bound_action_args_array_pat(ids_from_closure.len()),
init: Some(Box::new(Expr::Await(AwaitExpr {
span: DUMMY_SP,
arg: Box::new(Expr::Call(CallExpr {
Expand Down Expand Up @@ -509,28 +513,12 @@ impl<C: Comments> ServerActions<C> {
// Also prepend the decryption decl into the body.
// var [arg1, arg2, arg3] = await decryptActionBoundArgs(actionId,
// $$ACTION_CLOSURE_BOUND)
let mut pats = vec![];
for i in 0..ids_from_closure.len() {
pats.push(Some(Pat::Ident(
Ident::new(
format!("$$ACTION_ARG_{i}").into(),
DUMMY_SP,
self.private_ctxt,
)
.into(),
)));
}
let decryption_decl = VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Var,
decls: vec![VarDeclarator {
span: DUMMY_SP,
name: Pat::Array(ArrayPat {
span: DUMMY_SP,
elems: pats,
optional: false,
type_ann: None,
}),
name: self.create_bound_action_args_array_pat(ids_from_closure.len()),
init: Some(Box::new(Expr::Await(AwaitExpr {
span: DUMMY_SP,
arg: Box::new(Expr::Call(CallExpr {
Expand Down Expand Up @@ -620,22 +608,15 @@ impl<C: Comments> ServerActions<C> {

let mut new_params: Vec<Param> = vec![];

// Add the collected closure variables as the first parameters to the
// Add the collected closure variables as the first parameter to the
// function. They are unencrypted and passed into this function by the
// cache wrapper.
if !ids_from_closure.is_empty() {
for i in 0..ids_from_closure.len() {
new_params.push(Param {
span: DUMMY_SP,
decorators: vec![],
pat: Ident::new(
format!("$$ACTION_ARG_{i}").into(),
DUMMY_SP,
self.private_ctxt,
)
.into(),
});
}
new_params.push(Param {
span: DUMMY_SP,
decorators: vec![],
pat: self.create_bound_action_args_array_pat(ids_from_closure.len()),
});
}

for p in arrow.params.iter() {
Expand Down Expand Up @@ -773,24 +754,15 @@ impl<C: Comments> ServerActions<C> {

let mut new_params: Vec<Param> = vec![];

// Add the collected closure variables as the first parameters to the
// Add the collected closure variables as the first parameter to the
// function. They are unencrypted and passed into this function by the
// cache wrapper.
if !ids_from_closure.is_empty() {
for i in 0..ids_from_closure.len() {
new_params.push(Param {
span: DUMMY_SP,
decorators: vec![],
pat: Pat::Ident(
Ident::new(
format!("$$ACTION_ARG_{i}").into(),
DUMMY_SP,
self.private_ctxt,
)
.into(),
),
});
}
new_params.push(Param {
span: DUMMY_SP,
decorators: vec![],
pat: self.create_bound_action_args_array_pat(ids_from_closure.len()),
});
}

for p in function.params.iter() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* __next_internal_action_entry_do_not_use__ {"803128060c414d59f8552e4788b846c0d2b7f74743":"$$RSC_SERVER_CACHE_0"} */ import { registerServerReference } from "private-next-rsc-server-reference";
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption";
import { cache as $$cache__ } from "private-next-rsc-cache-wrapper";
export var $$RSC_SERVER_CACHE_0 = $$cache__("default", "803128060c414d59f8552e4788b846c0d2b7f74743", 2, async function fn($$ACTION_ARG_0, $$ACTION_ARG_1) {
export var $$RSC_SERVER_CACHE_0 = $$cache__("default", "803128060c414d59f8552e4788b846c0d2b7f74743", 2, async function fn([$$ACTION_ARG_0, $$ACTION_ARG_1]) {
console.log($$ACTION_ARG_0);
return {
foo: $$ACTION_ARG_1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* __next_internal_action_entry_do_not_use__ {"401c36b06e398c97abe5d5d7ae8c672bfddf4e1b91":"$$RSC_SERVER_ACTION_2","c03128060c414d59f8552e4788b846c0d2b7f74743":"$$RSC_SERVER_CACHE_0"} */ import { registerServerReference } from "private-next-rsc-server-reference";
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption";
import { cache as $$cache__ } from "private-next-rsc-cache-wrapper";
export var $$RSC_SERVER_CACHE_0 = $$cache__("default", "c03128060c414d59f8552e4788b846c0d2b7f74743", 2, async function cache($$ACTION_ARG_0, $$ACTION_ARG_1, e) {
export var $$RSC_SERVER_CACHE_0 = $$cache__("default", "c03128060c414d59f8552e4788b846c0d2b7f74743", 2, async function cache([$$ACTION_ARG_0, $$ACTION_ARG_1], e) {
const f = $$ACTION_ARG_0 + e;
return [
f,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* __next_internal_action_entry_do_not_use__ {"803128060c414d59f8552e4788b846c0d2b7f74743":"$$RSC_SERVER_CACHE_0"} */ import { registerServerReference } from "private-next-rsc-server-reference";
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption";
import { cache as $$cache__ } from "private-next-rsc-cache-wrapper";
export var $$RSC_SERVER_CACHE_0 = $$cache__("default", "803128060c414d59f8552e4788b846c0d2b7f74743", 2, async function($$ACTION_ARG_0, $$ACTION_ARG_1) {
export var $$RSC_SERVER_CACHE_0 = $$cache__("default", "803128060c414d59f8552e4788b846c0d2b7f74743", 2, async function([$$ACTION_ARG_0, $$ACTION_ARG_1]) {
console.log($$ACTION_ARG_0);
return {
foo: $$ACTION_ARG_1
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/use-cache/use-cache-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ export function cache(
)
}

args.unshift(...boundArgs)
args.unshift(boundArgs)
}

const temporaryReferences = createClientTemporaryReferenceSet()
Expand Down

0 comments on commit 883c337

Please sign in to comment.