异步修改数组的可观察项中的每个项目,并返回已修改的可观察项

时间:2019-04-05 01:54:19

标签: angular typescript observable

我正在使用ngrx在Angular中构建服务,该服务在商店中查找消息列表。一旦有了列表,它就需要为每个消息获取一些异步数据,并定义一个新对象(ModifiedMessage)。该服务应返回带有可修改消息列表的可观察项。

我的尝试

Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1
    Private WithEvents rtb As RichTextBox
    Private WithEvents btnExecute As Button
    Private editableBrowser As WebBrowser

    Public Sub New()
        InitializeComponent()
        Setup()
    End Sub

    Private Sub Setup()
        editableBrowser = New WebBrowser With {
            .DocumentText = "<html><body contenteditable=""true""></body></html>"
        }

        btnExecute = New System.Windows.Forms.Button()
        rtb = New System.Windows.Forms.RichTextBox()
        SuspendLayout()

        btnExecute.Location = New System.Drawing.Point(580, 20)
        btnExecute.Size = New System.Drawing.Size(135, 50)
        btnExecute.TabIndex = 0
        btnExecute.Text = "Execute"
        btnExecute.UseVisualStyleBackColor = True
        AddHandler btnExecute.Click, AddressOf btnExecute_Click

        rtb.Location = New System.Drawing.Point(20, 20)
        rtb.Size = New System.Drawing.Size(450, 350)
        rtb.TabIndex = 1

        ClientSize = New System.Drawing.Size(800, 450)
        Controls.Add(Me.rtb)
        Controls.Add(Me.btnExecute)
        ResumeLayout()
    End Sub

    Private Sub btnExecute_Click(sender As Object, e As EventArgs)
        ExcelWork()
        COMCleanUp()
    End Sub

    Private Function GetRTF1() As String
        rtb.Clear()
        ' add some text and format it.
        Dim formattedText As String = "some sample rtf"
        rtb.Text = "This is " & formattedText & " to format and copy."
        rtb.CreateControl()
        rtb.SelectionStart = rtb.Find(formattedText)
        rtb.SelectionLength = formattedText.Length
        rtb.SelectionColor = Color.Red
        rtb.SelectionFont = New Font(Font.FontFamily, Font.Size + 2.0F, FontStyle.Bold)
        Return rtb.Rtf
    End Function

    Sub ExcelWork()
        Dim app As New Excel.Application
        Dim wb As Excel.Workbook = app.Workbooks.Add()
        Dim rng As Excel.Range = DirectCast(wb.Worksheets(1), Excel.Worksheet).Range("A1")

        PlaceRtfFAsHtmlOnClipboard(GetRTF1)
        ' for some reason rng.PasteSpecial just pastes as 
        ' unformatted text.  manual pasting results in formatted 
        ' text.
        ' The Worsheet.Paste method as well as the Worsheet.PasteSpecial
        ' methods will paste the Clipboard HTML format
        rng.Worksheet.PasteSpecial(Format:="HTML")
        'rng.Worksheet.Paste(rng)

        wb.Saved = True
        app.Visible = True  ' hand control over to user
        app.UserControl = True
    End Sub

    Private Sub PlaceRtfFAsHtmlOnClipboard(rtf As String)

        ' Clear the browser
        editableBrowser.Document.ExecCommand("SelectAll", False, Nothing)
        editableBrowser.Document.ExecCommand("Cut", False, Nothing)

        '  put rtf on clipboard
        Clipboard.SetData(DataFormats.Rtf, rtf)

        ' and paste to the editable broswer
        editableBrowser.Document.ExecCommand("Paste", False, Nothing)
        editableBrowser.Document.ExecCommand("SelectAll", False, Nothing)

        ' copy the html to the Clipboard
        editableBrowser.Document.ExecCommand("Copy", False, Nothing)
    End Sub

    Private Sub COMCleanUp()
        Do
            GC.Collect()
            GC.WaitForPendingFinalizers()
        Loop While System.Runtime.InteropServices.Marshal.AreComObjectsAvailableForCleanup
    End Sub

End Class 

这不起作用,因为TypeScript抱怨我返回的是Observable []>而不是Observable []>。

我该如何实现?

1 个答案:

答案 0 :(得分:0)

我强烈建议您建立一个选择器来获取修改后的消息,因为这样会使状态选择逻辑脱离您的服务范围。

例如:

// ...
static getListOfModifiedMessages = createSelector(
  MessageSelectors.getListOfMessages,
  MessageSelectors.getUsers,
  (messages, users) => {
    return messages.map(({ id, userId }) =>
    {
      const { name } = users.find(user => user.id === message.userId);
      return new ModifiedMessage({ id, name });
    });
  }
);
// ...