Does FalcorJS router support returning promises? - falcor

In the examples (as far as I can tell, maybe I missed some subtlety in the router examples), raw values are returned from the get function. Is there a way to return a callback or promise in the get function? What's the best way to do that?
For now, I'm guessing I use Q.defer?

Examples in the Falcor Router documentation are using promises. For instance:
{
route: 'user.["name", "surname"]',
get: function(pathSet) {
// pathSet is ["user", ["name"]] or ["user", ["surname"]] or ["user", ["name", "surname"]]
if (this.userId == null) {
throw new Error("not authorized");
}
return userService.
get(this.userId).
then(function(user) {
// pathSet[1] is ["name"] or ["surname"] or ["name", "surname"]
return pathSet[1].map(function(key) {
return { path: ["user", key], value: user[key] };
});
});
}
}
userService.get is returning a promise and is not a directly consumable value. So anything that complies with the promise spec will do.

Related

Perform async action only when Option<> is Some

In a piece of code using language-ext library, I can perform an async action only when the Option<> intermediate result is actually filled:
async Task<Option<MyEntity>> FindEntityAsync(string entityId)
{
Option<MyEntity> entityOpt = await GetEntityAsync(entityId);
if (entityOpt.IsSome)
{
await DoSomethingAsync(entityOpt.First());
}
return entityOpt;
}
// Task<Option<MyEntity>> GetEntityAsync(string entityId) { ... }
// DoSomethingAsync could either be:
// Task DoSomethingAsync(MyEntity entity) { ... }
// or:
// Task<Unit> DoSomethingAsync(MyEntity entity)
I'm looking for a more idiomatic way (for such library) to achieve the same.
I tried the following but it does not work:
// look ma! No async/await here
Task<Option<MyEntity>> FindEntityAsync(string entityId)
{
Task<Option<MyEntity>> result =
from entity in GetEntityAsync(entityId)
from _ in DoSomethingAsync(entity).Map(Some)
select entity;
return result;
}
I experienced some LanguageExt.ValueIsNoneException when the Option<> is None.
Ideally I'd like to user an IterXxx type of operator in order to traverse the wrapped Option<MyEntity> only when there is something:
Task<Option<MyEntity>> FindEntityAsync(string entityId)
{
Task<Option<MyEntity>> result = GetEntityAsync(entityId);
result.IterXxxx(async entity => await DoSomethingAsync(entity));
return result;
}
but I cannot find any suitable signature working with an async action. Any hint?
There is special type OptionAsync<T> for combining two monads - Option and Async.
For more info please refer to github - https://github.com/louthy/language-ext/issues/206

How to spy on a Falcor Data Model constructor in Jasmine 1

I am trying to mock the constructor returned by require('falcor'); I have two routes and one calls the other route using var dataModel = new falcor({source: this});
Code looks like so
var falcor = require('falcor');
module.exports = {
route: 'items',
get: function (pathSet) {
var dataModel = new falcor({source: this});
var ids = '1';
dataModel.get('itemIds', ids).then(function (response) {
// Code I can't get to in Jasmine 1.x tests
});
}
}
I want the constructor to return a spy so I can call Promise.resolve and send back mock data for testing purposes. I'm not sure how to do this without moving the call into another module that I can mock separately. I think some questions that may help me here are
Where do I find the constructor functions defined by modules like falcor? I have tried looking into the 'global' object but have had no luck. If I did find this constructor, could I just replace it with a spyOn(global, 'falcor').andReturn(/* object with a mocked get method*/); ?
Is there a better way that makes testing easier to call a route from inside another route?
Thanks for any help.
To start w/ question 2: yes, to get data from another route, return refs to that route. Don't instantiate another model w/i the route. E.g.
const itemsRoute = {
route: 'items[{keys:indices}]',
get(pathSet) {
// map indices to item ids, likely via DB call
// in this case, SomeDataModel handles network requests to your data store and returns a promise
return SomeDataModel.getItemsByIndices(pathSet.indices)
.then(ids => ids.map((id, idx) => ({
path: ['items', pathSet.indices[idx]],
value: {
$type: 'ref',
value: ['itemById', id]
}
})));
}
};
const itemByIdRoute = {
route: 'itemById[{keys:ids}].name',
get(pathSet) {
return new Promise((resolve) => {
resolve(pathSet.idx.map(id => ({
path: ['itemById', id, 'name'],
value: {
$type: 'atom',
value: `I am item w/ id ${id}`
}
})));
});
}
};
When a request comes in for (e.g.) [items, 2, name], it will hit the first items route, resolve [items, 2] to [itemById, someId], and resolve the remaining name key in the itemsById route.
As for question 1: rather than mocking falcor, just mock whatever you are using to make the remote call to your data source. In the above case, just mock SomeDataModel

How to know if a certain future is complete by avoiding a chain of future as return types?

