aberdeen 1.18.2 → 1.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (152) hide show
  1. package/README.md +9 -0
  2. package/dist/src/aberdeen.d.ts +27 -0
  3. package/{dist-docs/assets/aberdeen/aberdeen.js → dist/src/aberdeen.dev.js} +776 -15
  4. package/dist/src/aberdeen.dev.js.map +12 -0
  5. package/dist/src/aberdeen.js +59 -13
  6. package/dist/src/aberdeen.js.map +3 -3
  7. package/dist/src/devtools.d.ts +26 -0
  8. package/html-to-aberdeen +9 -5
  9. package/package.json +17 -10
  10. package/skill/A.md +127 -0
  11. package/skill/Dispatcher.md +51 -0
  12. package/skill/OPAQUE.md +15 -0
  13. package/skill/PromiseProxy.md +25 -0
  14. package/skill/Route.md +50 -0
  15. package/skill/SKILL.md +272 -156
  16. package/skill/applyCanon.md +20 -0
  17. package/skill/applyPrediction.md +15 -0
  18. package/skill/back.md +11 -0
  19. package/skill/clean.md +43 -0
  20. package/skill/clone.md +15 -0
  21. package/skill/copy.md +40 -0
  22. package/skill/count.md +32 -0
  23. package/skill/cssVars.md +15 -0
  24. package/skill/darkMode.md +26 -0
  25. package/skill/derive.md +56 -0
  26. package/skill/developer-tools.md +23 -0
  27. package/skill/disableCreateDestroy.md +11 -0
  28. package/skill/dump.md +37 -0
  29. package/skill/freeze.md +20 -0
  30. package/skill/full-example-multi-page-app.md +146 -0
  31. package/skill/go.md +25 -0
  32. package/skill/grow.md +13 -0
  33. package/skill/html-to-aberdeen.md +9 -0
  34. package/skill/insertCss.md +79 -0
  35. package/skill/insertGlobalCss.md +68 -0
  36. package/skill/interceptLinks.md +19 -0
  37. package/skill/invertString.md +32 -0
  38. package/skill/isEmpty.md +38 -0
  39. package/skill/map.md +16 -0
  40. package/skill/matchCurrent.md +27 -0
  41. package/skill/merge.md +27 -0
  42. package/skill/mount.md +48 -0
  43. package/skill/multiMap.md +15 -0
  44. package/skill/onEach.md +14 -0
  45. package/skill/partition.md +15 -0
  46. package/skill/peek.md +36 -0
  47. package/skill/persistScroll.md +13 -0
  48. package/skill/proxy.md +11 -0
  49. package/skill/push.md +13 -0
  50. package/skill/ref.md +39 -0
  51. package/skill/runQueue.md +34 -0
  52. package/skill/setErrorHandler.md +48 -0
  53. package/skill/setLog.md +9 -0
  54. package/skill/setSpacingCssVars.md +31 -0
  55. package/skill/shrink.md +13 -0
  56. package/skill/unmountAll.md +9 -0
  57. package/skill/unproxy.md +43 -0
  58. package/skill/up.md +13 -0
  59. package/src/aberdeen.ts +194 -73
  60. package/src/devtools.ts +685 -0
  61. package/dist-docs/.nojekyll +0 -1
  62. package/dist-docs/Tutorial/index.html +0 -305
  63. package/dist-docs/aberdeen/A/index.html +0 -116
  64. package/dist-docs/aberdeen/CUSTOM_DUMP/index.html +0 -7
  65. package/dist-docs/aberdeen/NO_COPY/index.html +0 -2
  66. package/dist-docs/aberdeen/OPAQUE/index.html +0 -12
  67. package/dist-docs/aberdeen/PromiseProxy/index.html +0 -8
  68. package/dist-docs/aberdeen/clean/index.html +0 -11
  69. package/dist-docs/aberdeen/clone/index.html +0 -5
  70. package/dist-docs/aberdeen/copy/index.html +0 -22
  71. package/dist-docs/aberdeen/count/index.html +0 -7
  72. package/dist-docs/aberdeen/cssVars/index.html +0 -11
  73. package/dist-docs/aberdeen/darkMode/index.html +0 -9
  74. package/dist-docs/aberdeen/default/index.html +0 -342
  75. package/dist-docs/aberdeen/derive/index.html +0 -18
  76. package/dist-docs/aberdeen/disableCreateDestroy/index.html +0 -6
  77. package/dist-docs/aberdeen/dump/index.html +0 -11
  78. package/dist-docs/aberdeen/index.html +0 -54
  79. package/dist-docs/aberdeen/insertCss/index.html +0 -30
  80. package/dist-docs/aberdeen/insertGlobalCss/index.html +0 -19
  81. package/dist-docs/aberdeen/invertString/index.html +0 -13
  82. package/dist-docs/aberdeen/isEmpty/index.html +0 -12
  83. package/dist-docs/aberdeen/map/index.html +0 -26
  84. package/dist-docs/aberdeen/merge/index.html +0 -11
  85. package/dist-docs/aberdeen/mount/index.html +0 -17
  86. package/dist-docs/aberdeen/multiMap/index.html +0 -28
  87. package/dist-docs/aberdeen/onEach/index.html +0 -19
  88. package/dist-docs/aberdeen/partition/index.html +0 -36
  89. package/dist-docs/aberdeen/peek/index.html +0 -40
  90. package/dist-docs/aberdeen/proxy/index.html +0 -35
  91. package/dist-docs/aberdeen/ref/index.html +0 -13
  92. package/dist-docs/aberdeen/runQueue/index.html +0 -13
  93. package/dist-docs/aberdeen/setErrorHandler/index.html +0 -16
  94. package/dist-docs/aberdeen/setSpacingCssVars/index.html +0 -8
  95. package/dist-docs/aberdeen/unmountAll/index.html +0 -5
  96. package/dist-docs/aberdeen/unproxy/index.html +0 -11
  97. package/dist-docs/assets/aberdeen/aberdeen.d.ts +0 -963
  98. package/dist-docs/assets/aberdeen/aberdeen.js.map +0 -11
  99. package/dist-docs/assets/aberdeen/dispatcher.d.ts +0 -57
  100. package/dist-docs/assets/aberdeen/dispatcher.js +0 -66
  101. package/dist-docs/assets/aberdeen/dispatcher.js.map +0 -10
  102. package/dist-docs/assets/aberdeen/helpers/reverseSortedSet.d.ts +0 -96
  103. package/dist-docs/assets/aberdeen/prediction.d.ts +0 -33
  104. package/dist-docs/assets/aberdeen/prediction.js +0 -112
  105. package/dist-docs/assets/aberdeen/prediction.js.map +0 -10
  106. package/dist-docs/assets/aberdeen/route.d.ts +0 -137
  107. package/dist-docs/assets/aberdeen/route.js +0 -287
  108. package/dist-docs/assets/aberdeen/route.js.map +0 -10
  109. package/dist-docs/assets/aberdeen/transitions.d.ts +0 -18
  110. package/dist-docs/assets/aberdeen/transitions.js +0 -47
  111. package/dist-docs/assets/aberdeen/transitions.js.map +0 -10
  112. package/dist-docs/assets/custom.js +0 -573
  113. package/dist-docs/assets/favicon.png +0 -0
  114. package/dist-docs/assets/hierarchy.js +0 -1
  115. package/dist-docs/assets/highlight.css +0 -134
  116. package/dist-docs/assets/icons.js +0 -18
  117. package/dist-docs/assets/icons.svg +0 -1
  118. package/dist-docs/assets/main.js +0 -60
  119. package/dist-docs/assets/navigation.js +0 -1
  120. package/dist-docs/assets/search.js +0 -1
  121. package/dist-docs/assets/style.css +0 -1640
  122. package/dist-docs/dispatcher/Dispatcher/index.html +0 -21
  123. package/dist-docs/dispatcher/MATCH_FAILED/index.html +0 -2
  124. package/dist-docs/dispatcher/MATCH_REST/index.html +0 -2
  125. package/dist-docs/dispatcher/index.html +0 -4
  126. package/dist-docs/hierarchy.html +0 -1
  127. package/dist-docs/index.html +0 -61
  128. package/dist-docs/media/CHANGELOG.md +0 -266
  129. package/dist-docs/modules.html +0 -1
  130. package/dist-docs/prediction/applyCanon/index.html +0 -13
  131. package/dist-docs/prediction/applyPrediction/index.html +0 -8
  132. package/dist-docs/prediction/index.html +0 -9
  133. package/dist-docs/route/Route/index.html +0 -23
  134. package/dist-docs/route/back/index.html +0 -4
  135. package/dist-docs/route/current/index.html +0 -2
  136. package/dist-docs/route/go/index.html +0 -14
  137. package/dist-docs/route/index.html +0 -15
  138. package/dist-docs/route/interceptLinks/index.html +0 -8
  139. package/dist-docs/route/matchCurrent/index.html +0 -10
  140. package/dist-docs/route/persistScroll/index.html +0 -6
  141. package/dist-docs/route/push/index.html +0 -6
  142. package/dist-docs/route/setLog/index.html +0 -3
  143. package/dist-docs/route/up/index.html +0 -5
  144. package/dist-docs/sitemap.xml +0 -239
  145. package/dist-docs/transitions/grow/index.html +0 -6
  146. package/dist-docs/transitions/index.html +0 -5
  147. package/dist-docs/transitions/shrink/index.html +0 -6
  148. package/skill/aberdeen.md +0 -4586
  149. package/skill/dispatcher.md +0 -129
  150. package/skill/prediction.md +0 -73
  151. package/skill/route.md +0 -322
  152. package/skill/transitions.md +0 -59
