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: Expose class methods on the app instance level #426

Closed
rbardini opened this issue Jul 9, 2018 · 0 comments
Closed

Proposal: Expose class methods on the app instance level #426

rbardini opened this issue Jul 9, 2018 · 0 comments

Comments

@rbardini
Copy link
Member

rbardini commented Jul 9, 2018

Currently

We can declare methods inside createApp to expose application-specific behavior:

const FooApp = createApp({
  name: 'Foo',
  bar() { // exposed method
    console.log('bar')
  }
})

This requires accessing the options property of the app instance to retrieve the methods from another app:

const BazComponent = observe(app =>
  streamProps()
    .set({
      foo: app.get('foo').options.bar, // retrieve `bar` method out of `foo` app options
    })
    .get$()
)(({ bar }) => /* `bar` method as prop */)

const BazApp = createApp({
  name: 'Baz',
  providers: [
    {
      name: 'component',
      useValue: BazComponent,
    },
    {
      name: 'foo',
      deps: ['rootApp'],
      useFactory({ rootApp }) {
        return rootApp.getAppInstance('Foo')
      }
    }
  ]
})

Proposal

Add a new, optional methods (Object) property to the options argument of createApp, which has its properties added to the app's this object, thus allowing us to access the methods directly from the app instance:

const FooApp = createApp({
  name: 'Foo',
  methods: { // specify what methods this app exposes
    bar() {
      console.log('bar')
    }
  }
})
const BazComponent = observe(app =>
  streamProps()
    .set({
      foo: app.get('foo').bar, // retrieve `bar` method out of `foo` app directly
    })
    .get$()
)(({ bar }) => /* `bar` method as prop */)

const BazApp = createApp({
  name: 'Baz',
  providers: [
    {
      name: 'component',
      useValue: BazComponent,
    },
    {
      name: 'foo',
      deps: ['rootApp'],
      useFactory({ rootApp }) {
        return rootApp.getAppInstance('Foo')
      }
    }
  ]
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants