api.modifyClass sometimes(!) not working

Reclassifying this as bug.

I have added a console.log in the plugin API code app/assets/javascripts/discourse/app/lib/plugin-api.js so it logs whenever modifyClass is being called.

I have removed all external plugins to make sure there was not a conflict somewhere.

Repro:

  • create an empty forum on stable (so no Ember CLI). This is not working on tests-passed (without Ember CLI) either. I did not test this with Ember CLI.

  • add a theme component with this in Common - Head

#1 Working

<script type="text/discourse-plugin" version="0.1">
    api.modifyClass('model:user', {
      pluginId: 'test-tc',
      testFunction: function() {
        return 1;
      } 
    });
</script>
  • load the home page

  • console shows modifyClass called for model:user _application-08d9058ddd37ba80992f770509f4919ad0738a17f14fb85167b1dc1f32f8b56e.js:23490:16 Object { pluginId: "test-tc", testFunction: testFunction() }

  • enter Discourse.currentUser.testFunction() in console

  • “1” is printed

#2 Failing

  • Go to a topic, for instance ‘Welcome to Discourse’ and reload the page
  • console shows the same “modifyClass called” logs
  • enter Discourse.currentUser.testFunction() in console
  • Uncaught TypeError: Discourse.currentUser.testFunction is not a function is printed

#3 Failing with warning

  • Append a single line to top of the theme component so it looks like this:
<script type="text/discourse-plugin" version="0.1">
    const userModel = api.container.lookup("model:user");

    api.modifyClass('model:user', {
      pluginId: 'test-tc',
      testFunction: function() {
        return 1;
      } 
    });

</script>
  • Go to a topic, for instance ‘Welcome to Discourse’ and reload the page
  • console shows the same “modifyClass called” logs
  • console shows a warning "model:user" was already cached in the container. Changes won't be applied.
  • enter Discourse.currentUser.testFunction() in console
  • Uncaught TypeError: Discourse.currentUser.testFunction is not a function is printed

#4 Working

  • Move the lookup line to bottom of the theme component so it looks like this:
<script type="text/discourse-plugin" version="0.1">
    api.modifyClass('model:user', {
      pluginId: 'test-tc',
      testFunction: function() {
        return 1;
      } 
    });

    const userModel = api.container.lookup("model:user");
</script>
  • Go to a topic, for instance ‘Welcome to Discourse’ and reload the page
  • console shows the same “modifyClass called” logs
  • enter Discourse.currentUser.testFunction() in console
  • “1” is printed :partying_face:
4 Likes