Skip to content

Commit

Permalink
Merge pull request #2 from thomax-it/non-recursive-pinning
Browse files Browse the repository at this point in the history
Non recursive pinning
  • Loading branch information
fischerscode authored Oct 3, 2019
2 parents 5dfc6e5 + 3663314 commit e10cef2
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 38 deletions.
14 changes: 14 additions & 0 deletions coroutines/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ launch { // Coroutine builder
```
We use a coroutine builder because `find()` is a suspend function.

We can also, use a function like a coroutine builder, it will be provider us a flexibility call our query without any extensions function.

````kotlin
launchQuery(query) {
// doing operations like find, get, first and count
}
````

It uses a a regular coroutine builder `launch` and pass as receiver a `ParseQueryOperation``

### ParseCloud

We can call cloud function inline:
Expand Down Expand Up @@ -60,6 +70,10 @@ launch { // Coroutine builder
}
```

### Parse Object

We can save, pinning and fetch parse objects use coroutines as well.

## Contributing
When contributing to the `coroutines` module, please first consider if the extension function you are wanting to add would potentially be better suited in the main `parse` module. If it is something specific to Kotlin users or only useful in a Kotlin project, feel free to make a PR adding it to this module. Otherwise, consider adding the addition to the `parse` module itself, so that it is still usable in Java.

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@file:JvmName("ParseCloudCoroutinesExtensions")

package com.parse.coroutines
package com.parse.coroutines.cloudfunction

import com.parse.ParseCloud
import kotlin.coroutines.resume
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.parse.coroutines.read

interface ParseQueryOperation<out T> {
suspend fun find(): List<T>
suspend fun get(id: String): T
suspend fun first(): T
suspend fun count(): Int
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.parse.coroutines.read

import com.parse.ParseObject
import com.parse.ParseQuery
import com.parse.coroutines.read.query.countInternal
import com.parse.coroutines.read.query.findInternal
import com.parse.coroutines.read.query.firstInternal
import com.parse.coroutines.read.query.getInternal

class ParseQueryOperationImpl<T : ParseObject>(private val query: ParseQuery<T>) : ParseQueryOperation<T> {

override suspend fun find(): List<T> = query.findInternal()

override suspend fun get(id: String): T = query.getInternal(id)

override suspend fun first(): T = query.firstInternal()

override suspend fun count(): Int = query.countInternal()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@file:JvmName("ParseObjectCoroutinesExtensions")
@file:Suppress("EXTENSION_SHADOWED_BY_MEMBER")

package com.parse.coroutines.read.parse_object

import com.parse.ParseObject
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

suspend fun <T : ParseObject> ParseObject.fetch(): T {
return suspendCoroutine { continuation ->

fetchInBackground<T> { obj, e ->
if (e == null) continuation.resume(obj)
else continuation.resumeWithException(e)
}
}
}

suspend fun <T : ParseObject> ParseObject.fetchIfNeeded(): T {
return suspendCoroutine { continuation ->

fetchIfNeededInBackground<T> { obj, e ->
if (e == null) continuation.resume(obj)
else continuation.resumeWithException(e)
}
}
}

suspend fun <T : ParseObject> ParseObject.fetchFromLocal(): T {
return suspendCoroutine { continuation ->

fetchFromLocalDatastoreInBackground<T> { obj, e ->
if (e == null) continuation.resume(obj)
else continuation.resumeWithException(e)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@file:JvmName("ParseQueryCoroutinesBuilder")
@file:Suppress("EXTENSION_SHADOWED_BY_MEMBER")

package com.parse.coroutines.read.query

import com.parse.ParseObject
import com.parse.ParseQuery
import com.parse.coroutines.read.ParseQueryOperation
import com.parse.coroutines.read.ParseQueryOperationImpl
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext

fun <T : ParseObject> CoroutineScope.launchQuery(
query: ParseQuery<T>,
context: CoroutineContext = EmptyCoroutineContext,
block: suspend ParseQueryOperation<T>.() -> Unit
) : Job {
return launch(context) {
block.invoke(ParseQueryOperationImpl(query))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
@file:JvmName("ParseQueryCoroutinesExtensions")

package com.parse.coroutines.read.query

import com.parse.ParseObject
import com.parse.ParseQuery
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException

suspend fun <T : ParseObject> ParseQuery<T>.find(): List<T> {
return findInternal()
}

internal suspend fun <T : ParseObject> ParseQuery<T>.findInternal(): List<T> {
return suspendCancellableCoroutine { continuation ->
continuation.invokeOnCancellation {
cancel()
}

findInBackground { objects, e ->
if (e == null) continuation.resume(objects)
else continuation.resumeWithException(e)
}
}
}

suspend fun <T : ParseObject> ParseQuery<T>.get(id: String): T {
return getInternal(id)
}

internal suspend fun <T : ParseObject> ParseQuery<T>.getInternal(id: String): T {
return suspendCancellableCoroutine { continuation ->
continuation.invokeOnCancellation {
cancel()
}

getInBackground(id) { obj, e ->
if (e == null) continuation.resume(obj)
else continuation.resumeWithException(e)
}
}
}

suspend fun <T : ParseObject> ParseQuery<T>.first(): T {
return firstInternal()
}

internal suspend fun <T : ParseObject> ParseQuery<T>.firstInternal(): T {
return suspendCancellableCoroutine { continuation ->
continuation.invokeOnCancellation {
cancel()
}

getFirstInBackground { obj, e ->
if (e == null) continuation.resume(obj)
else continuation.resumeWithException(e)
}
}
}

suspend fun <T : ParseObject> ParseQuery<T>.count(): Int {
return countInternal()
}

internal suspend fun <T : ParseObject> ParseQuery<T>.countInternal(): Int {
return suspendCancellableCoroutine { continuation ->
continuation.invokeOnCancellation {
cancel()
}

countInBackground { count, e ->
if (e == null) continuation.resume(count)
else continuation.resumeWithException(e)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@file:JvmName("ParseUserCoroutinesExtensions")

package com.parse.coroutines
package com.parse.coroutines.user

import com.parse.ParseUser
import kotlin.coroutines.resume
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@file:JvmName("ParseObjectCoroutinesWriteExtensions")
@file:Suppress("EXTENSION_SHADOWED_BY_MEMBER")

package com.parse.coroutines.write.parse_object

import com.parse.ParseObject
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

suspend fun ParseObject.save() {
return suspendCoroutine { continuation ->
saveInBackground {
if (it == null) continuation.resume(Unit)
else continuation.resumeWithException(it)
}
}
}

suspend fun ParseObject.pin() {
return suspendCoroutine { continuation ->
pinInBackground {
if (it == null) continuation.resume(Unit)
else continuation.resumeWithException(it)
}
}
}

0 comments on commit e10cef2

Please sign in to comment.