Skip to content

Added explicit AsyncTask->storeLocal(), removed AsyncTask->__construct() object storage#1322

Merged
dktapps merged 3 commits into
masterfrom
explicit-store-local
Sep 10, 2017
Merged

Added explicit AsyncTask->storeLocal(), removed AsyncTask->__construct() object storage#1322
dktapps merged 3 commits into
masterfrom
explicit-store-local

Conversation

@dktapps
Copy link
Copy Markdown
Member

@dktapps dktapps commented Aug 26, 2017

Introduction

Far too often I see people using IDEs which generate the constructors for them and then accidentally unintentionally store things in the object store. This parent constructor behaviour is unexpected.

If a developer wants to store something, they should now do so explicitly by calling storeLocal(). The intention of this is more clear and there is no doubt about what such a call will do.

This pull request also clarifies the documentation on using the scheduler object storage.

Changes

API changes

  • AsyncTask no longer has its own constructor.
  • Tasks wishing to store objects for later use must use storeLocal() instead.

Backwards compatibility

This is a backwards-compatibility break for any plugins which used the constructor to store objects.

Tests

TesterPlugin submodule has been updated to reflect the changes in this pull request.

@dktapps dktapps added BC break Breaks API compatibility Category: API Related to the plugin API PR: Contribution labels Aug 26, 2017
@dktapps dktapps requested a review from SOF3 August 26, 2017 10:17
* WARNING: As of pthreads v3.1.6, arrays are converted to Volatile objects when assigned as members of Threaded objects.
* Keep this in mind when using arrays stored as members of your AsyncTask.
*
* WARNING: Do not call PocketMine-MP API methods from other Threads!!
Copy link
Copy Markdown
Member

@Muqsit Muqsit Aug 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? Does "other Threads" include the main thread? Because I call NBT::readCompressed() and Item::nbtDeserialize() in one of my plugins asynchronously.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of things are not initialized on workers that are initialized on the main thread, which may cause unexpected behaviour. For example, because the ItemFactory is not initialized, calling nbtDeserialize() on a worker will give you a generic Item object no matter what item was stored.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh noes... I messed up in many places.

Far too often I see people using IDEs which generate the constructors for them and then accidentally unintentionally store things in the object store. This parent constructor behaviour is unexpected. If a developer wants to store something, they should now do so explicitly by calling storeLocal().
@dktapps dktapps force-pushed the explicit-store-local branch 2 times, most recently from 426f75c to ec76bd8 Compare August 26, 2017 11:59
* Call this method from {@link AsyncTask#onCompletion} to fetch the data stored in the constructor, if any, and
* clears it from the storage.
* Saves mixed data in ServerScheduler's object storage on the main thread. You may use this to retain references to
* objects or arrays which you need to access in {@link AsyncTask#onCompletion} which cannot be stored as a
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onCompletion() or onProgressUpdate().

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's best not to introduce more confusion over fetch vs peek. This documents behaviour and the most common use, it's up to developers to decide how to use it.

@dktapps dktapps changed the base branch from master to php/7.0 August 31, 2017 20:13
@dktapps dktapps changed the base branch from php/7.0 to master September 10, 2017 19:30
@dktapps dktapps merged commit aaa3b6e into master Sep 10, 2017
@dktapps dktapps deleted the explicit-store-local branch September 10, 2017 19:31
@dktapps dktapps added Type: Enhancement Contributes features or other improvements to PocketMine-MP and removed Type: Contribution labels Nov 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BC break Breaks API compatibility Category: API Related to the plugin API Type: Enhancement Contributes features or other improvements to PocketMine-MP

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants