.net - Create an IObservable from a method and others -
is code below correctly written return iobservable in terms of rx library? seems work correct, wondering i'm using correctly.
private iobservable<searchresult[]> search(string query) { return observable.create((iobserver<searchresult[]> observer)=> { list<searchresult> result = new list<searchresult>(); foreach (testsgroupmeta group in engine.groups) { string name = group.tostring(); if (name.indexof(query, stringcomparison.invariantcultureignorecase) != -1) { result.add(new searchresult{ name = name, type = "group"}); } foreach (testmethodmeta method in group.methods) { name = method.tostring(); if (name.indexof(query, stringcomparison.invariantcultureignorecase) != -1) { result.add(new searchresult {name = name, type = "method"}); } } } observer.onnext(result.toarray()); observer.oncompleted(); return () => {}; }); }
the usage this.
private void searchform_load(object sender, eventargs e) { var textchanged = observable.fromeventpattern<eventargs>(txtquery, "textchanged") .select(_ => ((textbox)_.sender).text); var searchresult = query in textchanged query.length >= 3 result in search(query) select result; _resultswatcher = searchresult .throttle(timespan.frommilliseconds(200)) .observeon(lvresults) .subscribe(resuts => { lvresults.beginupdate(); lvresults.items.clear(); foreach (searchresult result in resuts) { listviewitem item = new listviewitem(result.name); item.subitems.add(result.type); lvresults.items.add(item); } lvresults.endupdate(); }); }
also, there way create iobservable such method?
task<list<searchresult>> search(string query){...}
or, other approaches create asynchronous search on large dataset , have iobservable it.
thx
you can create observable task such,
task<list<searchresult>> searchasync(string query){...} var observable = observable.fromasync<list<searchresult>>(()=> searchasync(query));
if function returning 1 result in future consider using task instead?
public async task<searchresult[]> searchasync(string query){ }
Comments
Post a Comment