Skip to content

Commit

Permalink
Add methods to load query data into a result list
Browse files Browse the repository at this point in the history
When a query is fetched on the data source, the data source calls
store.query.loaded(query, entities) to load the entities into the result
list.
  • Loading branch information
danschultz committed Aug 18, 2011
1 parent bd1b24b commit bd7f584
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 23 deletions.
12 changes: 9 additions & 3 deletions src/mesh/core/List.as
Expand Up @@ -9,6 +9,7 @@ package mesh.core
import mx.collections.ArrayList;
import mx.collections.IList;
import mx.events.CollectionEvent;
import mx.events.CollectionEventKind;

/**
* Dispatched when the IList has been updated in some way.
Expand Down Expand Up @@ -36,9 +37,7 @@ package mesh.core
{
super();
_dispatcher = new EventDispatcher(this);

_list = new ArrayList(source);
_list.addEventListener(CollectionEvent.COLLECTION_CHANGE, handleListCollectionChange);
list = new ArrayList(source);
}

/**
Expand Down Expand Up @@ -209,6 +208,13 @@ package mesh.core
return _list.toArray();
}

protected function set list(value:IList):void
{
_list = value != null ? value : new ArrayList();
_list.addEventListener(CollectionEvent.COLLECTION_CHANGE, handleListCollectionChange);
dispatchEvent( new CollectionEvent(CollectionEvent.COLLECTION_CHANGE, false, false, CollectionEventKind.RESET) );
}

/**
* @inheritDoc
*/
Expand Down
51 changes: 39 additions & 12 deletions src/mesh/model/query/Queries.as
@@ -1,8 +1,11 @@
package mesh.model.query
{
import collections.HashMap;
import flash.utils.Dictionary;

import mesh.model.source.Source;
import mesh.model.store.Store;

import mx.collections.IList;

/**
* A class that caches the results of each query in the store.
Expand All @@ -11,17 +14,46 @@ package mesh.model.query
*/
public class Queries
{
private var _cache:Dictionary = new Dictionary();

private var _store:Store;
private var _cache:HashMap = new HashMap();
private var _dataSource:Source;

/**
* Constructor.
*
* @param store The store that owns these queries.
* @param dataSource The store's data source.
*/
public function Queries(store:Store)
public function Queries(store:Store, dataSource:Source)
{
_store = store;
_dataSource = dataSource;
}

/**
* Checks if the given query has been cached.
*
* @param query The query to check.
* @return <code>true</code> if the query was found.
*/
public function contains(query:Query):Boolean
{
return _cache[query] != null;
}

/**
* Called by the data source to load the fetched entities of a query into the result list.
*
* @param query The query that was loaded.
* @param entities The entities that were fetched.
*/
public function loaded(query:Query, entities:IList):void
{
if (!contains(query)) {
throw new ArgumentError("Query '" + query + "' not found in cache.");
}
results(query).loaded(entities);
}

/**
Expand All @@ -31,17 +63,12 @@ package mesh.model.query
* @param query The query to get the results for.
* @return The query's result.
*/
public function result(query:Query):ResultList
public function results(query:Query):ResultList
{
if (!isCached(query)) {
_cache.put(query, new ResultList(query, _store).refresh());
if (!contains(query)) {
_cache[query] = new ResultList(query, _dataSource).refresh();
}
return _cache.grab(query);
}

private function isCached(query:Query):Boolean
{
return _cache.containsKey(query);
return _cache[query];
}
}
}
23 changes: 18 additions & 5 deletions src/mesh/model/query/ResultList.as
@@ -1,7 +1,9 @@
package mesh.model.query
{
import mesh.core.List;
import mesh.model.store.Store;
import mesh.model.source.Source;

import mx.collections.IList;

/**
* The <code>ResultList</code> is a list of entities after an execution of a
Expand All @@ -12,19 +14,29 @@ package mesh.model.query
public class ResultList extends List
{
private var _query:Query;
private var _store:Store;
private var _source:Source;

/**
* Constructor.
*
* @param query The query bound to this result.
* @param store The store that the query is executing against.
* @param store The source that will fetch the query.
*/
public function ResultList(query:Query, store:Store)
public function ResultList(query:Query, source:Source)
{
super();
_query = query;
_store = store;
_source = source;
}

/**
* Used internally by Mesh to load the fetched data from a data source into the result list.
*
* @param list The list of data to populate the result with.
*/
internal function loaded(results:IList):void
{
list = results;
}

/**
Expand All @@ -34,6 +46,7 @@ package mesh.model.query
*/
public function refresh():ResultList
{
_source.fetch(_query);
return this;
}

Expand Down
13 changes: 10 additions & 3 deletions src/mesh/model/store/Store.as
Expand Up @@ -29,8 +29,6 @@ package mesh.model.store
private var _changes:HashSet = new HashSet();
private var _commits:Array = [];

private var _queries:Queries;

private var _dataSource:Source;

/**
Expand Down Expand Up @@ -106,7 +104,7 @@ package mesh.model.store

// A result list is being requested.
if (args[0] is Query) {
return _queries.result(args[0]);
return query.results(args[0]);
}

throw new ArgumentError("Invalid find arguments: " + args);
Expand Down Expand Up @@ -168,6 +166,15 @@ package mesh.model.store
_index.remove(entity);
_changes.remove(entity);
}

private var _queries:Queries;
/**
* The queries and their cached results that have been invoked on this store.
*/
public function get query():Queries
{
return _queries;
}
}
}

Expand Down

0 comments on commit bd7f584

Please sign in to comment.