Revert cached state

This commit is contained in:
Dan Harrin
2025-04-30 10:07:43 +01:00
parent 11dda09e6b
commit 916af44ccb
5 changed files with 69 additions and 120 deletions
@@ -128,40 +128,38 @@ class SpatieMediaLibraryImageColumn extends ImageColumn
*/
public function getState(): array
{
return $this->cacheState(function (): array {
$record = $this->getRecord();
$record = $this->getRecord();
if ($this->hasRelationship($record)) {
$record = $this->getRelationshipResults($record);
}
if ($this->hasRelationship($record)) {
$record = $this->getRelationshipResults($record);
}
$records = Arr::wrap($record);
$records = Arr::wrap($record);
$state = [];
$state = [];
$collection = $this->getCollection() ?? 'default';
$collection = $this->getCollection() ?? 'default';
foreach ($records as $record) {
/** @var Model $record */
$state = [
...$state,
...$record->getRelationValue('media')
->when(
! $collection instanceof AllMediaCollections,
fn (MediaCollection $mediaCollection) => $mediaCollection->filter(fn (Media $media): bool => $media->getAttributeValue('collection_name') === $collection),
)
->when(
$this->hasMediaFilter(),
fn (Collection $media) => $this->filterMedia($media)
)
->sortBy('order_column')
->pluck('uuid')
->all(),
];
}
foreach ($records as $record) {
/** @var Model $record */
$state = [
...$state,
...$record->getRelationValue('media')
->when(
! $collection instanceof AllMediaCollections,
fn (MediaCollection $mediaCollection) => $mediaCollection->filter(fn (Media $media): bool => $media->getAttributeValue('collection_name') === $collection),
)
->when(
$this->hasMediaFilter(),
fn (Collection $media) => $this->filterMedia($media)
)
->sortBy('order_column')
->pluck('uuid')
->all(),
];
}
return array_unique($state);
});
return array_unique($state);
}
public function applyEagerLoading(Builder | Relation $query): Builder | Relation
@@ -28,45 +28,43 @@ class SpatieTagsColumn extends TextColumn
*/
public function getState(): array
{
return $this->cacheState(function (): array {
$state = parent::getState();
$state = parent::getState();
if ($state && (! $state instanceof Collection)) {
return $state;
if ($state && (! $state instanceof Collection)) {
return $state;
}
$record = $this->getRecord();
if ($this->hasRelationship($record)) {
$record = $this->getRelationshipResults($record);
}
$records = Arr::wrap($record);
$state = [];
foreach ($records as $record) {
/** @var Model $record */
if (! (method_exists($record, 'tags') && method_exists($record, 'tagsWithType'))) {
continue;
}
$record = $this->getRecord();
$type = $this->getType();
if ($this->hasRelationship($record)) {
$record = $this->getRelationshipResults($record);
if ($this->isAnyTagTypeAllowed()) {
$tags = $record->getRelationValue('tags');
} else {
$tags = $record->tagsWithType($type);
}
$records = Arr::wrap($record);
$state = [
...$state,
...$tags->pluck('name')->all(),
];
}
$state = [];
foreach ($records as $record) {
/** @var Model $record */
if (! (method_exists($record, 'tags') && method_exists($record, 'tagsWithType'))) {
continue;
}
$type = $this->getType();
if ($this->isAnyTagTypeAllowed()) {
$tags = $record->getRelationValue('tags');
} else {
$tags = $record->tagsWithType($type);
}
$state = [
...$state,
...$tags->pluck('name')->all(),
];
}
return array_unique($state);
});
return array_unique($state);
}
public function type(string | AllTagTypes | Closure | null $type): static
+13 -50
View File
@@ -25,11 +25,6 @@ trait HasCellState
protected ?string $inverseRelationshipName = null;
/**
* @var array<string, mixed>
*/
protected array $cachedState = [];
public function inverseRelationship(?string $name): static
{
$this->inverseRelationshipName = $name;
@@ -77,24 +72,22 @@ trait HasCellState
public function getState(): mixed
{
return $this->cacheState(function (): mixed {
$state = ($this->getStateUsing !== null) ?
$this->evaluate($this->getStateUsing) :
$this->getStateFromRecord();
$state = ($this->getStateUsing !== null) ?
$this->evaluate($this->getStateUsing) :
$this->getStateFromRecord();
if (is_string($state) && ($separator = $this->getSeparator())) {
$state = explode($separator, $state);
$state = (count($state) === 1 && blank($state[0])) ?
[] :
$state;
}
if (is_string($state) && ($separator = $this->getSeparator())) {
$state = explode($separator, $state);
$state = (count($state) === 1 && blank($state[0])) ?
[] :
$state;
}
if (blank($state)) {
$state = $this->getDefaultState();
}
if (blank($state)) {
$state = $this->getDefaultState();
}
return $state;
});
return $state;
}
public function getStateFromRecord(): mixed
@@ -133,11 +126,6 @@ trait HasCellState
return $state->all();
}
public function clearCachedState(): void
{
$this->cachedState = [];
}
public function separator(string | Closure | null $separator = ','): static
{
$this->separator = $separator;
@@ -300,29 +288,4 @@ trait HasCellState
return (string) str($name)->beforeLast('.');
}
protected function cacheState(Closure $state): mixed
{
$record = $this->getRecord();
if (! $record) {
return null;
}
if ($this instanceof Column) {
$recordKey = $this->getLivewire()->getTableRecordKey($record);
} else {
$recordKey = (string) $record->getKey();
}
if (blank($recordKey)) {
return $state();
}
if (array_key_exists($recordKey, $this->cachedState)) {
return $this->cachedState[$recordKey];
}
return $this->cachedState[$recordKey] = $state();
}
}
@@ -39,9 +39,7 @@ class Count extends Summarizer
foreach ($query->clone()->distinct()->pluck($attribute) as $value) {
$column->record($this->getQuery()->getModel()->setKeyName($attribute)->setAttribute($attribute, $value));
$column->clearCachedState();
$columnState = $column->getState();
$column->clearCachedState();
$color = json_encode($column->getColor($columnState));
$icon = $column->getIcon($columnState);
@@ -180,8 +180,6 @@ class TestsColumns
$livewireClass = $this->instance()::class;
$column->clearCachedState();
$state = $column->getState();
if (is_array($state)) {
@@ -218,8 +216,6 @@ class TestsColumns
$livewireClass = $this->instance()::class;
$column->clearCachedState();
$state = $column->getState();
if (is_array($state)) {
@@ -256,8 +252,6 @@ class TestsColumns
$livewireClass = $this->instance()::class;
$column->clearCachedState();
Assert::assertEquals(
$value,
$column->formatState($column->getState()),
@@ -285,8 +279,6 @@ class TestsColumns
$livewireClass = $this->instance()::class;
$column->clearCachedState();
Assert::assertFalse(
$column->formatState($column->getState()) == $value,
message: "Failed asserting that a table column with name [{$name}] does not have a formatted state of [{$value}] for record [{$record->getKey()}] on the [{$livewireClass}] component.",