Skip to content

Commit 894d906

Browse files
authored
Merge pull request #67 from canjs/off-mo
Disconnect MutationObserver when can-globals key changes
2 parents 3303dfa + 59d32f9 commit 894d906

File tree

2 files changed

+3
-103
lines changed

2 files changed

+3
-103
lines changed

can-dom-mutate.js

Lines changed: 3 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
var globals = require('can-globals');
34
var getRoot = require('can-globals/global/global');
45
var getMutationObserver = require('can-globals/mutation-observer/mutation-observer');
56
var namespace = require('can-namespace');
@@ -50,15 +51,6 @@ function deleteRelatedData(node, key) {
5051
function toMutationEvent(node, mutation) {
5152
return {target: node, sourceMutation: mutation};
5253
}
53-
/*
54-
function toMutationEvents (nodes) {
55-
var events = [];
56-
for (var i = 0, length = nodes.length; i < length; i++) {
57-
events.push({target: nodes[i]});
58-
}
59-
return events;
60-
}
61-
*/
6254

6355
function getDocumentListeners (target, key) {
6456
// TODO: it's odd these functions read DOCUMENT() instead of
@@ -129,41 +121,15 @@ function flushCallbacks(callbacks, arg){
129121
safeCallbacks[c](arg);
130122
}
131123
}
132-
/*
133-
function flushRecords(){
134-
if(recordsAndCallbacks === null) {
135-
return;
136-
}
137-
var safeBatch = recordsAndCallbacks;
138-
recordsAndCallbacks = null;
139-
140-
var batchCount = safeBatch.length;
141-
142-
for (var i = 0; i < batchCount; i++) {
143-
var batchData = safeBatch[i];
144-
flushCallbacks(batchData.callbacks, batchData.arg);
145-
}
146-
}
147124

148-
function flushAsync(callbacks, arg) {
149-
if(recordsAndCallbacks === null) {
150-
recordsAndCallbacks = [{arg: arg, callbacks: callbacks}];
151-
nextTick(flushRecords);
152-
} else {
153-
recordsAndCallbacks.push({arg: arg, callbacks: callbacks});
154-
}
155-
}
156-
*/
157125
function dispatch(getListeners, targetKey) {
158126

159127
return function dispatchEvents(event) {
160-
161128
var targetListeners = getListeners(event.target, targetKey);
162129

163130
if (targetListeners) {
164131
flushCallbacks(targetListeners, event);
165132
}
166-
167133
};
168134
}
169135

@@ -200,6 +166,7 @@ function observeMutations(target, observerKey, config, handler) {
200166
};
201167

202168
if (observerData.observingCount === 0) {
169+
globals.onKeyValue('MutationObserver', setupObserver);
203170
setupObserver();
204171
}
205172

@@ -213,72 +180,11 @@ function observeMutations(target, observerKey, config, handler) {
213180
observerData.observer.disconnect();
214181
}
215182
deleteRelatedData(target, observerKey);
183+
globals.offKeyValue('MutationObserver', setupObserver);
216184
}
217185
}
218186
};
219187
}
220-
/*
221-
function handleTreeMutations(mutations) {
222-
// in IE11, if the document is being removed
223-
// (such as when an iframe is added and then removed)
224-
// all of the global constructors will not exist
225-
// If this happens before a tree mutation is handled,
226-
// this will throw an `Object expected` error.
227-
if (typeof Set === "undefined") { return; }
228-
229-
if(this.flushing === true) {
230-
this.mutations.push.apply(this.mutations, mutations);
231-
return;
232-
}
233-
this.flushing = true;
234-
this.mutations = [].slice.call(mutations);
235-
236-
var mutation;
237-
238-
var mutationCount = mutations.length;
239-
var added = new Set(), removed = new Set();
240-
241-
while(mutation = this.mutations.shift()) {
242-
var removedCount = mutation.removedNodes.length;
243-
for (var r = 0; r < removedCount; r++) {
244-
// get what already isn't in `removed`
245-
var newRemoved = util.addToSet( getAllNodes(mutation.removedNodes[r]), removed);
246-
dispatchRemoval( newRemoved.map(toMutationEvent), null, true, flushCallbacks );
247-
}
248-
249-
var addedCount = mutation.addedNodes.length;
250-
for (var a = 0; a < addedCount; a++) {
251-
var newAdded = util.addToSet( getAllNodes(mutation.addedNodes[a]), added);
252-
dispatchInsertion( newAdded.map(toMutationEvent), null, true, flushCallbacks );
253-
}
254-
255-
}
256-
257-
this.flushing = false;
258-
//dispatchRemoval( toMutationEvents( canReflect.toArray(removed) ), null, true, flushCallbacks );
259-
//dispatchInsertion( toMutationEvents( canReflect.toArray(added) ), null, true, flushCallbacks );
260-
}
261-
262-
function handleAttributeMutations(mutations) {
263-
var mutationCount = mutations.length;
264-
for (var m = 0; m < mutationCount; m++) {
265-
var mutation = mutations[m];
266-
if (mutation.type === 'attributes') {
267-
var node = mutation.target;
268-
var attributeName = mutation.attributeName;
269-
var oldValue = mutation.oldValue;
270-
dispatchAttributeChange([{
271-
target: node,
272-
attributeName: attributeName,
273-
oldValue: oldValue
274-
}], null, true, flushCallbacks);
275-
}
276-
}
277-
}
278-
*/
279-
280-
281-
282188

283189
var treeMutationConfig = {
284190
subtree: true,
@@ -456,10 +362,6 @@ function dispatchTreeMutation(mutation, processedState) {
456362
}
457363

458364

459-
460-
461-
462-
463365
var FLUSHING_MUTATIONS = [];
464366
var IS_FLUSHING = false;
465367

node/node.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,4 @@ var mutationObserverKey = 'MutationObserver';
179179
setMutateStrategy(globals.getKeyValue(mutationObserverKey));
180180
globals.onKeyValue(mutationObserverKey, setMutateStrategy);
181181

182-
//mutate.isConnected = isConnected;
183-
184182
module.exports = namespace.domMutateNode = domMutate.node = mutate;

0 commit comments

Comments
 (0)