public async Task Extensions_AsyncEither_FlatMap() { var some = AsyncOption.Some <string, string>("abc"); var none = AsyncOption.None <string, string>("ex"); Assert.AreEqual(await some.FlatMap(val => Task.FromResult(Option.Some <string, string>(val + "d"))).ValueOrException(), "abcd"); Assert.AreEqual(await some.FlatMap(val => Task.FromResult(Option.None <string, string>("ex"))).ValueOrException(), "ex"); Assert.AreEqual(await none.FlatMap(val => Task.FromResult(Option.Some <string, string>(val + "d"))).ValueOrException(), "ex"); Assert.AreEqual(await none.FlatMap(val => Task.FromResult(Option.None <string, string>("ex"))).ValueOrException(), "ex"); Assert.AreEqual(await some.FlatMap(val => Task.FromResult(Option.Some <string>(val + "d")), "ex").ValueOrException(), "abcd"); Assert.AreEqual(await some.FlatMap(val => Task.FromResult(Option.None <string>()), "ex").ValueOrException(), "ex"); Assert.AreEqual(await none.FlatMap(val => Task.FromResult(Option.Some <string>(val + "d")), "ex").ValueOrException(), "ex"); Assert.AreEqual(await none.FlatMap(val => Task.FromResult(Option.None <string>()), "ex").ValueOrException(), "ex"); Assert.AreEqual(await some.FlatMap(val => Task.FromResult(Option.Some <string>(val + "d")), () => "ex").ValueOrException(), "abcd"); Assert.AreEqual(await some.FlatMap(val => Task.FromResult(Option.None <string>()), () => "ex").ValueOrException(), "ex"); Assert.AreEqual(await none.FlatMap(val => Task.FromResult(Option.Some <string>(val + "d")), () => "ex").ValueOrException(), "ex"); Assert.AreEqual(await none.FlatMap(val => Task.FromResult(Option.None <string>()), () => "ex").ValueOrException(), "ex"); Assert.AreEqual(await some.FlatMap(val => Task.FromResult(Option.Some <string, string>(val + "d")).ToAsyncOption()).ValueOrException(), "abcd"); Assert.AreEqual(await some.FlatMap(val => Task.FromResult(Option.None <string, string>("ex")).ToAsyncOption()).ValueOrException(), "ex"); Assert.AreEqual(await none.FlatMap(val => Task.FromResult(Option.Some <string, string>(val + "d")).ToAsyncOption()).ValueOrException(), "ex"); Assert.AreEqual(await none.FlatMap(val => Task.FromResult(Option.None <string, string>("ex")).ToAsyncOption()).ValueOrException(), "ex"); Assert.AreEqual(await some.FlatMap(val => Task.FromResult(Option.Some <string>(val + "d")).ToAsyncOption(), "ex").ValueOrException(), "abcd"); Assert.AreEqual(await some.FlatMap(val => Task.FromResult(Option.None <string>()).ToAsyncOption(), "ex").ValueOrException(), "ex"); Assert.AreEqual(await none.FlatMap(val => Task.FromResult(Option.Some <string>(val + "d")).ToAsyncOption(), "ex").ValueOrException(), "ex"); Assert.AreEqual(await none.FlatMap(val => Task.FromResult(Option.None <string>()).ToAsyncOption(), "ex").ValueOrException(), "ex"); Assert.AreEqual(await some.FlatMap(val => Task.FromResult(Option.Some <string>(val + "d")).ToAsyncOption(), () => "ex").ValueOrException(), "abcd"); Assert.AreEqual(await some.FlatMap(val => Task.FromResult(Option.None <string>()).ToAsyncOption(), () => "ex").ValueOrException(), "ex"); Assert.AreEqual(await none.FlatMap(val => Task.FromResult(Option.Some <string>(val + "d")).ToAsyncOption(), () => "ex").ValueOrException(), "ex"); Assert.AreEqual(await none.FlatMap(val => Task.FromResult(Option.None <string>()).ToAsyncOption(), () => "ex").ValueOrException(), "ex"); }
public async Task Extensions_AsyncEither_Matching() { var some = AsyncOption.Some <string, string>("abc"); var none = AsyncOption.None <string, string>("ex"); var success = await some.Match( some : val => val, none : ex => ex ); var failure = await none.Match( some : val => val, none : ex => ex ); Assert.AreEqual(success, "abc"); Assert.AreEqual(failure, "ex"); await none.Match( some : val => Assert.Fail(), none : ex => Assert.AreEqual(ex, "ex") ); await some.Match( some : val => Assert.AreEqual(val, "abc"), none : ex => Assert.Fail() ); }
public async Task Extensions_AsyncEither_RetrievalAndContainment() { var some = AsyncOption.Some <string, string>("abc"); var none = AsyncOption.None <string, string>("ex"); Assert.IsTrue(await some.Contains("abc")); Assert.IsFalse(await some.Contains("0")); Assert.IsTrue(await some.Exists(value => value == "abc")); Assert.IsFalse(await some.Exists(value => value == "0")); Assert.IsFalse(await none.Contains("abc")); Assert.IsFalse(await none.Exists(value => true)); Assert.IsTrue(await some.Or("0").Contains("abc")); Assert.IsTrue(await none.Or("0").Contains("0")); Assert.IsTrue(await some.Or(() => "0").Contains("abc")); Assert.IsTrue(await none.Or(() => "0").Contains("0")); Assert.AreEqual(await some.ValueOr("0"), "abc"); Assert.AreEqual(await none.ValueOr("0"), "0"); Assert.AreEqual(await some.ValueOr(() => "0"), "abc"); Assert.AreEqual(await none.ValueOr(() => "0"), "0"); Assert.AreEqual(await some.ValueOrException(), "abc"); Assert.AreEqual(await none.ValueOrException(), "ex"); }
public async Task Extensions_AsyncMaybe_Matching() { var some = AsyncOption.Some <string>("abc"); var none = AsyncOption.None <string>(); var success = await some.Match( some : val => val, none : () => "ex" ); var failure = await none.Match( some : val => val, none : () => "ex" ); Assert.AreEqual(success, "abc"); Assert.AreEqual(failure, "ex"); await none.Match( some : val => Assert.Fail(), none : () => { } ); await some.Match( some : val => Assert.AreEqual(val, "abc"), none : () => Assert.Fail() ); }
public async Task Extensions_AsyncMaybe_Filter() { var some = AsyncOption.Some("abc"); var none = AsyncOption.None <string>(); Assert.IsTrue(await some.Filter(value => value.StartsWith("a")).HasValue); Assert.IsFalse(await some.Filter(value => value.StartsWith("0")).HasValue); Assert.IsFalse(await none.Filter(value => value.StartsWith("a")).HasValue); }
public async Task Extensions_AsyncMaybe_Map() { var some = AsyncOption.Some("abc"); var none = AsyncOption.None <string>(); Assert.AreEqual(await some.Map(val => val + "d").ValueOr("0"), "abcd"); Assert.AreEqual(await none.Map(val => val + "d").ValueOr("0"), "0"); Assert.AreEqual(await some.Map(val => Task.FromResult(val + "d")).ValueOr("0"), "abcd"); Assert.AreEqual(await none.Map(val => Task.FromResult(val + "d")).ValueOr("0"), "0"); }
public async Task Extensions_AsyncEither_Filter() { var some = AsyncOption.Some <string, string>("abc"); var none = AsyncOption.None <string, string>("ex"); Assert.AreEqual(await some.Filter(value => value.StartsWith("a"), "ex").ValueOrException(), "abc"); Assert.AreEqual(await some.Filter(value => value.StartsWith("0"), "ex").ValueOrException(), "ex"); Assert.AreEqual(await none.Filter(value => value.StartsWith("a"), "ex2").ValueOrException(), "ex"); Assert.AreEqual(await some.Filter(value => value.StartsWith("a"), () => "ex").ValueOrException(), "abc"); Assert.AreEqual(await some.Filter(value => value.StartsWith("0"), () => "ex").ValueOrException(), "ex"); Assert.AreEqual(await none.Filter(value => value.StartsWith("a"), () => "ex2").ValueOrException(), "ex"); }
public async Task Extensions_AsyncMaybe_FlatMapOption() { var some = AsyncOption.Some("abc"); var none = AsyncOption.None <string>(); Assert.AreEqual(await some.FlatMap(val => Option.Some(val + "d")).ValueOr("0"), "abcd"); Assert.AreEqual(await some.FlatMap(val => Option.None <string>()).ValueOr("0"), "0"); Assert.AreEqual(await none.FlatMap(val => Option.Some(val + "d")).ValueOr("0"), "0"); Assert.AreEqual(await none.FlatMap(val => Option.None <string>()).ValueOr("0"), "0"); Assert.AreEqual(await some.FlatMap(val => Option.Some <string, string>(val + "d")).ValueOr("0"), "abcd"); Assert.AreEqual(await some.FlatMap(val => Option.None <string, string>("ex")).ValueOr("0"), "0"); Assert.AreEqual(await none.FlatMap(val => Option.Some <string, string>(val + "d")).ValueOr("0"), "0"); Assert.AreEqual(await none.FlatMap(val => Option.None <string, string>("ex")).ValueOr("0"), "0"); }
public async Task Extensions_AsyncEither_Map() { var some = AsyncOption.Some <string, string>("abc"); var none = AsyncOption.None <string, string>("ex"); Assert.AreEqual(await some.Map(val => val + "d").ValueOrException(), "abcd"); Assert.AreEqual(await none.Map(val => val + "d").ValueOrException(), "ex"); Assert.AreEqual(await some.MapException(ex => ex + "d").ValueOrException(), "abc"); Assert.AreEqual(await none.MapException(ex => ex + "d").ValueOrException(), "exd"); Assert.AreEqual(await some.Map(val => Task.FromResult(val + "d")).ValueOrException(), "abcd"); Assert.AreEqual(await none.Map(val => Task.FromResult(val + "d")).ValueOrException(), "ex"); Assert.AreEqual(await some.MapException(ex => Task.FromResult(ex + "d")).ValueOrException(), "abc"); Assert.AreEqual(await none.MapException(ex => Task.FromResult(ex + "d")).ValueOrException(), "exd"); }
public async Task Extensions_AsyncMaybeEither_Conversions() { var someMaybe = AsyncOption.Some <string>("abc"); var noneMaybe = AsyncOption.None <string>(); var someEither = AsyncOption.Some <string, string>("abc"); var noneEither = AsyncOption.None <string, string>("ex"); Assert.AreEqual(await someMaybe.WithException("ex").ValueOrException(), "abc"); Assert.AreEqual(await noneMaybe.WithException("ex").ValueOrException(), "ex"); Assert.AreEqual(await someMaybe.WithException(() => "ex").ValueOrException(), "abc"); Assert.AreEqual(await noneMaybe.WithException(() => "ex").ValueOrException(), "ex"); Assert.AreEqual(await someEither.WithoutException().ValueOr("ex2"), "abc"); Assert.AreEqual(await noneEither.WithoutException().ValueOr("ex2"), "ex2"); Assert.AreEqual(await someEither.WithoutException().ValueOr(() => "ex2"), "abc"); Assert.AreEqual(await noneEither.WithoutException().ValueOr(() => "ex2"), "ex2"); }
public async Task Extensions_AsyncMaybe_Creation() { var some1 = new AsyncOption <string>(Task.FromResult(Option.Some <string>("abc"))); var none1 = new AsyncOption <string>(Task.FromResult(Option.None <string>())); var some2a = Option.Some <string>("abc").ToAsyncOption(); var none2a = Option.None <string>().ToAsyncOption(); var some2b = Task.FromResult(Option.Some <string>("abc")).ToAsyncOption(); var none2b = Task.FromResult(Option.None <string>()).ToAsyncOption(); var some3a = AsyncOption.Some("abc"); var some3b = AsyncOption.Some(Task.FromResult("abc")); var none3 = AsyncOption.None <string>(); // Awaiting the whole option Assert.IsTrue((await some1).HasValue); Assert.IsFalse((await none1).HasValue); Assert.IsTrue((await some2a).HasValue); Assert.IsFalse((await none2a).HasValue); Assert.IsTrue((await some2b).HasValue); Assert.IsFalse((await none2b).HasValue); Assert.IsTrue((await some3a).HasValue); Assert.IsTrue((await some3b).HasValue); Assert.IsFalse((await none3).HasValue); // awaiting only HasValue Assert.IsTrue(await some1.HasValue); Assert.IsFalse(await none1.HasValue); Assert.IsTrue(await some2a.HasValue); Assert.IsFalse(await none2a.HasValue); Assert.IsTrue(await some2b.HasValue); Assert.IsFalse(await none2b.HasValue); Assert.IsTrue(await some3a.HasValue); Assert.IsTrue(await some3b.HasValue); Assert.IsFalse(await none3.HasValue); }
/// <summary> /// 与服务器同步邮件信息 /// </summary> /// <param name="record"></param> public void syncUserMail(ASObject record) { if (record == null) return; AsyncOption option = new AsyncOption("MailManager.syncUserMail"); option.asyncData = record; option.showWaitingBox = false; Remoting.call("MailManager.syncUserMail", new object[] { record }, this, option); }
public void onRemotingException(string callUID, string methodName, string message, string code, ASObject exception, AsyncOption option) { System.Diagnostics.Debug.WriteLine(message); }
public void onRemotingCallback(string callUID, string methodName, object result, AsyncOption option) { switch (methodName) { case "MailManager.syncUserMail": { int is_synced_value = -1; ASObject mail = option.asyncData as ASObject; int is_synced = NumberUtil.parseInt(mail["is_synced"] == null ? "0" : mail["is_synced"].ToString()); if (is_synced == 0) { is_synced = 1; is_synced_value = 1; } if (is_synced == 1) { //同步文件 string mail_file = mail["mail_file"] as string; string store_path = System.IO.Path.Combine(new string[] { Desktop.instance.ApplicationPath, "mail" }); string mail_file_path = store_path + mail_file; if (File.Exists(mail_file_path)) { string uuid = mail["uuid"] as string; Dictionary<string, string> param = new Dictionary<string, string>(); param["uuid"] = MailReceiveWorker.getFilePath(uuid); using (var client = new CookieAwareWebClient()) { client.Param = param; string uri = Desktop.getAbsoluteUrl(upload_mail_message); mail_file_path = mail_file_path.Replace("/", "\\"); client.UploadFileAsync(new Uri(uri), mail_file_path); } is_synced_value = 2; } } if (is_synced_value != -1) { mail["is_synced"] = is_synced_value; updateMailRecord(mail, new string[] { "is_synced" }); } } break; } }
public void onRemotingException(string callUID, string methodName, string message, string code, wos.rpc.core.ASObject exception, AsyncOption option) { this.Dispatcher.BeginInvoke((System.Action)delegate { ime.controls.MessageBox.Show(message, "错误", MessageBoxButton.OK, MessageBoxImage.Error); }, System.Windows.Threading.DispatcherPriority.ApplicationIdle); }
public void onRemotingCallback(string callUID, string methodName, object result, AsyncOption option) { switch (methodName) { case "MailManager.getAllMailAccounts": cb_getMailAccounts(result); break; } }
public void onRemotingCallback(string callUID, string methodName, object result, AsyncOption option) { switch (methodName) { case "MailManager.getMailBoxView": { ASObject record = result as ASObject; if (record == null || record.Count == 0) { syncMailServer(); return; } List<string> uids = new List<string>(); using (DataSet ds = new DataSet()) { string query = "select mail_uid from ML_Mail where owner_user_id=@owner_user_id"; using (SQLiteCommand cmd = new SQLiteCommand(query, DBWorker.GetConnection())) { cmd.Parameters.AddWithValue("@owner_user_id", Desktop.instance.loginedPrincipal.id); using (SQLiteDataAdapter q = new SQLiteDataAdapter(cmd)) { q.Fill(ds); } } if (ds.Tables.Count > 0) { using (DataTable dt = ds.Tables[0]) { foreach (DataRow row in dt.Rows) { if (String.IsNullOrWhiteSpace(row[0] as string)) continue; uids.Add(row[0].ToString()); } } } } List<ASObject> createList = new List<ASObject>(); foreach (KeyValuePair<string, object> val in record) { if (uids.Contains(val.Key)) continue; createList.Add(val.Value as ASObject); } if (createList.Count > 0) { workInfo.IsNewMail = true; Thread t = new Thread(() => { AutoResetEvent reset = new AutoResetEvent(false); int total = createList.Count; int progress = 0; try { workInfo.SetInfo("正在从服务器获取邮件视图。。。"); foreach (ASObject mail in createList) { workInfo.SetProgress(total, progress++); workInfo.AddDetail("从服务器同步邮件-" + mail.getString("subject"), Colors.Black); MailWorker.instance.dispatchMailEvent(MailWorker.Event.Create, mail, null); reset.WaitOne(200); } } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); } reset.Set(); syncMailServer(); }); t.IsBackground = true; t.Start(); }else syncMailServer(); } break; case "MailManager.getUserMailAccounts": { if (result == null || !(result is string)) { workInfo.CloseWindow(); return; } recvs.Clear(); JArray array = JArray.Parse(result as string); object[] record = JsonUtil.toRawArray(array); foreach (object o in record) { recvs.Add(o as ASObject); } if (recvs.Count > 0) { interval = new System.Timers.Timer(DEFAULT_DELAY); interval.AutoReset = true; interval.Elapsed += onInterval_Elapsed; interval.Start(); stopInterval = new System.Timers.Timer(2000); stopInterval.AutoReset = true; stopInterval.Elapsed += onStopInterval_Elapsed; stopInterval.Start(); execute(recvs); } else workInfo.CloseWindow(); } break; case "MailManager.getMoveMailBoxView": { if (result == null || (result as object[]) == null) return; object[] record = result as object[]; if (record.Length == 0) return; StringBuilder sb = new StringBuilder(); foreach (string s in record) { sb.Append("'").Append(s).Append("',"); } sb.Remove(sb.Length - 1, 1); //删除被移动的邮件在本地的文件 List<ASObject> files = new List<ASObject>(); string sql = "select mail_uid, mail_file from ML_Mail where mail_uid in (" + sb.ToString() + ")"; using (DataSet ds = new DataSet()) { try { using (SQLiteCommand cmd = new SQLiteCommand(sql.ToString(), DBWorker.GetConnection())) { using (SQLiteDataAdapter q = new SQLiteDataAdapter(cmd)) { q.Fill(ds); } } } catch (Exception ex) { Debug.Write(ex.StackTrace); } if (ds.Tables.Count > 0) { using (DataTable dt = ds.Tables[0]) { foreach (DataRow row in dt.Rows) { ASObject mail = new ASObject(); foreach (DataColumn column in dt.Columns) { mail[column.ColumnName] = row[column]; } files.Add(mail); } } } } if (files.Count > 0) { foreach (ASObject file in files) { if (File.Exists(Path.Combine(store_path, file.getString("mail_file")))) { File.Delete(Path.Combine(store_path, file.getString("mail_file"))); if (Directory.Exists(Path.Combine(store_path, file.getString("mail_uid")))) { Directory.Delete(Path.Combine(store_path, file.getString("mail_uid")), true); } } } } //删除被移动的邮件 sql = "delete from ML_Mail where mail_uid in (" + sb.ToString() + ")"; try { using (var cmd = new SQLiteCommand(sql, DBWorker.GetConnection())) { cmd.ExecuteNonQuery(); } } catch (Exception ex) { System.Diagnostics.Debug.Write(ex.StackTrace); } MailWorker.instance.dispatchMailEvent(MailWorker.Event.Reset, null, null); } break; } }
private void syncMailServer() { try { //访问MailManager.getBlackList() 获取黑名单列表 object result = Remoting.call("MailManager.getBlackList", new object[] { }); MailWorker.instance.Blacks = result as ASObject; } catch (Exception ex) { Debug.Write(ex.StackTrace); } //访问MailManager.getUserMailAccounts() 获取接受邮件的账户列表 AsyncOption option = new AsyncOption("MailManager.getUserMailAccounts"); option.showWaitingBox = false; Remoting.call("MailManager.getUserMailAccounts", new object[] { }, this, option); }
private void recvMaill(ASObject ac, string pubId, bool isJoin = false) { string account = ac.getString("account"); try { if (!pubIds.Contains(pubId)) { MessageManager.instance.subscribeMessage(pubId); pubIds.Add(pubId); } executeRecvMaill(ac, pubId); AsyncOption option = new AsyncOption("MailManager.recvTaskFinished"); option.showWaitingBox = false; Remoting.call("MailManager.recvTaskFinished", new object[] { account }, this, option); if (pubIds.Contains(pubId)) { MessageManager.instance.endPublish(pubId); pubIds.Remove(pubId); } } catch (Exception ex) { error(ex.Message); return; } finally { AsyncOption option = new AsyncOption("MailManager.recvTaskFinished"); option.showWaitingBox = false; Remoting.call("MailManager.recvTaskFinished", new object[] { account }, this, option); if (pubIds.Contains(pubId)) { MessageManager.instance.endPublish(pubId); pubIds.Remove(pubId); } } if (!isJoin) { recvs.Remove(ac); execute(recvs); } else { joinList.Remove(ac); isJoinAccept = false; } }