-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mutation): notify change intersection (#15)
* chore: remove redundant code * refactor: clean up mutation polyfill * feat: add MutationObserver to global * refactor: remove casts * feat(mutation): add better change notification. * feat: remove redundent dispose. * refactor: clean up mutation-observer methods * fix: debounce change events * closes #14
- Loading branch information
1 parent
e577862
commit 12266c7
Showing
6 changed files
with
132 additions
and
108 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
|
@@ -7,25 +7,80 @@ import { NodeJsPlatform } from './nodejs-platform'; | |
import { NodeJsFeature } from './nodejs-feature'; | ||
import { NodeJsDom } from './nodejs-dom'; | ||
import { jsdom } from 'jsdom'; | ||
import { MutationObserver } from './polyfills/mutation-observer'; | ||
import { MutationNotifier } from './polyfills/mutation-observer'; | ||
|
||
let _patchedjsdom = false; | ||
|
||
export function buildPal(): { global: IGlobal, platform: IPlatform, dom: IDom, feature: IFeature } { | ||
var _global: IGlobal = <IGlobal>jsdom(undefined, {}).defaultView; | ||
var global: IGlobal = <IGlobal>jsdom(undefined, {}).defaultView; | ||
|
||
if (!_patchedjsdom) { | ||
patchNotifyChange(global); | ||
_patchedjsdom = true; | ||
} | ||
|
||
ensurePerformance(_global.window); | ||
ensureMutationObserver(_global.window); | ||
ensurePerformance(global.window); | ||
ensureMutationObserver(global.window); | ||
|
||
var _platform = new NodeJsPlatform(_global); | ||
var _dom = new NodeJsDom(_global); | ||
var _feature = new NodeJsFeature(_global); | ||
var platform = new NodeJsPlatform(global); | ||
var dom = new NodeJsDom(global); | ||
var feature = new NodeJsFeature(global); | ||
|
||
return { | ||
global: _global, | ||
platform: _platform, | ||
dom: _dom, | ||
feature: _feature | ||
global: global, | ||
platform: platform, | ||
dom: dom, | ||
feature: feature | ||
}; | ||
} | ||
|
||
let intersectSetter = function (proto, propertyName: string, intersect: Function) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
MeirionHughes
Author
Contributor
|
||
let old = Object.getOwnPropertyDescriptor(proto, propertyName); | ||
let oldSet = old.set; | ||
let newSet = function set(V) { | ||
oldSet.call(this, V); | ||
intersect(this); | ||
}; | ||
Object.defineProperty(proto, propertyName, { | ||
set: newSet, | ||
get: old.get, | ||
configurable: old.configurable, | ||
enumerable: old.enumerable | ||
}); | ||
}; | ||
|
||
let intersectMethod = function (proto, methodName: string, intersect: Function) { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
let orig = proto[methodName]; | ||
proto[methodName] = function (...args) { | ||
var ret = orig.apply(this, args); | ||
intersect(this); | ||
return ret; | ||
}; | ||
}; | ||
|
||
function patchNotifyChange(window: Window) { | ||
let notifyInstance = MutationNotifier.getInstance(); | ||
let notify = function (node: Node) { notifyInstance.notifyChanged(node); }; | ||
|
||
let node_proto = (<any>window)._core.Node.prototype; | ||
|
||
intersectMethod(node_proto, "appendChild", notify); | ||
intersectMethod(node_proto, "insertBefore", notify); | ||
intersectMethod(node_proto, "removeChild", notify); | ||
intersectMethod(node_proto, "replaceChild", notify); | ||
intersectSetter(node_proto, "nodeValue", notify); | ||
intersectSetter(node_proto, "textContent", notify); | ||
|
||
let element_proto = (<any>window)._core.Element.prototype; | ||
|
||
intersectMethod(element_proto, "setAttribute", notify); | ||
intersectMethod(element_proto, "removeAttribute", notify); | ||
intersectMethod(element_proto, "removeAttributeNode", notify); | ||
intersectMethod(element_proto, "removeAttributeNS", notify); | ||
} | ||
|
||
|
||
function ensurePerformance(window) { | ||
if (window.performance === undefined) { | ||
window.performance = {}; | ||
|
@@ -45,5 +100,7 @@ function ensurePerformance(window) { | |
} | ||
|
||
function ensureMutationObserver(window) { | ||
require('./polyfills/mutation-observer').polyfill(window); | ||
if (!window.MutationObserver) { | ||
window.MutationObserver = MutationObserver; | ||
} | ||
} |
Oops, something went wrong.
typo? "intersect" vs "intercept"?