OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 // implementation of (1) and (2) have "optimized" states which represent | 56 // implementation of (1) and (2) have "optimized" states which represent |
57 // common cases which can be handled more efficiently. | 57 // common cases which can be handled more efficiently. |
58 | 58 |
59 var observationState = %GetObservationState(); | 59 var observationState = %GetObservationState(); |
60 if (IS_UNDEFINED(observationState.callbackInfoMap)) { | 60 if (IS_UNDEFINED(observationState.callbackInfoMap)) { |
61 observationState.callbackInfoMap = %ObservationWeakMapCreate(); | 61 observationState.callbackInfoMap = %ObservationWeakMapCreate(); |
62 observationState.objectInfoMap = %ObservationWeakMapCreate(); | 62 observationState.objectInfoMap = %ObservationWeakMapCreate(); |
63 observationState.notifierObjectInfoMap = %ObservationWeakMapCreate(); | 63 observationState.notifierObjectInfoMap = %ObservationWeakMapCreate(); |
64 observationState.pendingObservers = null; | 64 observationState.pendingObservers = null; |
65 observationState.nextCallbackPriority = 0; | 65 observationState.nextCallbackPriority = 0; |
| 66 observationState.microtaskScheduled = false; |
66 } | 67 } |
67 | 68 |
68 function ObservationWeakMap(map) { | 69 function ObservationWeakMap(map) { |
69 this.map_ = map; | 70 this.map_ = map; |
70 } | 71 } |
71 | 72 |
72 ObservationWeakMap.prototype = { | 73 ObservationWeakMap.prototype = { |
73 get: function(key) { | 74 get: function(key) { |
74 key = %UnwrapGlobalProxy(key); | 75 key = %UnwrapGlobalProxy(key); |
75 if (!IS_SPEC_OBJECT(key)) return UNDEFINED; | 76 if (!IS_SPEC_OBJECT(key)) return UNDEFINED; |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 !%IsAccessAllowedForObserver( | 388 !%IsAccessAllowedForObserver( |
388 callback, changeRecord.object, changeRecord.name))) { | 389 callback, changeRecord.object, changeRecord.name))) { |
389 return; | 390 return; |
390 } | 391 } |
391 | 392 |
392 var callbackInfo = CallbackInfoNormalize(callback); | 393 var callbackInfo = CallbackInfoNormalize(callback); |
393 if (!observationState.pendingObservers) | 394 if (!observationState.pendingObservers) |
394 observationState.pendingObservers = nullProtoObject(); | 395 observationState.pendingObservers = nullProtoObject(); |
395 observationState.pendingObservers[callbackInfo.priority] = callback; | 396 observationState.pendingObservers[callbackInfo.priority] = callback; |
396 callbackInfo.push(changeRecord); | 397 callbackInfo.push(changeRecord); |
397 %SetMicrotaskPending(true); | 398 EnqueueObserveMicrotask(); |
398 } | 399 } |
399 | 400 |
400 function ObjectInfoEnqueueExternalChangeRecord(objectInfo, changeRecord, type) { | 401 function ObjectInfoEnqueueExternalChangeRecord(objectInfo, changeRecord, type) { |
401 if (!ObjectInfoHasActiveObservers(objectInfo)) | 402 if (!ObjectInfoHasActiveObservers(objectInfo)) |
402 return; | 403 return; |
403 | 404 |
404 var hasType = !IS_UNDEFINED(type); | 405 var hasType = !IS_UNDEFINED(type); |
405 var newRecord = hasType ? | 406 var newRecord = hasType ? |
406 { object: ObjectInfoGetObject(objectInfo), type: type } : | 407 { object: ObjectInfoGetObject(objectInfo), type: type } : |
407 { object: ObjectInfoGetObject(objectInfo) }; | 408 { object: ObjectInfoGetObject(objectInfo) }; |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
568 } | 569 } |
569 | 570 |
570 function ObjectDeliverChangeRecords(callback) { | 571 function ObjectDeliverChangeRecords(callback) { |
571 if (!IS_SPEC_FUNCTION(callback)) | 572 if (!IS_SPEC_FUNCTION(callback)) |
572 throw MakeTypeError("observe_non_function", ["deliverChangeRecords"]); | 573 throw MakeTypeError("observe_non_function", ["deliverChangeRecords"]); |
573 | 574 |
574 while (CallbackDeliverPending(callback)) {} | 575 while (CallbackDeliverPending(callback)) {} |
575 } | 576 } |
576 | 577 |
577 function ObserveMicrotaskRunner() { | 578 function ObserveMicrotaskRunner() { |
| 579 observationState.microtaskScheduled = false; |
578 var pendingObservers = observationState.pendingObservers; | 580 var pendingObservers = observationState.pendingObservers; |
579 if (pendingObservers) { | 581 if (pendingObservers) { |
580 observationState.pendingObservers = null; | 582 observationState.pendingObservers = null; |
581 for (var i in pendingObservers) { | 583 for (var i in pendingObservers) { |
582 CallbackDeliverPending(pendingObservers[i]); | 584 CallbackDeliverPending(pendingObservers[i]); |
583 } | 585 } |
584 } | 586 } |
585 } | 587 } |
586 RunMicrotasks.runners.push(ObserveMicrotaskRunner); | 588 |
| 589 function EnqueueObserveMicrotask() { |
| 590 if (observationState.microtaskScheduled) |
| 591 return; |
| 592 |
| 593 RunMicrotasks.queue.push(ObserveMicrotaskRunner); |
| 594 %SetMicrotaskPending(true); |
| 595 observationState.microtaskScheduled = true; |
| 596 } |
587 | 597 |
588 function SetupObjectObserve() { | 598 function SetupObjectObserve() { |
589 %CheckIsBootstrapping(); | 599 %CheckIsBootstrapping(); |
590 InstallFunctions($Object, DONT_ENUM, $Array( | 600 InstallFunctions($Object, DONT_ENUM, $Array( |
591 "deliverChangeRecords", ObjectDeliverChangeRecords, | 601 "deliverChangeRecords", ObjectDeliverChangeRecords, |
592 "getNotifier", ObjectGetNotifier, | 602 "getNotifier", ObjectGetNotifier, |
593 "observe", ObjectObserve, | 603 "observe", ObjectObserve, |
594 "unobserve", ObjectUnobserve | 604 "unobserve", ObjectUnobserve |
595 )); | 605 )); |
596 InstallFunctions($Array, DONT_ENUM, $Array( | 606 InstallFunctions($Array, DONT_ENUM, $Array( |
597 "observe", ArrayObserve, | 607 "observe", ArrayObserve, |
598 "unobserve", ArrayUnobserve | 608 "unobserve", ArrayUnobserve |
599 )); | 609 )); |
600 InstallFunctions(notifierPrototype, DONT_ENUM, $Array( | 610 InstallFunctions(notifierPrototype, DONT_ENUM, $Array( |
601 "notify", ObjectNotifierNotify, | 611 "notify", ObjectNotifierNotify, |
602 "performChange", ObjectNotifierPerformChange | 612 "performChange", ObjectNotifierPerformChange |
603 )); | 613 )); |
604 } | 614 } |
605 | 615 |
606 SetupObjectObserve(); | 616 SetupObjectObserve(); |
OLD | NEW |