ai icon indicating copy to clipboard operation
ai copied to clipboard

`useObject` should support onChunk callback option.

Open kosukeoya opened this issue 10 months ago • 0 comments

Description

Although onFinish is executed after all chunks have been read, there are cases where we want it to be executed every time a chunk is read.

I would like to call tiptap's setContent after each chunk read of the useObject. Here is an implementation I can do right now

const Component = () => {
  const { object } = useObject({ api: 'http://example.com' });

  useEffect(() => {
    if (object.content) {
      editor?.commands.setContent(md.render(object.content));
    }
  }, [object.content, editor])
}

However, this code will result in an infinite loop because the editor instance is updated each time setContent is executed.

It would be better to implement something like the following

const Component = () => {
  const { object } = useObject({ api: 'http://example.com' }, {
   onChunk: (object) => {
      if (object.content) {
        editor?.commands.setContent(md.render(object.content));
      }
    }
  });
}

kosukeoya avatar Apr 24 '25 14:04 kosukeoya