-
Notifications
You must be signed in to change notification settings - Fork 47
Deferred unique writes #193
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
Open
nick-potts
wants to merge
16
commits into
hirethunk:main
Choose a base branch
from
nick-potts:latest-attribute
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
79bc431
WIP: Latest attribute to only run handle writes once during replay
nick-potts 9dc021d
use wormhole
nick-potts 6929360
Fix styling
nick-potts 1fb01e1
Clear queue
nick-potts c9916e2
Rename to UniqueBy, add Verbs::whenUnique()
nick-potts 07466c0
rename tests
nick-potts 8815079
add replay only option, so you can still commit and return data
nick-potts 231ef3a
Fix styling
nick-potts 513b0cc
fix AppliesToState
nick-potts 5b1176e
Merge remote-tracking branch 'origin/latest-attribute' into latest-at…
nick-potts 07f7846
Fix styling
nick-potts 2af9cdf
rename to defer
nick-potts f2710b0
Fix styling
nick-potts fc7f85f
Formatting
nick-potts 8408005
Ensure ordering
nick-potts 52faec6
Fix styling
nick-potts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <?php | ||
|
|
||
| namespace Thunk\Verbs\Attributes\Hooks; | ||
|
|
||
| use Attribute; | ||
| use Thunk\Verbs\Lifecycle\Hook; | ||
|
|
||
| #[Attribute(Attribute::TARGET_METHOD)] | ||
| class DeferFor implements HookAttribute | ||
| { | ||
| public const EVENT_CLASS = 'event_class_name'; | ||
|
|
||
| /** | ||
| * This attribute is used to defer the handling of a hook until | ||
| * after data is committed/replayed. You can use this attribute | ||
| * to prevent duplicate writes by ensuring that the hook is only | ||
| * handled once for a given set of state properties. | ||
| * | ||
| * @param string|string[] $property_name The state property name(s) to be unique by | ||
| * @param string $name Defaults to the event's class name | ||
| * @param bool $replay_only Only defer for replayed events | ||
| */ | ||
| public function __construct( | ||
| public string|array|null $property_name, | ||
| public string $name = self::EVENT_CLASS, | ||
| public bool $replay_only = false, | ||
| ) {} | ||
|
|
||
| public function applyToHook(Hook $hook): void | ||
| { | ||
| $hook->deferred_attribute = $this; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| <?php | ||
|
|
||
| namespace Thunk\Verbs\Lifecycle; | ||
|
|
||
| use Thunk\Verbs\Attributes\Hooks\DeferFor; | ||
| use Thunk\Verbs\Event; | ||
| use Thunk\Verbs\State; | ||
| use Thunk\Verbs\Support\EventStateRegistry; | ||
| use Thunk\Verbs\Support\StateCollection; | ||
| use Thunk\Verbs\Support\Wormhole; | ||
|
|
||
| class DeferredWriteQueue | ||
| { | ||
| private array $callbacks = []; | ||
|
|
||
| private int $count = 0; | ||
|
|
||
| public function addHook(Event $event, DeferFor $deferred, callable $callback): void | ||
| { | ||
| /** @var string[] $propertyNames */ | ||
| $propertyNames = is_array($deferred->property_name) ? $deferred->property_name : [$deferred->property_name]; | ||
|
|
||
| $uniqueByKey = ''; | ||
| $states = new StateCollection; | ||
| foreach ($propertyNames as $property) { | ||
| if ($property === null) { | ||
| $uniqueByKey .= 'null'; | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| $states = $states->merge(app(EventStateRegistry::class)->statesForProperty($event, $property)); | ||
| } | ||
|
|
||
| $uniqueByKey .= $states->map(fn (State $state) => $state->id)->implode('|'); | ||
|
|
||
| $name = $deferred->name === DeferFor::EVENT_CLASS ? get_class($event) : $deferred->name; | ||
|
|
||
| $this->callbacks[$name][$uniqueByKey][$this->count++] = [$event, $callback, true]; | ||
| } | ||
|
|
||
| /** | ||
| * @param iterable<State|string|null> $states | ||
| */ | ||
| public function addCallback(iterable $states, callable $callback, string $name): void | ||
| { | ||
| $id = ''; | ||
| foreach ($states as $state) { | ||
| if ($state === null) { | ||
| $id .= 'null'; | ||
|
|
||
| continue; | ||
| } | ||
| if (is_string($state)) { | ||
| $id .= $state; | ||
|
|
||
| continue; | ||
| } | ||
| if ($state instanceof State) { | ||
| $id .= $state->id; | ||
|
|
||
| continue; | ||
| } | ||
| throw new \InvalidArgumentException('Invalid state type'); | ||
| } | ||
|
|
||
| unset($this->callbacks[$name][$id]); | ||
| $this->callbacks[$name][$id][$this->count++] = [null, $callback, false]; | ||
| } | ||
|
|
||
| public function flush(): void | ||
| { | ||
| foreach ($this->callbacks as $namedCallbacks) { | ||
| foreach ($namedCallbacks as $stateGroup) { | ||
| $lastCallback = end($stateGroup); | ||
| [$event, $callback, $isEventCallback] = $lastCallback; | ||
|
|
||
| if ($isEventCallback) { | ||
| app(Wormhole::class)->warp($event, $callback); | ||
| } else { | ||
| $callback(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| $this->callbacks = []; | ||
| $this->count = 0; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Potentially this should default to true. Event::commit() will default to returning values, and only replays will benefit initially.