Search code examples
javascriptrxjssolid-js

Where is the subscribe function coming from in the SolidJS docs?


I have checked thoroughly and found that SolidJS does not have an observable where one can subscribe to store reactive primitives, only signals as far as I know. What is the observable used here if that is the case?

const unsubscribe = store.subscribe(({ todos }) => {
  setState('todos', reconcile(todos))
});

onCleanup(() => unsubscribe());

If subscribing to such values is not possible, how do I track the changes inside my store and execute some statements accordingly with the store just as a tracking dependency?


Solution

  • Solid has a subscribe function exported from the core library so that you can listen to external observables, like the ones created by RxJS, by creating a signal.

    An RxJS observable pushes changes to its listeners via an event. The signal you get from the subscribe method will allow you to track the changes.

    So subscribe is a method to interface with other reactive libraries.

    Now about the subscribe function on the snippet you share, it seems the store value is an external observable that emits todos. The callback receives this todos values and updates a solid store by setting new todos. The snippet demonstrates how to use the reconcile function.