@audiotool/nexus - v0.0.12
    Preparing search index...

    Interface NexusEventManager

    Can be used to subscribe to changes in the document.

    Events for all existing entities are dispatched after index.SyncedDocument.start is called; after this, they are dispatched either:

    • as a result of remote changes, which happens while the transaction lock isn't taken by your code
    • as a result of local changes made using a document.TransactionBuilder
    nexus.events.onCreate("tonematrix", (tm) => {
    console.debug("tonematrix created")
    return () => console.debug("tonematrix removed")
    })
    await nexus.modify(t => t.create("tonematrix", {}))

    When modifying the document using the document.TransactionBuilder, the event callbacks are executed immediately during the callback that creates the modification(s).

    const tm = await nexus.modify((t) => t.create("tonematrix", {}))

    // attach onUpdate callback
    nexus.events.onUpdate(tm.fields.isActive, (isActive) =>
    console.debug("(2) updating", isActive === tm.fields.isActive.value),
    )

    await nexus.modify((t) => {
    console.debug("(1) will update")
    // update the field - callback above is executed before this method returns
    t.update(tm.fields.isActive, true)
    console.debug("(3) have updated")
    })
    interface NexusEventManager {
        onCreate<T extends (keyof EntityTypes) | "*">(
            entityType: T,
            callback: (
                entity: NexusEntityUnion<T extends "*" ? keyof EntityTypes : T>,
            ) => void | (() => void),
        ): Terminable;
        onPointingTo(
            to: NexusLocation,
            callback: (from: NexusLocation) => void,
            initialTrigger?: boolean,
        ): Terminable;
        onRemove<F extends keyof EntityTypes>(
            entity: "*" | NexusEntity<F>,
            callback: (entity: NexusEntityUnion<F>) => void,
        ): Terminable;
        onStopPointingTo(
            to: NexusLocation,
            callback: (from: NexusLocation) => void,
        ): Terminable;
        onUpdate<P extends PrimitiveType>(
            field: PrimitiveField<P, "mut">,
            callback: (value: P) => void,
            initialTrigger?: boolean,
        ): Terminable;
    }
    Index

    Methods

    • Subscribe to the event that an entity of a specific type is created.

      Type Parameters

      • T extends (keyof EntityTypes) | "*"

      Parameters

      • entityType: T

        The entity type to subscribe to, or * to subscribe to the creation of all entity types.

      • callback: (
            entity: NexusEntityUnion<T extends "*" ? keyof EntityTypes : T>,
        ) => void | (() => void)

        The callback called right after the entity is created.

        nexus.events.onCreate("tonematrix", (tm) => {
        console.debug("tonematrix", tm.id, "created")
        return () => console.debug("tonematrix", tm.id, "removed")
        })
          • (
                entity: NexusEntityUnion<T extends "*" ? keyof EntityTypes : T>,
            ): void | (() => void)
          • Parameters

            Returns void | (() => void)

            Optionally a function that's called when the created entity is removed.

      Returns Terminable

      A terminable that when terminated will stop dispatching new onCreate events. Cleanup functions that were returned during entity creation will still be called on removal of the entity.

    • Subscribe to the event that some pointer in the document starts pointing to a given location.

      Parameters

      • to: NexusLocation

        The location that is being pointed to

      • callback: (from: NexusLocation) => void

        Called right after from starts pointing to to.

          • (from: NexusLocation): void
          • Parameters

            • from: NexusLocation

              The location that points to to. from is always the location of a pointer field.

            Returns void

      • initialTrigger: boolean = true

        Whether the callback should be executed immediately with all pointers pointing to to.

      Returns Terminable

      A terminable that when terminated will stop dispatching new onPointingTo events.

      const tm = await nexus.modify(t => t.create("tonematrix", {}))
      nexus.events.onPointingTo(tm.fields.audioOutput, (from) =>
      console.debug(
      "pointing from field",
      from.toString(),
      "which is entity",
      nexus.queryEntities.getEntity(from.entityId)?.id
      ))

      If the pointer is the result of an entity being created, then onCreate is called before this callback.

    • Subscribe to an event where an entity is removed.

      Type Parameters

      Parameters

      • entity: "*" | NexusEntity<F>

        The entity whose removal triggers the callback, or * to subscribe to the removal of all entities.

      • callback: (entity: NexusEntityUnion<F>) => void

        The callback called right after the entity is removed.

      Returns Terminable

      A terminable that when terminated will stop dispatching new onRemove events.

      const tm = await nexus.modify(t => t.create("tonematrix", {}))
      nexus.events.onRemove(tm, (tm) => console.debug("tonematrix", tm.id, "removed"))
    • Subscribe to the event that some pointer in the document stops pointing to a given location.

      Parameters

      • to: NexusLocation

        The location that is being stopped pointing to.

      • callback: (from: NexusLocation) => void

        Called right after from stops pointing to to.

          • (from: NexusLocation): void
          • Parameters

            • from: NexusLocation

              The location that stops pointing to to. from is always the location of a pointer field.

            Returns void

      Returns Terminable

      A terminable that when terminated will stop dispatching new onStopPointingTo events.

      const tm = await nexus.modify(t => t.create("tonematrix", {}))
      nexus.events.onStopPointingTo(tm.fields.audioOutput, (from) =>
      console.debug("pointing from field", from.toString(), "which is entity", nexus.queryEntities.getEntity(from.entityId)?.id
      ))

      If the pointer is the result of an entity being removed, then onRemove is called after this callback.

    • Subscribe to updates of a mutable primitive field in the nexus document.

      Type Parameters

      Parameters

      • field: PrimitiveField<P, "mut">

        primitive field whose updates are subscribed to

      • callback: (value: P) => void

        The callback called right after the update.

          • (value: P): void
          • Parameters

            • value: P

              the new value the field receives, and will always match the current field value of the field listened on.

            Returns void

      • initialTrigger: boolean = true

        Whether the callback should be triggered immediately with the current value of the field

      Returns Terminable

      A terminable that when terminated will stop dispatching new onUpdate events.