-
Notifications
You must be signed in to change notification settings - Fork 585
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 initialSubscriptions
sync config option to bootstrap initial set of flexible sync subscriptions
#4561
Closed
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tomduncalf
force-pushed
the
td/flx-bootstrap-initial-subs
branch
from
May 6, 2022 09:57
9a4832c
to
e5c10d9
Compare
tomduncalf
changed the title
Td/flx bootstrap initial subs
Add May 9, 2022
initialSubscriptions
sync config option to bootstrap initial set of flexible sync subscriptions
tomduncalf
force-pushed
the
td/flx-bootstrap-initial-subs
branch
from
May 9, 2022 10:58
e8f17e3
to
d994a1e
Compare
tomduncalf
requested review from
kneth,
takameyer and
kraenhansen
and removed request for
kneth
May 9, 2022 10:58
Spotted something that needs changing, temporarily closing |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What, How & Why?
This closes #4515, by introducing an
initialSubscriptions
option on the Realm'ssync
config, which allows you to bootstrap an initial set of subscriptions (using the same callback format assubscriptions.update
). This will return a promise, resolved when the new subscriptions have been synchronised – i.e. if the user callsawait Realm.open
, it will not resolve until all the initial subscription data has been synchronised.This is as part of the additive changes to flexible sync, in response to feedback that this a common use case. It's worth pointing out that this functionality should probably reside in core, but it was determined that there was not time to implement that, so for now each SDK is implementing it separately.
By default, the callback is only called when the Realm does not already exist, to allow the user to bootstrap an initial subscription set. The
rerunOnStartup
flag allows the user to rerun the callback every time the Realm is opened – the intended use case for this is that a user could implement e.g. a date range query in their subscription, which is updated every time the app is opened, in order to work around the lack of date range operators in RQL.For example, to implement a subscription for "all people born in the last 30 days", which updates the date range every time you start the app, you could do something like:
One gotcha to be aware of with this is that if you inline your config with the call to
Realm.open
and then try to use therealm
variable that you are declaring inside theupdate
callback, it won't work as it's not yet defined – you need to use the Realm instance provided as a callback argument. I'm not sure if there's anything we can do about this (except try to signpost it if we think it's a big issue?) andonFirstOpen
presumably has the same issue.Alternatives considered
I am open to feedback on the API for this, as I do feel it's perhaps a bit awkward that we are introducing a new concept that is actually quite similar to
onFirstOpen
.initialSubscriptions.update
is presented as a callback for updating subscriptions, but in actual fact the user could do anything in this callback, so it could be a more generic thing. I felt like this was the "least bad" solution in the end – alternatives I considered, and the reasons for rejecting them are:onFirstOpen
config option and making itawait
able (right now it does not return a promise), and adding a counterpartonOpen
which is called every time the Realm opens to cover thererunOnStartup
caseonFirstOpen
does not work for callingsubscriptions.update
, because the call toonFirstOpen
from core is already in a write transaction. Therefore callingsubscriptions.update
from here fails, asupdate
opens its own write transaction.onFirstOpen
is implemented usinginitialization_function
in core, this would require changes to core to add the equivalent foronOpen
(which requires touch a lot of files to pass it through the various configs and layers) and to make itawait
able. We would also need to change the semantics ofonFirstOpen
and/orupdate
with regard to write transactions.onOpen
, with arerunOnOpenOnStartup
flag, which lets the user do initial work they want of any kind – essentially an SDK-sideawait
able replacement foronFirstOpen
)onFirstOpen
– would it be a replacement for it? Or are there some semantic differences?return realm.subscriptions.waitForSynchronization();
from theironOpen
callback, otherwise we wouldn't wait for synchronisation.waitForSynchronisation
flag to do this implicitly, but that feels messy.lib/extensions.js
new Realm
– right now all our config options are supported for bothnew Realm
andRealm.open
so I thought we should uphold this.One other idea I did have was:
initialSubscriptions.update
callback internally wrapped insubscriptions.update
and change its signature toupdate: (subs: MutableSubscriptionSet, realm: Realm)
realm.subscriptions.update
, and it would guide them more clearly to just using this callback for updating subscriptionsrealm
, but looking at it with fresh eyes maybe this is actually a nicer solution? Thoughts welcome!☑️ ToDos
Compatibility
label is updated or copied from previous entryBreaking
label has been applied or is not necessaryIf this PR adds or changes public API's: