Skip to content

Commit

Permalink
Stagger entity creation
Browse files Browse the repository at this point in the history
  • Loading branch information
yusijs committed Feb 20, 2024
1 parent 19944c9 commit d0e605d
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions entities/create-entities-mqtt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export const createEntitiesMqtt = async () => {
all: true,
},
});
newFeatures.forEach((d) => {
for await (const { value: d, done } of staggerEntities(newFeatures)) {
if (done) break;
const homelyDevice = d.device!.toJSON();
const device = {
ids: [homelyDevice.id],
Expand Down Expand Up @@ -71,5 +72,28 @@ export const createEntitiesMqtt = async () => {
}
}
);
});
}
};

type ReturnStagger<T> =
| {
value: T;
done: false;
}
| {
value: null;
done: true;
};

async function* staggerEntities<T>(
entities: Array<T>
): AsyncIterable<ReturnStagger<T>> {
for (const entity of entities) {
yield { value: entity, done: false };
await new Promise<void>((r) => setTimeout(() => r(), 500));
}
yield {
value: null,
done: true,
};
}

0 comments on commit d0e605d

Please sign in to comment.