@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
    recoverOrphans synchronizeRecoverOrphans

    Whenever a change is made via put, patch, and delete or received from get, discover, and recoverOrphans, 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 put. 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.

    The source code for this library is available on GitHub.

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    Accessors

    Methods

    CRUD Methods

    Query Methods

    Session Management

    Synchronize Methods

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

    Constructors

    Properties

    ajv_: Promise<Ajv> | undefined
    applyPatch_:
        | Promise<
            <T>(
                document: T,
                patch: readonly Operation[],
                validateOperation?: boolean | Validator<T>,
                mutateDocument?: boolean,
                banPrototypeModifications?: boolean,
            ) => PatchResult<T>,
        >
        | undefined
    callbacks: Set<GraffitiSynchronizeCallback> = ...
    graffiti: Graffiti

    Accessors

    • get applyPatch(): Promise<
          <T>(
              document: T,
              patch: readonly Operation[],
              validateOperation?: boolean | Validator<T>,
              mutateDocument?: boolean,
              banPrototypeModifications?: boolean,
          ) => PatchResult<T>,
      >

      Returns Promise<
          <T>(
              document: T,
              patch: readonly Operation[],
              validateOperation?: boolean | Validator<T>,
              mutateDocument?: boolean,
              banPrototypeModifications?: boolean,
          ) => PatchResult<T>,
      >

    Methods

    CRUD Methods

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

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

    It is not possible to re-put an object that has been deleted to ensure a person's right to be forgotten. In cases where deleting and restoring an object is useful, an object's allowed property can be set to an empty list to hide it from all actors except the creator.

    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.

    The retrieved object is type-checked against the provided JSON schema otherwise a GraffitiErrorSchemaMismatch is thrown.

    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.

    patch: (
        patch: GraffitiPatch,
        url: string | GraffitiObjectUrl,
        session: GraffitiSession,
    ) => Promise<GraffitiObjectBase> = ...

    Patches an existing object at a given url. The patching 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.

    put: <Schema extends JSONSchema>(
        object: GraffitiPutObject<Schema>,
        session: GraffitiSession,
    ) => Promise<GraffitiObjectBase> = ...

    Creates a new object or replaces an existing object. An object can only be replaced by the same actor that created it.

    Replacement occurs when the url of the replaced object exactly matches an existing object's URL.

    Type Declaration

    GraffitiErrorNotFound if a url is provided that has not been created yet or the actor is not allowed to see it.

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

    Query Methods

    channelStats: (session: GraffitiSession) => GraffitiChannelStatsStream

    Returns statistics about all the channels that an actor has posted to. This method will not return statistics related to any other actor's channel usage.

    Like recoverOrphans, this method is not useful for most applications, but necessary for getting a global view of all an actor's Graffiti data to implement something like Facebook's Activity Log or a debugging interface.

    Like discover, objects are returned asynchronously as they are discovered and the stream will end once all leads have been exhausted.

    Type Declaration

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

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

    The continuation may also include duplicates of objects that were already returned by the original stream. This is dependent on how much state the underlying implementation maintains.

    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 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. It is also possible that duplicate objects are returned and their lastModified fields must be used to determine which object is the most recent.

    Type Declaration

    recoverOrphans: <Schema extends JSONSchema>(
        schema: Schema,
        session: GraffitiSession,
    ) => GraffitiObjectStream<Schema> = ...

    Discovers objects not contained in any channels that were created by the querying actor and match the given JSON Schema. Unlike discover, this method will not return objects created by other actors.

    Like channelStats, this method is not useful for most applications, but necessary for getting a global view of all an actor's Graffiti data to implement something like Facebook's Activity Log or a debugging interface.

    Like discover, objects are returned asynchronously as they are discovered, the stream will end once all leads have been exhausted, and the stream can be continued using the continue method or cursor string.

    Type Declaration

    Session Management

    login: (proposal?: { actor?: string; scope?: {} }) => 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

      • (proposal?: { actor?: string; scope?: {} }): Promise<void>
      • Parameters

        • Optionalproposal: { actor?: string; scope?: {} }

          Suggestions for the permissions that the login process should grant. The login process may not provide the exact proposed permissions.

          • 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.

          • Optionalscope?: {}

            A yet to be defined permissions scope. An application may use this to indicate the minimum necessary scope needed to operate. For example, it may need to be able read private messages from a certain set of channels, or write messages that follow a particular schema.

            The login process should make it clear what scope an application is requesting and allow the user to enhance or reduce that scope as necessary.

        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:

    Synchronize Methods