Skip to content

Add EventuallyQueue API #1291

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

Merged
merged 6 commits into from
Feb 10, 2021
Merged

Add EventuallyQueue API #1291

merged 6 commits into from
Feb 10, 2021

Conversation

dplewis
Copy link
Member

@dplewis dplewis commented Feb 3, 2021

Closes: #1230

This is a similar feature to the iOS SDK EventuallyQueue to allow a user to cache objects to be saved later. Inspired by @francimedia repo.

This has a lot of room for improvement, any feedback is appreciated.

  • Save internal unique hash on object to the DB to ensure operations won't be duplicated.
  • If the operation happens at a later time update object internal state
  • Change Polling setInterval to setTimeout?

Parse.Object

Saves / Deletes this object to the server at some unspecified time in the future, even if Parse is currently inaccessible.

Use this when you may not have a solid network connection, and don't need to know when the save completes.
If there is some problem with the object such that it can't be saved, it will be silently discarded.

Objects saved / deleted with this method will be stored locally in an on-disk cache until they can be delivered to Parse.
They will be sent immediately if possible. Otherwise, they will be sent the next time a network connection is
available. Objects saved this way will persist even after the app is closed, in which case they will be sent the
next time the app is opened / SDK is initialized.

await object.saveEventually(options);
await object.destroyEventually(options);

Advance API

Add Object to Queue

Adding unique hashes to your objects is highly recommended

const object = new Parse.Object('TestObject');
object.save({ hash: 'unique' });

await Parse.EventuallyQueue.save(object, options);
await Parse.EventuallyQueue.destroy(object, options);

Send Queue to Parse Server

await Parse.EventuallyQueue.sendQueue();

You can send the queue either right after adding an object or on connect (or other events).

Clear Queue

await Parse.EventuallyQueue.clear();

Polling

This will call sendQueue when a connection to the server is established. Uses the serverURL used to initialize sdk

Parse.EventuallyQueue.poll();

@codecov
Copy link

codecov bot commented Feb 3, 2021

Codecov Report

Merging #1291 (cf80916) into master (c8270a7) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##            master     #1291    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files           58        59     +1     
  Lines         5650      5788   +138     
  Branches      1273      1299    +26     
==========================================
+ Hits          5650      5788   +138     
Impacted Files Coverage Δ
src/EventuallyQueue.js 100.00% <100.00%> (ø)
src/Parse.js 100.00% <100.00%> (ø)
src/ParseObject.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c8270a7...cf80916. Read the comment docs.

@dplewis dplewis marked this pull request as ready for review February 3, 2021 08:57
@dplewis dplewis requested review from davimacedo and mtrezza and removed request for SebC99 and davimacedo February 5, 2021 23:55
@mtrezza
Copy link
Member

mtrezza commented Feb 6, 2021

Looks good, should this be flagged as experimental in the changelog like we do with Parse Server sometimes?

@dplewis
Copy link
Member Author

dplewis commented Feb 6, 2021

Since we can’t turn it off it’s not experimental.

length = await Parse.EventuallyQueue.length();
assert.strictEqual(length, 0);

// TODO: Properly handle SingleInstance
Copy link
Member Author

@dplewis dplewis Feb 6, 2021

Choose a reason for hiding this comment

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

I’m using JSON.stringify to save the object in local storage. When I retrieve it’s no longer a class instance or same reference as the stored object. Is their a way to fix this? In a singleInstance environment like the browser objects with the same id have the same state and values. I can’t pass obj1 into destroy since it didn’t get an objectId from save.

Copy link
Member

@mtrezza mtrezza Feb 6, 2021

Choose a reason for hiding this comment

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

Not sure I understood this correctly, but Object.create takes the prototype to recreate the instance. Depending on what exactly goes missing, JSON.stringify only stringifies enumerable, own properties, so sometime you may have to write a custom method to serialize/encode and then deserialize/decode an object.

Copy link
Member Author

@dplewis dplewis Feb 6, 2021

Choose a reason for hiding this comment

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

Stringify calls toJSON() internally. I ran into this issue with the LocalDatastore as well I think.

Copy link
Member Author

@dplewis dplewis left a comment

Choose a reason for hiding this comment

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

I have a few questions that I would like feedback on. @mtrezza @davimacedo

Copy link
Member

@mtrezza mtrezza left a comment

Choose a reason for hiding this comment

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

Maybe add a chapter about this in the README so people become aware and try this this out? Otherwise LGTM!

@dplewis dplewis merged commit 3d7d269 into master Feb 10, 2021
@dplewis dplewis deleted the saveEventually branch February 10, 2021 21:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature saveEventually for offline use
2 participants