Skip to content
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

Add react/promise v2 to v3 transition helper #26

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

SQKo
Copy link
Member

@SQKo SQKo commented Dec 9, 2023

This is helper class for transitioning react/promise v2 to react/promise v3. No, we do not need to make sudden breaking changes to force everyone replace done() and then() because it is difficult to determine whether the promise should be chained or ends there.

With this, users are not forced to go with v3, they can force their composer install to v2, and the polyfill will wraps it directly to the v2 codes.

BC Note: progress/onProgress is not supported (it was deprecated long ago in v2, so the requirement is now atleast v2.8)

However, our internal codes must be changed to the react/promise v3 styles, while using the provided helper class, instead of the react/promise v3 class directly.

Many things that still need to be adjusted such as the ratelimit promise rejection since the react/promise reject() requires the value to be a throwable now (currently, the wrapper will force non exceptions values as resolved).

It has been tested with very simple basic requests.

$http->get('sticker-packs')->done(function () {
    echo 'GOT ALL STICKER PACK';
}, function ($e) {
    var_dump($e->code);
});

$http->get('sticker-packs/847199849233514549')->done(function ($res) { // this endpoint no longer accessible by bots
    var_dump('GOT WUMPUS STICKER PACK', $res);
});

Log for both promise v2 and promise v3

[2023-12-09T14:38:09.549068+00:00] logger-name.DEBUG: BUCKET getsticker-packs queued REQ GET sticker-packs [] []
[2023-12-09T14:38:09.582967+00:00] logger-name.DEBUG: BUCKET getsticker-packs/847199849233514549 queued REQ GET sticker-packs/847199849233514549 [] []
[2023-12-09T14:38:10.431113+00:00] logger-name.DEBUG: REQ GET sticker-packs successful [] []
[2023-12-09T14:38:10.431460+00:00] logger-name.DEBUG: http not checking {"waiting":1,"empty":true} []
GOT ALL STICKER PACK

