对结构映射按ID进行去抖动。

huangapple go评论72阅读模式
英文:

Debounce a struct map grouped by id

问题

我正在尝试弄清楚是否有一种方法可以对按id分组的结构映射进行去抖动。

使用以下结构体作为示例:

type Message struct {
	id    int
	attrs []string
}

这是我创建列表的方式:

messages := []Message{
	Message{
		id:    10,
		attrs: []string{"blah", "foo"},
	},
	Message{
		id:    10,
		attrs: []string{"add", "ddsds"},
	},
	Message{
		id:    11,
		attrs: []string{"foo", "bar"},
	},
	Message{
		id:    12,
		attrs: []string{"xyz"},
	},
}

然后将其转换为按id分组的映射:

collections := make(map[int][]Message)

for _, j := range messages {
	collections[j.id] = append(collections[j.id], j)
}

这是主要函数:

debounced := debounce.New(100 * time.Millisecond)

for _, messageMap := range collections {
	for _, message := range messageMap {
		debounced(func() {
			fmt.Println("id", message.id, "attrs =>", message.attrs)
		})
	}
}

time.Sleep(500 * time.Millisecond)

输出结果为:

id 12 attrs => [xyz]

这是不正确的。看起来它只获取了最后一条消息。

英文:

I am trying to figure out if there is a way to debounced a struct map that is group by an id.

Using the following struct for this example:

type Message struct {
	id    int
	attrs []string
}

This is how I am creating the list

messages := []Message{
		Message{
			id:    10,
			attrs: []string{"blah", "foo"},
		},
		Message{
			id:    10,
			attrs: []string{"add", "ddsds"},
		},
		Message{
			id:    11,
			attrs: []string{"foo", "bar"},
		},
		Message{
			id:    12,
			attrs: []string{"xyz"},
		},
	}

And then turning into a map grouped by id:

collections := make(map[int][]Message)

	for _, j := range messages {
		collections[j.id] = append(collections[j.id], j)
	}

This is the main function:

debounced := debounce.New(100 * time.Millisecond)

	for _, messageMap := range collections {
		for _, message := range messageMap {
			debounced(func() {
				fmt.Println("id", message.id, "attrs =>", message.attrs)
			})
		}
	}

	time.Sleep(500 * time.Millisecond)

The output is:

id 12 attrs => [xyz]

This is not correct. And it looks like it just getting the last message.

答案1

得分: 1

我搞定了。我只需要在第一个循环后等待:

debounced := debounce.New(100 * time.Millisecond)

for _, messageMap := range collections {
    for _, message := range messageMap {
        message := message // 复制循环变量
        debounced(func() {
            fmt.Println("id", message.id, "attrs =>", message.attrs)
        })
    }
    time.Sleep(500 * time.Millisecond)
}
英文:

I got it to work. I just need to wait after the first loop:

debounced := debounce.New(100 * time.Millisecond)

	for _, messageMap := range collections {
		for _, message := range messageMap {
			message := message // Copy the loop var
			debounced(func() {
				fmt.Println("id", message.id, "attrs =>", message.attrs)
			})
		}
		time.Sleep(500 * time.Millisecond)
	}

答案2

得分: 0

使用闭包时,你捕获的是循环变量,该变量在每次迭代时都会被重写。你需要捕获它的一个副本:

for _, message := range messageMap {
    messageCopy := message  // 复制循环变量
    debounced(func() {
        fmt.Println("id", messageCopy.id, "attrs =>", messageCopy.attrs)
    })
}
英文:

With the closure you are capturing the loop variable, which is rewritten for every iteration. Capture a copy of it:

for _, message := range messageMap {
            message:=message  // Copy the loop var
            debounced(func() {
                fmt.Println("id", message.id, "attrs =>", message.attrs)
            })
        }

</details>



huangapple
  • 本文由 发表于 2022年8月27日 02:47:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/73505345.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定