<commit_before>import PostView from "discourse/views/post";

function createPollView(container, post, poll, vote) {
  const controller = container.lookup("controller:poll", { singleton: false }),
        view = container.lookup("view:poll");

  controller.set("vote", vote);
  controller.setProperties({ model: Em.Object.create(poll), post });
  view.set("controller", controller);

  return view;
}

export default {
  name: "extend-for-poll",

  initialize(container) {

    const messageBus = container.lookup("message-bus:main");

    // listen for back-end to tell us when a post has a poll
    messageBus.subscribe("/polls", data => {
      const post = container.lookup("controller:topic").get('modee.postStream').findLoadedPost(data.post_id);
      // HACK to trigger the "postViewUpdated" event
      Em.run.next(_ => post.set("cooked", post.get("cooked") + " "));
    });

    // overwrite polls
    PostView.reopen({
      _createPollViews: function($post) {
        const post = this.get("post"),
              polls = post.get("polls"),
              votes = post.get("polls_votes") || {};

        // don't even bother when there's no poll
        if (!polls) { return; }

        // clean-up if needed
        this._cleanUpPollViews();

        const pollViews = {};

        // iterate over all polls
        $(".poll", $post).each(function() {
          const $div = $("<div>"),
                $poll = $(this),
                pollName = $poll.data("poll-name"),
                pollView = createPollView(container, post, polls[pollName], votes[pollName]);

          $poll.replaceWith($div);
          pollView.renderer.replaceIn(pollView, $div[0]);
          pollViews[pollName] = pollView;
        });

        messageBus.subscribe("/polls/" + this.get("post.id"), results => {
          if (results && results.polls) {
            _.forEach(results.polls, poll => {
              if (pollViews[poll.name]) {
                pollViews[poll.name].get("controller").set("model", Em.Object.create(poll));
              }
            });
          }
        });

        this.set("pollViews", pollViews);
      }.on("postViewInserted", "postViewUpdated"),

      _cleanUpPollViews: function() {
        messageBus.unsubscribe("/polls/" + this.get("post.id"));

        if (this.get("pollViews")) {
          _.forEach(this.get("pollViews"), v => v.destroy());
        }
      }.on("willClearRender")
    });
  }
};
<commit_msg>FIX: editing a post with a poll breaks sometimes<commit_after>import PostView from "discourse/views/post";

function createPollView(container, post, poll, vote) {
  const controller = container.lookup("controller:poll", { singleton: false }),
        view = container.lookup("view:poll");

  controller.set("vote", vote);
  controller.setProperties({ model: Em.Object.create(poll), post });
  view.set("controller", controller);

  return view;
}

export default {
  name: "extend-for-poll",

  initialize(container) {

    const messageBus = container.lookup("message-bus:main");

    // listen for back-end to tell us when a post has a poll
    messageBus.subscribe("/polls", data => {
      const post = container.lookup("controller:topic").get('modee.postStream').findLoadedPost(data.post_id);
      // HACK to trigger the "postViewUpdated" event
      Em.run.next(_ => post.set("cooked", post.get("cooked") + " "));
    });

    // overwrite polls
    PostView.reopen({
      _createPollViews: function($post) {
        const post = this.get("post"),
              polls = post.get("polls"),
              votes = post.get("polls_votes") || {};

        // don't even bother when there's no poll
        if (!polls) { return; }

        // clean-up if needed
        this._cleanUpPollViews();

        const pollViews = {};

        // iterate over all polls
        $(".poll", $post).each(function() {
          const $div = $("<div>"),
                $poll = $(this),
                pollName = $poll.data("poll-name"),
                pollView = createPollView(container, post, polls[pollName], votes[pollName]);

          $poll.replaceWith($div);
          Em.run.next(_ => pollView.renderer.replaceIn(pollView, $div[0]));
          pollViews[pollName] = pollView;
        });

        messageBus.subscribe("/polls/" + this.get("post.id"), results => {
          if (results && results.polls) {
            _.forEach(results.polls, poll => {
              if (pollViews[poll.name]) {
                pollViews[poll.name].get("controller").set("model", Em.Object.create(poll));
              }
            });
          }
        });

        this.set("pollViews", pollViews);
      }.on("postViewInserted", "postViewUpdated"),

      _cleanUpPollViews: function() {
        messageBus.unsubscribe("/polls/" + this.get("post.id"));

        if (this.get("pollViews")) {
          _.forEach(this.get("pollViews"), v => v.destroy());
        }
      }.on("willClearRender")
    });
  }
};

{'fraction_non_alphanumeric': 0.16454159077109898, 'fraction_numerical': 0.0004047763610605141, 'mean_word_length': 2.4059269469331497, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '375527', 'n_tokens_mistral': 1469, 'n_tokens_neox': 1416, 'n_words': 283}