-
Notifications
You must be signed in to change notification settings - Fork 8
#113 - Implement experimental web worker support #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
aaa321b
refactor: Remove out of sync version file. Remove unnecessary gulp file.
treshugart 3e36a5e
feat: Add experimental web worker support.
treshugart 5e88c1e
refactor: Fix bad equality check.
treshugart d46282f
refactor: let -> const
treshugart ff5dba0
refactor: Update merge() and render() to accept a done callback.
treshugart d80c6aa
refactor(test): Refactor assertion for async renders.
treshugart 166fe0b
docs: Update docs for web workers.
treshugart b95cc2e
build: Fix some build issues. Still need a index.min.js.
treshugart 22a4f5d
refactor: Refactor tests and start major refactor to make api consist…
treshugart 8ce2f30
refactor: Simplify code.
treshugart e776b96
refactor: Massive major refactor. Added many features and removed som…
treshugart 27a1a9b
feat: Update docs. Add web worker support. Add to / from dom / vdom c…
treshugart dc04d36
refactor: Try and fix IE.
treshugart 2296875
refactor: Don't use object.assign because IE
treshugart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import * as types from './types'; | ||
import compareNode from './compare/node'; | ||
import realNode from './util/real-node'; | ||
import realNodeMap from './util/real-node-map'; | ||
|
||
function diffNode (source, destination) { | ||
let nodeInstructions = compareNode(source, destination); | ||
|
||
// If there are instructions (even an empty array) it means the node can be | ||
// diffed and doesn't have to be replaced. If the instructions are falsy | ||
// it means that the nodes are not similar (cannot be changed) and must be | ||
// replaced instead. | ||
if (nodeInstructions) { | ||
return nodeInstructions.concat(diff({ source, destination })); | ||
} | ||
|
||
return [{ | ||
destination, | ||
source, | ||
type: types.REPLACE_CHILD | ||
}]; | ||
} | ||
|
||
export default function diff (opts) { | ||
const src = opts.source; | ||
const dst = opts.destination; | ||
|
||
if (!src || !dst) { | ||
return []; | ||
} | ||
|
||
let instructions = opts.root ? diffNode(src, dst) : []; | ||
|
||
const srcChs = src.childNodes; | ||
const dstChs = dst.childNodes; | ||
const srcChsLen = srcChs ? srcChs.length : 0; | ||
const dstChsLen = dstChs ? dstChs.length : 0; | ||
|
||
for (let a = 0; a < dstChsLen; a++) { | ||
const curSrc = srcChs[a]; | ||
const curDst = dstChs[a]; | ||
|
||
// If there is no matching destination node it means we need to remove the | ||
// current source node from the source. | ||
if (!curSrc) { | ||
instructions.push({ | ||
destination: dstChs[a], | ||
source: src, | ||
type: types.APPEND_CHILD | ||
}); | ||
continue; | ||
} else { | ||
// Ensure the real node is carried over even if the destination isn't used. | ||
// This is used in the render() function to keep track of the real node | ||
// that corresponds to a virtual node if a virtual tree is being used. | ||
if (typeof curDst.__id !== 'undefined') { | ||
realNodeMap.set(curDst.__id, realNode(curSrc)); | ||
} | ||
} | ||
|
||
instructions = instructions.concat(diffNode(curSrc, curDst)); | ||
} | ||
|
||
if (dstChsLen < srcChsLen) { | ||
for (let a = dstChsLen; a < srcChsLen; a++) { | ||
instructions.push({ | ||
destination: srcChs[a], | ||
source: src, | ||
type: types.REMOVE_CHILD | ||
}); | ||
} | ||
} | ||
|
||
return instructions; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import diff from './diff-main'; | ||
|
||
self.addEventListener('message', e => { | ||
const instructions = diff(e.data); | ||
self.postMessage(instructions); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,18 @@ | ||
import * as types from './types'; | ||
import compareNode from './compare/node'; | ||
import realNode from './util/real-node'; | ||
import realNodeMap from './util/real-node-map'; | ||
import diffMain from './diff-main'; | ||
import DiffWorker from 'worker-loader!./diff-worker'; | ||
|
||
const { Node } = window; | ||
|
||
function diffNode (source, destination) { | ||
let nodeInstructions = compareNode(source, destination); | ||
|
||
// If there are instructions (even an empty array) it means the node can be | ||
// diffed and doesn't have to be replaced. If the instructions are falsy | ||
// it means that the nodes are not similar (cannot be changed) and must be | ||
// replaced instead. | ||
if (nodeInstructions) { | ||
return nodeInstructions.concat(diff({ source, destination })); | ||
} | ||
|
||
return [{ | ||
destination, | ||
source, | ||
type: types.REPLACE_CHILD | ||
}]; | ||
function diffWorker (opts) { | ||
const worker = new DiffWorker(); | ||
const { done } = opts; | ||
worker.addEventListener('message', e => done(e.data)); | ||
delete opts.done; | ||
worker.postMessage(opts); | ||
} | ||
|
||
export default function diff (opts = {}) { | ||
const src = opts.source; | ||
const dst = opts.destination; | ||
|
||
if (!src || !dst) { | ||
return []; | ||
} | ||
|
||
let instructions = opts.root ? diffNode(src, dst) : []; | ||
|
||
const srcChs = src.childNodes; | ||
const dstChs = dst.childNodes; | ||
const srcChsLen = srcChs ? srcChs.length : 0; | ||
const dstChsLen = dstChs ? dstChs.length : 0; | ||
|
||
for (let a = 0; a < dstChsLen; a++) { | ||
const curSrc = srcChs[a]; | ||
const curDst = dstChs[a]; | ||
|
||
// If there is no matching destination node it means we need to remove the | ||
// current source node from the source. | ||
if (!curSrc) { | ||
instructions.push({ | ||
destination: dstChs[a], | ||
source: src, | ||
type: types.APPEND_CHILD | ||
}); | ||
continue; | ||
} else { | ||
// Ensure the real node is carried over even if the destination isn't used. | ||
// This is used in the render() function to keep track of the real node | ||
// that corresponds to a virtual node if a virtual tree is being used. | ||
if (!(curDst instanceof Node)) { | ||
realNodeMap.set(curDst, realNode(curSrc)); | ||
} | ||
} | ||
|
||
instructions = instructions.concat(diffNode(curSrc, curDst)); | ||
} | ||
|
||
if (dstChsLen < srcChsLen) { | ||
for (let a = dstChsLen; a < srcChsLen; a++) { | ||
instructions.push({ | ||
destination: srcChs[a], | ||
source: src, | ||
type: types.REMOVE_CHILD | ||
}); | ||
} | ||
} | ||
|
||
return instructions; | ||
const { source, destination, done } = opts; | ||
const canDiffInWorker = done && !(source instanceof Node && destination instanceof Node); | ||
return canDiffInWorker ? diffWorker(opts) : diffMain(opts); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,19 @@ | ||
import diff from './diff'; | ||
import patch from './patch'; | ||
|
||
export default function (opts) { | ||
var inst = diff(opts); | ||
patch(inst); | ||
return inst; | ||
export default function (opts = {}) { | ||
const { source, destination, done } = opts; | ||
if (done) { | ||
return diff({ | ||
source, | ||
destination, | ||
done (instructions) { | ||
patch(instructions); | ||
done(instructions); | ||
} | ||
}); | ||
} | ||
const instructions = diff(opts); | ||
patch(instructions); | ||
return instructions; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,11 @@ | ||
import WeakMap from './weak-map'; | ||
export default new WeakMap(); | ||
// import WeakMap from './weak-map'; | ||
// export default new WeakMap(); | ||
const map = []; | ||
export default { | ||
get (id) { | ||
return map[id]; | ||
}, | ||
set (id, node) { | ||
map[id] = node; | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import realNodeMap from './real-node-map'; | ||
|
||
const { Node } = window; | ||
const isWindow = typeof window !== 'undefined'; | ||
const { Node } = isWindow ? window : self; | ||
|
||
export default function (node) { | ||
return node instanceof Node ? node : realNodeMap.get(node); | ||
return isWindow && node instanceof Node ? node : realNodeMap.get(node.__id); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,10 +66,12 @@ function translateFromReact (item) { | |
return item; | ||
} | ||
|
||
let count = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely better ways, but this gets the initial spike working. |
||
export default function element (name, attrs = {}, ...chren) { | ||
const isAttrsNode = isChildren(attrs); | ||
const data = separateData(isAttrsNode ? {} : attrs); | ||
const node = data.node; | ||
node.__id = ++count; | ||
node.nodeType = 1; | ||
node.tagName = ensureTagName(name); | ||
node.attributes = data.attrs; | ||
|
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't use weakmaps if we're using scalar values as keys.