Skip to content

Commit

Permalink
feat: support iterator lists
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 19, 2024
1 parent dc4f0e2 commit bbd512a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 14 deletions.
1 change: 0 additions & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export default unjs({
rules: {
"unicorn/prefer-code-point": 0,
"unicorn/no-null": 0,
"unicorn/no-array-for-each": 0,
"unicorn/prefer-spread": 0,
},
});
23 changes: 14 additions & 9 deletions src/compiler/generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1089,13 +1089,13 @@ export function generateMethodStructs(
ctx: CodeGeneratorFileContext,
node: s.Node,
): void {
node.interface.methods.forEach((method) => {
for (const method of node.interface.methods) {
const paramNode = lookupNode(ctx, method.paramStructType);
const resultNode = lookupNode(ctx, method.resultStructType);
generateNode(ctx, paramNode);
generateNode(ctx, resultNode);
generateResultPromise(ctx, resultNode);
});
}
}

export function generateServer(
Expand Down Expand Up @@ -1171,7 +1171,9 @@ export function generateServer(
// Generate server constructor
{
const serverMethods: ts.Expression[] = [];
node.interface.methods.forEach((method, index) => {

let index = 0;
for (const method of node.interface.methods) {
serverMethods.push(
f.createObjectLiteralExpression(
[
Expand All @@ -1181,7 +1183,7 @@ export function generateServer(
f.createIdentifier(clientName),
"methods",
),
index,
index++,
),
),
f.createPropertyAssignment(
Expand All @@ -1195,7 +1197,7 @@ export function generateServer(
true, // multiline
),
);
});
}

members.push(
f.createConstructorDeclaration(
Expand Down Expand Up @@ -1347,7 +1349,8 @@ export function generateClient(
),
);

node.interface.methods.forEach((method, index) => {
let index = 0;
for (const method of node.interface.methods) {
generateClientMethod(
ctx,
node,
Expand All @@ -1356,9 +1359,9 @@ export function generateClient(
methodDefs,
methodDefTypes,
method,
index,
index++,
);
});
}

ctx.statements.push(
f.createClassDeclaration([EXPORT], clientName, undefined, [], members),
Expand Down Expand Up @@ -1522,7 +1525,9 @@ export function generateResultPromise(
}
};

fields.forEach((field) => generatePromiseFieldMethod(field));
for (const field of fields) {
generatePromiseFieldMethod(field);
}

{
members.push(
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/conn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ export class Conn {
populateMessageCapTable(payload: Payload): void {
const msg = payload.segment.message;
const ctab = payload.capTable;
ctab.forEach((desc) => {
for (const desc of ctab) {
switch (desc.which()) {
case CapDescriptor.NONE: {
msg.addCap(null);
Expand Down Expand Up @@ -390,7 +390,7 @@ export class Conn {
throw new Error(format(RPC_UNKNOWN_CAP_DESCRIPTOR, desc.which()));
}
}
});
}
}

addImport(id: number): Client {
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/promised-answer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function promisedAnswerOpsToTransform(
list: List<PromisedAnswer_Op>,
): PipelineOp[] {
const transform: PipelineOp[] = [];
list.forEach((op) => {
for (const op of list) {
switch (op.which()) {
case PromisedAnswer_Op.GET_POINTER_FIELD: {
transform.push(<PipelineOp>{
Expand All @@ -33,6 +33,6 @@ export function promisedAnswerOpsToTransform(
default:
// nothing
}
});
}
return transform;
}
13 changes: 13 additions & 0 deletions src/serialization/pointers/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,19 @@ export class List<T> extends Pointer {
for (let i = 0; i < length; i++) callbackfn(this.get(i), i);
}

[Symbol.iterator](): Iterator<T> {
const length = this.getLength();
let i = 0;
return {
next: () => {
if (i < length) {
return { value: this.get(i++), done: false };
}
return { value: undefined, done: true };
},
};
}

get(_index: number): T {
return get(_index, this);
}
Expand Down

0 comments on commit bbd512a

Please sign in to comment.