OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2014 The Polymer Project Authors. All rights reserved. | |
3 * This code may only be used under the BSD style license found at http://polyme
r.github.io/LICENSE.txt | |
4 * The complete set of authors may be found at http://polymer.github.io/AUTHORS.
txt | |
5 * The complete set of contributors may be found at http://polymer.github.io/CON
TRIBUTORS.txt | |
6 * Code distributed by Google as part of the polymer project is also | |
7 * subject to an additional IP rights grant found at http://polymer.github.io/PA
TENTS.txt | |
8 */ | |
9 | |
10 (function(scope) { | |
11 | |
12 /** | |
13 * @class Polymer | |
14 */ | |
15 | |
16 // imports | |
17 var endOfMicrotask = scope.endOfMicrotask; | |
18 | |
19 // logging | |
20 var log = window.WebComponents ? WebComponents.flags.log : {}; | |
21 | |
22 // inject style sheet | |
23 var style = document.createElement('style'); | |
24 style.textContent = 'template {display: none !important;} /* injected by platfor
m.js */'; | |
25 var head = document.querySelector('head'); | |
26 head.insertBefore(style, head.firstChild); | |
27 | |
28 | |
29 /** | |
30 * Force any pending data changes to be observed before | |
31 * the next task. Data changes are processed asynchronously but are guaranteed | |
32 * to be processed, for example, before paintin. This method should rarely be | |
33 * needed. It does nothing when Object.observe is available; | |
34 * when Object.observe is not available, Polymer automatically flushes data | |
35 * changes approximately every 1/10 second. | |
36 * Therefore, `flush` should only be used when a data mutation should be | |
37 * observed sooner than this. | |
38 * | |
39 * @method flush | |
40 */ | |
41 // flush (with logging) | |
42 var flushing; | |
43 function flush() { | |
44 if (!flushing) { | |
45 flushing = true; | |
46 endOfMicrotask(function() { | |
47 flushing = false; | |
48 log.data && console.group('flush'); | |
49 Platform.performMicrotaskCheckpoint(); | |
50 log.data && console.groupEnd(); | |
51 }); | |
52 } | |
53 }; | |
54 | |
55 // polling dirty checker | |
56 // flush periodically if platform does not have object observe. | |
57 if (!Observer.hasObjectObserve) { | |
58 var FLUSH_POLL_INTERVAL = 125; | |
59 window.addEventListener('WebComponentsReady', function() { | |
60 flush(); | |
61 // watch document visiblity to toggle dirty-checking | |
62 var visibilityHandler = function() { | |
63 // only flush if the page is visibile | |
64 if (document.visibilityState === 'hidden') { | |
65 if (scope.flushPoll) { | |
66 clearInterval(scope.flushPoll); | |
67 } | |
68 } else { | |
69 scope.flushPoll = setInterval(flush, FLUSH_POLL_INTERVAL); | |
70 } | |
71 }; | |
72 if (typeof document.visibilityState === 'string') { | |
73 document.addEventListener('visibilitychange', visibilityHandler); | |
74 } | |
75 visibilityHandler(); | |
76 }); | |
77 } else { | |
78 // make flush a no-op when we have Object.observe | |
79 flush = function() {}; | |
80 } | |
81 | |
82 if (window.CustomElements && !CustomElements.useNative) { | |
83 var originalImportNode = Document.prototype.importNode; | |
84 Document.prototype.importNode = function(node, deep) { | |
85 var imported = originalImportNode.call(this, node, deep); | |
86 CustomElements.upgradeAll(imported); | |
87 return imported; | |
88 }; | |
89 } | |
90 | |
91 // exports | |
92 scope.flush = flush; | |
93 // bc | |
94 Platform.flush = flush; | |
95 | |
96 })(window.Polymer); | |
97 | |
OLD | NEW |