Skip to content

Commit

Permalink
Add class to manage queuing and creation of commits
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultz committed Aug 20, 2011
1 parent 86aa999 commit 68cff01
Show file tree
Hide file tree
Showing 9 changed files with 389 additions and 154 deletions.
14 changes: 1 addition & 13 deletions src/mesh/core/List.as
Expand Up @@ -28,7 +28,7 @@ package mesh.core
public class List extends Proxy implements IList, IEventDispatcher
{
private var _dispatcher:EventDispatcher;
private var _list:ArrayList;
private var _list:IList;

/**
* @copy mx.collections.ArrayList#ArrayList()
Expand Down Expand Up @@ -232,18 +232,6 @@ package mesh.core
return _list.length;
}

/**
* @copy mx.collections.ArrayList#source
*/
public function get source():Array
{
return _list.source;
}
public function set source(value:Array):void
{
_list.source = value;
}

// Proxy methods to support for each..in loops.

/**
Expand Down
37 changes: 17 additions & 20 deletions src/mesh/model/source/FixtureSource.as
Expand Up @@ -5,7 +5,10 @@ package mesh.model.source

import mesh.core.object.merge;
import mesh.model.Entity;
import mesh.model.store.Commit;
import mesh.model.store.Store;

import mx.rpc.Fault;

public class FixtureSource extends Source
{
Expand All @@ -27,35 +30,34 @@ package mesh.model.source
/**
* @inheritDoc
*/
override public function create(store:Store, entity:Entity):void
override public function create(commit:Commit, entity:Entity):void
{
entity.busy();

var data:Object = serialize([entity])[0];
setTimeout(function():void
{
if (!entity.isNew) {
update(store, entity);
update(commit, entity);
} else {
synced([entity], [++_idCounter]);
entity.id = ++_idCounter;
_fixtures[entity.id] = data;
commit.completed([entity]);
}
}, latency);
}

/**
* @inheritDoc
*/
override public function destroy(store:Store, entity:Entity):void
override public function destroy(commit:Commit, entity:Entity):void
{
entity.busy();

var data:Object = serialize([entity])[0];
setTimeout(function():void
{
if (_fixtures[data.id] != null) {
delete _fixtures[data.id];
synced([entity]);
commit.completed([entity]);
} else {
errored([entity]);
commit.failed([entity], new Fault("", "Entity '" + entity.reflect.name + "' with ID=" + data.id + " does not exist"));
}
}, latency);
}
Expand All @@ -65,14 +67,11 @@ package mesh.model.source
*/
override public function retrieve(store:Store, entity:Entity):void
{
entity.busy();

var data:Object = serialize([entity])[0];
setTimeout(function():void
{
if (_fixtures[data.id] != null) {
//entity.deserialize(_fixtures[data.id]);
synced([entity]);
} else {
entity.errored();
}
Expand All @@ -82,18 +81,16 @@ package mesh.model.source
/**
* @inheritDoc
*/
override public function update(store:Store, entity:Entity):void
override public function update(commit:Commit, entity:Entity):void
{
entity.busy();

var data:Object = serialize([entity])[0];
setTimeout(function():void
{
if (data.id != null && data.id > 0) {
_fixtures[data.id] = data;
synced([entity]);
if (_fixtures[data.id] == null) {
commit.failed([entity], new Fault("", "Entity '" + entity.reflect.name + "' with ID=" + data.id + " does not exist"));
} else {
errored([entity]);
_fixtures[data.id] = data;
commit.completed([entity]);
}
}, latency);
}
Expand Down
33 changes: 17 additions & 16 deletions src/mesh/model/source/MultiSource.as
Expand Up @@ -5,6 +5,7 @@ package mesh.model.source

import mesh.core.reflection.reflect;
import mesh.model.Entity;
import mesh.model.store.Commit;
import mesh.model.store.Store;

/**
Expand Down Expand Up @@ -48,33 +49,33 @@ package mesh.model.source
/**
* @inheritDoc
*/
override public function create(store:Store, entity:Entity):void
override public function create(commit:Commit, entity:Entity):void
{
invoke(store, "create", entity);
invoke(commit, "create", entity);
}

/**
* @inheritDoc
*/
override public function createEach(store:Store, entities:Array):void
override public function createEach(commit:Commit, entities:Array):void
{
invokeEach(store, "createEach", entities);
invokeEach(commit, "createEach", entities);
}

/**
* @inheritDoc
*/
override public function destroy(store:Store, entity:Entity):void
override public function destroy(commit:Commit, entity:Entity):void
{
invoke(store, "destroy", entity);
invoke(commit, "destroy", entity);
}

/**
* @inheritDoc
*/
override public function destroyEach(store:Store, entities:Array):void
override public function destroyEach(commit:Commit, entities:Array):void
{
invokeEach(store, "destroyEach", entities);
invokeEach(commit, "destroyEach", entities);
}

/**
Expand All @@ -96,17 +97,17 @@ package mesh.model.source
/**
* @inheritDoc
*/
override public function update(store:Store, entity:Entity):void
override public function update(commit:Commit, entity:Entity):void
{
invoke(store, "update", entity);
invoke(commit, "update", entity);
}

/**
* @inheritDoc
*/
override public function updateEach(store:Store, entities:Array):void
override public function updateEach(commit:Commit, entities:Array):void
{
invokeEach(store, "updateEach", entities);
invokeEach(commit, "updateEach", entities);
}

/**
Expand All @@ -121,19 +122,19 @@ package mesh.model.source
return _mapping[entity];
}

private function invoke(store:Store, method:String, entity:Entity):void
private function invoke(storeOrCommit:Object, method:String, entity:Entity):void
{
var type:Class = entity.reflect.clazz;
throwIfUnmapped(type);
sourceFor(type)[method](store, entity);
sourceFor(type)[method](storeOrCommit, entity);
}

private function invokeEach(store:Store, method:String, entities:Array):void
private function invokeEach(storeOrCommit:Object, method:String, entities:Array):void
{
var grouped:Dictionary = groupByType(entities);
for (var type:* in grouped) {
throwIfUnmapped(type);
sourceFor(type)[method](store, grouped[type]);
sourceFor(type)[method](storeOrCommit, grouped[type]);
}
}

Expand Down
87 changes: 20 additions & 67 deletions src/mesh/model/source/Source.as
Expand Up @@ -4,6 +4,7 @@ package mesh.model.source

import mesh.core.reflection.reflect;
import mesh.model.Entity;
import mesh.model.store.Commit;
import mesh.model.store.Query;
import mesh.model.store.Store;

Expand All @@ -24,45 +25,42 @@ package mesh.model.source

}

public function commit(store:Store, entities:Array):void
public function commit(commit:Commit):void
{
createEach(store, entities.filter(function(entity:Entity, ...args):Boolean
{
return entity.isNew && entity.isDirty;
}));
if (commit.create.length > 0) {
createEach(commit, commit.create);
}

updateEach(store, entities.filter(function(entity:Entity, ...args):Boolean
{
return entity.isPersisted && entity.isDirty;
}));
if (commit.update.length > 0) {
updateEach(commit, commit.update);
}

destroyEach(store, entities.filter(function(entity:Entity, ...args):Boolean
{
return entity.isDestroyed && entity.isDirty;
}));
if (commit.destroy.length > 0) {
destroyEach(commit, commit.destroy);
}
}

public function create(store:Store, entity:Entity):void
public function create(commit:Commit, entity:Entity):void
{
throw new IllegalOperationError("EntitySource.create() is not implemented.");
}

public function createEach(store:Store, entities:Array):void
public function createEach(commit:Commit, entities:Array):void
{
for each (var entity:Entity in entities) {
create(store, entity);
create(commit, entity);
}
}

public function destroy(store:Store, entity:Entity):void
public function destroy(commit:Commit, entity:Entity):void
{
throw new IllegalOperationError("EntitySource.destroy() is not implemented.");
}

public function destroyEach(store:Store, entities:Array):void
public function destroyEach(commit:Commit, entities:Array):void
{
for each (var entity:Entity in entities) {
destroy(store, entity);
destroy(commit, entity);
}
}

Expand All @@ -83,60 +81,15 @@ package mesh.model.source
}
}

public function update(store:Store, entity:Entity):void
public function update(commit:Commit, entity:Entity):void
{
throw new IllegalOperationError("EntitySource.update() is not implemented.");
}

public function updateEach(store:Store, entities:Array):void
{
for each (var entity:Entity in entities) {
update(store, entity);
}
}

/**
* Puts a list of entities into a busy state, which signifies that they're in the process
* of being created, updated or destroyed from the data source.
*
* @param entities The entities to put into a busy state.
*/
protected function busy(entities:Array):void
{
for each (var entity:Entity in entities) {
entity.busy();
}
}

/**
* Marks a list of entities as being synced, which signifies that the entity's data and the
* data source's data are the same. You may optionally pass a set of IDs to set on the
* entities.
*
* @param entities The entities to mark as synced.
* @param ids A list of IDs to populate the entities with.
*/
protected function synced(entities:Array, ids:Array = null):void
{
var len:int = entities.length;
for (var i:int = 0; i < len; i++) {
var entity:Entity = (entities[i] as Entity);
if (ids != null) {
entity.id = ids[i];
}
entity.synced();
}
}

/**
* Marks a list of entities as erroring out while attempting to commit their data.
*
* @param entities The entities to mark as errored.
*/
protected function errored(entities:Array):void
public function updateEach(commit:Commit, entities:Array):void
{
for each (var entity:Entity in entities) {
entity.errored();
update(commit, entity);
}
}

Expand Down

0 comments on commit 68cff01

Please sign in to comment.