Dart DocumentationiceStore

Store class

Persistent storage for ICE projects.

Projects are unique by title, which is used to lookup projects in the store. The list of projects is guaranteed to remain ordered by last modification so that the most recently worked on projects are listed first. After every update, the list is sync'd with localStorage to prevent work from being lost.

class Store implements HashMap<String, HashMap> {
 /// The key used to identify the data in localStorage.
 const String codeEditor = 'codeeditor';
 List _projects;

 Store() { }

 static String encode(String string) {
   var gzip = js.context.RawDeflate.deflate(string);
   return CryptoUtils.bytesToBase64(gzip.codeUnits);
 }
 static String decode(String string) {
   var bytes = CryptoUtils.base64StringToBytes(string);
   var gzip = new String.fromCharCodes(bytes);
   return js.context.RawDeflate.inflate(gzip);
 }

 int get length => projects.length;

 HashMap operator [](String key) {
   return projects.
     firstWhere(
       (p) => p['title'] == key,
       orElse: () => null
     );
 }

 void operator []=(String key, HashMap data) {
   data['title'] = key;

   _updateAtIndex(_indexOfKey(key), data);

   _sync();
 }

 int _indexOfKey(String key) => projects.indexOf(this[key]);

 void _updateAtIndex(i, data) {
   if (i == -1) {
     projects.insert(0, data);
   }
   else {
     projects[i] = data;
   }
 }

 bool get isEmpty => projects.isEmpty;
 Iterable<String> get keys => projects.map((p)=> p['title']);
 Iterable<HashMap> get values => projects.map((p)=> p);
 bool containsKey(key) => keys.contains(key);
 bool containsValue(value) => values.contains(value);
 void forEach(f) {
   projects.forEach((p)=> f(p['title'], p));
 }
 HashMap remove(key) {
   var i = _indexOfKey(key);
   if (i == -1) return null;

   var removed = projects.removeAt(i);
   _sync();
   return removed;
 }
 void clear() {
   _projects = [];
   _sync();
 }
 HashMap putIfAbsent(key, f) {
   var i = _indexOfKey(key);
   if (i == -1) {
     this[key] = f();
   }
   return this[key];
 }
 void addAll(recs) {
   recs.forEach((key, rec)=> this[key] = rec);
 }


 /// The list of all projects in the store.
 List get projects {
   if (_projects != null) return _projects;

   var json = window.localStorage[codeEditor];
   return _projects = (json == null) ? [] : JSON.parse(json);
 }

 /// Force the list of projects to refresh itself by reloading from
 /// localStorage.
 void refresh() => _projects = null;

 void _sync() {
   window.localStorage[codeEditor] = JSON.stringify(projects);
 }
}

Implements

HashMap<String, HashMap>

Static Methods

String encode(String string) #

static String encode(String string) {
 var gzip = js.context.RawDeflate.deflate(string);
 return CryptoUtils.bytesToBase64(gzip.codeUnits);
}

String decode(String string) #

static String decode(String string) {
 var bytes = CryptoUtils.base64StringToBytes(string);
 var gzip = new String.fromCharCodes(bytes);
 return js.context.RawDeflate.inflate(gzip);
}

Constructors

new Store() #

Store() { }

Properties

const String codeEditor #

The key used to identify the data in localStorage.

const String codeEditor = 'codeeditor'

final bool isEmpty #

Returns true if there is no {key, value} pair in the map.

docs inherited from Map<K, V>
bool get isEmpty => projects.isEmpty;

final Iterable<String> keys #

The keys of this.

docs inherited from Map<K, V>
Iterable<String> get keys => projects.map((p)=> p['title']);

final int length #

The number of {key, value} pairs in the map.

docs inherited from Map<K, V>
int get length => projects.length;

final List projects #

The list of all projects in the store.

List get projects {
 if (_projects != null) return _projects;

 var json = window.localStorage[codeEditor];
 return _projects = (json == null) ? [] : JSON.parse(json);
}

final Iterable<HashMap> values #

The values of this.

docs inherited from Map<K, V>
Iterable<HashMap> get values => projects.map((p)=> p);

Operators

HashMap operator [](String key) #

Returns the value for the given key or null if key is not in the map. Because null values are supported, one should either use containsKey to distinguish between an absent key and a null value, or use the putIfAbsent method.

docs inherited from Map<K, V>
HashMap operator [](String key) {
 return projects.
   firstWhere(
     (p) => p['title'] == key,
     orElse: () => null
   );
}

void operator []=(String key, HashMap data) #

Associates the key with the given value.

docs inherited from Map<K, V>
void operator []=(String key, HashMap data) {
 data['title'] = key;

 _updateAtIndex(_indexOfKey(key), data);

 _sync();
}

Methods

void addAll(recs) #

void addAll(recs) {
 recs.forEach((key, rec)=> this[key] = rec);
}

void clear() #

Removes all pairs from the map.

docs inherited from Map<K, V>
void clear() {
 _projects = [];
 _sync();
}

bool containsKey(key) #

Returns whether this map contains the given key.

docs inherited from Map<K, V>
bool containsKey(key) => keys.contains(key);

bool containsValue(value) #

Returns whether this map contains the given value.

docs inherited from Map<K, V>
bool containsValue(value) => values.contains(value);

void forEach(f) #

Applies f to each {key, value} pair of the map.

It is an error to add or remove keys from the map during iteration.

docs inherited from Map<K, V>
void forEach(f) {
 projects.forEach((p)=> f(p['title'], p));
}

HashMap putIfAbsent(key, f) #

If key is not associated to a value, calls ifAbsent and updates the map by mapping key to the value returned by ifAbsent. Returns the value in the map.

It is an error to add or remove keys from map during the call to ifAbsent.

docs inherited from Map<K, V>
HashMap putIfAbsent(key, f) {
 var i = _indexOfKey(key);
 if (i == -1) {
   this[key] = f();
 }
 return this[key];
}

void refresh() #

Force the list of projects to refresh itself by reloading from localStorage.

void refresh() => _projects = null;

HashMap remove(key) #

Removes the association for the given key. Returns the value for key in the map or null if key is not in the map. Note that values can be null and a returned null value does not always imply that the key is absent.

docs inherited from Map<K, V>
HashMap remove(key) {
 var i = _indexOfKey(key);
 if (i == -1) return null;

 var removed = projects.removeAt(i);
 _sync();
 return removed;
}

String toString() #

inherited from HashMap

Returns a string representation of this object.

docs inherited from Object
String toString() => Maps.mapToString(this);