This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
libtf:documentation:api:persistence [2018/02/24 19:08] – lee | libtf:documentation:api:persistence [2018/09/24 15:45] (current) – removed lee | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | [[libtf: | ||
- | |||
- | ====== LibTF.Persistence ====== | ||
- | |||
- | Most web browsers only allow for 10 megs (though some are as low as 5) of on-disk storage per origin. | ||
- | |||
- | In an attempt to minimize this issue, LibTF uses a defaults and delta persistence system. | ||
- | |||
- | In order to be persistent, all persistent objects must ultimately derive from the LibTF.PersistentObject class. | ||
- | |||
- | Each PersistentObject that is tracked by LibTF.Persistence has an " | ||
- | |||
- | A PersistentObject may be the " | ||
- | |||
- | You won't generally need to worry too much about the id hierarchy. | ||
- | |||
- | |||
- | ===== Fields ===== | ||
- | |||
- | None. | ||
- | |||
- | |||
- | ===== Methods ===== | ||
- | |||
- | ==== LibTF.Persistence.setDefaults( id, objectClass, | ||
- | |||
- | * //id// is a toplevel persistence id which will be used to fetch the object later. | ||
- | * // | ||
- | * // | ||
- | * Returns: Nothing. | ||
- | * Throws: Error if: //id// contains invalid characters, // | ||
- | |||
- | Define a top-level PersistentObject within your game. setDefaults() must only be called at initialization time, from story javascript or initialization passages. | ||
- | |||
- | <code javascript> | ||
- | LibTF.Persistence.setDefaults( " | ||
- | " | ||
- | " | ||
- | " | ||
- | " | ||
- | " | ||
- | " | ||
- | " | ||
- | " | ||
- | " | ||
- | ], | ||
- | " | ||
- | } ); | ||
- | </ | ||
- | |||
- | |||
- | ==== LibTF.Persistence.instantiate( id, forceClass=null ) ==== | ||
- | |||
- | * //id// is a persistence id denoting an object (whose class is ultimately derived from PersistentObject) to instantiate. | ||
- | * // | ||
- | * Returns: An instantiated object, previously defined with setDefaults(). | ||
- | * Throws: Error if: //id// contains invalid characters, // | ||
- | |||
- | Instantiates a working copy of an object (whose class was ultimately derived from PersistentObject), | ||
- | |||
- | //DO NOT// store these objects themselves in the story format' | ||
- | |||
- | In the Sugarcube 2 and Snowman story formats, you may use the tfObj() function as a shorthand for this, since it is used so much. | ||
- | |||
- | <code javascript> | ||
- | var joe = LibTF.Persistence.instantiate( " | ||
- | alert( " | ||
- | </ | ||
- | |||
- | Sugarcube/ | ||
- | <code javascript> | ||
- | alert( " | ||
- | </ | ||
- | |||
- | |||
- | ==== LibTF.Persistence.has( id ) ==== | ||
- | |||
- | * //id// is a persistence id to check for. | ||
- | * Returns: True there is a data record for //id// in the LibTF persistence engine, or False if there is not. | ||
- | * Throws: Error if: //id// contains invalid characters. | ||
- | |||
- | Checks to see if there is a record for //id// in the LibTF persistence engine. | ||
- | |||
- | <code javascript> | ||
- | if( LibTF.Persistence.has(" | ||
- | alert( "It would appear that european_swallow has an irregular article defined!" | ||
- | </ | ||
- | |||
- | |||
- | ==== LibTF.Persistence.get( id ) ==== | ||
- | |||
- | * //id// is the persistence id whose data record we want to retrieve. | ||
- | * Returns: The data record associated with //id//, or null if there isn't one. | ||
- | * Throws: Error if: //id// contains invalid characters. | ||
- | |||
- | Checks to see if there is a record for //id// in the LibTF persistence engine. | ||
- | |||
- | <code javascript> | ||
- | var article = LibTF.Persistence.get( " | ||
- | alert( "You see " + article | ||
- | </ | ||
- | (Would print "You see a european swallow here!", | ||
- | |||
- | |||
- | ==== LibTF.Persistence.set( id, value ) ==== | ||
- | |||
- | * //id// is the persistence id whose data record we want to set. | ||
- | * //value // is the value we want to set it to. | ||
- | * Returns: Nothing, which is a bug. It ought to return the previous value. | ||
- | * Throws: Error if: //id// contains invalid characters. | ||
- | |||
- | Sets the data record associated with //id// in the LibTF persistence engine. | ||
- | |||
- | <code javascript> | ||
- | LibTF.Persistence.set( " | ||
- | </ | ||
- | |||
- | |||
- | ==== LibTF.Persistence.reset( id ) ==== | ||
- | |||
- | * //id// is the persistence id whose data record we want to reset. | ||
- | * Returns: the previous value (before reset). | ||
- | * Throws: Error if: //id// contains invalid characters. | ||
- | |||
- | Resets the data record associated with //id// to whatever it was defined with in setDefaults(). | ||
- | |||
- | <code javascript> | ||
- | LibTF.Persistence.reset( " | ||
- | </ | ||
- | |||
- | [[libtf: | ||