Skip to content

Commit e8b4418

Browse files
refactor: improve " $$flush" fn
1 parent eacf012 commit e8b4418

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

src/Entity.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,10 @@ export function Entity<$$Schema extends DefaultSchema>(
291291
}
292292

293293
" $$flush"() {
294+
const queuedEvents = [...this[" $$queuedEvents"]];
294295
this[" $$queuedEvents"] = [];
296+
297+
return queuedEvents;
295298
}
296299

297300
" $$createEvent"<EventName extends InferEventNameFromSchema<$$Schema>>(

src/createRepository.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export function createRepository<
168168
const _entity = entity as Entity<$$Schema>;
169169

170170
// 1. copy queued events
171-
const queuedEvents = [..._entity[" $$queuedEvents"]];
171+
const queuedEvents = _entity[" $$flush"]();
172172

173173
// 2. commit events to adapter
174174
await args.adapter.commitEvents({
@@ -178,10 +178,7 @@ export function createRepository<
178178
state: _entity.state,
179179
});
180180

181-
// 3. flush queued events
182-
_entity[" $$flush"]();
183-
184-
// 4. run plugins in parallel (only if there are events)
181+
// 3. run plugins in parallel (only if there are events)
185182
if (args.plugins && args.plugins.length > 0 && queuedEvents.length > 0) {
186183
const pluginResults = await Promise.allSettled(
187184
args.plugins.map((plugin) =>

src/types/Entity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export interface Entity<$$Schema> {
110110
) => void;
111111

112112
/** @internal */
113-
" $$flush": () => void;
113+
" $$flush": () => InferEventFromSchema<$$Schema>[];
114114

115115
/** @internal */
116116
" $$createEvent": <EventName extends InferEventNameFromSchema<$$Schema>>(

0 commit comments

Comments
 (0)