[2023-12-09T14:38:10.777658+00:00] logger-name.WARNING: REQ GET sticker-packs/847199849233514549 failed: Discord\Http\Exceptions\NoPermissionsException: Forbidden - {     "message": "Bots cannot use this endpoint",     "code": 20001 } in D:\Data\DiscordPHP-Http\src\Discord\Http.php:516 Stack trace: #0 D:\Data\DiscordPHP-Http\src\Discord\Http.php(398): Discord\Http\Http->handleError() 
#1 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Internal\FulfilledPromise.php(47): Discord\Http\Http->Discord\Http\{closure}() #2 
D:\Data\DiscordPHP-Http\vendor\react\promise\src\Promise.php(173): React\Promise\Internal\FulfilledPromise->then() #3 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Promise.php(221): React\Promise\Promise::React\Promise\{closure}() #4 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Promise.php(286): React\Promise\Promise->settle() #5 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Deferred.php(45): React\Promise\Promise::React\Promise\{closure}() #6 D:\Data\DiscordPHP-Http\vendor\react\http\src\Io\Transaction.php(90): React\Promise\Deferred->resolve() #7 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Internal\FulfilledPromise.php(47): React\Http\Io\Transaction->React\Http\Io\{closure}() #8 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Promise.php(173): React\Promise\Internal\FulfilledPromise->then() #9 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Promise.php(221): React\Promise\Promise::React\Promise\{closure}() #10 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Promise.php(286): React\Promise\Promise->settle() #11 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Internal\FulfilledPromise.php(47): React\Promise\Promise::React\Promise\{closure}() #12 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Promise.php(180): React\Promise\Internal\FulfilledPromise->then() #13 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Promise.php(221): React\Promise\Promise::React\Promise\{closure}() #14 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Promise.php(286): React\Promise\Promise->settle() #15 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Internal\FulfilledPromise.php(47): React\Promise\Promise::React\Promise\{closure}() #16 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Promise.php(177): React\Promise\Internal\FulfilledPromise->then() #17 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Promise.php(221): React\Promise\Promise::React\Promise\{closure}() #18 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Promise.php(286): React\Promise\Promise->settle() 
#19 D:\Data\DiscordPHP-Http\vendor\react\http\src\Io\Transaction.php(193): React\Promise\Promise::React\Promise\{closure}() #20 D:\Data\DiscordPHP-Http\vendor\evenement\evenement\src\EventEmitterTrait.php(143): React\Http\Io\Transaction->React\Http\Io\{closure}() #21 
D:\Data\DiscordPHP-Http\vendor\react\http\src\Io\ReadableBodyStream.php(50): Evenement\EventEmitter->emit() #22 D:\Data\DiscordPHP-Http\vendor\react\http\src\Io\ReadableBodyStream.php(151): React\Http\Io\ReadableBodyStream->close() #23 D:\Data\DiscordPHP-Http\vendor\react\http\src\Io\ReadableBodyStream.php(33): React\Http\Io\ReadableBodyStream->handleEnd() #24 D:\Data\DiscordPHP-Http\vendor\evenement\evenement\src\EventEmitterTrait.php(143): React\Http\Io\ReadableBodyStream->React\Http\Io\{closure}() #25 D:\Data\DiscordPHP-Http\vendor\react\http\src\Io\CloseProtectionStream.php(96): Evenement\EventEmitter->emit() #26 D:\Data\DiscordPHP-Http\vendor\react\http\src\Io\ClientRequestStream.php(212): React\Http\Io\CloseProtectionStream->handleData() #27 D:\Data\DiscordPHP-Http\vendor\evenement\evenement\src\EventEmitterTrait.php(143): React\Http\Io\ClientRequestStream->handleData() #28 D:\Data\DiscordPHP-Http\vendor\react\stream\src\Util.php(71): Evenement\EventEmitter->emit() #29 D:\Data\DiscordPHP-Http\vendor\evenement\evenement\src\EventEmitterTrait.php(143): React\Stream\Util::React\Stream\{closure}() #30 D:\Data\DiscordPHP-Http\vendor\react\stream\src\DuplexResourceStream.php(196): Evenement\EventEmitter->emit() #31 D:\Data\DiscordPHP-Http\vendor\react\event-loop\src\StreamSelectLoop.php(246): React\Stream\DuplexResourceStream->handleData() #32 D:\Data\DiscordPHP-Http\vendor\react\event-loop\src\StreamSelectLoop.php(213): React\EventLoop\StreamSelectLoop->waitForStreamActivity() #33 D:\Data\DiscordPHP-Http\test.php(38): React\EventLoop\StreamSelectLoop->run() #34 {main} [] []  
[2023-12-09T14:38:10.779149+00:00] logger-name.DEBUG: http not checking {"waiting":0,"empty":true} []
Unhandled promise rejection with Discord\Http\Exceptions\NoPermissionsException: Forbidden - {
    "message": "Bots cannot use this endpoint",
    "code": 20001
} in D:\Data\DiscordPHP-Http\src\Discord\Http.php:516
Stack trace:
#0 D:\Data\DiscordPHP-Http\src\Discord\Http.php(398): Discord\Http\Http->handleError()
#1 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Internal\FulfilledPromise.php(47): Discord\Http\Http->Discord\Http\{closure}()    
#2 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Promise.php(173): React\Promise\Internal\FulfilledPromise->then()
#3 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Promise.php(221): React\Promise\Promise::React\Promise\{closure}()
#4 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Promise.php(286): React\Promise\Promise->settle()
#5 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Deferred.php(45): React\Promise\Promise::React\Promise\{closure}()
#6 D:\Data\DiscordPHP-Http\vendor\react\http\src\Io\Transaction.php(90): React\Promise\Deferred->resolve()
#7 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Internal\FulfilledPromise.php(47): React\Http\Io\Transaction->React\Http\Io\{closure}()
#8 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Promise.php(173): React\Promise\Internal\FulfilledPromise->then()
#9 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Promise.php(221): React\Promise\Promise::React\Promise\{closure}()
#10 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Promise.php(286): React\Promise\Promise->settle()
#11 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Internal\FulfilledPromise.php(47): React\Promise\Promise::React\Promise\{closure}()
#12 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Promise.php(180): React\Promise\Internal\FulfilledPromise->then()
#13 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Promise.php(221): React\Promise\Promise::React\Promise\{closure}()
#14 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Promise.php(286): React\Promise\Promise->settle()
#15 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Internal\FulfilledPromise.php(47): React\Promise\Promise::React\Promise\{closure}()
#16 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Promise.php(177): React\Promise\Internal\FulfilledPromise->then()
#17 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Promise.php(221): React\Promise\Promise::React\Promise\{closure}()
#18 D:\Data\DiscordPHP-Http\vendor\react\promise\src\Promise.php(286): React\Promise\Promise->settle()
#19 D:\Data\DiscordPHP-Http\vendor\react\http\src\Io\Transaction.php(193): React\Promise\Promise::React\Promise\{closure}()
#20 D:\Data\DiscordPHP-Http\vendor\evenement\evenement\src\EventEmitterTrait.php(143): React\Http\Io\Transaction->React\Http\Io\{closure}()
#21 D:\Data\DiscordPHP-Http\vendor\react\http\src\Io\ReadableBodyStream.php(50): Evenement\EventEmitter->emit()
#22 D:\Data\DiscordPHP-Http\vendor\react\http\src\Io\ReadableBodyStream.php(151): React\Http\Io\ReadableBodyStream->close()
#23 D:\Data\DiscordPHP-Http\vendor\react\http\src\Io\ReadableBodyStream.php(33): React\Http\Io\ReadableBodyStream->handleEnd()        
#24 D:\Data\DiscordPHP-Http\vendor\evenement\evenement\src\EventEmitterTrait.php(143): React\Http\Io\ReadableBodyStream->React\Http\Io\{closure}()
#25 D:\Data\DiscordPHP-Http\vendor\react\http\src\Io\CloseProtectionStream.php(96): Evenement\EventEmitter->emit()
#26 D:\Data\DiscordPHP-Http\vendor\react\http\src\Io\ClientRequestStream.php(212): React\Http\Io\CloseProtectionStream->handleData()  
#27 D:\Data\DiscordPHP-Http\vendor\evenement\evenement\src\EventEmitterTrait.php(143): React\Http\Io\ClientRequestStream->handleData()#28 D:\Data\DiscordPHP-Http\vendor\react\stream\src\Util.php(71): Evenement\EventEmitter->emit()
#29 D:\Data\DiscordPHP-Http\vendor\evenement\evenement\src\EventEmitterTrait.php(143): React\Stream\Util::React\Stream\{closure}()    
#30 D:\Data\DiscordPHP-Http\vendor\react\stream\src\DuplexResourceStream.php(196): Evenement\EventEmitter->emit()
#31 D:\Data\DiscordPHP-Http\vendor\react\event-loop\src\StreamSelectLoop.php(246): React\Stream\DuplexResourceStream->handleData()    
#32 D:\Data\DiscordPHP-Http\vendor\react\event-loop\src\StreamSelectLoop.php(213): React\EventLoop\StreamSelectLoop->waitForStreamActivity()
#33 D:\Data\DiscordPHP-Http\test.php(38): React\EventLoop\StreamSelectLoop->run()
#34 {main}

