Class Store

An in memory store that has a bunch of usefful methods for retrieving Atomic Data Resources. It is also resposible for keeping the Resources in sync with Subscribers (components that use the Resource), and for managing the current Agent (User).

Hierarchy

  • Store

Constructors

Properties

_resources: Map<string, Resource>

All the resources of the store

agent?: Agent

Current Agent, used for signing commits. Is required for posting things.

client: Client
injectedFetch: ((input: URL | RequestInfo, init?: RequestInit) => Promise<Response>)

Type declaration

    • (input: URL | RequestInfo, init?: RequestInit): Promise<Response>
    • Parameters

      • input: URL | RequestInfo
      • Optional init: RequestInit

      Returns Promise<Response>

serverUrl: string

The base URL of an Atomic Server. This is where to send commits, create new instances, search, etc.

subscribers: Map<string, ResourceCallback[]>

A list of all functions that need to be called when a certain resource is updated

webSockets: Map<string, WebSocket>

Mapped from origin to websocket

Accessors

Methods

  • Deprecated

    Will be marked private in the future, please use addResources

    Adds a Resource to the store and notifies subscribers. Replaces existing resources, unless this new resource is explicitly incomplete.

    Parameters

    Returns void

  • Checks is a set of URL parts can be combined into an available subject. Will retry until it works.

    Parameters

    • Rest ...parts: string[]

    Returns Promise<string>

  • Checks if a subject is free to use

    Parameters

    • subject: string

    Returns Promise<boolean>

  • Creates a random URL. Add a classnme (e.g. 'persons') to make a nicer name

    Parameters

    • Optional className: string
    • Optional parentSubject: string

    Returns string

  • Always fetches resource from the server then adds it to the store.

    Parameters

    • subject: string

      The resource URL to be fetched

    • opts: { body?: string | ArrayBuffer; fromProxy?: boolean; method?: "GET" | "POST"; noWebSocket?: boolean; setLoading?: boolean } = {}
      • Optional body?: string | ArrayBuffer

        HTTP Body for POSTing

      • Optional fromProxy?: boolean

        Fetch it from the /path endpoint of your server URL. This effectively is a proxy / cache.

      • Optional method?: "GET" | "POST"

        HTTP Method, defaults to GET

      • Optional noWebSocket?: boolean

        Do not use WebSockets, use HTTP(S)

      • Optional setLoading?: boolean

        Overwrites the existing resource and sets it to loading.

    Returns Promise<Resource>

  • Parameters

    • path: string
    • firstTry: boolean = true

    Returns Promise<string>

  • Returns the Currently set Agent, returns null if there is none. Make sure to first run store.setAgent().

    Returns undefined | Agent

  • Returns the WebSocket for the current Server URL

    Returns undefined | WebSocket

  • Returns the ancestry of a resource, starting with the resource itself.

    Parameters

    Returns Promise<string[]>

  • Gets a resource by URL. Fetches and parses it if it's not available in the store. Not recommended to use this for rendering, because it might cause resources to be fetched multiple times.

    Parameters

    • subject: string

    Returns Promise<Resource>

  • Gets a resource by URL. Fetches and parses it if it's not available in the store. Instantly returns an empty loading resource, while the fetching is done in the background . If the subject is undefined, an empty non-saved resource will be returned.

    Parameters

    • subject: string = unknownSubject
    • opts: FetchOpts = {}

    Returns Resource

  • Returns the base URL of the companion server

    Returns string

  • Opens a Websocket for some subject URL, or returns the existing one.

    Parameters

    • subject: string

    Returns undefined | WebSocket

  • Inject a custom fetch implementation to use when fetching resources over http

    Parameters

    • fetchOverride: ((input: URL | RequestInfo, init?: RequestInit) => Promise<Response>)
        • (input: URL | RequestInfo, init?: RequestInit): Promise<Response>
        • Parameters

          • input: URL | RequestInfo
          • Optional init: RequestInit

          Returns Promise<Response>

    Returns void

  • If the store does not have an active internet connection, will return false. This may affect some functionality. For example, some checks will not be performed client side when offline.

    Returns boolean

  • Let's subscribers know that a resource has been changed. Time to update your views!

    Parameters

    Returns Promise<void>

  • This is called when Errors occur in some of the library functions. Set your errorhandler function to store.errorHandler.

    Parameters

    • e: string | Error

    Returns void

  • Opens a WebSocket for this Atomic Server URL

    Parameters

    • url: string

    Returns void

  • Parses the HTML document for JSON-AD data in tags, adds it to the store

    Returns void

  • Sends an HTTP POST request to the server to the Subject. Parses the returned Resource and adds it to the store.

    Parameters

    • url: string
    • data: string | ArrayBuffer

    Returns Promise<Resource>

  • Fetches all Classes and Properties from your current server, including external resources. This helps to speed up time to interactive, but may not be necessary for all applications.

    Returns Promise<void>

  • Removes (destroys / deletes) resource from this store

    Parameters

    • subject: string

    Returns void

  • Changes the Subject of a Resource. Checks if the new name is already taken, errors if so.

    Parameters

    Returns Promise<void>

  • Sets the current Agent, used for signing commits. Authenticates all open websockets, and retries previously failed fetches.

    Warning: doing this stores the Private Key of the Agent in memory. This might have security implications for your application.

    Parameters

    • agent: undefined | Agent

    Returns void

  • Sets the Server base URL, without the trailing slash.

    Parameters

    • url: string

    Returns void

  • Registers a callback for when the a resource is updated. When you call this, you should probably also call .unsubscribe some time later.

    Parameters

    Returns void

  • Parameters

    • subject: string

    Returns void

  • Parameters

    • subject: string

    Returns void

  • Parameters

    • files: File[]
    • parent: string

    Returns Promise<string[]>

Generated using TypeDoc