-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[v5] Remove batching #2869
[v5] Remove batching #2869
Changes from 1 commit
145df4a
db2473a
9437c3d
fbfc4ce
293d0a8
c257b1a
b546eb6
d98b6cc
3c51e60
1cbe559
34f611f
8f3d717
37547c3
00f1d74
e530b5b
9aac8e9
8e539e9
ea41473
0af90a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- xstate@5.19.2
- xstate@5.19.1
- xstate@5.19.0
- xstate@5.18.2
- xstate@5.18.1
- xstate@5.18.0
- xstate@5.17.4
- xstate@5.17.3
- xstate@5.17.2
- xstate@5.17.1
- xstate@5.17.0
- xstate@5.16.0
- xstate@5.15.0
- xstate@5.14.0
- xstate@5.13.2
- xstate@5.13.1
- xstate@5.13.0
- xstate@5.12.0
- xstate@5.11.0
- xstate@5.10.0
- xstate@5.9.1
- xstate@5.9.0
- xstate@5.8.2
- xstate@5.8.1
- xstate@5.8.0
- xstate@5.7.1
- xstate@5.7.0
- xstate@5.6.2
- xstate@5.6.1
- xstate@5.6.0
- xstate@5.5.2
- xstate@5.5.1
- xstate@5.5.0
- xstate@5.4.1
- xstate@5.4.0
- xstate@5.3.1
- xstate@5.3.0
- xstate@5.2.1
- xstate@5.2.0
- xstate@5.1.0
- xstate@5.0.2
- xstate@5.0.1
- xstate@5.0.0
- xstate@5.0.0-beta.54
- xstate@5.0.0-beta.53
- xstate@5.0.0-beta.52
- xstate@5.0.0-beta.51
- xstate@5.0.0-beta.50
- xstate@5.0.0-beta.49
- xstate@5.0.0-beta.48
- xstate@5.0.0-beta.47
- xstate@5.0.0-beta.46
- xstate@5.0.0-beta.45
- xstate@5.0.0-beta.44
- xstate@5.0.0-beta.43
- xstate@5.0.0-beta.42
- xstate@5.0.0-beta.41
- xstate@5.0.0-beta.40
- xstate@5.0.0-beta.39
- xstate@5.0.0-beta.38
- xstate@5.0.0-beta.37
- xstate@5.0.0-beta.36
- xstate@5.0.0-beta.35
- xstate@5.0.0-beta.34
- xstate@5.0.0-beta.33
- xstate@5.0.0-beta.32
- xstate@5.0.0-beta.31
- xstate@5.0.0-beta.30
- xstate@5.0.0-beta.29
- xstate@5.0.0-beta.28
- xstate@5.0.0-beta.27
- xstate@5.0.0-beta.26
- xstate@5.0.0-beta.25
- xstate@5.0.0-beta.24
- xstate@5.0.0-beta.23
- xstate@5.0.0-beta.22
- xstate@5.0.0-beta.21
- xstate@5.0.0-beta.20
- xstate@5.0.0-beta.19
- xstate@5.0.0-beta.18
- xstate@5.0.0-beta.17
- xstate@5.0.0-beta.16
- xstate@5.0.0-beta.15
- xstate@5.0.0-beta.14
- xstate@5.0.0-beta.13
- xstate@5.0.0-beta.12
- xstate@5.0.0-beta.11
- xstate@5.0.0-beta.10
- xstate@5.0.0-beta.9
- xstate@5.0.0-beta.8
- xstate@5.0.0-beta.7
- xstate@5.0.0-alpha.6
- xstate@5.0.0-alpha.5
- xstate@5.0.0-alpha.4
- xstate@5.0.0-alpha.3
- xstate@5.0.0-alpha.2
- xstate@5.0.0-alpha.1
- xstate@5.0.0-alpha.0
- @xstate/vue@4.0.2
- @xstate/vue@4.0.1
- @xstate/vue@4.0.0
- @xstate/vue@3.1.4
- @xstate/vue@3.1.3
- @xstate/vue@3.1.2
- @xstate/vue@3.1.1
- @xstate/vue@3.1.0
- @xstate/vue@3.0.3
- @xstate/vue@3.0.2
- @xstate/vue@3.0.1
- @xstate/vue@3.0.0
- @xstate/vue@3.0.0-beta.6
- @xstate/vue@3.0.0-beta.5
- @xstate/vue@3.0.0-beta.4
- @xstate/vue@3.0.0-beta.3
- @xstate/vue@3.0.0-beta.2
- @xstate/vue@3.0.0-alpha.1
- @xstate/vue@3.0.0-alpha.0
- @xstate/test@1.0.0-beta.5
- @xstate/test@1.0.0-beta.4
- @xstate/test@1.0.0-beta.3
- @xstate/test@1.0.0-beta.2
- @xstate/svelte@4.0.3
- @xstate/svelte@4.0.2
- @xstate/svelte@4.0.1
- @xstate/svelte@4.0.0
- @xstate/svelte@3.0.5
- @xstate/svelte@3.0.4
- @xstate/svelte@3.0.3
- @xstate/svelte@3.0.2
- @xstate/svelte@3.0.1
- @xstate/svelte@3.0.0
- @xstate/store@3.2.1
- @xstate/store@3.2.0
- @xstate/store@3.1.0
- @xstate/store@3.0.1
- @xstate/store@3.0.0
- @xstate/store@2.6.2
- @xstate/store@2.6.1
- @xstate/store@2.6.0
- @xstate/store@2.5.0
- @xstate/store@2.4.0
- @xstate/store@2.3.0
- @xstate/store@2.2.1
- @xstate/store@2.2.0
- @xstate/store@2.1.0
- @xstate/store@2.0.0
- @xstate/store@1.0.0
- @xstate/store@0.0.5
- @xstate/store@0.0.4
- @xstate/store@0.0.3
- @xstate/store@0.0.2
- @xstate/solid@1.0.2
- @xstate/solid@1.0.1
- @xstate/solid@1.0.0
- @xstate/solid@0.2.3
- @xstate/solid@0.2.2
- @xstate/solid@0.2.1
- @xstate/solid@0.2.0
- @xstate/react@5.0.3
- @xstate/react@5.0.2
- @xstate/react@5.0.1
- @xstate/react@5.0.0
- @xstate/react@4.1.3
- @xstate/react@4.1.2
- @xstate/react@4.1.1
- @xstate/react@4.1.0
- @xstate/react@4.0.3
- @xstate/react@4.0.2
- @xstate/react@4.0.1
- @xstate/react@4.0.0
- @xstate/react@4.0.0-beta.12
- @xstate/react@4.0.0-beta.11
- @xstate/react@4.0.0-beta.10
- @xstate/react@4.0.0-beta.9
- @xstate/react@4.0.0-beta.8
- @xstate/react@4.0.0-beta.7
- @xstate/react@4.0.0-beta.6
- @xstate/react@4.0.0-beta.5
- @xstate/react@4.0.0-beta.4
- @xstate/react@4.0.0-beta.3
- @xstate/react@4.0.0-alpha.2
- @xstate/react@4.0.0-alpha.1
- @xstate/react@4.0.0-alpha.0
- @xstate/graph@3.0.2
- @xstate/graph@3.0.1
- @xstate/graph@3.0.0
- @xstate/graph@2.0.1
- @xstate/graph@2.0.0
- @xstate/graph@2.0.0-beta.6
- @xstate/graph@2.0.0-beta.5
- @xstate/graph@2.0.0-beta.4
- @xstate/graph@2.0.0-beta.3
- @xstate/fsm@3.0.0-beta.3
- @xstate/fsm@3.0.0-beta.2
- @xstate/fsm@3.0.0-alpha.1
- @xstate/fsm@3.0.0-alpha.0
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
export class Mailbox<T> { | ||
private events: T[] = []; | ||
private index: number = 0; | ||
|
||
public status: 'deferred' | 'idle' | 'processing' = 'deferred'; | ||
|
||
public get size(): number { | ||
return this.events.length - this.index; | ||
} | ||
|
||
public clear(): void { | ||
this.events.length = 0; | ||
this.index = 0; | ||
} | ||
|
||
public enqueue(event: T): void { | ||
this.events.push(event); | ||
} | ||
|
||
public dequeue(): T | undefined { | ||
const event = this.events[this.index]; | ||
|
||
if (!event) { | ||
return undefined; | ||
} | ||
|
||
this.index++; | ||
|
||
if (this.index > this.events.length - 1) { | ||
this.events.length = 0; | ||
this.index = 0; | ||
} | ||
|
||
return event; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ import type { | |
Subscribable | ||
} from './types'; | ||
import { isExecutableAction } from '../actions/ExecutableAction'; | ||
import { Mailbox } from './Mailbox'; | ||
|
||
export type StateListener< | ||
TContext extends MachineContext, | ||
|
@@ -110,8 +111,7 @@ export class Interpreter< | |
public options: Readonly<InterpreterOptions>; | ||
|
||
public id: string; | ||
private mailbox: Array<SCXML.Event<TEvent>> = []; | ||
private mailboxStatus: 'deferred' | 'idle' | 'processing' = 'deferred'; | ||
private mailbox: Mailbox<SCXML.Event<TEvent>> = new Mailbox(); | ||
private delayedEventsMap: Record<string, number> = {}; | ||
private listeners: Set< | ||
StateListener<TContext, TEvent, TTypestate> | ||
|
@@ -418,18 +418,19 @@ export class Interpreter< | |
} | ||
|
||
private flush() { | ||
this.mailboxStatus = 'processing'; | ||
while (this.mailbox.length) { | ||
const event = this.mailbox.shift()!; | ||
|
||
this.mailbox.status = 'processing'; | ||
let event = this.mailbox.dequeue(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This implementation doesn't scale too well with multiple consumers of the mailbox because a lot of the delicate queueing/dequeuing management becomes the responsibility of each consumer. I think I've managed to avoid this in this PR: With these changes, the consumer only needs to provide a "process" callback to the created mailbox and call simple |
||
while (event) { | ||
// TODO: handle errors | ||
this.forward(event); | ||
|
||
const nextState = this.nextState(event); | ||
|
||
this.update(nextState); | ||
|
||
event = this.mailbox.dequeue(); | ||
} | ||
this.mailboxStatus = 'idle'; | ||
this.mailbox.status = 'idle'; | ||
} | ||
|
||
/** | ||
|
@@ -469,7 +470,7 @@ export class Interpreter< | |
this.clock.clearTimeout(this.delayedEventsMap[key]); | ||
} | ||
|
||
this.mailbox.length = 0; | ||
this.mailbox.clear(); | ||
this.status = InterpreterStatus.Stopped; | ||
registry.free(this.sessionId); | ||
|
||
|
@@ -518,9 +519,9 @@ export class Interpreter< | |
); | ||
} | ||
|
||
this.mailbox.push(_event); | ||
this.mailbox.enqueue(_event); | ||
|
||
if (this.mailboxStatus === 'idle') { | ||
if (this.mailbox.status === 'idle') { | ||
this.flush(); | ||
} | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This strategy doesn't scale because we retain all the queued items up until we drain the queue. The items that were already processed should be immediately disposed because we don't have any use for them from that point in time.
I've reimplemented this mailbox using a simple linked list to avoid this problem altogether:
37547c3