Giter Club home page Giter Club logo

Comments (6)

diamondburned avatar diamondburned commented on July 28, 2024

Oh boy, this smells like Promises.

You should manually synchronize Permissions instead; there's always the option of manually checking if something is in the state.

from arikawa.

mavolin avatar mavolin commented on July 28, 2024

Oh boy, this smells like Promises.

What do you think will hinder this from working correctly?

You should manually synchronize Permissions instead; there's always the option of manually checking if something is in the state.

This, of course, is possible, this is rather for convenience and an attempt to save 12+ loc that have to be written instead of 1.

from arikawa.

diamondburned avatar diamondburned commented on July 28, 2024

What do you think will hinder this from working correctly?

Go's design is very anti-Promises, its entire point is to be explicit in asynchronicity. This means in a good API, the caller must have full control over that.

Not to mention, I personally hate to use Promises. It will really clutter up the API if we decide to do that.

from arikawa.

mavolin avatar mavolin commented on July 28, 2024

I think you misunderstood me. I didn't propose a system that returns a Promise but, just one that internally will parallelize the execution of two possible API requests, reducing the wait time practically in half.

Example:

func (s *State) Permissions(channelID, userID discord.Snowflake) (discord.Permissions, error) {
	if s.FetchSync {
		return s.permissionsSync(channelID, userID)
	}

	return s.permissionsAsync(channelID, userID)
}

func (s *State) permissionsSync(channelID, userID discord.Snowflake) (discord.Permissions, error) {
	ch, err := s.Channel(channelID)
	if err != nil {
		return 0, errors.Wrap(err, "Failed to get channel")
	}

	g, err := s.Guild(ch.GuildID)
	if err != nil {
		return 0, errors.Wrap(err, "failed to get guild")
	}

	m, err := s.Member(ch.GuildID, userID)
	if err != nil {
		return 0, errors.Wrap(err, "failed to get member")
	}

	return discord.CalcOverwrites(*g, *ch, *m), nil
}

func (s *State) permissionsAsync(
	channelID, userID discord.Snowflake) (discord.Permissions, error) {

	ch, err := s.Channel(channelID)
	if err != nil {
		return 0, errors.Wrap(err, "Failed to get channel")
	}

	guild, guildErr := make(chan *discord.Guild), make(chan error)
	member, memberErr := make(chan *discord.Member), make(chan error)

	go func() {
		g, err := s.Guild(ch.GuildID)
		if err != nil {
			err = errors.Wrap(err, "failed to get guild")
		}

		guild <- g
		guildErr <- err
	}()

	go func() {
		m, err := s.Member(ch.GuildID, userID)
		if err != nil {
			err = errors.Wrap(err, "failed to get member")
		}

		member <- m
		memberErr <- err
	}()

	g, err := <-guild, <-guildErr
	if err != nil {
		return 0, err
	}

	m, err := <-member, <-memberErr
	if err != nil {
		return 0, err
	}

	return discord.CalcOverwrites(*g, *ch, *m), nil
}

from arikawa.

diamondburned avatar diamondburned commented on July 28, 2024

This works, but 2 changes need to be done:

  1. This should check for a state first before asynchronously requesting. Goroutines are cheap, but not free.
  2. Given the above, Permissions could always call it asynchronously.

from arikawa.

diamondburned avatar diamondburned commented on July 28, 2024

This should do:

func (s *State) Permissions(channelID, userID discord.Snowflake) (discord.Permissions, error) {
	ch, err := s.Channel(channelID)
	if err != nil {
		return 0, errors.Wrap(err, "Failed to get channel")
	}

	var wg sync.WaitGroup

	g, guilderr := s.Store.Guild(ch.GuildID)
	if guilderr != nil {
		wg.Add(1)
		go func() {
			g, guilderr = s.Session.Guild(ch.GuildID)
			wg.Done()
		}()
	}

	m, membererr := s.Store.Member(ch.GuildID, userID)
	if membererr != nil {
		wg.Add(1)
		go func() {
			m, membererr = s.Store.Member(ch.GuildID, userID)
			wg.Done()
		}()
	}

	wg.Wait()

	if guilderr != nil {
		return 0, errors.Wrap(guilderr, "failed to get guild")
	}
	if membererr != nil {
		return 0, errors.Wrap(membererr, "failed to get member")
	}

	return discord.CalcOverwrites(*g, *ch, *m), nil
}

from arikawa.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.