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
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 #
final Iterable<String> keys #
final int 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); }
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.
HashMap operator [](String key) { return projects. firstWhere( (p) => p['title'] == key, orElse: () => null ); }
Methods
void addAll(recs) #
void addAll(recs) { recs.forEach((key, rec)=> this[key] = rec); }
void clear() #
bool containsKey(key) #
bool containsValue(value) #
void forEach(f) #
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
.
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.
HashMap remove(key) { var i = _indexOfKey(key); if (i == -1) return null; var removed = projects.removeAt(i); _sync(); return removed; }
String toString() #
Returns a string representation of this object.
String toString() => Maps.mapToString(this);