Skip to content

Conversation

@cwillisf
Copy link
Contributor

Proposed Changes

This change creates a new addHelper method, used to add a Helper to the storage module. Every registered Helper now has a priority value, and Helpers are consulted in descending priority order during a load.

Reason for Changes

This allows scratch-desktop to inject its own Electron-specific Helper into the storage system. In the longer term I think this will also allow us to simplify the storage module, and in particular sets us up to eliminate some redundant code in ScratchStorage.js and WebHelper.js.

Followup

If this PR is merged I will file these as followup tasks:

  1. The store method should support multiple Helpers, consulted in priority order like in the load case.
    • The webHelper property on ScratchStorage should be removed.
    • The store method on a Helper should distinguish between "I don't handle this kind of asset" (perhaps returning null) and "I tried to handle this and failed" (perhaps returning and rejecting a Promise). In the former case we should move on to the next helper; in the latter we should probably fail the overall store call.
    • This may also involve moving some of the AssetType filtering logic from WebHelper into ScratchStorage.
  2. The WebHelper should only handle one "store" and should not contain failover logic.
    • IMO it still makes sense to provide load, save, and update handlers as long as they all operate on the same "store" on the web.
    • Any code which currently adds multiple stores (say, one for projects and another for images & sounds) should be converted to add multiple WebHelper instances instead.
    • The load method on WebHelper should be simplified to remove tryNextSource: instead it should rely on tryNextHelper in ScratchStorage.

Test Coverage

Please show how you have added tests to cover your changes

The list of helpers in `ScratchStorage` is no longer fixed: now it's
possible to add a new helper using the `addHelper` method. Helpers are
sorted by their relative priority values, which are provided at add
time.

As a follow-up task, `WebHelper` should probably be adjusted to
represent only one endpoint, with create/update/delete features for that
one endpoint. A normal Scratch session might then have one `WebHelper`
for projects and a second for images and sounds. The main reason to do
this would be to concentrate the "try the next place" logic in
`ScratchStorage` instead of having a second version of it in
`WebHelper`.
@cwillisf cwillisf added this to the October 2018 milestone Oct 24, 2018
@cwillisf cwillisf self-assigned this Oct 24, 2018
This new test verifies that helpers are used in the correct order, corresponding to the priority
value provided to `addHelper`.
Copy link
Contributor

@rschamp rschamp left a comment

Choose a reason for hiding this comment

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

The tests are a little hard to read and have state shared between them to function. Not a super big deal but would be nice to fix (as long as that's what's happening!)

test('addHelper', t => {
storage.addHelper(new FakeHelper('third', true), -50);
storage.addHelper(new FakeHelper('first', false), 50);
storage.addHelper(new FakeHelper('second', false), 0);

This comment was marked as abuse.


test('priorities', t => {
t.deepEqual(logEntries, []);
return storage.load('fakeAssetType', 'fakeAssetId', 'fakeDataFormat').then(() => {

This comment was marked as abuse.

This comment was marked as abuse.

These changes break the `addHelper` tests into several phases which don't share state, and hopefully
improve readability of the tests.
@cwillisf cwillisf force-pushed the storage-helper-priorities branch from 970ee74 to c7bba01 Compare October 31, 2018 21:28
@rschamp rschamp assigned cwillisf and unassigned rschamp and benjiwheeler Nov 1, 2018
@cwillisf cwillisf merged commit 29daf3f into scratchfoundation:develop Nov 1, 2018
@cwillisf cwillisf deleted the storage-helper-priorities branch November 1, 2018 18:38
@rschamp
Copy link
Contributor

rschamp commented Nov 1, 2018

🎉 This PR is included in version 1.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants