jameslaneconkling / falcor-local-datasource Goto Github PK
View Code? Open in Web Editor NEWFalcor DataSource interface exposing a local JSONGraph object
Falcor DataSource interface exposing a local JSONGraph object
to log the returned JSONGraphEnvelope from get/set/call. This could help understand the relationship between:
Sometimes I wonder if anyone tests out their posted code. In your example, under the todos key, you use ids 'id_0' and 'id_1', while under the key todosById you use ids 'id_1' and 'id_2'.
Version 1.2.0 (the latest, as far as I know) - in utils.js function isPathValues() fails to check if it has been passed an array. So, if you pass it a JSONGraphEnvelope, which is an object, an exception is thrown when the function calls .reduce() on it. A simple "Array.isArray(pathValues) &&" preceding that fixes the problem.
The sample code seems to work fine, but the problem is that if you ask for the number of todos before AND between the 2 calls, you'll get 2. However, if you ask for the number of todos AFTER the 2 calls, you'll get 4! Here is my code. Note that I used the code in the README file to set up the model, but used (my preferred) async/await to query the model. I did need to make these changes:
const falcor = require('falcor');
// const LocalDatasource = require('../src/index');
const LocalDatasource = require('falcor-local-datasource');
async function main() {
const graph = {
todos: {
0: { $type: 'ref', value: ['todosById', 'id_0'] },
1: { $type: 'ref', value: ['todosById', 'id_1'] },
length: 2,
add(graph, args) {
const newTodoLabel = args[0];
const todoCount = graph.todos.length;
// NOTE: this is a pretty naive way to derive new ids. a more robust approach might generate unique ids using
// a specific node in the graph, or use a closure, or some other mechanism to yield unique incremental values
const newId = `id_${todoCount}`;
// return array of pathValues
return [
{
path: ['todos', todoCount],
value: { $type: 'ref', value: ['todosById', newId] }
},
{
path: ['todosById', newId, 'label'],
value: newTodoLabel
},
{
path: ['todosById', newId, 'completed'],
value: false
},
{
path: ['todos', 'length'],
value: todoCount + 1
}
];
}
},
todosById: {
id_0: { label: 'tap dance', completed: false },
id_1: { label: 'see above', completed: false }
}
};
const model = new falcor.Model({ source: new LocalDatasource(graph) });
try {
let n = await model.getValue('todos.length');
console.log(`1. There are ${n} todos`);
await model.call(['todos', 'add'], ['dupstep dance party']);
n = await model.getValue('todos.length');
console.log(`2. There are ${n} todos`);
await model.call(['todos', 'add'], ['jumpstyle'],
[[['label', 'completed']]],
[['length']]);
n = await model.getValue('todos.length');
console.log(`3. There are ${n} todos`);
}
catch (err) {
console.error(err);
}
} // main()
main();
Now, there is a way to fix this code, i.e. get 2, then 3, then 4 for the number of todos, but I don't think I should need to do this. The fix is to change the first "call" to:
await model.call(['todos', 'add'], ['dupstep dance party'], [], [['length']]);
The way I understand the 4th argument is that it specifies values that I need in the code that uses model.call(), i.e. the code above, and I don't need it since I'm going to make another model.call() immediately after. However, the JavaScript function that implements the "add" operation returns 4 paths that should force the local cache to update its value for todos.length, and it doesn't seem to be doing that.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.