aberdeen 0.1.2 → 0.2.1
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.
- package/.github/workflows/deploy.yml +43 -0
- package/.vscode/launch.json +23 -0
- package/README.md +11 -5
- package/{dist → dist-min}/aberdeen.d.ts +28 -153
- package/dist-min/aberdeen.js +2 -0
- package/dist-min/aberdeen.js.map +1 -0
- package/dist-min/prediction.d.ts +29 -0
- package/dist-min/prediction.js +2 -0
- package/dist-min/prediction.js.map +1 -0
- package/dist-min/route.d.ts +16 -0
- package/dist-min/route.js +2 -0
- package/dist-min/route.js.map +1 -0
- package/dist-min/transitions.d.ts +18 -0
- package/dist-min/transitions.js +2 -0
- package/dist-min/transitions.js.map +1 -0
- package/examples/input/index.html +8 -0
- package/examples/input/input.css +56 -0
- package/examples/input/input.js +66 -0
- package/examples/list/index.html +7 -0
- package/examples/list/list.js +47 -0
- package/examples/router/index.html +8 -0
- package/examples/router/page-home.js +12 -0
- package/examples/router/page-list.js +35 -0
- package/examples/router/page-settings.js +6 -0
- package/examples/router/router.js +76 -0
- package/examples/router/style.css +88 -0
- package/examples/tic-tac-toe/index.html +8 -0
- package/examples/tic-tac-toe/tic-tac-toe.css +50 -0
- package/examples/tic-tac-toe/tic-tac-toe.js +90 -0
- package/package.json +35 -27
- package/src/aberdeen.ts +2037 -0
- package/src/prediction.ts +117 -0
- package/src/route.ts +121 -0
- package/src/transitions.ts +73 -0
- package/tests/_fakedom.js +255 -0
- package/tests/_init.js +81 -0
- package/tests/array.js +109 -0
- package/tests/binding.js +106 -0
- package/tests/browsers.js +22 -0
- package/tests/clean.js +26 -0
- package/tests/count.js +105 -0
- package/tests/create.js +92 -0
- package/tests/destroy.js +270 -0
- package/tests/dom.js +219 -0
- package/tests/errors.js +114 -0
- package/tests/immediate.js +87 -0
- package/tests/map.js +76 -0
- package/tests/objmap.js +40 -0
- package/tests/onEach.js +392 -0
- package/tests/prediction.js +97 -0
- package/tests/props.js +49 -0
- package/tests/schedule.js +44 -0
- package/tests/scope.js +277 -0
- package/tests/sort.js +105 -0
- package/tests/store.js +254 -0
- package/tsconfig.json +67 -0
- package/dist/aberdeen.js +0 -1842
- package/dist/aberdeen.min.js +0 -1
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import {withEmitHandler} from 'aberdeen'
|
|
2
|
+
|
|
3
|
+
type ObsCollection = any
|
|
4
|
+
type Patch = Map<ObsCollection, Map<any, [any, any]>>;
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
function recordPatch(func: () => void): Patch {
|
|
8
|
+
const recordingPatch = new Map()
|
|
9
|
+
withEmitHandler(function(index, newData, oldData) {
|
|
10
|
+
addToPatch(recordingPatch, this, index, newData, oldData)
|
|
11
|
+
}, func)
|
|
12
|
+
return recordingPatch
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function addToPatch(patch: Patch, collection: ObsCollection, index: any, newData: any, oldData: any) {
|
|
16
|
+
let collectionMap = patch.get(collection)
|
|
17
|
+
if (!collectionMap) {
|
|
18
|
+
collectionMap = new Map()
|
|
19
|
+
patch.set(collection, collectionMap)
|
|
20
|
+
}
|
|
21
|
+
let prev = collectionMap.get(index)
|
|
22
|
+
if (prev) oldData = prev[1]
|
|
23
|
+
if (newData === oldData) collectionMap.delete(index)
|
|
24
|
+
else collectionMap.set(index, [newData, oldData])
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function emitPatch(patch: Patch) {
|
|
28
|
+
for(let [collection, collectionMap] of patch) {
|
|
29
|
+
for(let [index, [newData, oldData]] of collectionMap) {
|
|
30
|
+
collection.emitChange(index, newData, oldData)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function mergePatch(target: Patch, source: Patch, reverse: boolean = false) {
|
|
36
|
+
for(let [collection, collectionMap] of source) {
|
|
37
|
+
for(let [index, [newData, oldData]] of collectionMap) {
|
|
38
|
+
addToPatch(target, collection, index, reverse ? oldData : newData, reverse ? newData : oldData)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function silentlyApplyPatch(patch: Patch, force: boolean = false): boolean {
|
|
44
|
+
for(let [collection, collectionMap] of patch) {
|
|
45
|
+
for(let [index, [newData, oldData]] of collectionMap) {
|
|
46
|
+
let actualData = collection.rawGet(index)
|
|
47
|
+
if (actualData !== oldData) {
|
|
48
|
+
if (force) setTimeout(() => { throw new Error(`Applying invalid patch: data ${actualData} is unequal to expected old data ${oldData} for index ${index}`)}, 0)
|
|
49
|
+
else return false
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
for(let [collection, collectionMap] of patch) {
|
|
54
|
+
for(let [index, [newData, oldData]] of collectionMap) {
|
|
55
|
+
collection.rawSet(index, newData)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return true
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
const appliedPredictions: Array<Patch> = []
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Run the provided function, while treating all changes to Observables as predictions,
|
|
66
|
+
* meaning they will be reverted when changes come back from the server (or some other
|
|
67
|
+
* async source).
|
|
68
|
+
* @param predictFunc The function to run. It will generally modify some Observables
|
|
69
|
+
* to immediately reflect state (as closely as possible) that we expect the server
|
|
70
|
+
* to communicate back to us later on.
|
|
71
|
+
* @returns A `Patch` object. Don't modify it. This is only meant to be passed to `applyCanon`.
|
|
72
|
+
*/
|
|
73
|
+
export function applyPrediction(predictFunc: () => void): Patch {
|
|
74
|
+
let patch = recordPatch(predictFunc)
|
|
75
|
+
appliedPredictions.push(patch)
|
|
76
|
+
emitPatch(patch)
|
|
77
|
+
return patch
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Temporarily revert all outstanding predictions, optionally run the provided function
|
|
82
|
+
* (which will generally make authoritative changes to the data based on a server response),
|
|
83
|
+
* and then attempt to reapply the predictions on top of the new canonical state, dropping
|
|
84
|
+
* any predictions that can no longer be applied cleanly (the data has been modified) or
|
|
85
|
+
* that were specified in `dropPredictions`.
|
|
86
|
+
*
|
|
87
|
+
* All of this is done such that redraws are only triggered if the overall effect is an
|
|
88
|
+
* actual change to an `Observable`.
|
|
89
|
+
* @param canonFunc The function to run without any predictions applied. This will typically
|
|
90
|
+
* make authoritative changes to the data, based on a server response.
|
|
91
|
+
* @param dropPredictions An optional list of predictions (as returned by `applyPrediction`)
|
|
92
|
+
* to undo. Typically, when a server response for a certain request is being handled,
|
|
93
|
+
* you'd want to drop the prediction that was done for that request.
|
|
94
|
+
*/
|
|
95
|
+
export function applyCanon(canonFunc?: (() => void), dropPredictions: Array<Patch> = []) {
|
|
96
|
+
|
|
97
|
+
let resultPatch = new Map()
|
|
98
|
+
for(let prediction of appliedPredictions) mergePatch(resultPatch, prediction, true)
|
|
99
|
+
silentlyApplyPatch(resultPatch, true)
|
|
100
|
+
|
|
101
|
+
for(let prediction of dropPredictions) {
|
|
102
|
+
let pos = appliedPredictions.indexOf(prediction)
|
|
103
|
+
if (pos >= 0) appliedPredictions.splice(pos, 1)
|
|
104
|
+
}
|
|
105
|
+
if (canonFunc) mergePatch(resultPatch, recordPatch(canonFunc))
|
|
106
|
+
|
|
107
|
+
for(let idx=0; idx<appliedPredictions.length; idx++) {
|
|
108
|
+
if (silentlyApplyPatch(appliedPredictions[idx])) {
|
|
109
|
+
mergePatch(resultPatch, appliedPredictions[idx])
|
|
110
|
+
} else {
|
|
111
|
+
appliedPredictions.splice(idx, 1)
|
|
112
|
+
idx--
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
emitPatch(resultPatch)
|
|
117
|
+
}
|
package/src/route.ts
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import {Store, observe, immediateObserve, inhibitEffects} from 'aberdeen'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A `Store` object that holds the following keys:
|
|
5
|
+
* - `path`: The current path of the URL split into components. For instance `/` or `/users/123/feed`. Updates will be reflected in the URL and will *push* a new entry to the browser history.
|
|
6
|
+
* - `p`: Array containing the path segments. For instance `[]` or `['users', 123, 'feed']`. Updates will be reflected in the URL and will *push* a new entry to the browser history. Also, the values of `p` and `path` will be synced.
|
|
7
|
+
* - `search`: An observable object containing search parameters (a split up query string). For instance `{order: "date", title: "something"}` or just `{}`. Updates will be reflected in the URL, modifying the current history state.
|
|
8
|
+
* - `hash`: The document hash part of the URL. For instance `"#section-title"`. It can also be an empty string. Updates will be reflected in the URL, modifying the current history state.
|
|
9
|
+
* - `state`: The browser history *state* object for the current page. Creating or removing top-level keys will cause *pushing* a new entry to the browser history.
|
|
10
|
+
*
|
|
11
|
+
* The following key may also be written to `route` but will be immediately and silently removed:
|
|
12
|
+
* - `mode`: As described above, this library takes a best guess about whether pushing an item to the browser history makes sense or not. When `mode` is...
|
|
13
|
+
* - `"push"`: Force creation of a new browser history entry.
|
|
14
|
+
* - `"replace"`: Update the current history entry, even when updates to other keys would normally cause a *push*.
|
|
15
|
+
* - `"back"`: Unwind the history (like repeatedly pressing the *back* button) until we find a page that matches the given `path`, `search` and top-level `state` keys, and then *replace* that state by the full given state.
|
|
16
|
+
*/
|
|
17
|
+
export const route = new Store()
|
|
18
|
+
|
|
19
|
+
// Contains url (path+search) and state for all history entries for this session, with the current
|
|
20
|
+
// entry on top. It is used for `mode: "back"`, to know how many entries to go back.
|
|
21
|
+
let stack: Array<{url: string, state: object}> = []
|
|
22
|
+
|
|
23
|
+
// Keep track of the initial history length, so we'll always know how long our `stack` should be.
|
|
24
|
+
const initialHistoryLength = history.length;
|
|
25
|
+
|
|
26
|
+
// Keep a copy of the last known history state, so we can tell if the user changed one of its
|
|
27
|
+
// top-level keys, so we can decide between push/replace when `mode` is not set.
|
|
28
|
+
let prevHistoryState: any
|
|
29
|
+
|
|
30
|
+
// Reflect changes to the browser URL (back/forward navigation) in the `route` and `stack`.
|
|
31
|
+
function handleLocationUpdate(event?: PopStateEvent) {
|
|
32
|
+
const search: any= {}
|
|
33
|
+
for(let [k, v] of new URLSearchParams(location.search)) {
|
|
34
|
+
search[k] = v
|
|
35
|
+
}
|
|
36
|
+
prevHistoryState = event ? (event.state || {}) : {}
|
|
37
|
+
stack.length = Math.max(1, history.length - initialHistoryLength + 1)
|
|
38
|
+
stack[stack.length-1] = {url: location.pathname + location.search, state: prevHistoryState}
|
|
39
|
+
route.set({
|
|
40
|
+
path: location.pathname,
|
|
41
|
+
p: location.pathname.slice(1).split('/'),
|
|
42
|
+
search: search,
|
|
43
|
+
hash: location.hash,
|
|
44
|
+
state: prevHistoryState,
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
handleLocationUpdate()
|
|
48
|
+
window.addEventListener("popstate", handleLocationUpdate);
|
|
49
|
+
|
|
50
|
+
// These immediate-mode observers will rewrite the data in `route` to its canonical form.
|
|
51
|
+
// We want to to this immediately, so that user-code running immediately after a user-code
|
|
52
|
+
// initiated `set` will see the canonical form (instead of doing a rerender shortly after,
|
|
53
|
+
// or crashing due to non-canonical data).
|
|
54
|
+
immediateObserve(() => {
|
|
55
|
+
let path = ''+route.get('path')
|
|
56
|
+
if (!path.startsWith('/')) path = '/'+path
|
|
57
|
+
route.set('path', path)
|
|
58
|
+
route.set('p', path.slice(1).split('/'))
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
immediateObserve(() => {
|
|
62
|
+
const p = route.get('p')
|
|
63
|
+
if (!(p instanceof Array)) {
|
|
64
|
+
console.error(`aberdeen route: 'p' must be a non-empty array, not ${JSON.stringify(p)}`)
|
|
65
|
+
route.set('p', ['']) // This will cause a recursive call this observer.
|
|
66
|
+
} else if (p.length == 0) {
|
|
67
|
+
route.set('p', ['']) // This will cause a recursive call this observer.
|
|
68
|
+
} else {
|
|
69
|
+
route.set('path', '/' + p.join('/'))
|
|
70
|
+
}
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
immediateObserve(() => {
|
|
74
|
+
if (route.getType('search') !== 'object') route.set('search', {})
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
immediateObserve(() => {
|
|
78
|
+
if (route.getType('state') !== 'object') route.set('state', {})
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
immediateObserve(() => {
|
|
82
|
+
let hash = ''+(route.get('hash') || '')
|
|
83
|
+
if (hash && !hash.startsWith('#')) hash = '#'+hash
|
|
84
|
+
route.set('hash', hash)
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
// This deferred-mode observer will update the URL and history based on `route` changes.
|
|
88
|
+
observe(() => {
|
|
89
|
+
// Get and delete mode without triggering anything.
|
|
90
|
+
const mode = route.get('mode')
|
|
91
|
+
if (mode) inhibitEffects(() => route.delete('mode'))
|
|
92
|
+
const state = route.get('state')
|
|
93
|
+
|
|
94
|
+
// Construct the URL.
|
|
95
|
+
const path = route.get('path')
|
|
96
|
+
const search = new URLSearchParams(route.get('search')).toString()
|
|
97
|
+
const url = (search ? path+'?'+search : path) + route.get('hash')
|
|
98
|
+
|
|
99
|
+
// Change browser state, according to `mode`.
|
|
100
|
+
if (mode === 'back') {
|
|
101
|
+
let goDelta = 0
|
|
102
|
+
while(stack.length > 1) {
|
|
103
|
+
const item = stack[stack.length-1]
|
|
104
|
+
if (item.url === url && JSON.stringify(Object.keys(state||{})) === JSON.stringify(Object.keys(item.state||{}))) break // Found it!
|
|
105
|
+
goDelta--
|
|
106
|
+
stack.pop()
|
|
107
|
+
}
|
|
108
|
+
if (goDelta) history.go(goDelta)
|
|
109
|
+
// We'll replace the state async, to give the history.go the time to take affect first.
|
|
110
|
+
setTimeout(() => history.replaceState(state, '', url), 0)
|
|
111
|
+
stack[stack.length-1] = {url,state}
|
|
112
|
+
} else if (mode === 'push' || (!mode && (location.pathname !== path || JSON.stringify(Object.keys(state||{})) !== JSON.stringify(Object.keys(prevHistoryState||{}))))) {
|
|
113
|
+
history.pushState(state, '', url)
|
|
114
|
+
stack.push({url,state})
|
|
115
|
+
} else {
|
|
116
|
+
history.replaceState(state, '', url)
|
|
117
|
+
stack[stack.length-1] = {url,state}
|
|
118
|
+
}
|
|
119
|
+
prevHistoryState = state
|
|
120
|
+
})
|
|
121
|
+
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import {scheduleDomReader, scheduleDomWriter} from 'aberdeen'
|
|
2
|
+
|
|
3
|
+
const FADE_TIME = 400
|
|
4
|
+
const GROW_SHRINK_TRANSITION = `margin ${FADE_TIME}ms ease-out, transform ${FADE_TIME}ms ease-out`
|
|
5
|
+
|
|
6
|
+
function getGrowShrinkProps(el: HTMLElement) {
|
|
7
|
+
const parentStyle: any = el.parentElement ? getComputedStyle(el.parentElement) : {}
|
|
8
|
+
const isHorizontal = parentStyle.display === 'flex' && (parentStyle.flexDirection||'').startsWith('row')
|
|
9
|
+
return isHorizontal ?
|
|
10
|
+
{marginLeft: `-${el.offsetWidth/2}px`, marginRight: `-${el.offsetWidth/2}px`, transform: "scaleX(0)"} :
|
|
11
|
+
{marginBottom: `-${el.offsetHeight/2}px`, marginTop: `-${el.offsetHeight/2}px`, transform: "scaleY(0)"}
|
|
12
|
+
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Do a grow transition for the given element. This is meant to be used as a
|
|
16
|
+
* handler for the `create` property.
|
|
17
|
+
*
|
|
18
|
+
* @param el The element to transition.
|
|
19
|
+
*
|
|
20
|
+
* The transition doesn't look great for table elements, and may have problems
|
|
21
|
+
* for other specific cases as well.
|
|
22
|
+
*/
|
|
23
|
+
export function grow(el: HTMLElement): void {
|
|
24
|
+
// This timeout is to await all other elements having been added to the Dom
|
|
25
|
+
scheduleDomReader(() => {
|
|
26
|
+
// Make the element size 0 using transforms and negative margins.
|
|
27
|
+
// This causes a browser layout, as we're querying el.offset<>.
|
|
28
|
+
let props = getGrowShrinkProps(el)
|
|
29
|
+
|
|
30
|
+
// The timeout is in order to batch all reads and then all writes when there
|
|
31
|
+
// are multiple simultaneous grow transitions.
|
|
32
|
+
scheduleDomWriter(() => {
|
|
33
|
+
Object.assign(el.style, props)
|
|
34
|
+
|
|
35
|
+
// This timeout is to combine multiple transitions into a single browser layout
|
|
36
|
+
scheduleDomReader(() => {
|
|
37
|
+
// Make sure the layouting has been performed, to cause transitions to trigger
|
|
38
|
+
el.offsetHeight
|
|
39
|
+
scheduleDomWriter(() => {
|
|
40
|
+
// Do the transitions
|
|
41
|
+
el.style.transition = GROW_SHRINK_TRANSITION
|
|
42
|
+
for(let prop in props) el.style[prop as any] = ""
|
|
43
|
+
setTimeout(() => {
|
|
44
|
+
// Reset the element to a clean state
|
|
45
|
+
el.style.transition = ""
|
|
46
|
+
}, FADE_TIME)
|
|
47
|
+
})
|
|
48
|
+
})
|
|
49
|
+
})
|
|
50
|
+
})
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Do a shrink transition for the given element, and remove it from the DOM
|
|
54
|
+
* afterwards. This is meant to be used as a handler for the `destroy` property.
|
|
55
|
+
*
|
|
56
|
+
* @param el The element to transition and remove.
|
|
57
|
+
*
|
|
58
|
+
* The transition doesn't look great for table elements, and may have problems
|
|
59
|
+
* for other specific cases as well.
|
|
60
|
+
*/
|
|
61
|
+
export function shrink(el: HTMLElement): void {
|
|
62
|
+
scheduleDomReader(() => {
|
|
63
|
+
const props = getGrowShrinkProps(el)
|
|
64
|
+
// The timeout is in order to batch all reads and then all writes when there
|
|
65
|
+
// are multiple simultaneous shrink transitions.
|
|
66
|
+
scheduleDomWriter(() => {
|
|
67
|
+
el.style.transition = GROW_SHRINK_TRANSITION
|
|
68
|
+
Object.assign(el.style, props)
|
|
69
|
+
|
|
70
|
+
setTimeout(() => el.remove(), FADE_TIME)
|
|
71
|
+
})
|
|
72
|
+
})
|
|
73
|
+
}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
let insertedCss = '';
|
|
2
|
+
|
|
3
|
+
global.document = {
|
|
4
|
+
createElement: tag => new Element(tag),
|
|
5
|
+
createTextNode: text => new TextNode(text),
|
|
6
|
+
head: {
|
|
7
|
+
appendChild: function(el) {
|
|
8
|
+
if (el.tag!=='style') {
|
|
9
|
+
throw new Error("only <style> inserts in head can be emulated");
|
|
10
|
+
}
|
|
11
|
+
insertedCss += el.innerText;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
global.window = {};
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
let newCount = 0, changeCount = 0;
|
|
19
|
+
global.resetCounts = function() { newCount = changeCount = 0; };
|
|
20
|
+
global.getCounts = function() { return {new: newCount, change: changeCount}; };
|
|
21
|
+
|
|
22
|
+
let timeouts = [];
|
|
23
|
+
let currentTime = 0;
|
|
24
|
+
|
|
25
|
+
global.setTimeout = function(func,time) {
|
|
26
|
+
timeouts.push({func, time: time+currentTime});
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
global.passTime = function(ms) {
|
|
30
|
+
let targetTime = ms==null ? undefined : currentTime + ms;
|
|
31
|
+
while(timeouts.length) {
|
|
32
|
+
// Find the timeout that should occur first
|
|
33
|
+
let smallestIdx = 0;
|
|
34
|
+
for(let idx=1; idx<timeouts.length; idx++) {
|
|
35
|
+
if (timeouts[idx].time < timeouts[smallestIdx].time) smallestIdx = idx;
|
|
36
|
+
}
|
|
37
|
+
let timeout = timeouts[smallestIdx];
|
|
38
|
+
// If this timeout is not due yet, we're done
|
|
39
|
+
if (targetTime!=null && timeout.time > targetTime) break;
|
|
40
|
+
// Timeout is due! Remove it from the list, update the currentTime, and fire the callback!
|
|
41
|
+
timeouts.splice(smallestIdx, 1);
|
|
42
|
+
currentTime = timeout.time;
|
|
43
|
+
timeout.func();
|
|
44
|
+
}
|
|
45
|
+
currentTime = targetTime==null ? 0 : targetTime
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class Node {
|
|
50
|
+
get nextSibling() {
|
|
51
|
+
return this.getSibling(+1);
|
|
52
|
+
}
|
|
53
|
+
get previousSibling() {
|
|
54
|
+
return this.getSibling(-1);
|
|
55
|
+
}
|
|
56
|
+
getSibling(delta) {
|
|
57
|
+
if (!this.parentNode) return;
|
|
58
|
+
let siblings = this.parentNode.childNodes;
|
|
59
|
+
let idx = siblings.indexOf(this);
|
|
60
|
+
if (idx < 0) throw new Error("not part of siblings!?");
|
|
61
|
+
return siblings[idx+delta];
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
class Element extends Node {
|
|
66
|
+
constructor(tag) {
|
|
67
|
+
super();
|
|
68
|
+
this.tag = tag;
|
|
69
|
+
this.childNodes = [];
|
|
70
|
+
this._style = {};
|
|
71
|
+
this.attrs = {};
|
|
72
|
+
this.events = {};
|
|
73
|
+
newCount++;
|
|
74
|
+
}
|
|
75
|
+
appendChild(node) {
|
|
76
|
+
this.insertBefore(node, null);
|
|
77
|
+
}
|
|
78
|
+
insertBefore(node, ref) {
|
|
79
|
+
if (node.parentNode) node.parentNode.removeChild(node);
|
|
80
|
+
else changeCount++;
|
|
81
|
+
node.parentNode = this;
|
|
82
|
+
if (ref) {
|
|
83
|
+
let idx = this.childNodes.indexOf(ref);
|
|
84
|
+
if (idx<0) throw new Error("non-existing ref node");
|
|
85
|
+
this.childNodes.splice(idx, 0, node);
|
|
86
|
+
} else {
|
|
87
|
+
this.childNodes.push(node);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
removeChild(node) {
|
|
91
|
+
let idx = this.childNodes.indexOf(node);
|
|
92
|
+
if (idx<0) throw new Error("no such child");
|
|
93
|
+
this.childNodes.splice(idx, 1);
|
|
94
|
+
node.parentNode = null;
|
|
95
|
+
changeCount++;
|
|
96
|
+
}
|
|
97
|
+
remove() {
|
|
98
|
+
this.parentNode.removeChild(this);
|
|
99
|
+
}
|
|
100
|
+
replaceChild(newNode, oldNode) {
|
|
101
|
+
this.insertBefore(newNode, oldNode);
|
|
102
|
+
this.removeChild(oldNode);
|
|
103
|
+
}
|
|
104
|
+
setAttribute(k, v) {
|
|
105
|
+
this.attrs[k] = ''+v;
|
|
106
|
+
changeCount++;
|
|
107
|
+
}
|
|
108
|
+
getAttribute(k) {
|
|
109
|
+
return this.attrs[k]
|
|
110
|
+
}
|
|
111
|
+
removeAttribute(k) {
|
|
112
|
+
delete this.attrs[k];
|
|
113
|
+
changeCount++;
|
|
114
|
+
}
|
|
115
|
+
get classList() {
|
|
116
|
+
return {
|
|
117
|
+
add: name => {
|
|
118
|
+
let set = this._getClassSet();
|
|
119
|
+
set.add(name);
|
|
120
|
+
this._setClassSet(set);
|
|
121
|
+
changeCount++;
|
|
122
|
+
},
|
|
123
|
+
remove: name => {
|
|
124
|
+
let set = this._getClassSet();
|
|
125
|
+
set.delete(name);
|
|
126
|
+
this._setClassSet(set);
|
|
127
|
+
changeCount++;
|
|
128
|
+
},
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
get parentElement() {
|
|
132
|
+
if (this.parentNode instanceof Element) return this.parentNode;
|
|
133
|
+
}
|
|
134
|
+
_getClassSet() {
|
|
135
|
+
return new Set(this.attrs.class ? this.attrs.class.split(' ') : []);
|
|
136
|
+
}
|
|
137
|
+
_setClassSet(map) {
|
|
138
|
+
this.attrs.class = Array.from(map).sort().join(' ');
|
|
139
|
+
if (!this.attrs.class) delete this.attrs.class;
|
|
140
|
+
}
|
|
141
|
+
get firstChild() {
|
|
142
|
+
return this.childNodes[0];
|
|
143
|
+
}
|
|
144
|
+
get lastChild() {
|
|
145
|
+
return this.childNodes[this.childNodes.length-1];
|
|
146
|
+
}
|
|
147
|
+
set style(val) {
|
|
148
|
+
if (val !== '') throw new Error("non-empty style string cannot be emulated");
|
|
149
|
+
this._style = {};
|
|
150
|
+
changeCount++;
|
|
151
|
+
}
|
|
152
|
+
set textContent(text) {
|
|
153
|
+
this.childNodes = [new TextNode(text)];
|
|
154
|
+
}
|
|
155
|
+
set innerHTML(html) {
|
|
156
|
+
this.childNodes = [];
|
|
157
|
+
if (html) {
|
|
158
|
+
let n = new Element('fake-emulated-html');
|
|
159
|
+
n.textContent = html;
|
|
160
|
+
this.appendChild(n);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
get style() {
|
|
164
|
+
for(let k in this._style) {
|
|
165
|
+
if (this._style[k] === '') delete this._style[k];
|
|
166
|
+
}
|
|
167
|
+
return this._style;
|
|
168
|
+
}
|
|
169
|
+
set className(v) {
|
|
170
|
+
this.attrs.class = v;
|
|
171
|
+
changeCount++;
|
|
172
|
+
}
|
|
173
|
+
get offsetHeight() {
|
|
174
|
+
return 20;
|
|
175
|
+
}
|
|
176
|
+
get offsetWidth() {
|
|
177
|
+
return 200;
|
|
178
|
+
}
|
|
179
|
+
toString() {
|
|
180
|
+
let props = Object.assign({}, this);
|
|
181
|
+
for(let k in this.attrs) props['@'+k] = this.attrs[k];
|
|
182
|
+
for(let k in this.style) props[':'+k] = this._style[k];
|
|
183
|
+
delete props.tag;
|
|
184
|
+
delete props.attrs;
|
|
185
|
+
delete props._style;
|
|
186
|
+
delete props.events;
|
|
187
|
+
delete props.childNodes;
|
|
188
|
+
delete props.parentNode;
|
|
189
|
+
|
|
190
|
+
let arr = [];
|
|
191
|
+
for(let k in props) arr.push(k+'='+JSON.stringify(props[k]));
|
|
192
|
+
arr.sort();
|
|
193
|
+
for(let child of this.childNodes) arr.push(child.toString());
|
|
194
|
+
|
|
195
|
+
return this.tag + `{${arr.join(' ')}}`;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
addEventListener(name, func) {
|
|
199
|
+
this.events[name] = this.events[name] || new Set();
|
|
200
|
+
this.events[name].add(func);
|
|
201
|
+
changeCount++;
|
|
202
|
+
}
|
|
203
|
+
removeEventListener(name, func) {
|
|
204
|
+
if (this.events[name]) {
|
|
205
|
+
if (this.events[name].delete(func)) {
|
|
206
|
+
changeCount++
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
event(info) {
|
|
211
|
+
if (typeof info === 'string') info = {type: info};
|
|
212
|
+
let type = info.type;
|
|
213
|
+
info.target = this;
|
|
214
|
+
info.preventDefault = function(){};
|
|
215
|
+
info.stopPropagation = function(){ info.stopped = true; };
|
|
216
|
+
let node = this;
|
|
217
|
+
while(node && !info.stopped) {
|
|
218
|
+
let funcs = node.events[type];
|
|
219
|
+
if (funcs) {
|
|
220
|
+
for(let func of funcs) {
|
|
221
|
+
func.call(node, info);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
node = node.parentNode;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
getElementById(id) {
|
|
228
|
+
if (this.attrs.id === id) return this;
|
|
229
|
+
for(let child of this.childNodes) {
|
|
230
|
+
let el = child.getElementById(id);
|
|
231
|
+
if (el) return el;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
class TextNode extends Node {
|
|
237
|
+
constructor(textContent) {
|
|
238
|
+
super();
|
|
239
|
+
this.textContent = '' + textContent;
|
|
240
|
+
newCount++;
|
|
241
|
+
}
|
|
242
|
+
toString() {
|
|
243
|
+
return JSON.stringify(this.textContent);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function getComputedStyle(el) {
|
|
248
|
+
return el._style;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
global.Node = Node;
|
|
253
|
+
global.TextNode = TextNode;
|
|
254
|
+
global.Element = Element;
|
|
255
|
+
global.getComputedStyle = getComputedStyle;
|
package/tests/_init.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import * as _ from './_fakedom.js';
|
|
2
|
+
import mocha from 'mocha'
|
|
3
|
+
import {deepEqual as equal} from 'fast-equals'
|
|
4
|
+
|
|
5
|
+
import * as aberdeen from '../dist-min/aberdeen.js'
|
|
6
|
+
import * as transitions from '../dist-min/transitions.js'
|
|
7
|
+
import * as prediction from '../dist-min/prediction.js'
|
|
8
|
+
Object.assign(global, aberdeen, transitions, prediction)
|
|
9
|
+
|
|
10
|
+
let currentMountSeq = new Store(0)
|
|
11
|
+
mocha.beforeEach(() => {
|
|
12
|
+
document.body = document.createElement('body')
|
|
13
|
+
resetCounts()
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
mocha.afterEach(() => {
|
|
17
|
+
unmount()
|
|
18
|
+
passTime(2001) // wait for deletion transitions
|
|
19
|
+
assertBody(``)
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
function toDisplay(value) {
|
|
23
|
+
if (value instanceof Map) {
|
|
24
|
+
let results = []
|
|
25
|
+
value.forEach((value,key) => results.push(JSON.stringify(key)+": "+JSON.stringify(value)))
|
|
26
|
+
return "map{" + results.join(", ") + "}"
|
|
27
|
+
} else {
|
|
28
|
+
return JSON.stringify(value)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
global.AssertError = class extends Error {
|
|
33
|
+
constructor(text, actual, expected, expectLiteral) {
|
|
34
|
+
text += `
|
|
35
|
+
Actual: ${toDisplay(actual)}
|
|
36
|
+
Expected: ${expectLiteral ? expected : toDisplay(expected)}`
|
|
37
|
+
super(text)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
global.assert = function(bool, msg) {
|
|
42
|
+
if (!bool) throw new AssertError(`assert failed${msg ? ": "+msg : ""}`, bool, "something trueish", true)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
global.assertEqual = function(actual, expected, msg) {
|
|
46
|
+
if (!equal(actual,expected)) throw new AssertError(`equal failed${msg ? ": "+msg : ""}`, actual, expected)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
global.getBody = function() {
|
|
50
|
+
return document.body.toString().replace(/^body{/,'').replace(/}$/,'')
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
global.assertBody = function(expected) {
|
|
54
|
+
let actual = getBody()
|
|
55
|
+
if (actual !== expected) throw new AssertError(`assertBody failed`, actual, expected)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
global.assertThrow = function(what, func) {
|
|
59
|
+
if (typeof what == 'function') {
|
|
60
|
+
func = what
|
|
61
|
+
what = undefined
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
func()
|
|
65
|
+
} catch(e) {
|
|
66
|
+
if (what && e.toString().indexOf(what)<0) throw new AssertError(`wrong exception`, e.toString(), `something containing "${what}"`, true)
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
throw new AssertError(`exception expected`, undefined, `something containing "${what}"`, true)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
global.objToMap = function(obj) {
|
|
73
|
+
if (typeof obj === 'object' && obj && obj.constructor===Object) {
|
|
74
|
+
let map = new Map()
|
|
75
|
+
for(let k in obj) {
|
|
76
|
+
map.set(k, objToMap(obj[k]))
|
|
77
|
+
}
|
|
78
|
+
return map
|
|
79
|
+
}
|
|
80
|
+
return obj
|
|
81
|
+
}
|