@@ -1,10 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../src/dispatcher.ts"],
4
- "sourcesContent": [
5
- "/**\n * Symbol to return when a custom {@link Dispatcher.addRoute} matcher cannot match a segment.\n */\nexport const MATCH_FAILED: unique symbol = Symbol(\"MATCH_FAILED\");\n\n/**\n * Special {@link Dispatcher.addRoute} matcher that matches the rest of the segments as an array of strings.\n */\nexport const MATCH_REST: unique symbol = Symbol(\"MATCH_REST\");\n\ntype Matcher = string | ((segment: string) => any) | typeof MATCH_REST;\n\ntype ExtractParamType<M> = M extends string\n? never : (\n M extends ((segment: string) => infer R)\n ? Exclude<R, typeof MATCH_FAILED>\n : (M extends typeof MATCH_REST ? string[] : never)\n);\n\ntype ParamsFromMatchers<T extends Matcher[]> = T extends [infer M1, ...infer Rest]\n? (\n M1 extends Matcher\n ? (\n ExtractParamType<M1> extends never\n ? ParamsFromMatchers<Rest extends Matcher[] ? Rest : []>\n : [ExtractParamType<M1>, ...ParamsFromMatchers<Rest extends Matcher[] ? Rest : []>]\n ) : never\n) : [];\n\ninterface DispatcherRoute {\n matchers: Matcher[];\n handler: (...params: any[]) => void;\n}\n\n/**\n * Simple route matcher and dispatcher.\n * \n * Example usage:\n * \n * ```ts\n * import * as route from 'aberdeen/route';\n * import { Dispatcher, MATCH_REST } from 'aberdeen/dispatcher';\n * \n * const dispatcher = new Dispatcher();\n * \n * dispatcher.addRoute(\"user\", Number, \"stream\", String, (id, stream) => {\n * console.log(`User ${id}, stream ${stream}`);\n * });\n *\n * dispatcher.dispatch([\"user\", \"42\", \"stream\", \"music\"]);\n * // Logs: User 42, stream music\n * \n * dispatcher.addRoute(\"search\", MATCH_REST, (terms: string[]) => {\n * console.log(\"Search terms:\", terms);\n * });\n * \n * dispatcher.dispatch([\"search\", \"classical\", \"piano\"]);\n * // Logs: Search terms: [ 'classical', 'piano' ]\n * ```\n */\nexport class Dispatcher {\n private routes: Array<DispatcherRoute> = [];\n \n /**\n * Add a route with matchers and a handler function.\n * @param args An array of matchers followed by a handler function. Each matcher can be:\n * - A string: matches exactly that string.\n * - A function: takes a string segment and returns a value (of any type) if it matches, or {@link MATCH_FAILED} if it doesn't match. The return value (if not `MATCH_FAILED` and not `NaN`) is passed as a parameter to the handler function. The standard JavaScript functions `Number` and `String` can be used to match numeric and string segments respectively.\n * - The special {@link MATCH_REST} symbol: matches the rest of the segments as an array of strings. Only one `MATCH_REST` is allowed.\n * @template T - Array of matcher types.\n * @template H - Handler function type, inferred from the matchers.\n */\n addRoute<T extends Matcher[], H extends (...args: ParamsFromMatchers<T>) => void>(...args: [...T, H]): void {\n const matchers = args.slice(0, -1) as Matcher[];\n const handler = args[args.length - 1] as (...args: any) => any;\n\n if (typeof handler !== \"function\") {\n throw new Error(\"Last argument should be a handler function\");\n }\n \n const restCount = matchers.filter(m => m === MATCH_REST).length;\n if (restCount > 1) {\n throw new Error(\"Only one MATCH_REST is allowed\");\n }\n\n this.routes.push({ matchers, handler });\n }\n \n /**\n * Dispatches the given segments to the first route handler that matches.\n * @param segments Array of string segments to match against the added routes. When using this class with the Aberdeen `route` module, one would typically pass `route.current.p`.\n * @returns True if a matching route was found and handled, false otherwise.\n */\n dispatch(segments: string[]): boolean {\n for (const route of this.routes) {\n const args = matchRoute(route, segments);\n if (args) {\n route.handler(...args);\n return true;\n }\n }\n return false;\n }\n}\n\nfunction matchRoute(route: DispatcherRoute, segments: string[]): any[] | undefined {\n const args: any[] = [];\n let segmentIndex = 0;\n\n for (const matcher of route.matchers) {\n if (matcher === MATCH_REST) {\n const len = segments.length - (route.matchers.length - 1);\n if (len < 0) return;\n args.push(segments.slice(segmentIndex, segmentIndex + len));\n segmentIndex += len;\n continue;\n }\n\n if (segmentIndex >= segments.length) return;\n const segment = segments[segmentIndex];\n \n if (typeof matcher === \"string\") {\n if (segment !== matcher) return;\n } else if (typeof matcher === \"function\") {\n const result = matcher(segment);\n if (result === MATCH_FAILED || (typeof result === 'number' && isNaN(result))) return;\n args.push(result);\n }\n \n segmentIndex++;\n }\n if (segmentIndex === segments.length) return args; // success!\n}\n"
6
- ],
7
- "mappings": ";AAGO,IAAM,eAA8B,OAAO,cAAc;AAKzD,IAAM,aAA4B,OAAO,YAAY;AAAA;AAoDrD,MA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,YAAY,YAAY;AAAA,MACtC,MAAM,SAAS,QAAQ,OAAO;AAAA,MAC9B,IAAI,WAAW,gBAAiB,OAAO,WAAW,YAAY,MAAM,MAAM;AAAA,QAAI;AAAA,MAC9E,KAAK,KAAK,MAAM;AAAA,IACpB;AAAA,IAEA;AAAA,EACJ;AAAA,EACA,IAAI,iBAAiB,SAAS;AAAA,IAAQ,OAAO;AAAA;",
8
- "debugId": "EB41818E6FB7C09864756E2164756E21",
9
- "names": []
10
- }
@@ -1,96 +0,0 @@
1
- export type ReverseSortedSetPointer = symbol;
2
- type SkipItem<T> = {
3
- [idx: ReverseSortedSetPointer]: T;
4
- };
5
- /**
6
- * A set-like collection of objects that can do iteration sorted by a specified index property.
7
- * It also allows retrieving an object by its index property, and quickly getting the object
8
- * that comes immediately after a given object.
9
- *
10
- * It's implemented as a skiplist, maintaining all meta-data as part of the objects that it
11
- * is tracking, for performance.
12
- */
13
- export declare class ReverseSortedSet<T extends SkipItem<T>, KeyPropT extends keyof T> {
14
- private keyProp;
15
- private tail;
16
- private symbols;
17
- /**
18
- * Create an empty SortedSet.
19
- *
20
- * @param keyProp The name of the property that should be used as the index of this collection. Comparison
21
- * using `<` will be done on this property, so it should probably be a number or a string (or something that
22
- * has a useful toString-conversion).
23
- */
24
- constructor(keyProp: KeyPropT);
25
- /**
26
- * Add an object to the `SortedSet`.
27
- *
28
- * @param item The object to be added to the set. One or more properties with
29
- * `Symbol` keys will be added to it, for `SortedSet` internals.
30
- * @returns `true` if the item was added, or `false` if it was *not* added
31
- * because the item already was part of this set.
32
- *
33
- * Note that though an item object may only be part of a particular `SortedSet`
34
- * once, index properties may be duplicate and an item object may be part of
35
- * more than one `SortedSet`.
36
- *
37
- * **IMPORTANT:** After adding an object, do not modify its index property,
38
- * as this will lead to undefined (broken) behavior on the entire set.
39
- *
40
- * Time complexity: O(log n)
41
- */
42
- add(item: T): boolean;
43
- /**
44
- * @param item An object to test for inclusion in the set.
45
- * @returns true if this object item is already part of the set.
46
- */
47
- has(item: T): boolean;
48
- /**
49
- * Remove and return the last item.
50
- * @returns what was previously the last item in the sorted set, or `undefined` if the set was empty.
51
- */
52
- fetchLast(): T | undefined;
53
- /**
54
- * @returns whether the set is empty (`true`) or has at least one item (`false`).
55
- */
56
- isEmpty(): boolean;
57
- /**
58
- * Retrieve an item object based on its index property value.
59
- *
60
- * @param indexValue The index property value to search for.
61
- * @returns `undefined` if the index property value does not exist in the `SortedSet` or
62
- * otherwise the *first* item object that has this index value (meaning any further
63
- * instances can be iterated using `next()`).
64
- *
65
- * Time complexity: O(log n)
66
- */
67
- get(indexValue: T[KeyPropT]): T | undefined;
68
- /**
69
- * The iterator will go through the items in reverse index-order.
70
- */
71
- [Symbol.iterator](): IterableIterator<T>;
72
- /**
73
- * Given an item object, returns the one that comes right before in the set.
74
- * @param item The object to start from.
75
- * @returns The next object, or `undefined` if there is none.
76
- *
77
- * Time complexity: O(1)
78
- */
79
- prev(item: T): T | undefined;
80
- /**
81
- * Remove an item object from the set, deleting all meta-data keys that
82
- * were created on `add()`.
83
- * @param item The object to be removed.
84
- * @returns `true` on success or `false` when the item was not part of the set.
85
- *
86
- * Time complexity: O(log n)
87
- */
88
- remove(item: T): boolean;
89
- /**
90
- * Remove all items for the set.
91
- *
92
- * Time complexity: O(n)
93
- */
94
- clear(): void;
95
- }
96
- export {};
@@ -1,33 +0,0 @@
1
- import type { TargetType } from "./aberdeen.js";
2
- /**
3
- * Represents a set of changes that can be applied to proxied objects.
4
- * This is an opaque type - its internal structure is not part of the public API.
5
- * @private
6
- */
7
- export type Patch = Map<TargetType, Map<any, [any, any]>>;
8
- /**
9
- * Run the provided function, while treating all changes to Observables as predictions,
10
- * meaning they will be reverted when changes come back from the server (or some other
11
- * async source).
12
- * @param predictFunc The function to run. It will generally modify some Observables
13
- * to immediately reflect state (as closely as possible) that we expect the server
14
- * to communicate back to us later on.
15
- * @returns A `Patch` object. Don't modify it. This is only meant to be passed to `applyCanon`.
16
- */
17
- export declare function applyPrediction(predictFunc: () => void): Patch;
18
- /**
19
- * Temporarily revert all outstanding predictions, optionally run the provided function
20
- * (which will generally make authoritative changes to the data based on a server response),
21
- * and then attempt to reapply the predictions on top of the new canonical state, dropping
22
- * any predictions that can no longer be applied cleanly (the data has been modified) or
23
- * that were specified in `dropPredictions`.
24
- *
25
- * All of this is done such that redraws are only triggered if the overall effect is an
26
- * actual change to an `Observable`.
27
- * @param canonFunc The function to run without any predictions applied. This will typically
28
- * make authoritative changes to the data, based on a server response.
29
- * @param dropPredictions An optional list of predictions (as returned by `applyPrediction`)
30
- * to undo. Typically, when a server response for a certain request is being handled,
31
- * you'd want to drop the prediction that was done for that request.
32
- */
33
- export declare function applyCanon(canonFunc?: () => void, dropPredictions?: Array<Patch>): void;
@@ -1,112 +0,0 @@
1
- // src/prediction.ts
2
- import {
3
- defaultEmitHandler,
4
- withEmitHandler,
5
- EMPTY,
6
- MAP_SIZE_SYMBOL
7
- } from "./aberdeen.js";
8
- function recordPatch(func) {
9
- const recordingPatch = new Map;
10
- withEmitHandler((target, index, newData, oldData) => {
11
- addToPatch(recordingPatch, target, index, newData, oldData);
12
- }, func);
13
- return recordingPatch;
14
- }
15
- function addToPatch(patch, collection, index, newData, oldData) {
16
- let collectionMap = patch.get(collection);
17
- if (!collectionMap) {
18
- collectionMap = new Map;
19
- patch.set(collection, collectionMap);
20
- }
21
- const prev = collectionMap.get(index);
22
- const oldData0 = prev ? prev[1] : oldData;
23
- if (newData === oldData0)
24
- collectionMap.delete(index);
25
- else
26
- collectionMap.set(index, [newData, oldData0]);
27
- }
28
- function emitPatch(patch) {
29
- for (const [collection, collectionMap] of patch) {
30
- for (const [index, [newData, oldData]] of collectionMap) {
31
- defaultEmitHandler(collection, index, newData, oldData);
32
- }
33
- }
34
- }
35
- function mergePatch(target, source, reverse = false) {
36
- for (const [collection, collectionMap] of source) {
37
- for (const [index, [newData, oldData]] of collectionMap) {
38
- addToPatch(target, collection, index, reverse ? oldData : newData, reverse ? newData : oldData);
39
- }
40
- }
41
- }
42
- function silentlyApplyPatch(patch, force = false) {
43
- for (const [collection, collectionMap] of patch) {
44
- for (const [index, [newData, oldData]] of collectionMap) {
45
- if (index === MAP_SIZE_SYMBOL)
46
- continue;
47
- const actualData = collection instanceof Map ? collection.has(index) ? collection.get(index) : EMPTY : (index in collection) ? collection[index] : EMPTY;
48
- if (actualData !== oldData) {
49
- if (force)
50
- setTimeout(() => {
51
- throw new Error(`Applying invalid patch: data ${String(actualData)} is unequal to expected old data ${String(oldData)} for index ${String(index)}`);
52
- }, 0);
53
- else
54
- return false;
55
- }
56
- }
57
- }
58
- for (const [collection, collectionMap] of patch) {
59
- for (const [index, [newData, oldData]] of collectionMap) {
60
- if (index === MAP_SIZE_SYMBOL)
61
- continue;
62
- if (collection instanceof Map) {
63
- if (newData === EMPTY)
64
- collection.delete(index);
65
- else
66
- collection.set(index, newData);
67
- } else {
68
- if (newData === EMPTY)
69
- delete collection[index];
70
- else
71
- collection[index] = newData;
72
- }
73
- }
74
- }
75
- return true;
76
- }
77
- var appliedPredictions = [];
78
- function applyPrediction(predictFunc) {
79
- const patch = recordPatch(predictFunc);
80
- appliedPredictions.push(patch);
81
- emitPatch(patch);
82
- return patch;
83
- }
84
- function applyCanon(canonFunc, dropPredictions = []) {
85
- const resultPatch = new Map;
86
- for (const prediction of appliedPredictions)
87
- mergePatch(resultPatch, prediction, true);
88
- silentlyApplyPatch(resultPatch, true);
89
- for (const prediction of dropPredictions) {
90
- const pos = appliedPredictions.indexOf(prediction);
91
- if (pos >= 0)
92
- appliedPredictions.splice(pos, 1);
93
- }
94
- if (canonFunc)
95
- mergePatch(resultPatch, recordPatch(canonFunc));
96
- for (let idx = 0;idx < appliedPredictions.length; idx++) {
97
- if (silentlyApplyPatch(appliedPredictions[idx])) {
98
- mergePatch(resultPatch, appliedPredictions[idx]);
99
- } else {
100
- appliedPredictions.splice(idx, 1);
101
- idx--;
102
- }
103
- }
104
- emitPatch(resultPatch);
105
- }
106
- export {
107
- applyPrediction,
108
- applyCanon
109
- };
110
-
111
- //# debugId=0E6977F82274D4EC64756E2164756E21
112
- //# sourceMappingURL=prediction.js.map
@@ -1,10 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../src/prediction.ts"],
4
- "sourcesContent": [
5
- "import {\n\tdefaultEmitHandler,\n\twithEmitHandler,\n\tEMPTY,\n\tMAP_SIZE_SYMBOL,\n} from \"./aberdeen.js\";\nimport type { TargetType } from \"./aberdeen.js\";\n\n/**\n * Represents a set of changes that can be applied to proxied objects.\n * This is an opaque type - its internal structure is not part of the public API.\n * @private\n */\nexport type Patch = Map<TargetType, Map<any, [any, any]>>;\n\nfunction recordPatch(func: () => void): Patch {\n\tconst recordingPatch = new Map();\n\twithEmitHandler((target, index, newData, oldData) => {\n\t\taddToPatch(recordingPatch, target, index, newData, oldData);\n\t}, func);\n\treturn recordingPatch;\n}\n\nfunction addToPatch(\n\tpatch: Patch,\n\tcollection: TargetType,\n\tindex: any,\n\tnewData: any,\n\toldData: any,\n) {\n\tlet collectionMap = patch.get(collection);\n\tif (!collectionMap) {\n\t\tcollectionMap = new Map();\n\t\tpatch.set(collection, collectionMap);\n\t}\n\tconst prev = collectionMap.get(index);\n\tconst oldData0 = prev ? prev[1] : oldData;\n\tif (newData === oldData0) collectionMap.delete(index);\n\telse collectionMap.set(index, [newData, oldData0]);\n}\n\nfunction emitPatch(patch: Patch) {\n\tfor (const [collection, collectionMap] of patch) {\n\t\tfor (const [index, [newData, oldData]] of collectionMap) {\n\t\t\tdefaultEmitHandler(collection, index, newData, oldData);\n\t\t}\n\t}\n}\n\nfunction mergePatch(target: Patch, source: Patch, reverse = false) {\n\tfor (const [collection, collectionMap] of source) {\n\t\tfor (const [index, [newData, oldData]] of collectionMap) {\n\t\t\taddToPatch(\n\t\t\t\ttarget,\n\t\t\t\tcollection,\n\t\t\t\tindex,\n\t\t\t\treverse ? oldData : newData,\n\t\t\t\treverse ? newData : oldData,\n\t\t\t);\n\t\t}\n\t}\n}\n\nfunction silentlyApplyPatch(patch: Patch, force = false): boolean {\n\tfor (const [collection, collectionMap] of patch) {\n\t\tfor (const [index, [newData, oldData]] of collectionMap) {\n\t\t\t// MAP_SIZE_SYMBOL is synthetic - size is derived from add/delete operations\n\t\t\tif (index === MAP_SIZE_SYMBOL) continue;\n\t\t\tconst actualData =\n\t\t\t\tcollection instanceof Map\n\t\t\t\t\t? collection.has(index)\n\t\t\t\t\t\t? collection.get(index)\n\t\t\t\t\t\t: EMPTY\n\t\t\t\t\t: index in (collection as any)\n\t\t\t\t\t\t? (collection as any)[index]\n\t\t\t\t\t\t: EMPTY;\n\t\t\tif (actualData !== oldData) {\n\t\t\t\tif (force)\n\t\t\t\t\tsetTimeout(() => {\n\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t`Applying invalid patch: data ${String(actualData)} is unequal to expected old data ${String(oldData)} for index ${String(index)}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}, 0);\n\t\t\t\telse return false;\n\t\t\t}\n\t\t}\n\t}\n\tfor (const [collection, collectionMap] of patch) {\n\t\tfor (const [index, [newData, oldData]] of collectionMap) {\n\t\t\tif (index === MAP_SIZE_SYMBOL) continue;\n\t\t\tif (collection instanceof Map) {\n\t\t\t\tif (newData === EMPTY) collection.delete(index);\n\t\t\t\telse collection.set(index, newData);\n\t\t\t} else {\n\t\t\t\tif (newData === EMPTY) delete (collection as any)[index];\n\t\t\t\telse (collection as any)[index] = newData;\n\t\t\t}\n\t\t}\n\t}\n\treturn true;\n}\n\nconst appliedPredictions: Array<Patch> = [];\n\n/**\n * Run the provided function, while treating all changes to Observables as predictions,\n * meaning they will be reverted when changes come back from the server (or some other\n * async source).\n * @param predictFunc The function to run. It will generally modify some Observables\n * \tto immediately reflect state (as closely as possible) that we expect the server\n * to communicate back to us later on.\n * @returns A `Patch` object. Don't modify it. This is only meant to be passed to `applyCanon`.\n */\nexport function applyPrediction(predictFunc: () => void): Patch {\n\tconst patch = recordPatch(predictFunc);\n\tappliedPredictions.push(patch);\n\temitPatch(patch);\n\treturn patch;\n}\n\n/**\n * Temporarily revert all outstanding predictions, optionally run the provided function\n * (which will generally make authoritative changes to the data based on a server response),\n * and then attempt to reapply the predictions on top of the new canonical state, dropping\n * any predictions that can no longer be applied cleanly (the data has been modified) or\n * that were specified in `dropPredictions`.\n *\n * All of this is done such that redraws are only triggered if the overall effect is an\n * actual change to an `Observable`.\n * @param canonFunc The function to run without any predictions applied. This will typically\n * make authoritative changes to the data, based on a server response.\n * @param dropPredictions An optional list of predictions (as returned by `applyPrediction`)\n * to undo. Typically, when a server response for a certain request is being handled,\n * you'd want to drop the prediction that was done for that request.\n */\nexport function applyCanon(\n\tcanonFunc?: () => void,\n\tdropPredictions: Array<Patch> = [],\n) {\n\tconst resultPatch = new Map();\n\tfor (const prediction of appliedPredictions)\n\t\tmergePatch(resultPatch, prediction, true);\n\tsilentlyApplyPatch(resultPatch, true);\n\n\tfor (const prediction of dropPredictions) {\n\t\tconst pos = appliedPredictions.indexOf(prediction);\n\t\tif (pos >= 0) appliedPredictions.splice(pos, 1);\n\t}\n\tif (canonFunc) mergePatch(resultPatch, recordPatch(canonFunc));\n\n\tfor (let idx = 0; idx < appliedPredictions.length; idx++) {\n\t\tif (silentlyApplyPatch(appliedPredictions[idx])) {\n\t\t\tmergePatch(resultPatch, appliedPredictions[idx]);\n\t\t} else {\n\t\t\tappliedPredictions.splice(idx, 1);\n\t\t\tidx--;\n\t\t}\n\t}\n\n\temitPatch(resultPatch);\n}\n"
6
- ],
7
- "mappings": ";AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAeA,SAAS,WAAW,CAAC,MAAyB;AAAA,EAC7C,MAAM,iBAAiB,IAAI;AAAA,EAC3B,gBAAgB,CAAC,QAAQ,OAAO,SAAS,YAAY;AAAA,IACpD,WAAW,gBAAgB,QAAQ,OAAO,SAAS,OAAO;AAAA,KACxD,IAAI;AAAA,EACP,OAAO;AAAA;AAGR,SAAS,UAAU,CAClB,OACA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AAO,KAAK,CAAC;AAAA,MAChC;AAAA;AAAA,EAEF;AAAA,EAEA,UAAU,WAAW;AAAA;",
8
- "debugId": "0E6977F82274D4EC64756E2164756E21",
9
- "names": []
10
- }
@@ -1,137 +0,0 @@
1
- type NavType = "load" | "back" | "forward" | "go" | "push";
2
- /**
3
- * The class for the global `route` object.
4
- */
5
- export interface Route {
6
- /** The current path of the URL as a string. For instance `"/"` or `"/users/123/feed"`. Paths are normalized to always start with a `/` and never end with a `/` (unless it's the root path). */
7
- path: string;
8
- /** An convenience array containing path segments, mapping to `path`. For instance `[]` (for `"/"`) or `['users', '123', 'feed']` (for `"/users/123/feed"`). */
9
- p: string[];
10
- /** The hash fragment including the leading `#`, or an empty string. For instance `"#my_section"` or `""`. */
11
- hash: string;
12
- /** The query string interpreted as search parameters. So `"a=x&b=y"` becomes `{a: "x", b: "y"}`. */
13
- search: Record<string, string>;
14
- /** An object to be used for any additional data you want to associate with the current page. Data should be JSON-compatible. */
15
- state: Record<string, any>;
16
- /** The navigation depth of the current session. Starts at 1. Writing to this property has no effect. */
17
- depth: number;
18
- /** The navigation action that got us to this page. Writing to this property has no effect.
19
- - `"load"`: An initial page load.
20
- - `"back"` or `"forward"`: When we navigated backwards or forwards in the stack.
21
- - `"go"`: When we added a new page on top of the stack.
22
- - `"push"`: When we added a new page on top of the stack, merging with the current page.
23
- Mostly useful for page transition animations. Writing to this property has no effect.
24
- */
25
- nav: NavType;
26
- }
27
- /**
28
- * Configure logging on route changes.
29
- * @param value `true` to enable logging to console, `false` to disable logging, or a custom logging function. Defaults to `false`.
30
- */
31
- export declare function setLog(value: boolean | ((...args: any[]) => void)): void;
32
- type RouteTarget = string | (string | number)[] | Partial<Omit<Omit<Route, "p">, "search"> & {
33
- /** An convenience array containing path segments, mapping to `path`. For instance `[]` (for `"/"`) or `['users', 123, 'feed']` (for `"/users/123/feed"`). Values may be integers but will be converted to strings.*/
34
- p: (string | number)[];
35
- /** The query string interpreted as search parameters. So `"a=x&b=y"` becomes `{a: "x", b: "y", c: 42}`. Values may be integers but will be converted to strings. */
36
- search: Record<string, string | number>;
37
- }>;
38
- /**
39
- * Navigate to a new URL by pushing a new history entry.
40
- *
41
- * Note that this happens synchronously, immediately updating `route` and processing any reactive updates based on that.
42
- *
43
- * @param target A subset of the {@link Route} properties to navigate to. If neither `p` nor `path` is given, the current path is used. For other properties, an empty/default value is assumed if not given. For convenience:
44
- * - You may pass a string instead of an object, which is interpreted as the `path`.
45
- * - You may pass an array instead of an object, which is interpreted as the `p` array.
46
- * - If you pass `p`, it may contain numbers, which will be converted to strings.
47
- * - If you pass `search`, its values may be numbers, which will be converted to strings.
48
- *
49
- * Examples:
50
- * ```js
51
- * // Navigate to /users/123
52
- * route.go("/users/123");
53
- *
54
- * // Navigate to /users/123?tab=feed#top
55
- * route.go({p: ["users", 123], search: {tab: "feed"}, hash: "top"});
56
- * ```
57
- */
58
- export declare function go(target: RouteTarget, nav?: NavType): void;
59
- /**
60
- * Returns `true` if the current route matches `target`.
61
- *
62
- * Path must match exactly. Any search params specified in `target` must be present
63
- * in the current URL, but extra params in the current URL are allowed.
64
- *
65
- * Reactive: only reevaluates when the path changes to/from the target path, and
66
- * when target k/v search pairs are (un)set.
67
- *
68
- * Primary usage: 'active' status for menu items.
69
- *
70
- * @example
71
- * ```js
72
- * // This example assumes interceptLinks() has been called
73
- * A('a.my-button text=Users href=/users .is-active=', route.matchCurrent('/users'));
74
- *
75
- * // Alternatively a route object can be given
76
- * route.matchCurrent({path: '/users', search: {tab: 'profile'}});
77
- * ```
78
- */
79
- export declare function matchCurrent(target: RouteTarget): boolean;
80
- /**
81
- * Modify the current route by merging `target` into it (using {@link aberdeen.merge | A.merge}), pushing a new history entry.
82
- *
83
- * This is useful for things like opening modals or side panels, where you want a browser back action to return to the previous state.
84
- *
85
- * @param target Same as for {@link go}, but merged into the current route instead deleting all state.
86
- * @param nav The navigation type to use. Defaults to `undefined`, meaning the navigation type is unchanged from the current route,
87
- * preventing unwanted page transition animations.
88
- */
89
- export declare function push(target: RouteTarget, nav?: NavType): void;
90
- /**
91
- * Try to go back in history to the first entry that matches the given target. If none is found, the given state will replace the current page. This is useful for "cancel" or "close" actions that should return to the previous page if possible, but create a new page if not (for instance when arriving at the current page through a direct link).
92
- *
93
- * Consider using {@link up} to go up in the path hierarchy.
94
- *
95
- * @param target The target route to go back to. May be a subset of {@link Route}, or a string (for `path`), or an array of strings (for `p`).
96
- */
97
- export declare function back(target?: RouteTarget): void;
98
- /**
99
- * Navigate up in the path hierarchy, by going back to the first history entry
100
- * that has a shorter path than the current one. If there's none, we just shorten
101
- * the current path.
102
- *
103
- * Note that going back in browser history happens asynchronously, so `route` will not be updated immediately.
104
- */
105
- export declare function up(stripCount?: number): void;
106
- /**
107
- * The global {@link Route} object reflecting the current URL and browser history state. Changes you make to this affect the current browser history item (modifying the URL if needed).
108
- */
109
- export declare const current: Route;
110
- /**
111
- * Restore and store the vertical and horizontal scroll position for
112
- * the parent element to the page state.
113
- *
114
- * @param {string} name - A unique (within this page) name for this
115
- * scrollable element. Defaults to 'main'.
116
- *
117
- * The scroll position will be persisted in `route.aux.scroll.<name>`.
118
- */
119
- export declare function persistScroll(name?: string): void;
120
- /**
121
- * Intercept clicks and Enter key presses on links (`<a>` tags) and use Aberdeen routing
122
- * instead of browser navigation for local paths (paths without a protocol or host).
123
- *
124
- * This allows you to use regular HTML anchor tags for navigation without needing to
125
- * manually attach click handlers to each link.
126
- *
127
- * @example
128
- * ```js
129
- * // In your root component:
130
- * route.interceptLinks();
131
- *
132
- * // Now you can use regular anchor tags:
133
- * A('a text=About href=/corporate/about');
134
- * ```
135
- */
136
- export declare function interceptLinks(): void;
137
- export {};