gambar

Real changes will not affect to Bots yet until the DiscordPHP library package is updated to use the provided polyfill class.

If possible, I'd make this separate package and should be only installed for those still doing transition. But I have not find any way yet.

Note: I personally bump this implementation for v11 DiscordPHP, after releasing v10

@SQKo SQKo added the enhancement New feature or request label Dec 9, 2023
@SQKo SQKo requested a review from a team December 9, 2023 14:47
@SQKo SQKo self-assigned this Dec 9, 2023
@SQKo SQKo marked this pull request as draft December 9, 2023 14:54
@SQKo SQKo force-pushed the promise-v2-v3-transition branch 2 times, most recently from ac1c925 to d763628 Compare December 10, 2023 04:29
@SQKo SQKo force-pushed the promise-v2-v3-transition branch from d763628 to c30387f Compare December 10, 2023 05:08
@SQKo
Copy link
Member Author

SQKo commented Dec 10, 2023

Apparently, when trying to implement this in DiscordPHP bot package, I got dependency error:

Problem 6
    - wyrihaximus/react-cache-redis is locked to version 4.3.0 and an update of this package was not requested.
    - wyrihaximus/react-cache-redis 4.3.0 requires react/promise ^2.8 -> found react/promise[v2.8.0, v2.9.0, v2.10.0, v2.11.0] but it conflicts with your root composer.json require (^3).

The redis cache does not support react/promise v3 yet, while the react-cache-redis itself is optional, it is the main goal of v10 release..
therefore we better wait before actually implementing this helper.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

1 participant