import _ = require("lodash"); import { AbstractSkeletosState } from "../../extendible/AbstractSkeletosState"; import { SkeletosCursor } from "../../base/SkeletosCursor"; import { SkeletosTransaction } from "../../base/SkeletosTransaction"; /** * A SkeletosDictionary allows you to build a dictionary where the keys are of type string and the * values are typed according to the generic template parameters, where the type needs to be a * subclass of AbstractSkeletosState. * * All key-value pairs in the dictionary are stored in the SkeletosDb. Additionally, every value * is a complete AbstractSkeletosState that you can make references to. * * A SkeletosDictionary is an AbstractSkeletosState itself, so the dictionary points to some cursor, and the * children cursors of that becomes the key-value pairs of the dictionary. * * You can also build a dictionary where the values are references to state stored elsewhere in the * tree. So you can imagine building a centralized cache A of all your users indexed by their ID, then * build out specific cache B indexed by username where the values are a reference to those in A. Or * you can build out a list of users in a project using SkeletosList where the the items in the list are * a reference to the values in the cache A. * * Note that the values in the dictionary do not support polymorphic types. That is, you cannot initialize * a dictionary with StateTypeA, put(..) a value of type StateTypeB, then expect StateTypeB to be returned on a * get(..) call. If StateTypeB is a subclass of StateTypeA, then an object of StateTypeA will be returned. If not, * an error will be thrown. To work around this, you can implement polymorphism in a container state type that * holds what type of object you are storing. * * The dictionary also does not support values of generic types. For example, you cannot expect the dictionary to * work with * class MyGenericState extends AbstractSkeletosState { .. } * * This form is a compile type dependency that is currently not supported. */ export declare class SkeletosDictionary extends AbstractSkeletosState { private static PROP_TYPE; private _typeConstructor; private _nameOfAttributeOfValueToCopyIntoKeyInto; /** * Create a new SkeletosDictionary at the supplied cursor. * * You will also need to supply the constructor of the type of value that you wish to store in * the dictionary. The dictionary will use this constructor to create a new object on get(..) calls. * * ------------------------------------------------------- * * The last argument, nameOfAttributeOfValueToCopyIntoKeyInto, is as interesting and neat to use as it is * confusing at first. Let's say you have a UserDataState that looks like this: * * UserDataState * -- id * -- name * -- age * * Now you are building a cache of users, where the key is the ID of the user. Let's say, you want to add a new * user you retrieved from the server: * * * const userDict = new SkeletosDictionary(usersCursor, UserDataState); * * const newUser: UserDataState = userDict.put(userDto.id); * newUser.id = userDto.id; // See comment below for this line * newUser.name = userDto.name; * newUser.age = userDto.age; * * * For the line marked with the comment, notice how you had to assign the ID of the user, even though you have * supplied the ID of the user as the key into the dictionary? This is redundant. Instead, check this out: * * * const userDict = new SkeletosDictionary(usersCursor, UserDataState, "id"); * * const newUser: UserDataState = userDict.put(userDto.id); * newUser.name = userDto.name; * newUser.age = userDto.age; * * * Here, we did not have to assign the ID. How did that work? Because we supplied, in the constructor, * "id" for nameOfAttributeOfValueToCopyIntoKeyInto argument. Thus, on every put(..), the SkeletosDictionary * will use the value of nameOfAttributeOfValueToCopyIntoKeyInto as the attribute of the value to insert * the key into. * * ------------------------------------------------------- * * @param cursor * @param typeConstructor * @param nameOfAttributeOfValueToCopyIntoKeyInto * @return {[stateType]} */ constructor(cursor: SkeletosCursor, typeConstructor: typeof AbstractSkeletosState, nameOfAttributeOfValueToCopyIntoKeyInto?: string); /** * Create a copy of the SkeletosDictionary from the given SkeletosDictionary, and make this new copy a modifiable * copy by supplying a SkeletosTransaction. * * @param stateToMakeCopyOf * @param transaction */ constructor(stateToMakeCopyOf: SkeletosDictionary, transaction: SkeletosTransaction); /** * Returns the nameOfAttributeOfValueToCopyIntoKeyInto. What is that? * * Let's say you have a UserDataState that looks like this: * * UserDataState * -- id * -- name * -- age * * Now you are building a cache of users, where the key is the ID of the user. Let's say, you want to add a new * user you retrieved from the server: * * * const userDict = new SkeletosDictionary(usersCursor, UserDataState); * * const newUser: UserDataState = userDict.put(userDto.id); * newUser.id = userDto.id; // See comment below for this line * newUser.name = userDto.name; * newUser.age = userDto.age; * * * For the line marked with the comment, notice how you had to assign the ID of the user, even though you have * supplied the ID of the user as the key into the dictionary? This is redundant. Instead, check this out: * * * const userDict = new SkeletosDictionary(usersCursor, UserDataState, "id"); * * const newUser: UserDataState = userDict.put(userDto.id); * newUser.name = userDto.name; * newUser.age = userDto.age; * * * Here, we did not have to assign the ID. How did that work? Because we supplied, in the constructor, * "id" for nameOfAttributeOfValueToCopyIntoKeyInto argument. Thus, on every put(..), the SkeletosDictionary * will use the value of nameOfAttributeOfValueToCopyIntoKeyInto as the attribute of the value to insert * the key into. * * @returns {string} */ readonly nameOfAttributeOfValueToCopyIntoKeyInto: string; /** * Whether or not a value for the given key exists in this dictionary. * * @param {string} key * @return {boolean} */ exists(key: string): boolean; /** * Returns the value at the given key. Returns null if the value does not exist for the given key. * * @param {string} key * @return {T} */ get(key: string): X; /** * If the value exists at the given key, then retrieves that value. If it doesn't exist, then this API behaves * like the put(..) API. Use this function instead of: * * var item: T; * if (dict.exists(key)) { * item = dict.get(key); * } else { * item = dict.put(key); * } * * So instead you would write: * * var item: T = dict.getIfExistsOrPut(key); * * @param key */ getIfExistsOrPut(key: string, typeConstructorHint?: typeof AbstractSkeletosState): X; /** * Reserves a spot in the SkeletosDictionary for the given key and returns the corresponding value. * * This API is a bit different than regular Map or Dictionary put API. Instead of looking like this: * * value.id = "123"; * value.name = "Blah"; * dict.put("123", value); * * It looks like this: * * var value = dict.put("123"); * value.id = "123"; * value.name = "Blah"; * * The reason for the single argument .put API is that this dictionary is backed by a tree database, and every * key and value in this dictionary belongs in that central database. And the tree database only accepts primitive * types to be stored in every node, so you cannot insert a complex object. Thus, when you put a key into the * dictionary, we reserve a spot for that key and return a value, which you can then modify. The type of value * returned is the one that is used as the generic template parameter to create this dictionary. * * Note: that if a value at the given key already existed before this put call, it will be deleted first. * Adds a new key-value pair and returns the value so you can mutate it further. * * Note 2: if a nameOfAttributeOfValueToCopyIntoKeyInto was supplied in the constructor, then it will be used. See * the constructor documentation for more details. * * @param {T} item */ put(key: string, typeConstructorHint?: typeof AbstractSkeletosState): X; /** * Puts a key-value pair in the dictionary where the key is as specified and the value is a reference that points * to * referenceTo state. The referenceTo state object must be of the same type as what this dictionary was initialized * with. * * Note: that if a value at the given key already existed before this put call, it will be deleted first. * Adds a new key-value pair and returns the value so you can mutate it further. * * Note 2: calling internalPut() and setting the doNotCopyIntoKey arg to true will ensure that even if * _nameOfAttributeOfValueToCopyIntoKeyInto has been defined, putReference() will still not create an attribute * into this key (since this key is a reference). * * @param key * @param referenceTo * @returns {T} */ putReference(key: string, referenceTo: X): X; /** * A wrapper around `putReferences` which accepts a key attribute (which should be the unique ID of the object), * and a list of objects to put references to. * * @param keyAttr The name of the unique identifying attribute in each of the values, ex: * CommonConstants.ATTRIBUTE_ID * @param values The values to put references to. * @see #putReference */ putReferencesToAll(keyAttr: string, values: X[]): X[]; /** * Removes the key-value pair for the given key from the dictionary. * * @param {string} key */ remove(key: string): void; /** * Clears the entire dictionary by removing all the entries in this map. */ clear(): void; /** * Returns the dictionary as a _.Dictionary so that you can do other operations from lodash on the dictionary. * * Note: any changes made to the returned _.Dictionary would not be reflected back in this SkeletosDictionary. * * @return {_.Dictionary} */ asDictionary(): _.Dictionary; /** * Convenience method for _.keys(dict.asDictionary(), ...). * * @returns {string[]} */ keys(): string[]; /** * Convenience method for returning all the values as an array of States. * * @returns {T[]} */ values(): X[]; /** * Convenience method for _.forEach(dict.asDictionary(), ...). * * @type {[stateType]} */ forEach(iteratee: _.DictionaryIterator, thisArg?: any): _.Dictionary; /** * Convenience method for _.map(dict.asDictionary(), ...). * * @type {[stateType]} */ map(iteratee: _.DictionaryIterator, thisArg?: any): TResult[]; /** * Convieince method for _.isEmpty(this.asDictionary()) * * @returns {boolean} */ isEmpty(): boolean; /** * Ensures that the cursor to the dictionary exists even when there are no items in the dictionary. * * Useful when you want to make a reference to the dictionary. */ ensureCreated(): void; /** * The type of values stored in this dictionary. This does not include subclassed type. * * @returns {typeof AbstractSkeletosState} */ readonly valueType: typeof AbstractSkeletosState; /** * Constructs a new instance of the supplied type for this dictionary. * * @param cursor * @returns {T} */ private newValue; /** * Puts a key into the db. Optionally supply a boolean to force-control whether to copy key into. * * @param key * @param doNotCopyIntoKey * @returns {T} */ private internalPut; }