kevinresol / coconut.ds Goto Github PK
View Code? Open in Web Editor NEWData structures for Coconut
Data structures for Coconut
Suppose server response looks like this:
typedef UserData = {
id:String,
name:String,
}
then the data should provide API like this:
var user = new User({id:id, updater:<some func>});
user.refresh();
user.isInTransition.nextTime(v -> !v).handle(function(_) trace(user.name)); // 'old name'
user.update({name: 'new name'}) // use updater to send update to server
.handle(function(o) switch o {
case Success(_):
// ideally this doesn't need to query the server again
trace(user.name); // 'new name'
case Failure(e):
});
Basically, we use this to represent some remote data:
class User implements Model {
@:constant var id:String;
@:loaded var data:UserData = Server.loadUser(id);
}
var user = new User({id: 'the-id'});
But in some occasions, we have already fetched UserData
from somewhere else and would like to create User
from there, so that it doesn't need to load the server again.
class User implements Model {
@:constant var id:String;
@:constant var _data:UserData = @byDefault null;
@:loaded var data:UserData = _data == null ? _data : Server.loadUser(id);
}
var user = new User({id: prefetchedData.id, _data: prefetchedData});
But this doesn't look very elegant. Mostly because of the two different identifiers (_data
and data
)
class User implements Model {
@:constant var id:String;
@:constant var load:String->UserData;
@:loaded var data:UserData = load(id);
}
var user = new User({id: prefetchedData.id, load: function(id) id == prefetchedData.id ? prefetchedData : Server.loadUser(id)});
Well the above was my flow of thought. I think the desired API looks something like this:
var user = new User({id: id}); // init from id
$type(user.data); // Promised<UserData> or UserData+isInTransaction
var user = new User({data: data}); // use prefetched data
user.purge(); // kill the cache
user.data; // now it should load fresh from server
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.