dart - Prevent to read file many times -
i trying write i18n app. programme read json file, contains translation languages , based on json structure.
{ "en": { "text1": "hello", "text2": "march" }, "de": { "text1": "hallo", "text2": "märz" } } my programme read json file in async way file class, whole code
class="lang-dart prettyprint-override">import 'dart:io'; import 'dart:async'; import 'package:json_object/json_object.dart'; abstract class i18n { static _i18n _i18n; mill i18n(string file, string lang) { if(_i18n == null) { _i18n = new _i18n(file, lang); homecoming _i18n; } homecoming _i18n; } future<string> gettextbymap(string textid); } class _i18n implements i18n { file _file; string _lang; jsonobject _jsoncontainer; jsonobject _jsonfiltered; future<jsonobject> _imme; // parameters: // file: whole path , filename // lang: expected language _i18n(string file, this._lang) { this._file = new file(file); } // read file , homecoming content of file. future<string> _readfilefromstream() { var com = new completer(); this._file.exists() .then((fileexists) { if(!fileexists) { throw new stateerror('file not found'); } homecoming this._file.readasstring() .then((stream) => com.complete(stream)); }); homecoming com.future; } void _convertcontenttojson(string stream) { this._jsoncontainer = new jsonobject.fromjsonstring(stream); } future<jsonobject> _preparedata() { homecoming this._readfilefromstream().then((stream) { _convertcontenttojson(stream); this._jsonfiltered = this._jsoncontainer[this._lang]; homecoming this._jsonfiltered; }); } future<string> gettextbymap(string textid) { homecoming this._preparedata().then((filterd) { homecoming filterd[textid]; }); } } and main code
class="lang-dart prettyprint-override">import 'package:i18n/i18n.dart'; void main() { var i18n = new i18n('../hello.json', 'en'); i18n.gettextbymap('text1').then((val) => print(val)); i18n.gettextbymap('text2').then((val) => print(val)); } everything here, happen in dart async way, read json file etc. , everytime, when phone call method
class="lang-dart prettyprint-override">i18n.gettextbymap('text1').then((val) => print(val)); it gonna read json file 1 time again , again. tried rewrite method prevent reading json file many times
class="lang-dart prettyprint-override">future<string> gettextbymap(string textid) { if(this._jsonfiltered == null) { homecoming this._preparedata().then((filterd) { homecoming filterd[textid]; }); } homecoming new future(() => this._jsonfiltered[textid]); } but doesn't work too, because dart works in async way.
my question is, how can maintain json file content in object? read json file 1 time , maintain contents in object, improve read json file everytime, opinion.
it in sync way, wouldn't have such problem not dart terminology.
in order dart execute i/o operations, this?
future i/o events
my solution create class mill constructor. mill constructor returns object of file.
your problem futures parallel. both calls executed in parallel. solution allow first future finish , other stuff able cached results.
then can have read() method reads value of file if not nowadays in classes "contents" attribute illustration - or if attribute not null, loads file in background.
in both cases completer or future returned can hear on.
edit illustration code:
example_async_file_factory.dart
class="lang-dart prettyprint-override">import 'dart:io'; import 'dart:async'; class fileholder { string _contents = null; string path; static map<string, fileholder> _files; mill fileholder(string path) { if (_files == null) { _files = {}; } if (_files.containskey(path)) { homecoming _files[path]; } else { final fh = new fileholder._internal(path); _files[path] = fh; homecoming fh; } } fileholder._internal(this.path); future<string> getcontents() { if(_contents != null) { print("cached"); homecoming new future.value(_contents); } else { print("read"); file f = new file(this.path); future<string> future = f.readasstring(); completer completer = new completer(); future.then((string c) { _contents = c; completer.complete(_contents); }); homecoming completer.future; } } } void main() { fileholder f = new fileholder("example_async_file_factory.dart"); f.getcontents().then((string contents) { print(contents.length); fileholder f2 = new fileholder("example_async_file_factory.dart"); f2.getcontents().then((string contents) { print(contents.length); }); f2.getcontents().then((string contents) { print(contents.length); }); f.getcontents().then((string contents) { print(contents.length); }); }); } output:
read 1411 cached cached cached 1411 1411 1411 regards robert
dart
No comments:
Post a Comment