This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Improved API support for Firebase Database. #89
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -118,38 +118,95 @@ public void onDataChange(DataSnapshot snapshot) { | |
| @SuppressWarnings("unchecked") | ||
| @Override | ||
| public void onMethodCall(MethodCall call, final Result result) { | ||
| Map<String, ?> arguments = (Map<String, ?>) call.arguments; | ||
| if (call.method.equals("DatabaseReference#set")) { | ||
| Object value = arguments.get("value"); | ||
| getReference(arguments).setValue(value, new DefaultCompletionListener(result)); | ||
| } else if (call.method.equals("Query#observe")) { | ||
| String eventType = (String) arguments.get("eventType"); | ||
| int handle = nextHandle++; | ||
| EventObserver observer = new EventObserver(eventType, handle); | ||
| observers.put(handle, observer); | ||
| if (eventType.equals(EVENT_TYPE_VALUE)) { | ||
| getReference(arguments).addValueEventListener(observer); | ||
| } else { | ||
| getReference(arguments).addChildEventListener(observer); | ||
| } | ||
| result.success(handle); | ||
| } else if (call.method.equals("Query#removeObserver")) { | ||
| DatabaseReference reference = getReference(arguments); | ||
| int handle = (Integer) arguments.get("handle"); | ||
| EventObserver observer = observers.get(handle); | ||
| if (observer != null) { | ||
| if (observer.requestedEventType.equals(EVENT_TYPE_VALUE)) { | ||
| reference.removeEventListener((ValueEventListener) observer); | ||
| } else { | ||
| reference.removeEventListener((ChildEventListener) observer); | ||
| Map<String, Object> arguments = (Map<String, Object>) call.arguments; | ||
| switch (call.method) { | ||
| case "FirebaseDatabase#goOnline": | ||
| FirebaseDatabase.getInstance().goOnline(); | ||
| break; | ||
|
|
||
| case "FirebaseDatabase#goOffline": | ||
| FirebaseDatabase.getInstance().goOffline(); | ||
| break; | ||
|
|
||
| case "FirebaseDatabase#purgeOutstandingWrites": | ||
| FirebaseDatabase.getInstance().purgeOutstandingWrites(); | ||
| break; | ||
|
|
||
| case "FirebaseDatabase#setPersistenceEnabled": | ||
| { | ||
| boolean isEnabled = (boolean) arguments.get("enabled"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit] You can avoid cast with Boolean isEnabled = arguments.get("enabled");Similarly below. |
||
| FirebaseDatabase.getInstance().setPersistenceEnabled(isEnabled); | ||
| break; | ||
| } | ||
|
|
||
| case "FirebaseDatabase#setPersistenceCacheSizeBytes": | ||
| { | ||
| long cacheSize = (long) arguments.get("cacheSize"); | ||
| FirebaseDatabase.getInstance().setPersistenceCacheSizeBytes(cacheSize); | ||
| break; | ||
| } | ||
|
|
||
| case "DatabaseReference#set": | ||
| { | ||
| Object value = arguments.get("value"); | ||
| Object priority = arguments.get("priority"); | ||
| DatabaseReference reference = getReference(arguments); | ||
| if (priority != null) { | ||
| reference.setValue(value, priority, new DefaultCompletionListener(result)); | ||
| } else { | ||
| reference.setValue(value, new DefaultCompletionListener(result)); | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| case "DatabaseReference#setPriority": | ||
| { | ||
| Object priority = arguments.get("priority"); | ||
| DatabaseReference reference = getReference(arguments); | ||
| reference.setPriority(priority, new DefaultCompletionListener(result)); | ||
| break; | ||
| } | ||
|
|
||
| case "Query#observe": | ||
| { | ||
| String eventType = (String) arguments.get("eventType"); | ||
| int handle = nextHandle++; | ||
| EventObserver observer = new EventObserver(eventType, handle); | ||
| observers.put(handle, observer); | ||
| if (eventType.equals(EVENT_TYPE_VALUE)) { | ||
| getReference(arguments).addValueEventListener(observer); | ||
| } else { | ||
| getReference(arguments).addChildEventListener(observer); | ||
| } | ||
| result.success(handle); | ||
| break; | ||
| } | ||
|
|
||
| case "Query#removeObserver": | ||
| { | ||
| DatabaseReference reference = getReference(arguments); | ||
| int handle = (Integer) arguments.get("handle"); | ||
| EventObserver observer = observers.get(handle); | ||
| if (observer != null) { | ||
| if (observer.requestedEventType.equals(EVENT_TYPE_VALUE)) { | ||
| reference.removeEventListener((ValueEventListener) observer); | ||
| } else { | ||
| reference.removeEventListener((ChildEventListener) observer); | ||
| } | ||
| observers.delete(handle); | ||
| result.success(null); | ||
| break; | ||
| } else { | ||
| result.error("unknown_handle", "removeObserver called on an unknown handle", null); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| default: | ||
| { | ||
| result.notImplemented(); | ||
| break; | ||
| } | ||
| observers.delete(handle); | ||
| result.success(null); | ||
| } else { | ||
| result.error("unknown_handle", "removeObserver called on an unknown handle", null); | ||
| } | ||
| } else { | ||
| result.notImplemented(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,7 @@ part of firebase_database; | |
| /// `DatabaseReference`s (ie. `child`). | ||
| class DatabaseReference extends Query { | ||
| DatabaseReference._(FirebaseDatabase database, List<String> pathComponents) | ||
| : super._(database, pathComponents); | ||
| : super._(database: database, pathComponents: pathComponents); | ||
|
|
||
| /// Gets a DatabaseReference for the location at the specified relative | ||
| /// path. The relative path can either be a simple child key (e.g. ‘fred’) or | ||
|
|
@@ -60,7 +60,7 @@ class DatabaseReference extends Query { | |
| return new DatabaseReference._(_database, childPath); | ||
| } | ||
|
|
||
| /// Write data to this Firebase Database location. | ||
| /// Write `value` to the location with the specified `priority` if applicable. | ||
| /// | ||
| /// This will overwrite any data at this location and all child locations. | ||
| /// | ||
|
|
@@ -72,12 +72,53 @@ class DatabaseReference extends Query { | |
| /// | ||
| /// Passing null for the new value means all data at this location or any | ||
| /// child location will be deleted. | ||
| Future<Null> set(dynamic value) async { | ||
| await _database._channel.invokeMethod( | ||
| Future<Null> set(dynamic value, { dynamic priority }) { | ||
| return _database._channel.invokeMethod( | ||
| 'DatabaseReference#set', | ||
| { 'path': path, 'value': value }, | ||
| { 'path': path, 'value': value, 'priority': priority }, | ||
| ); | ||
| } | ||
|
|
||
| /// Sets a priority for the data at this Firebase Database location. | ||
| /// | ||
| /// Priorities can be used to provide a custom ordering for the children at a | ||
| /// location (if no priorities are specified, the children are ordered by | ||
| /// key). | ||
| /// | ||
| /// You cannot set a priority on an empty location. For this reason | ||
| /// set() should be used when setting initial data with a specific priority | ||
| /// and setPriority() should be used when updating the priority of existing | ||
| /// data. | ||
| /// | ||
| /// Children are sorted based on this priority using the following rules: | ||
| /// | ||
| /// Children with no priority come first. Children with a number as their | ||
| /// priority come next. They are sorted numerically by priority (small to | ||
| /// large). Children with a string as their priority come last. They are | ||
| /// sorted lexicographically by priority. Whenever two children have the same | ||
| /// priority (including no priority), they are sorted by key. Numeric keys | ||
| /// come first (sorted numerically), followed by the remaining keys (sorted | ||
| /// lexicographically). | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing |
||
| /// Note that priorities are parsed and ordered as IEEE 754 double-precision | ||
| /// floating-point numbers. Keys are always stored as strings and are treated | ||
| /// as numbers only when they can be parsed as a 32-bit integer | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit] Missing |
||
| Future<Null> setPriority(dynamic priority) async { | ||
| return _database._channel.invokeMethod( | ||
| 'DatabaseReference#setPriority', | ||
| { 'path': path, 'priority': priority }, | ||
| ); | ||
| } | ||
|
|
||
| /// Remove the data at this Firebase Database location. Any data at child | ||
| /// locations will also be deleted. | ||
| /// | ||
| /// The effect of the delete will be visible immediately and the corresponding | ||
| /// events will be triggered. Synchronization of the delete to the Firebase | ||
| /// Database servers will also be started. | ||
| /// | ||
| /// remove() is equivalent to calling set(null) | ||
| Future<Null> remove() => set(null); | ||
| } | ||
| class ServerValue { | ||
| static const timestamp = const {'.sv' : 'timestamp'}; | ||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid the unchecked cast warning, do