Skip to content

Commit 9714722

Browse files
committed
Improve async logic
1 parent 06185b1 commit 9714722

File tree

5 files changed

+34
-28
lines changed

5 files changed

+34
-28
lines changed

Diff for: cli

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ require __DIR__ . '/vendor/autoload.php';
1111

1212
$api = new \Lodestone\Api();
1313

14+
$start = microtime(true);
1415
print_r([
15-
'results' => $api->character()->classjobs('7322082')
16+
'results' => $api->character()->get('7322082')
1617
]);
18+
$finish = microtime(true);
19+
20+
echo "duration = ". ($finish - $start) ."ms";

Diff for: cli_async

+12-26
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*/
88

99
// composer auto loader
10+
use Lodestone\Http\AsyncHandler;
11+
1012
require __DIR__ . '/vendor/autoload.php';
1113

1214
$api = new \Lodestone\Api();
@@ -16,30 +18,14 @@ $start = microtime(true);
1618
// use async mode
1719
$api->config()->useAsync();
1820

19-
$responses = [];
20-
foreach(range(0,5) as $i) {
21-
echo "Loop: {$i} \n";
22-
// perform multiple requests
23-
$api->character()->get(730968);
24-
$api->character()->get(15609878);
25-
$api->character()->get(2269193);
26-
$api->character()->get(720399);
27-
$api->character()->get(14673089);
28-
$api->character()->get(24328626);
29-
$api->character()->get(24778921);
30-
$api->character()->get(6649088);
31-
$api->character()->get(1567731);
32-
$api->character()->get(11887821);
33-
34-
// settle responses (they will trigger in the order you added them)
35-
$responses = array_merge($responses, $api->http()->settle());
36-
sleep(1);
37-
}
38-
39-
$duration = microtime(true) - $start;
40-
41-
print_r([
42-
count($responses),
43-
"Duration = {$duration}"
44-
]);
21+
$api->requestId('profile')->character()->get(730968);
22+
$api->requestId('classjobs')->character()->classjobs(730968);
23+
$api->requestId('minions')->character()->minions(730968);
24+
$api->requestId('minions')->character()->mounts(730968);
25+
26+
$responses = $api->http()->settle();
27+
28+
29+
30+
print_r($responses);
4531

Diff for: src/Lodestone/Api.php

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Lodestone;
44

5+
use Lodestone\Http\AsyncHandler;
56
use Lodestone\Http\Http;
67
use Lodestone\Http\RequestConfig;
78
use Lodestone\Api\{
@@ -31,6 +32,12 @@ private function getApiNamespace($namespace)
3132

3233
return $class;
3334
}
35+
36+
public function requestId(string $name)
37+
{
38+
AsyncHandler::setRequestId($name);
39+
return $this;
40+
}
3441

3542
/**
3643
* Access the Lodestone API Configuration

Diff for: src/Lodestone/Http/AsyncHandler.php

+5
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,9 @@ public static function reset(): void
3939
self::$responses = [];
4040
self::$requestId = null;
4141
}
42+
43+
public static function setRequestId(string $name)
44+
{
45+
self::$requestId = $name;
46+
}
4247
}

Diff for: src/Lodestone/Parser/ParseCharacterClassJobs.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ public function handle(string $html)
101101
//
102102
$elemental = new ClassJobEureka('Elemental Level');
103103
$node = $this->dom->find('.character__job__list')[$elementalIndex];
104-
[$current, $max] = explode('/', $node->find('.character__job__exp')->text());
104+
105+
$currentmax = explode('/', $node->find('.character__job__exp')->text());
106+
$current = $currentmax[0] ?? null;
107+
$max = $currentmax[1] ?? null;
108+
105109
$current = filter_var(trim(str_ireplace('-', null, $current)) ?: 0, FILTER_SANITIZE_NUMBER_INT);
106110
$max = filter_var(trim(str_ireplace('-', null, $max)) ?: 0, FILTER_SANITIZE_NUMBER_INT);
107111

0 commit comments

Comments
 (0)