PlacesTransactions.rst (3330B)
1 PlacesTransactions 2 ================== 3 4 This module serves as the transactions manager for Places (hereinafter *PTM*). 5 We need a transaction manager because the bookmarking UI allows users to use 6 `Undo` and `Redo` functions. To implement transactions a layer has been inserted 7 between the UI and the Bookmarks API. 8 This layer stores all the requested changes in a stack and perform calls to the 9 API. Interall the transactions history is implemented as an array storing 10 changes from oldest to newest. 11 12 Transactions implements all the elementary UI commands: creating items, editing 13 their properties, and so forth. All the commands are stored in transactions 14 history and are executed in order. 15 16 Constructing transactions 17 ------------------------- 18 19 Transactions are exposed by the module as constructors 20 (e.g. PlacesTransactions.NewFolder). The input for these constructors is taken 21 in the form of a single argument, a plain object consisting of the properties 22 for the transaction. Input properties may either be required or optional (for 23 example, *keyword* is required for the ``EditKeyword`` transaction, but optional 24 for the ``NewBookmark`` transaction). 25 26 Executing Transactions (the `transact` method of transactions) 27 -------------------------------------------------------------- 28 29 Once a transaction is created, you must call it's ``transact`` method for it to 30 be executed and take effect. 31 That is an asynchronous method that takes no arguments, and returns a promise 32 that resolves once the transaction is executed. 33 34 Executing one of the transactions for creating items (``NewBookmark``, 35 ``NewFolder``, ``NewSeparator``) resolves to the new item's *GUID*. 36 There's no resolution value for other transactions. 37 38 If a transaction fails to execute, ``transact`` rejects and the transactions 39 history is not affected. As well, ``transact`` throws if it's called more than 40 once (successfully or not) on the same transaction object. 41 42 Batches 43 ------- 44 45 Sometimes it is useful to "batch" or "merge" transactions. 46 47 For example, something like "Bookmark All Tabs" may be implemented as one 48 NewFolder transaction followed by numerous NewBookmark transactions - all to be 49 undone or redone in a single undo or redo command. 50 51 Use ``PlacesTransactions.batch`` in such cases, passing an array of transactions 52 which will be executed in the given order and later be treated a a single entry 53 in the transactions history. ``PlacesTransactions.batch`` returns a promise 54 resolved when the batch has been executed. If a transaction depends on results 55 from a previous one, it can be replaced in the array with a function that takes 56 ``previousArguments`` as only argument, and returns a transaction. 57 58 The transactions-history structure 59 ---------------------------------- 60 61 The transactions-history is a two-dimensional stack of transactions: the 62 transactions are ordered in reverse to the order they were committed. 63 It's two-dimensional because PTM allows batching transactions together for the 64 purpose of undo or redo. 65 66 The undoPosition property is set to the index of the top entry. If there is no 67 entry at that index, there is nothing to undo. Entries prior to undoPosition, 68 if any, are redo entries, the first one being the top redo entry. 69 70 Sources 71 ------- 72 73 * :searchfox:`PlacesTransactions.sys.mjs <toolkit/components/places/PlacesTransactions.sys.mjs>`