Skip to content

Instantly share code, notes, and snippets.

@joshcirre
Last active September 19, 2024 02:45
Show Gist options
  • Save joshcirre/130aa23decdd98d791fb5cadd83a4b42 to your computer and use it in GitHub Desktop.
Save joshcirre/130aa23decdd98d791fb5cadd83a4b42 to your computer and use it in GitHub Desktop.
<?php
use function Livewire\Volt\{state, mount};
use Illuminate\Support\Facades\Cache;
use function Illuminate\Log\log;
state([
'message' => 'Press Defer to Begin',
'is_polling' => true,
]);
mount(function () {
if (Cache::has('deferred_message')) {
Cache::forget('deferred_message');
}
});
$defer = function () {
defer(function () {
log('Deffering this message.');
sleep(5);
Cache::put('deferred_message', 'This is a deferred message.');
});
$this->message = 'Hi there folks.';
};
$checkCache = function () {
$cachedMessage = Cache::get('deferred_message');
if ($cachedMessage !== null) {
$this->message = $cachedMessage;
$this->is_polling = false;
}
};
?>
<div>
<div class="flex flex-col space-y-4">
<x-primary-button wire:click='defer' class="w-32 mx-auto">Defer It</x-primary-button>
<div class="p-4 mt-4 text-center bg-gray-100 rounded-lg shadow-md" {!! when($is_polling, "wire:poll='checkCache'") !!}>
<p class="text-lg font-semibold text-gray-800">{{ $message }}</p>
</div>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment