Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: syscall/js: Add SetSymbol method on js.Value #62339

Open
zyxkad opened this issue Aug 29, 2023 · 11 comments
Open

proposal: syscall/js: Add SetSymbol method on js.Value #62339

zyxkad opened this issue Aug 29, 2023 · 11 comments
Labels
Milestone

Comments

@zyxkad
Copy link

zyxkad commented Aug 29, 2023

This method should works as same as js.Value.Set, but it uses Symbol as the key.
It's useful because we can use more js syntax sugar with symbols, such as for ... of, and we don't have to write extra javascripts to wrap them.

A suggestion signature:

func (js.Value)SetSymbol(symbol js.Value, value any)
@gopherbot gopherbot added this to the Proposal milestone Aug 29, 2023
@ianlancetaylor
Copy link
Contributor

CC @golang/js

@johanbrandhorst
Copy link
Member

Thanks for your proposal. Could you elaborate a little on what you have to do today? I'm just trying to understand exactly what the benefit is here. Do you have a practical example which would be greatly simplified with this change? Thanks!

@zyxkad
Copy link
Author

zyxkad commented Sep 1, 2023

yes, so basically, I'm making go channel iterable in js.
When I doing something will use lots time, it's better to return a channel than an array, because I can render the data already processed before all stuffs are done.

Example is at https://github.com/kmcsr/mcla/blob/main/cmds/mcla_wasm/utils.go#L67

@zyxkad
Copy link
Author

zyxkad commented Sep 1, 2023

Without symbols when I want to iter it, I have to call

let value;
while(!({value} = await iter.next()).done){
  /* do something with value */
}

if with iterator symbol, I can use for ... of, it's much simple than while next

for await (let value of iter){
  /* do something with value */
}

@zyxkad
Copy link
Author

zyxkad commented Sep 1, 2023

As you can see, the first example while next is I already compressed, if it should be step by step, then it will be:

while(true){
  let res = await iter.next()
  if(res.done){
    break
  }
  let value = res.value
  /* do something with value */
}

@zyxkad
Copy link
Author

zyxkad commented Jan 11, 2024

@johanbrandhorst Any progress?

@johanbrandhorst
Copy link
Member

No one is working on this as far as I know. If you want to work on this, feel free to experiment with it.

zyxkad added a commit to zyxkad/golang-go that referenced this issue Jan 11, 2024
@gopherbot
Copy link

Change https://go.dev/cl/555417 mentions this issue: syscall/js: add methods GetSymbol, SetSymbol, and DeleteSymbol

@johanbrandhorst
Copy link
Member

Thanks for opening a CL, I think this has to go through the proposal review process before we can merge it, but it's a great start! I think this sounds like a reasonable addition to the syscall/js package and you make a compelling case that this provides a significant boost to developer ergonomics without adding much maintenance burden. Could the proposal review committee add this to the next review?

@zyxkad
Copy link
Author

zyxkad commented Jan 11, 2024

Further proposal: Is it necessary to wrap an async channel in std? so other people do not need to write same logic again and again

@johanbrandhorst
Copy link
Member

I'm not sure what you mean - in any case, please write a separate issue for this proposal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

4 participants