@graffiti-garden/wrapper-synchronize
    Preparing search index...

    Class GraffitiSynchronize

    Wraps the Graffiti API so that changes made or received in one part of an application are automatically routed to other parts of the application. This is an important tool for building responsive and consistent user interfaces, and is built upon to make the Graffiti Vue Plugin and possibly other front-end libraries in the future.

    See a live example.

    Specifically, this library provides the following synchronize methods to correspond with each of the following Graffiti API methods:

    API Method Synchronize Method
    get synchronizeGet
    discover synchronizeDiscover

    Whenever a change is made via post and delete or received from get, discover, and continueDiscover, those changes are forwarded to the appropriate synchronize method. Each synchronize method returns an iterator that streams these changes continually until the user calls return on the iterator or breaks out of the loop, allowing for live updates without additional polling.

    Example 1: Suppose a user publishes a post using post. If the feed displaying that user's posts is using synchronizeDiscover to listen for changes, then the user's new post will instantly appear in their feed, giving the UI a responsive feel.

    Example 2: Suppose one of a user's friends changes their name. As soon as the user's application receives one notice of that change (using get or discover), then synchronizeDiscover listeners can be used to update all instance's of that friend's name in the user's application instantly, providing a consistent user experience.

    Additionally, the library supplies a synchronizeAll method that can be used to stream all the Graffiti changes that an application is aware of, which can be used for caching or history building.

    The source code for this library is available on GitHub.

    Implements

    Index

    Constructors

    Properties

    Accessors

    Methods

    0 - Synchronize Methods

    This group contains methods that listen for changes made via post, and delete or fetched from get, discover, or continueDiscover and then streams appropriate changes to provide a responsive and consistent user experience.

    1 - Single-Object Methods

    2 - Multi-Object Methods

    3 - Media Methods

    4 - Identity Methods

    Constructors

    Properties

    ajv_: Promise<Ajv> | undefined
    callbacks: Set<GraffitiSynchronizeCallback> = ...
    graffiti: Graffiti

    Accessors

    Methods

    0 - Synchronize Methods

    1 - Single-Object Methods

    delete: (
        url: string | GraffitiObjectUrl,
        session: GraffitiSession,
    ) => Promise<void> = ...

    Deletes an object from a given url that had previously been posted. The deleting actor must be the same as the actor that created the object.

    Type Declaration

    GraffitiErrorNotFound if the object does not exist, has already been deleted, or the actor is not allowed to access it.

    GraffitiErrorForbidden if the actor is not the same actor as the one who created the object.

    get: <Schema extends JSONSchema>(
        url: string | GraffitiObjectUrl,
        schema: Schema,
        session?: GraffitiSession | null,
    ) => Promise<GraffitiObject<Schema>> = ...

    Retrieves an object from a given url matching the provided schema.

    If the retreiving actor is not the object's actor, the object's allowed and channels properties are not revealed, similar to a BCC email.

    Type Declaration

      • <Schema extends JSONSchema>(
            url: string | GraffitiObjectUrl,
            schema: Schema,
            session?: GraffitiSession | null,
        ): Promise<GraffitiObject<Schema>>
      • Type Parameters

        Parameters

        • url: string | GraffitiObjectUrl

          The location of the object to get.

        • schema: Schema

          The JSON schema to validate the retrieved object against.

        • Optionalsession: GraffitiSession | null

          An implementation-specific object with information to authenticate the actor. If no session is provided, the retrieved object's allowed property must be undefined.

        Returns Promise<GraffitiObject<Schema>>

        Returns the retrieved object.

    GraffitiErrorNotFound if the object does not exist, has been deleted, or the actor is not allowed to access it.

    GraffitiErrorSchemaMismatch if the retrieved object does not match the provided schema.

    post

    post: <Schema extends JSONSchema>(
        partialObject: GraffitiPostObject<Schema>,
        session: GraffitiSession,
    ) => Promise<GraffitiObject<Schema>> = ...

    Creates a new object.

    Type Declaration

    2 - Multi-Object Methods

    continueDiscover: (
        cursor: string,
        session?: GraffitiSession | null,
    ) => GraffitiObjectStreamContinue<{}> = ...

    Continues a GraffitiObjectStream from a given cursor string. The continuation will return new objects that have been posted that match the original stream, and also returns the urls of objects that have been deleted, as marked by a tombstone.

    The cursor allows the client to serialize the state of the stream and continue it later. However this method loses any typing information that was present in the original stream. For better type safety and when serializing is not necessary, use the continue method instead, which is returned along with the cursor at the end of the original stream.

    GraffitiErrorForbidden if the actor provided in the session is not the same as the actor that initiated the original stream.

    discover: <Schema extends JSONSchema>(
        channels: string[],
        schema: Schema,
        session?: GraffitiSession | null,
    ) => GraffitiObjectStream<Schema> = ...

    Discovers objects created by any actor that are contained in at least one of the given channels and match the given JSON Schema.

    Objects are returned asynchronously as they are discovered but the stream will end once all leads have been exhausted. The GraffitiObjectStream ends by returning a continue method and a cursor string, each of which can be be used to poll for new objects. The continue method preserves the type safety of the stream and the cursor string can be serialized to continue the stream after an application is closed and reopened.

    discover will not return objects that the querying actor is not allowed to access. If the actor is not the creator of a discovered object, the allowed list will be masked to only contain the querying actor if the allowed list is not undefined (public). Additionally, if the actor is not the creator of a discovered object, any channels not specified by the discover method will not be revealed. This masking happens before the object is validated against the supplied schema.

    Since different implementations may fetch data from multiple sources there is no guarentee on the order that objects are returned in.

    Type Declaration

    3 - Media Methods

    deleteMedia: (mediaUrl: string, session: GraffitiSession) => Promise<void>

    Deletes media previously posted to a given URL.

    Type Declaration

      • (mediaUrl: string, session: GraffitiSession): Promise<void>
      • Parameters

        • mediaUrl: string

          A globally unique identifier and locator for the media.

        • session: GraffitiSession

          An implementation-specific object with information to authenticate the actor.

        Returns Promise<void>

    GraffitiErrorNotFound if no media at that URL exists.

    GraffitiErrorForbidden if the actor provided in the session is not the same as the actor that posted the media.

    getMedia: (
        mediaUrl: string,
        requirements: { accept?: string; maxBytes?: number },
        session?: GraffitiSession | null,
    ) => Promise<{ actor: string; allowed?: string[] | null; data: Blob }>

    Retrieves media from the given media URL, adhering to the given requirements.

    Type Declaration

      • (
            mediaUrl: string,
            requirements: { accept?: string; maxBytes?: number },
            session?: GraffitiSession | null,
        ): Promise<{ actor: string; allowed?: string[] | null; data: Blob }>
      • Parameters

        • mediaUrl: string

          A globally unique identifier and locator for the media.

        • requirements: { accept?: string; maxBytes?: number }

          A set of requirements the retrieved media must meet.

          • Optionalaccept?: string

            A list of acceptable media types for the retrieved media, formatted as like an HTTP Accept header

          • OptionalmaxBytes?: number

            The maximum acceptable size, in bytes, of the media.

        • Optionalsession: GraffitiSession | null

          An implementation-specific object with information to authenticate the actor.

        Returns Promise<{ actor: string; allowed?: string[] | null; data: Blob }>

        The URL of the retrieved media, as a Blob and the actor that posted it.

    GraffitiErrorNotFound if no media at that URL exists.

    GraffitiErrorTooLarge if the media exceeds the given maxBytes.

    GraffitiErrorNotAcceptable if the media does not match the given accept specification.

    postMedia: (
        media: { allowed?: string[] | null; data: Blob },
        session: GraffitiSession,
    ) => Promise<string>

    Uploads media data, such as an image or video.

    Unlike structured objects, media is not indexed for discovery and must be retrieved by its exact URL using getMedia

    Type Declaration

      • (
            media: { allowed?: string[] | null; data: Blob },
            session: GraffitiSession,
        ): Promise<string>
      • Parameters

        • media: { allowed?: string[] | null; data: Blob }
          • Optionalallowed?: string[] | null

            An optional list, identical in function to an object's allowed property, that specifies the actors who are allowed to access the media. If the list is undefined or null, anyone with the URL can access the media. If the list is empty, only the actor who posted the media can access it.

          • data: Blob

            The binary data of the media to be uploaded, along with its media type, formatted as a Blob.

        • session: GraffitiSession

          An implementation-specific object with information to authenticate the actor.

        Returns Promise<string>

        The URL that the media was posted to.

    4 - Identity Methods

    actorToHandle: (actor: string) => Promise<string>

    Retrieves the human-readable handle associated with the given actor. The handle may change over time and so it should be used for display purposes only.

    The inverse of handleToActor.

    Type Declaration

      • (actor: string): Promise<string>
      • Parameters

        • actor: string

        Returns Promise<string>

        A human-readable handle for the given actor.

    GraffitiErrorNotFound if a handle cannot be found for the given actor.

    handleToActor: (handle: string) => Promise<string>

    Retrieves the actor ID associated with the given handle.

    The inverse of actorToHandle.

    Type Declaration

      • (handle: string): Promise<string>
      • Parameters

        • handle: string

        Returns Promise<string>

        The actor ID for the given handle.

    GraffitiErrorNotFound if there is no actor with the given handle.

    login: (actor?: string) => Promise<void>

    Begins the login process. Depending on the implementation, this may involve redirecting to a login page or opening a popup, so it should always be called in response to a gesture, such as clicking a button, due to the feature-gating browser security feature.

    The session object is returned asynchronously via sessionEvents as a GraffitiLoginEvent with event type login.

    Type Declaration

      • (actor?: string): Promise<void>
      • Parameters

        • Optionalactor: string

          A suggested actor to login as. For example, if a user tries to edit a post but are not logged in, the interface can infer that they might want to log in as the actor who created the post they are attempting to edit.

          Even if provided, the implementation should allow the user to log in as a different actor if they choose.

        Returns Promise<void>

    logout: (session: GraffitiSession) => Promise<void>

    Begins the logout process for a particular session. Depending on the implementation, this may involve redirecting the user to a logout page or opening a popup, so it should always be called in response to a gesture, such as clicking a button, due to the feature-gating browser security feature.

    A confirmation will be returned asynchronously via sessionEvents as a GraffitiLogoutEvent as event type logout.

    Type Declaration

    sessionEvents: EventTarget

    An event target that can be used to listen for the following events and their corresponding event types: