Skip to content

Keep ChatCops.on() handlers across init and return an unsubscribe function #71

Closed

Description

@anurag629

ChatCops.on() only works if a widget instance already exists. With the script tag, auto-init waits for DOMContentLoaded, so a handler registered in an inline script right after the tag is silently dropped. Handlers are also lost when init() is called again, because the old instance is destroyed. On top of that, the Events docs show const unsub = ChatCops.on(...), but on() returns nothing, so calling unsub() throws.

The Events page has a few other mismatches:

  • The message payload is documented with a timestamp. It's actually { id, role, content, status?, errorType? }.
  • The error payload is a plain Error, not an object with a type.
  • The leadCaptured and preChatSubmit events aren't documented. The widget README also lists only four events.

Where to look

  • packages/widget/src/index.ts (the ChatCops object: init, on, off)
  • packages/widget/src/widget.ts (WidgetEventType, on, off, destroy)
  • website/src/content/docs/widget/events.mdx
  • packages/widget/README.md (the "Events" line)
  • website/src/content/docs/widget/api.mdx (documents on and off, and says init must be called before any other method)

Acceptance criteria

  • ChatCops keeps its own list of handlers and attaches them to each new instance in init(); off() removes a handler from both
  • Handlers registered before init() still fire once the widget exists, and they survive a second init()
  • ChatCops.on() returns a function that unsubscribes the handler
  • WidgetEventType is exported and used in the public on/off signatures, replacing string plus a cast
  • Tests cover: registering before init, surviving re-init, unsubscribe, and off()
  • events.mdx documents all six events with their real payloads, the widget README's event list is updated, and api.mdx matches the new on() behavior
  • Changeset for @chatcops/widget (minor)

Note: #41 also changes packages/widget/src/index.ts, so expect to rebase.


Want to work on this? Comment below and I'll assign it to you. Please take one issue at a time, branch off dev, and open your PR against dev with Closes #<this issue number>. Setup notes (pnpm, build before test) are in CONTRIBUTING.md.

Activity

  1. added
    help wantedExtra attention is needed
    widgetAffects @chatcops/widget package
    hacktoberfestGood pick for Hacktoberfest contributors
    on Sep 30, 2026
  2. bhushan4work commented on Oct 19, 2026

    @bhushan4work
    Contributor

    @anurag629 I'd like to pick this issue

  3. anurag629 commented on Oct 19, 2026

    @anurag629
    MemberAuthor

    Hey @bhushan4work, it's yours, as your second next to ogcops #86. Everything in the issue still matches dev. The note at the bottom is out of date: #41 is done, so there's no rebase to expect.

    Let me know if you have any questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

hacktoberfestGood pick for Hacktoberfest contributorshelp wantedExtra attention is neededwidgetAffects @chatcops/widget package

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions