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

[feature request] use function for supplies #13

Open
rossinek opened this issue Apr 13, 2018 · 0 comments
Open

[feature request] use function for supplies #13

rossinek opened this issue Apr 13, 2018 · 0 comments

Comments

@rossinek
Copy link

What problem does this feature solve?

Using use function to create mixin from supply definition is fine for components but it is not implemented to be used inside supplies. Sometimes there is a need for a resource that contains other resources. That's why I suggest adding the use method equivalent for in supply use.

Mixin created with use functions cause grasp call inside component's created hook, while inside supply the proper behaviour would be to call grasp on active state change (analogously for release).

What does the proposed API look like?

It could be a separate function or an additional flag for the use function.

Below I put an example helper that I created. Due to the fact that there is no access to registered supplies definitions and it is just simple proof-of-concept it accepts only supply definition as an argument. Another problem of this simple function is the lack of access to supplyCache (in my proof of concept it is imported).

import { supplyCache } from '@/supplies/cache'

export function include (supplyDef) {
  let name = ''
  const options = {
    created () {
      const resource = getResource(supplyDef, supplyCache)
      name = supplyDef.name

      if (!this.$supply) this.$supply = {}
      this.$supply[name] = resource
    },
    watch: {
      active (val, oldVal) {
        if (!this.$supply[name]) return
        if (val && !oldVal) {
          this.$supply[name].grasp()
        } else if (!val && oldVal) {
          this.$supply[name].release()
        }
      }
    }
  }
  return options
}

I am ready to prepare a better, more complete implementation. Cheers!

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

No branches or pull requests

1 participant