Foundry Virtual Tabletop - API Documentation - Version 14
    Preparing search index...

    Class SingletonEmbeddedCollection

    This class provides a foundry.utils.Collection wrapper around a singleton embedded Document so that it can be interacted with via a common interface.

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    _source: object[]

    The source data array from which the embedded collection is created

    documentClass: typeof Document

    The Document implementation used to construct instances within this collection.

    invalidDocumentIds: Set<string> = ...

    Record the set of document ids where the Document was not initialized because of invalid source data

    The parent Document to which this EmbeddedCollection instance belongs.

    name: string

    The name of this collection in the parent Document.

    _initialized: boolean = false

    Has this embedded collection been initialized as a one-time workflow?

    Accessors

    • get contents(): V[]

      Return an Array of all the entry values in the Collection

      Returns V[]

    • get documentName(): string | void

      The Document name of Documents stored in this collection.

      Returns string | void

    • get documentsByType(): Record<string, TDocument[]>

      This collection's contents grouped by subtype, lazily (re-)computed as needed. If the document type does not support subtypes, all will be in the "base" group.

      Returns Record<string, TDocument[]>

    Methods

    • Internal

      Follow-up actions to take when a database operation modifies Documents in this EmbeddedCollection.

      Parameters

      • action: DatabaseAction

        The database action performed

      • documents: any[]

        The array of modified Documents

      • result: any[]

        The result of the database operation

      • operation: DatabaseOperation

        Database operation details

      • user: BaseUser

        The User who performed the operation

      Returns void

    • Then iterating over a Collection, we should iterate over its values instead of over its entries

      Returns MapIterator<any>

    • Remove a document from the collection.

      Parameters

      • key: string

        The embedded Document ID.

      • Optionaloptions: { modifySource?: boolean } = {}

        Additional options to the delete operation.

        • OptionalmodifySource?: boolean

          Whether to modify the collection's source as part of the operation.

      Returns boolean

    • Filter the Collection, returning an Array of entries which match a functional condition.

      Parameters

      • condition: (value: any, index: number, collection: Collection<string, any>) => unknown

        The functional condition to test.

      Returns any[]

      An Array of matched values

      let c = new Collection([["a", "AA"], ["b", "AB"], ["c", "CC"]]);
      let hasA = c.filters(entry => entry.slice(0) === "A");
    • Find an entry in the Map using a functional condition.

      Parameters

      • condition: (value: any, index: number, collection: Collection<string, any>) => unknown

        The functional condition to test.

      Returns any

      The value, if found, otherwise undefined

      let c = new Collection([["a", "A"], ["b", "B"], ["c", "C"]]);
      c.get("a") === c.find(entry => entry === "A"); // true
    • Apply a function to each element of the collection

      Parameters

      • fn: (value: any, index: number) => void

        A function to apply to each element

      Returns void

      Array#forEach

      let c = new Collection([["a", {active: false}], ["b", {active: false}], ["c", {active: false}]]);
      c.forEach(e => e.active = true);
    • Get a document from the EmbeddedCollection by its ID.

      Parameters

      • id: string

        The ID of the Embedded Document to retrieve.

      • Optionaloptions: { invalid?: boolean; strict?: boolean } = {}

        Additional options to configure retrieval.

        • Optionalinvalid?: boolean

          Allow retrieving an invalid Embedded Document.

        • Optionalstrict?: boolean

          Throw an Error if the requested Embedded Document does not exist.

      Returns any

      The retrieved document instance, or undefined

      If strict is true and the Embedded Document cannot be found.

    • Obtain a temporary Document instance for a document id which currently has invalid source data.

      Parameters

      • id: string

        A document ID with invalid source data.

      • Optionaloptions: { strict?: boolean } = {}

        Additional options to configure retrieval.

        • Optionalstrict?: boolean

          Throw an Error if the requested ID is not in the set of invalid IDs for this collection.

      Returns any

      An in-memory instance for the invalid Document

      If strict is true and the requested ID is not in the set of invalid IDs for this collection.

    • Get an entry from the Collection by name. Use of this method assumes that the objects stored in the collection have a "name" attribute.

      Parameters

      • name: string

        The name of the entry to retrieve

      • Optionaloptions: { strict?: boolean } = {}

        Additional options that affect how entries are retrieved

        • Optionalstrict?: boolean

          Throw an Error if the requested name does not exist. Default false.

      Returns any

      The retrieved entry value, if one was found, otherwise undefined

      let c = new Collection([["a", "Alfred"], ["b", "Bob"], ["c", "Cynthia"]]);
      c.getName("Alfred"); // "Alfred"
      c.getName("D"); // undefined
      c.getName("D", {strict: true}); // throws Error
    • Initialize the EmbeddedCollection by synchronizing its Document instances with existing _source data. Importantly, this method does not make any modifications to the _source array. It is responsible for creating, updating, or removing Documents from the Collection.

      Parameters

      Returns void

    • Transform each element of the Collection into a new form, returning an Array of transformed values

      Type Parameters

      • U

      Parameters

      • transformer: (element: any, index: number, collection: Collection<string, any>) => U

        A transformation function applied to each entry value.

      Returns U[]

      An Array of transformed values

    • Reduce the Collection by applying an evaluator function and accumulating entries

      Type Parameters

      • U

      Parameters

      • reducer: (accum: U, element: T, index: number, collection: Collection<string, any>) => U

        A reducer function applied to each entry value.

      • initial: U

        An initial value which accumulates with each iteration

      Returns U

      The accumulated result

      let c = new Collection([["a", "A"], ["b", "B"], ["c", "C"]]);
      let letters = c.reduce((s, l) => {
      return s + l;
      }, ""); // "ABC"
    • Test whether a condition is met by some entry in the Collection.

      Parameters

      • condition: (element: T, index: number, set: Collection<string, any>) => boolean

        The functional condition to test.

      Returns boolean

      Was the test condition passed by at least one entry?

    • Convert the Collection to a primitive array of its contents.

      Returns object[]

      An array of contained values

    • Convert the EmbeddedCollection to an array of simple objects.

      Parameters

      • Optionalsource: boolean = true

        Draw data for contained Documents from the underlying data source?

      Returns object[]

      The extracted array of primitive objects

    • Protected

      Log warnings or errors when a Document is found to be invalid.

      Parameters

      • data: object

        The invalid Document's data.

      • err: Error

        The validation error.

      • Optionaloptions: { strict?: boolean } = {}

        Options to configure invalid Document handling.

        • Optionalstrict?: boolean

          Whether to throw an error or only log a warning.

      Returns void

    • Protected

      Initialize an embedded document and store it in the collection. The document may already exist, in which case we are reinitializing it with new _source data. The document may not yet exist, in which case we create a new Document instance using the provided source.

      Parameters

      Returns any

      The initialized document or null if no document was initialized