Skip to content

Commit

Permalink
Fix preflight.
Browse files Browse the repository at this point in the history
  • Loading branch information
jameswilddev committed Jul 11, 2024
1 parent b1da40f commit 8d5f6a7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
7 changes: 2 additions & 5 deletions laravel/src/SyncApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace JamesWildDev\ReactNativeAppHelpers;

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Arr;

/**
* Represents a sync API as a whole. Use this to configure your API once, then
Expand Down Expand Up @@ -107,11 +108,7 @@ public function generateRoutes(): void
foreach ($this->constants as $constant) {
$key = $constant->generateCamelCasedName();

$data = $constant->value;
ksort($data);
$version = hash('sha1', json_encode($data));

$singletons[$key] = compact('version');
$singletons[$key] = Arr::only($constant->getCachedValue(), 'version');
}

$collections = [];
Expand Down
29 changes: 17 additions & 12 deletions laravel/src/SyncApiConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,28 @@ public function hashData(array $data): string
return hash('sha1', json_encode($data));
}

public function generateConstantRoutes(): void
public function getCachedValue()
{
$kebabCasedName = $this->generateKebabCasedName();

Route::get(
$kebabCasedName,
function () use ($kebabCasedName) {
$data = Cache::remember(
'sync_api_constant_' . $kebabCasedName,
3600,
$this->valueFactory
);
return Cache::remember(
'sync-api-constant-' . $this->generateKebabCasedName(),
3600,
function () {
$data = ($this->valueFactory)();

return [
'version' => $this->hashData($data),
'data' => $data,
'version' => hash('sha1', json_encode($data)),
];
}
);
}

public function generateConstantRoutes(): void
{
Route::get(
$this->generateKebabCasedName(),
function () {
return $this->getCachedValue();
},
);
}
Expand Down

0 comments on commit 8d5f6a7

Please sign in to comment.