Scenario
If I want to read from a file and store the data in a Map, and if that map is being used multiple times for validation.
Is it possible for me to do this without having to change the return type of all methods, that use the above mentioned map, to Future?
Example:
Map metadata = null
Future readFromFile async {
.... metadata = await File.readingfromFile(...);
}
Future getRegion(..) async {
if(metadata == null) { await readFromFile() }
return metadata["region"]
}
Using the above code if a method(like isValidRegion,etc) that uses and needs getRegion(..) to complete, then the return type of isValidRegion should be converted to Future.
Future<bool> isValidRegion(..) async {
return ((await getRegionData(...)) != null )
}
If that isValidRegion is present within another methods, then the return type of them have to be changed to Future as well.
Future<String> parse(...) async {
....
if(await isValidRegion()) {
...
}
...
}
What is an elegant way to avoid this chain of futures as return types?
Async execution is contagious, there is nothing you can do to get back from async to sync execution.
What you can do is to do the read from the file synchronous to avoid the problem in the first place (if this is possible, if you read it from a network connection, this might not be possible).

How do I change my simple asynchronous Dart function to use the new async keyword?

I have a Dart function that looks like:
Future beAwesome() {
if (notActuallySupported) {
return new Future.error(new UnsupportedError('uh oh'));
}
return new Future.value(42);
}
// ...
beAwesome().then((answer) => print(answer));
I want to use the new async/await functionality. How do I change my function?
In general, add the word async after your function's signature and before the {. Also, return raw values instead of wrapping those values in futures. Also, throw actual exceptions instead of wrapping the errors with a future.
Here's the new version:
Future beAwesome() async {
if (notActuallySupported) {
throw new UnsupportedError('uh oh');
}
return 42;
}
// ...
var answer = await beAwesome();
print(answer);
Note that you should still use Future as the return-type annotation.

indexed_db getObject() - how to return result

I would like to know how to define the data type and how to return the object (record) using getObject(). Currently, the only way that I have been able to use the result (record) outside of the function that obtains it is to call another function with the result. That way, the data-type does not need to be specified. However if I want to return the value, I need to define the data-type and I can't find what it is. I tried "dynamic" but that didn't appear to work. For example ":
fDbSelectOneClient(String sKey, Function fSuccess, String sErmes) {
try {
idb.Transaction oDbTxn = ogDb1.transaction(sgTblClient, 'readwrite');
idb.ObjectStore oDbTable = oDbTxn.objectStore(sgTblClient);
idb.Request oDbReqGet = oDbTable.getObject(sKey);
oDbReqGet.onSuccess.listen((val){
if (oDbReqGet.result == null) {
window.alert("Record $sKey was not found - $sErmes");
} else {
///////return oDbReqGet.result; /// THIS IS WHAT i WANT TO DO
fSuccess(oDbReqGet.result); /// THIS IS WHAT i'm HAVING TO DO
}});
oDbReqGet.onError.first.then((e){window.alert(
"Error reading single Client. Key = $sKey. Error = ${e}");});
} catch (oError) {
window.alert("Error attempting to read record for Client $sKey.
Error = ${oError}");
}
}
fAfterAddOrUpdateClient(oDbRec) {
/// this is one of the functions used as "fSuccess above
As someone else once said (can't remember who), once you start using an async API, everything needs to be async.
A typical "Dart" pattern to do this would be to use a Future + Completer pair (although there's nothing inherently wrong with what you've done in your question above - it's more a question of style...).
Conceptually, the fDbSelectOneClient function creates a completer object, and the function returns the completer.future. Then, when the async call completes, you call completer.complete, passing the value in.
A user of the function would call fDbSelectOneClient(...).then((result) => print(result)); to make use of the result in an async way
Your code above could be refactored as follows:
import 'dart:async'; // required for Completer
Future fDbSelectOneClient(String sKey) {
var completer = new Completer();
try {
idb.Transaction oDbTxn = ogDb1.transaction(sgTblClient, 'readwrite');
idb.ObjectStore oDbTable = oDbTxn.objectStore(sgTblClient);
idb.Request oDbReqGet = oDbTable.getObject(sKey);
oDbReqGet.onSuccess.listen((val) => completer.complete(oDbReqGet.result));
oDbReqGet.onError.first.then((err) => completer.completeError(err));
}
catch (oError) {
completer.completeError(oError);
}
return completer.future; // return the future
}
// calling code elsewhere
foo() {
var key = "Mr Blue";
fDbSelectOneClient(key)
.then((result) {
// do something with result (note, may be null)
})
..catchError((err) { // note method chaining ..
// do something with error
};
}
This future/completer pair only works for one shot (ie, if the onSuccess.listen is called multiple times, then the second time you will get a "Future already completed" error. (I've made an assumption on the basis of the function name fDbSelectOneClient that you are only expecting to select a single record.
To return a value from a single future multiple times, you'll probably have to use the new streams feature of the Future - see here for more details: http://news.dartlang.org/2012/11/introducing-new-streams-api.html
Note also, that Futures and Completers support generics, so you can strongly type the return type as follows:
// strongly typed future
Future<SomeReturnType> fDbSelectOneClient(String sKey) {
var completer = new Completer<SomeReturnType>();
completer.complete(new SomeReturnType());
}
foo() {
// strongly typed result
fDbSelectOneClient("Mr Blue").then((SomeReturnType result) => print(result));
}

Resources