cleaning up (1)

This commit is contained in:
DadaMonad
2014-12-14 17:00:02 +00:00
parent 9b582fc795
commit 7696864841
50 changed files with 9302 additions and 6 deletions

View File

@@ -0,0 +1,21 @@
{
"name": "observe-shim",
"version": "0.4.2",
"main": "lib/observe-shim.js",
"devDependencies": {
"mocha": "~1.8.x",
"expect": "~0.2.0",
"sinon": "http://sinonjs.org/releases/sinon-1.6.0.js"
},
"homepage": "https://github.com/KapIT/observe-shim",
"_release": "0.4.2",
"_resolution": {
"type": "version",
"tag": "v0.4.2",
"commit": "75e8ea887c38bd1540fe4989a17b6e3751d7c7e5"
},
"_source": "git://github.com/KapIT/observe-shim.git",
"_target": "~0.4.2",
"_originalSource": "observe-shim",
"_direct": true
}

View File

@@ -0,0 +1,3 @@
{
"directory": "components"
}

View File

@@ -0,0 +1,5 @@
node_modules
npm-debug.log
.DS_Store
.idea
components

21
bower_components/observe-shim/.jshintrc vendored Normal file
View File

@@ -0,0 +1,21 @@
{
"bitwise": false,
"camelcase" : true,
"curly" : true,
"immed" : true,
"indent": 4,
"latedef" : true,
"newcap" : true,
"noarg" : true,
"undef" : true,
"unused" : true,
"strict" : true,
"trailing": true,
"node": true,
"es5": true,
"esnext": true,
"regexp": true,
"smarttabs": true,
"eqeqeq" : true,
"quotmark" : "single"
}

View File

@@ -0,0 +1,55 @@
// Copyright 2012 Kap IT (http://www.kapit.fr/)
//
// Licensed under the Apache License, Version 2.0 (the 'License');
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Author : François de Campredon (http://francois.de-campredon.fr/),
'use strict';
module.exports = function (grunt) {
require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);
grunt.initConfig({
jshint: {
options: {
jshintrc: '.jshintrc'
},
all: [ 'lib/*.js', 'test/*.js', 'Gruntfile.js' ]
},
mocha : {
index: ['test/index.html'],
options: {
run : true,
log : true
}
},
mochaTest : {
test: {
options: {
reporter: 'spec'
},
src: ['test/node-index.js']
}
},
clean : {
folder : ['docs']
},
docco: {
src: ['lib/observe-shim.js'],
options: {
output: 'docs/'
}
}
});
grunt.registerTask('test', ['jshint', 'mocha', 'mochaTest']);
grunt.registerTask('default', ['test', 'clean', 'docco']);
};

176
bower_components/observe-shim/LICENSE vendored Normal file
View File

@@ -0,0 +1,176 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS

67
bower_components/observe-shim/README.md vendored Normal file
View File

@@ -0,0 +1,67 @@
Object.observe shim
===================
See : [The harmony proposal page](http://wiki.ecmascript.org/doku.php?id=harmony:observe).
Goal:
----
This shim provides an implementation of the algorithms described in the harmony proposal, and is intended to work on all ES5-compliant browsers.
Dependencies :
--------------
While this implementation does not have dependencies, it tries to use "setImmediate" if present, and fall back on setTimeout if it is not, it is recommended to use a setImmediate shim for browsers that do not support it natively. ( A good one can be found [here](https://github.com/NobleJS/setImmediate) )
Limitations :
-------------
While this shim provides an implementation for the Object methods, and the Notifier prototype described in the proposal, it does not try to catch and notify by any means changes made to an object.
Instead it let you call manually the notify method :
Object.getNotifier(myObject).notify({ type : "updated" , ....});
ObserveUtils :
--------------
The ['observe-utils.js'](http://github.com/kapit/observe-utils/) utilities that facilitate the use of this shim can be found on his [own repository](http://github.com/kapit/observe-utils/)
Example :
-------
var myObject = {};
ObserveUtils.defineObservableProperties(myObject, "foo", "bar");
Object.observe(myObject, function (changes) {
console.log(changes);
});
myObject.foo = "Hello";
myObject.bar = "World";
//log
[
{
name : "foo",
object : myObject,
oldValue : undefined,
type : "updated"
},
{
name : "bar",
object : myObject,
oldValue : undefined,
type : "updated"
}
]
Build And Test:
---------------
Require [bower](https://github.com/twitter/bower) and [grunt-cli](https://github.com/gruntjs/grunt-cli) installed on your machine.
npm install & bower install
grunt // test and build
grunt test // test only

View File

@@ -0,0 +1,10 @@
{
"name": "observe-shim",
"version": "0.4.2",
"main": "lib/observe-shim.js",
"devDependencies": {
"mocha" : "~1.8.x",
"expect" : "~0.2.0",
"sinon" : "http://sinonjs.org/releases/sinon-1.6.0.js"
}
}

View File

@@ -0,0 +1,13 @@
{
"name": "observe-shim",
"version": "0.4.2",
"description": "An Object.observe harmony proposal shim",
"main": "lib/observe-shim.js",
"repo": "KapIT/observe-shim",
"scripts": ["lib/observe-shim.js"],
"keywords": [
"observe",
"data binding"
],
"license": "Apache"
}

View File

@@ -0,0 +1,192 @@
/*--------------------- Layout and Typography ----------------------------*/
body {
font-family: 'Palatino Linotype', 'Book Antiqua', Palatino, FreeSerif, serif;
font-size: 15px;
line-height: 22px;
color: #252519;
margin: 0; padding: 0;
}
a {
color: #261a3b;
}
a:visited {
color: #261a3b;
}
p {
margin: 0 0 15px 0;
}
h1, h2, h3, h4, h5, h6 {
margin: 0px 0 15px 0;
}
h1 {
margin-top: 40px;
}
hr {
border: 0 none;
border-top: 1px solid #e5e5ee;
height: 1px;
margin: 20px 0;
}
#container {
position: relative;
}
#background {
position: fixed;
top: 0; left: 525px; right: 0; bottom: 0;
background: #f5f5ff;
border-left: 1px solid #e5e5ee;
z-index: -1;
}
#jump_to, #jump_page {
background: white;
-webkit-box-shadow: 0 0 25px #777; -moz-box-shadow: 0 0 25px #777;
-webkit-border-bottom-left-radius: 5px; -moz-border-radius-bottomleft: 5px;
font: 10px Arial;
text-transform: uppercase;
cursor: pointer;
text-align: right;
}
#jump_to, #jump_wrapper {
position: fixed;
right: 0; top: 0;
padding: 5px 10px;
}
#jump_wrapper {
padding: 0;
display: none;
}
#jump_to:hover #jump_wrapper {
display: block;
}
#jump_page {
padding: 5px 0 3px;
margin: 0 0 25px 25px;
}
#jump_page .source {
display: block;
padding: 5px 10px;
text-decoration: none;
border-top: 1px solid #eee;
}
#jump_page .source:hover {
background: #f5f5ff;
}
#jump_page .source:first-child {
}
table td {
border: 0;
outline: 0;
}
td.docs, th.docs {
max-width: 450px;
min-width: 450px;
min-height: 5px;
padding: 10px 25px 1px 50px;
overflow-x: hidden;
vertical-align: top;
text-align: left;
}
.docs pre {
margin: 15px 0 15px;
padding-left: 15px;
}
.docs p tt, .docs p code {
background: #f8f8ff;
border: 1px solid #dedede;
font-size: 12px;
padding: 0 0.2em;
}
.pilwrap {
position: relative;
}
.pilcrow {
font: 12px Arial;
text-decoration: none;
color: #454545;
position: absolute;
top: 3px; left: -20px;
padding: 1px 2px;
opacity: 0;
-webkit-transition: opacity 0.2s linear;
}
td.docs:hover .pilcrow {
opacity: 1;
}
td.code, th.code {
padding: 14px 15px 16px 25px;
width: 100%;
vertical-align: top;
background: #f5f5ff;
border-left: 1px solid #e5e5ee;
}
pre, tt, code {
font-size: 12px; line-height: 18px;
font-family: Menlo, Monaco, Consolas, "Lucida Console", monospace;
margin: 0; padding: 0;
}
/*---------------------- Syntax Highlighting -----------------------------*/
td.linenos { background-color: #f0f0f0; padding-right: 10px; }
span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }
body .hll { background-color: #ffffcc }
body .c { color: #408080; font-style: italic } /* Comment */
body .err { border: 1px solid #FF0000 } /* Error */
body .k { color: #954121 } /* Keyword */
body .o { color: #666666 } /* Operator */
body .cm { color: #408080; font-style: italic } /* Comment.Multiline */
body .cp { color: #BC7A00 } /* Comment.Preproc */
body .c1 { color: #408080; font-style: italic } /* Comment.Single */
body .cs { color: #408080; font-style: italic } /* Comment.Special */
body .gd { color: #A00000 } /* Generic.Deleted */
body .ge { font-style: italic } /* Generic.Emph */
body .gr { color: #FF0000 } /* Generic.Error */
body .gh { color: #000080; font-weight: bold } /* Generic.Heading */
body .gi { color: #00A000 } /* Generic.Inserted */
body .go { color: #808080 } /* Generic.Output */
body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */
body .gs { font-weight: bold } /* Generic.Strong */
body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
body .gt { color: #0040D0 } /* Generic.Traceback */
body .kc { color: #954121 } /* Keyword.Constant */
body .kd { color: #954121; font-weight: bold } /* Keyword.Declaration */
body .kn { color: #954121; font-weight: bold } /* Keyword.Namespace */
body .kp { color: #954121 } /* Keyword.Pseudo */
body .kr { color: #954121; font-weight: bold } /* Keyword.Reserved */
body .kt { color: #B00040 } /* Keyword.Type */
body .m { color: #666666 } /* Literal.Number */
body .s { color: #219161 } /* Literal.String */
body .na { color: #7D9029 } /* Name.Attribute */
body .nb { color: #954121 } /* Name.Builtin */
body .nc { color: #0000FF; font-weight: bold } /* Name.Class */
body .no { color: #880000 } /* Name.Constant */
body .nd { color: #AA22FF } /* Name.Decorator */
body .ni { color: #999999; font-weight: bold } /* Name.Entity */
body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */
body .nf { color: #0000FF } /* Name.Function */
body .nl { color: #A0A000 } /* Name.Label */
body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
body .nt { color: #954121; font-weight: bold } /* Name.Tag */
body .nv { color: #19469D } /* Name.Variable */
body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
body .w { color: #bbbbbb } /* Text.Whitespace */
body .mf { color: #666666 } /* Literal.Number.Float */
body .mh { color: #666666 } /* Literal.Number.Hex */
body .mi { color: #666666 } /* Literal.Number.Integer */
body .mo { color: #666666 } /* Literal.Number.Oct */
body .sb { color: #219161 } /* Literal.String.Backtick */
body .sc { color: #219161 } /* Literal.String.Char */
body .sd { color: #219161; font-style: italic } /* Literal.String.Doc */
body .s2 { color: #219161 } /* Literal.String.Double */
body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
body .sh { color: #219161 } /* Literal.String.Heredoc */
body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
body .sx { color: #954121 } /* Literal.String.Other */
body .sr { color: #BB6688 } /* Literal.String.Regex */
body .s1 { color: #219161 } /* Literal.String.Single */
body .ss { color: #19469D } /* Literal.String.Symbol */
body .bp { color: #954121 } /* Name.Builtin.Pseudo */
body .vc { color: #19469D } /* Name.Variable.Class */
body .vg { color: #19469D } /* Name.Variable.Global */
body .vi { color: #19469D } /* Name.Variable.Instance */
body .il { color: #666666 } /* Literal.Number.Integer.Long */

View File

@@ -0,0 +1,428 @@
<!DOCTYPE html> <html> <head> <title>observe-shim.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> observe-shim.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> <p>Copyright 2012 Kap IT (http://www.kapit.fr/)</p>
<p>Licensed under the Apache License, Version 2.0 (the 'License');
you may not use this file except in compliance with the License.
You may obtain a copy of the License at</p>
<pre><code> http://www.apache.org/licenses/LICENSE-2.0
</code></pre>
<p>Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an 'AS IS' BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Author : François de Campredon (http://francois.de-campredon.fr/),</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <h1>Object.observe Shim</h1> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <p><em>See <a href="http://wiki.ecmascript.org/doku.php?id=harmony:observe">The harmony proposal page</a></em></p> </td> <td class="code"> <div class="highlight"><pre><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">global</span><span class="p">)</span> <span class="p">{</span>
<span class="s1">&#39;use strict&#39;</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <h2>Utilities</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>setImmediate shim used to deliver changes records asynchronously
use setImmediate if available</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">setImmediate</span> <span class="o">=</span> <span class="nx">global</span><span class="p">.</span><span class="nx">setImmediate</span> <span class="o">||</span> <span class="nx">global</span><span class="p">.</span><span class="nx">msSetImmediate</span><span class="p">,</span>
<span class="nx">clearImmediate</span> <span class="o">=</span> <span class="nx">global</span><span class="p">.</span><span class="nx">clearImmediate</span> <span class="o">||</span> <span class="nx">global</span><span class="p">.</span><span class="nx">msClearImmediate</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">setImmediate</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>fallback on setTimeout if not</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">setImmediate</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">func</span><span class="p">,</span> <span class="nx">args</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">setTimeout</span><span class="p">(</span><span class="nx">func</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">args</span><span class="p">);</span>
<span class="p">};</span>
<span class="nx">clearImmediate</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">id</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">clearTimeout</span><span class="p">(</span><span class="nx">id</span><span class="p">);</span>
<span class="p">};</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">&#182;</a> </div> <h2>WeakMap</h2> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">PrivateMap</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">WeakMap</span> <span class="o">!==</span> <span class="s1">&#39;undefined&#39;</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">&#182;</a> </div> <p>use weakmap if defined</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">PrivateMap</span> <span class="o">=</span> <span class="nx">WeakMap</span><span class="p">;</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">&#182;</a> </div> <p>else use ses like shim of WeakMap</p> </td> <td class="code"> <div class="highlight"><pre> <span class="cm">/* jshint -W016 */</span>
<span class="kd">var</span> <span class="nx">HIDDEN_PREFIX</span> <span class="o">=</span> <span class="s1">&#39;__weakmap:&#39;</span> <span class="o">+</span> <span class="p">(</span><span class="nb">Math</span><span class="p">.</span><span class="nx">random</span><span class="p">()</span> <span class="o">*</span> <span class="mi">1</span><span class="nx">e9</span> <span class="o">&gt;&gt;&gt;</span> <span class="mi">0</span><span class="p">),</span>
<span class="nx">counter</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">Date</span><span class="p">().</span><span class="nx">getTime</span><span class="p">()</span> <span class="o">%</span> <span class="mi">1</span><span class="nx">e9</span><span class="p">,</span>
<span class="nx">mascot</span> <span class="o">=</span> <span class="p">{};</span>
<span class="nx">PrivateMap</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
<span class="k">this</span><span class="p">.</span><span class="nx">name</span> <span class="o">=</span> <span class="nx">HIDDEN_PREFIX</span> <span class="o">+</span> <span class="p">(</span><span class="nb">Math</span><span class="p">.</span><span class="nx">random</span><span class="p">()</span> <span class="o">*</span> <span class="mi">1</span><span class="nx">e9</span> <span class="o">&gt;&gt;&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="nx">counter</span><span class="o">++</span> <span class="o">+</span> <span class="s1">&#39;__&#39;</span><span class="p">);</span>
<span class="p">};</span>
<span class="nx">PrivateMap</span><span class="p">.</span><span class="nx">prototype</span> <span class="o">=</span> <span class="p">{</span>
<span class="nx">has</span><span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">key</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">key</span> <span class="o">&amp;&amp;</span> <span class="nx">key</span><span class="p">.</span><span class="nx">hasOwnProperty</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">name</span><span class="p">);</span>
<span class="p">},</span>
<span class="nx">get</span><span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">key</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">value</span> <span class="o">=</span> <span class="nx">key</span> <span class="o">&amp;&amp;</span> <span class="nx">key</span><span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">name</span><span class="p">];</span>
<span class="k">return</span> <span class="nx">value</span> <span class="o">===</span> <span class="nx">mascot</span> <span class="o">?</span> <span class="kc">undefined</span> <span class="o">:</span> <span class="nx">value</span><span class="p">;</span>
<span class="p">},</span>
<span class="nx">set</span><span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">key</span><span class="p">,</span> <span class="nx">value</span><span class="p">)</span> <span class="p">{</span>
<span class="nb">Object</span><span class="p">.</span><span class="nx">defineProperty</span><span class="p">(</span><span class="nx">key</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">name</span><span class="p">,</span> <span class="p">{</span>
<span class="nx">value</span> <span class="o">:</span> <span class="k">typeof</span> <span class="nx">value</span> <span class="o">===</span> <span class="s1">&#39;undefined&#39;</span> <span class="o">?</span> <span class="nx">mascot</span> <span class="o">:</span> <span class="nx">value</span><span class="p">,</span>
<span class="nx">enumerable</span><span class="o">:</span> <span class="kc">false</span><span class="p">,</span>
<span class="nx">writable</span> <span class="o">:</span> <span class="kc">true</span><span class="p">,</span>
<span class="nx">configurable</span><span class="o">:</span> <span class="kc">true</span>
<span class="p">});</span>
<span class="p">},</span>
<span class="s1">&#39;delete&#39;</span><span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">key</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="k">delete</span> <span class="nx">key</span><span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">name</span><span class="p">];</span>
<span class="p">}</span>
<span class="p">};</span>
<span class="kd">var</span> <span class="nx">getOwnPropertyName</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">getOwnPropertyNames</span><span class="p">;</span>
<span class="nb">Object</span><span class="p">.</span><span class="nx">defineProperty</span><span class="p">(</span><span class="nb">Object</span><span class="p">,</span> <span class="s1">&#39;getOwnPropertyNames&#39;</span><span class="p">,</span> <span class="p">{</span>
<span class="nx">value</span><span class="o">:</span> <span class="kd">function</span> <span class="nx">fakeGetOwnPropertyNames</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">getOwnPropertyName</span><span class="p">(</span><span class="nx">obj</span><span class="p">).</span><span class="nx">filter</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">name</span><span class="p">.</span><span class="nx">substr</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nx">HIDDEN_PREFIX</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span> <span class="o">!==</span> <span class="nx">HIDDEN_PREFIX</span><span class="p">;</span>
<span class="p">});</span>
<span class="p">},</span>
<span class="nx">writable</span><span class="o">:</span> <span class="kc">true</span><span class="p">,</span>
<span class="nx">enumerable</span><span class="o">:</span> <span class="kc">false</span><span class="p">,</span>
<span class="nx">configurable</span><span class="o">:</span> <span class="kc">true</span>
<span class="p">});</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">&#182;</a> </div> <h2>Internal Properties</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">&#182;</a> </div> <p>An ordered list used to provide a deterministic ordering in which callbacks are called.
<a href="http://wiki.ecmascript.org/doku.php?id=harmony:observe_internals#observercallbacks">Corresponding Section in ECMAScript wiki</a></p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">observerCallbacks</span> <span class="o">=</span> <span class="p">[];</span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">&#182;</a> </div> <p>This object is used as the prototype of all the notifiers that are returned by Object.getNotifier(O).
<a href="http://wiki.ecmascript.org/doku.php?id=harmony:observe_internals#notifierprototype">Corresponding Section in ECMAScript wiki</a></p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">NotifierPrototype</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">create</span><span class="p">(</span><span class="nb">Object</span><span class="p">.</span><span class="nx">prototype</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">&#182;</a> </div> <p>Used to store immediate uid reference</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">changeDeliveryImmediateUid</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-14">&#182;</a> </div> <p>Used to schedule a call to _deliverAllChangeRecords</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">function</span> <span class="nx">setUpChangesDelivery</span><span class="p">()</span> <span class="p">{</span>
<span class="nx">clearImmediate</span><span class="p">(</span><span class="nx">changeDeliveryImmediateUid</span><span class="p">);</span>
<span class="nx">changeDeliveryImmediateUid</span> <span class="o">=</span> <span class="nx">setImmediate</span><span class="p">(</span><span class="nx">_deliverAllChangeRecords</span><span class="p">);</span>
<span class="p">}</span>
<span class="nb">Object</span><span class="p">.</span><span class="nx">defineProperty</span><span class="p">(</span><span class="nx">NotifierPrototype</span><span class="p">,</span> <span class="s1">&#39;notify&#39;</span><span class="p">,</span> <span class="p">{</span>
<span class="nx">value</span><span class="o">:</span> <span class="kd">function</span> <span class="nx">notify</span><span class="p">(</span><span class="nx">changeRecord</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">notifier</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="nb">Object</span><span class="p">(</span><span class="nx">notifier</span><span class="p">)</span> <span class="o">!==</span> <span class="nx">notifier</span><span class="p">)</span> <span class="p">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nx">TypeError</span><span class="p">(</span><span class="s1">&#39;this must be an Object, given &#39;</span> <span class="o">+</span> <span class="nx">notifier</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">notifier</span><span class="p">.</span><span class="nx">__target</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="nb">Object</span><span class="p">(</span><span class="nx">changeRecord</span><span class="p">)</span> <span class="o">!==</span> <span class="nx">changeRecord</span><span class="p">)</span> <span class="p">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nx">TypeError</span><span class="p">(</span><span class="s1">&#39;changeRecord must be an Object, given &#39;</span> <span class="o">+</span> <span class="nx">changeRecord</span><span class="p">);</span>
<span class="p">}</span>
<span class="kd">var</span> <span class="nx">type</span> <span class="o">=</span> <span class="nx">changeRecord</span><span class="p">.</span><span class="nx">type</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">type</span> <span class="o">!==</span> <span class="s1">&#39;string&#39;</span><span class="p">)</span> <span class="p">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nx">TypeError</span><span class="p">(</span><span class="s1">&#39;changeRecord.type must be a string, given &#39;</span> <span class="o">+</span> <span class="nx">type</span><span class="p">);</span>
<span class="p">}</span>
<span class="kd">var</span> <span class="nx">changeObservers</span> <span class="o">=</span> <span class="nx">changeObserversMap</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">notifier</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">changeObservers</span> <span class="o">||</span> <span class="nx">changeObservers</span><span class="p">.</span><span class="nx">length</span> <span class="o">===</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span><span class="p">;</span>
<span class="p">}</span>
<span class="kd">var</span> <span class="nx">target</span> <span class="o">=</span> <span class="nx">notifier</span><span class="p">.</span><span class="nx">__target</span><span class="p">,</span>
<span class="nx">newRecord</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">create</span><span class="p">(</span><span class="nb">Object</span><span class="p">.</span><span class="nx">prototype</span><span class="p">,</span> <span class="p">{</span>
<span class="s1">&#39;object&#39;</span><span class="o">:</span> <span class="p">{</span>
<span class="nx">value</span><span class="o">:</span> <span class="nx">target</span><span class="p">,</span>
<span class="nx">writable</span> <span class="o">:</span> <span class="kc">false</span><span class="p">,</span>
<span class="nx">enumerable</span> <span class="o">:</span> <span class="kc">true</span><span class="p">,</span>
<span class="nx">configurable</span><span class="o">:</span> <span class="kc">false</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">prop</span> <span class="k">in</span> <span class="nx">changeRecord</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">prop</span> <span class="o">!==</span> <span class="s1">&#39;object&#39;</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">value</span> <span class="o">=</span> <span class="nx">changeRecord</span><span class="p">[</span><span class="nx">prop</span><span class="p">];</span>
<span class="nb">Object</span><span class="p">.</span><span class="nx">defineProperty</span><span class="p">(</span><span class="nx">newRecord</span><span class="p">,</span> <span class="nx">prop</span><span class="p">,</span> <span class="p">{</span>
<span class="nx">value</span><span class="o">:</span> <span class="nx">value</span><span class="p">,</span>
<span class="nx">writable</span> <span class="o">:</span> <span class="kc">false</span><span class="p">,</span>
<span class="nx">enumerable</span> <span class="o">:</span> <span class="kc">true</span><span class="p">,</span>
<span class="nx">configurable</span><span class="o">:</span> <span class="kc">false</span>
<span class="p">});</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="nb">Object</span><span class="p">.</span><span class="nx">preventExtensions</span><span class="p">(</span><span class="nx">newRecord</span><span class="p">);</span>
<span class="nx">_enqueueChangeRecord</span><span class="p">(</span><span class="nx">notifier</span><span class="p">.</span><span class="nx">__target</span><span class="p">,</span> <span class="nx">newRecord</span><span class="p">);</span>
<span class="p">},</span>
<span class="nx">writable</span><span class="o">:</span> <span class="kc">true</span><span class="p">,</span>
<span class="nx">enumerable</span><span class="o">:</span> <span class="kc">false</span><span class="p">,</span>
<span class="nx">configurable</span> <span class="o">:</span> <span class="kc">true</span>
<span class="p">});</span>
<span class="nb">Object</span><span class="p">.</span><span class="nx">defineProperty</span><span class="p">(</span><span class="nx">NotifierPrototype</span><span class="p">,</span> <span class="s1">&#39;performChange&#39;</span><span class="p">,</span> <span class="p">{</span>
<span class="nx">value</span><span class="o">:</span> <span class="kd">function</span> <span class="nx">performChange</span><span class="p">(</span><span class="nx">changeType</span><span class="p">,</span> <span class="nx">changeFn</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">notifier</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="nb">Object</span><span class="p">(</span><span class="nx">notifier</span><span class="p">)</span> <span class="o">!==</span> <span class="nx">notifier</span><span class="p">)</span> <span class="p">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nx">TypeError</span><span class="p">(</span><span class="s1">&#39;this must be an Object, given &#39;</span> <span class="o">+</span> <span class="nx">notifier</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">notifier</span><span class="p">.</span><span class="nx">__target</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">changeType</span> <span class="o">!==</span> <span class="s1">&#39;string&#39;</span><span class="p">)</span> <span class="p">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nx">TypeError</span><span class="p">(</span><span class="s1">&#39;changeType must be a string given &#39;</span> <span class="o">+</span> <span class="nx">notifier</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">changeFn</span> <span class="o">!==</span> <span class="s1">&#39;function&#39;</span><span class="p">)</span> <span class="p">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nx">TypeError</span><span class="p">(</span><span class="s1">&#39;changeFn must be a function, given &#39;</span> <span class="o">+</span> <span class="nx">changeFn</span><span class="p">);</span>
<span class="p">}</span>
<span class="nx">_beginChange</span><span class="p">(</span><span class="nx">notifier</span><span class="p">.</span><span class="nx">__target</span><span class="p">,</span> <span class="nx">changeType</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">error</span><span class="p">,</span> <span class="nx">changeRecord</span><span class="p">;</span>
<span class="k">try</span> <span class="p">{</span>
<span class="nx">changeRecord</span> <span class="o">=</span> <span class="nx">changeFn</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="kc">undefined</span><span class="p">);</span>
<span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">error</span> <span class="o">=</span> <span class="nx">e</span><span class="p">;</span>
<span class="p">}</span>
<span class="nx">_endChange</span><span class="p">(</span><span class="nx">notifier</span><span class="p">.</span><span class="nx">__target</span><span class="p">,</span> <span class="nx">changeType</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">error</span> <span class="o">!==</span> <span class="s1">&#39;undefined&#39;</span><span class="p">)</span> <span class="p">{</span>
<span class="k">throw</span> <span class="nx">error</span><span class="p">;</span>
<span class="p">}</span>
<span class="kd">var</span> <span class="nx">changeObservers</span> <span class="o">=</span> <span class="nx">changeObserversMap</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">notifier</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">changeObservers</span><span class="p">.</span><span class="nx">length</span> <span class="o">===</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span><span class="p">;</span>
<span class="p">}</span>
<span class="kd">var</span> <span class="nx">target</span> <span class="o">=</span> <span class="nx">notifier</span><span class="p">.</span><span class="nx">__target</span><span class="p">,</span>
<span class="nx">newRecord</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">create</span><span class="p">(</span><span class="nb">Object</span><span class="p">.</span><span class="nx">prototype</span><span class="p">,</span> <span class="p">{</span>
<span class="s1">&#39;object&#39;</span><span class="o">:</span> <span class="p">{</span>
<span class="nx">value</span><span class="o">:</span> <span class="nx">target</span><span class="p">,</span>
<span class="nx">writable</span> <span class="o">:</span> <span class="kc">false</span><span class="p">,</span>
<span class="nx">enumerable</span> <span class="o">:</span> <span class="kc">true</span><span class="p">,</span>
<span class="nx">configurable</span><span class="o">:</span> <span class="kc">false</span>
<span class="p">},</span>
<span class="s1">&#39;type&#39;</span><span class="o">:</span> <span class="p">{</span>
<span class="nx">value</span><span class="o">:</span> <span class="nx">changeType</span><span class="p">,</span>
<span class="nx">writable</span> <span class="o">:</span> <span class="kc">false</span><span class="p">,</span>
<span class="nx">enumerable</span> <span class="o">:</span> <span class="kc">true</span><span class="p">,</span>
<span class="nx">configurable</span><span class="o">:</span> <span class="kc">false</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">changeRecord</span> <span class="o">!==</span> <span class="s1">&#39;undefined&#39;</span><span class="p">)</span> <span class="p">{</span>
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">prop</span> <span class="k">in</span> <span class="nx">changeRecord</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">prop</span> <span class="o">!==</span> <span class="s1">&#39;object&#39;</span> <span class="o">&amp;&amp;</span> <span class="nx">prop</span> <span class="o">!==</span> <span class="s1">&#39;type&#39;</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">value</span> <span class="o">=</span> <span class="nx">changeRecord</span><span class="p">[</span><span class="nx">prop</span><span class="p">];</span>
<span class="nb">Object</span><span class="p">.</span><span class="nx">defineProperty</span><span class="p">(</span><span class="nx">newRecord</span><span class="p">,</span> <span class="nx">prop</span><span class="p">,</span> <span class="p">{</span>
<span class="nx">value</span><span class="o">:</span> <span class="nx">value</span><span class="p">,</span>
<span class="nx">writable</span> <span class="o">:</span> <span class="kc">false</span><span class="p">,</span>
<span class="nx">enumerable</span> <span class="o">:</span> <span class="kc">true</span><span class="p">,</span>
<span class="nx">configurable</span><span class="o">:</span> <span class="kc">false</span>
<span class="p">});</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="nb">Object</span><span class="p">.</span><span class="nx">preventExtensions</span><span class="p">(</span><span class="nx">newRecord</span><span class="p">);</span>
<span class="nx">_enqueueChangeRecord</span><span class="p">(</span><span class="nx">notifier</span><span class="p">.</span><span class="nx">__target</span><span class="p">,</span> <span class="nx">newRecord</span><span class="p">);</span>
<span class="p">},</span>
<span class="nx">writable</span><span class="o">:</span> <span class="kc">true</span><span class="p">,</span>
<span class="nx">enumerable</span><span class="o">:</span> <span class="kc">false</span><span class="p">,</span>
<span class="nx">configurable</span> <span class="o">:</span> <span class="kc">true</span>
<span class="p">});</span></pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">&#182;</a> </div> <p>Implementation of the internal algorithm 'BeginChange'
described in the proposal.
<a href="http://wiki.ecmascript.org/doku.php?id=harmony:observe_internals#beginchange">Corresponding Section in ECMAScript wiki</a></p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">function</span> <span class="nx">_beginChange</span><span class="p">(</span><span class="nx">object</span><span class="p">,</span> <span class="nx">changeType</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">notifier</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">getNotifier</span><span class="p">(</span><span class="nx">object</span><span class="p">),</span>
<span class="nx">activeChanges</span> <span class="o">=</span> <span class="nx">activeChangesMap</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">notifier</span><span class="p">),</span>
<span class="nx">changeCount</span> <span class="o">=</span> <span class="nx">activeChangesMap</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">notifier</span><span class="p">)[</span><span class="nx">changeType</span><span class="p">];</span>
<span class="nx">activeChanges</span><span class="p">[</span><span class="nx">changeType</span><span class="p">]</span> <span class="o">=</span> <span class="k">typeof</span> <span class="nx">changeCount</span> <span class="o">===</span> <span class="s1">&#39;undefined&#39;</span> <span class="o">?</span> <span class="mi">1</span> <span class="o">:</span> <span class="nx">changeCount</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">&#182;</a> </div> <p>Implementation of the internal algorithm 'EndChange'
described in the proposal.
<a href="http://wiki.ecmascript.org/doku.php?id=harmony:observe_internals#endchange">Corresponding Section in ECMAScript wiki</a></p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">function</span> <span class="nx">_endChange</span><span class="p">(</span><span class="nx">object</span><span class="p">,</span> <span class="nx">changeType</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">notifier</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">getNotifier</span><span class="p">(</span><span class="nx">object</span><span class="p">),</span>
<span class="nx">activeChanges</span> <span class="o">=</span> <span class="nx">activeChangesMap</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">notifier</span><span class="p">),</span>
<span class="nx">changeCount</span> <span class="o">=</span> <span class="nx">activeChangesMap</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">notifier</span><span class="p">)[</span><span class="nx">changeType</span><span class="p">];</span>
<span class="nx">activeChanges</span><span class="p">[</span><span class="nx">changeType</span><span class="p">]</span> <span class="o">=</span> <span class="nx">changeCount</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="o">?</span> <span class="nx">changeCount</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">:</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-17">&#182;</a> </div> <p>Implementation of the internal algorithm 'ShouldDeliverToObserver'
described in the proposal.
<a href="http://wiki.ecmascript.org/doku.php?id=harmony:observe_internals#shoulddelivertoobserver">Corresponding Section in ECMAScript wiki</a></p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">function</span> <span class="nx">_shouldDeliverToObserver</span><span class="p">(</span><span class="nx">activeChanges</span><span class="p">,</span> <span class="nx">acceptList</span><span class="p">,</span> <span class="nx">changeType</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">doesAccept</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">acceptList</span><span class="p">)</span> <span class="p">{</span>
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">l</span> <span class="o">=</span> <span class="nx">acceptList</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">l</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">accept</span> <span class="o">=</span> <span class="nx">acceptList</span><span class="p">[</span><span class="nx">i</span><span class="p">];</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">activeChanges</span><span class="p">[</span><span class="nx">accept</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="kc">false</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">accept</span> <span class="o">===</span> <span class="nx">changeType</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">doesAccept</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nx">doesAccept</span><span class="p">;</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-18">&#182;</a> </div> <p>Map used to store corresponding notifier to an object</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">notifierMap</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">PrivateMap</span><span class="p">(),</span>
<span class="nx">changeObserversMap</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">PrivateMap</span><span class="p">(),</span>
<span class="nx">activeChangesMap</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">PrivateMap</span><span class="p">();</span></pre></div> </td> </tr> <tr id="section-19"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-19">&#182;</a> </div> <p>Implementation of the internal algorithm 'GetNotifier'
described in the proposal.
<a href="http://wiki.ecmascript.org/doku.php?id=harmony:observe_internals#getnotifier">Corresponding Section in ECMAScript wiki</a></p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">function</span> <span class="nx">_getNotifier</span><span class="p">(</span><span class="nx">target</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">notifierMap</span><span class="p">.</span><span class="nx">has</span><span class="p">(</span><span class="nx">target</span><span class="p">))</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">notifier</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">create</span><span class="p">(</span><span class="nx">NotifierPrototype</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-20"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-20">&#182;</a> </div> <p>we does not really need to hide this, since anyway the host object is accessible from outside of the
implementation. we just make it unwritable</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nb">Object</span><span class="p">.</span><span class="nx">defineProperty</span><span class="p">(</span><span class="nx">notifier</span><span class="p">,</span> <span class="s1">&#39;__target&#39;</span><span class="p">,</span> <span class="p">{</span> <span class="nx">value</span> <span class="o">:</span> <span class="nx">target</span> <span class="p">});</span>
<span class="nx">changeObserversMap</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="nx">notifier</span><span class="p">,</span> <span class="p">[]);</span>
<span class="nx">activeChangesMap</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="nx">notifier</span><span class="p">,</span> <span class="p">{});</span>
<span class="nx">notifierMap</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="nx">target</span><span class="p">,</span> <span class="nx">notifier</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nx">notifierMap</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">target</span><span class="p">);</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-21"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-21">&#182;</a> </div> <p>map used to store reference to a list of pending changeRecords
in observer callback.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">pendingChangesMap</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">PrivateMap</span><span class="p">();</span></pre></div> </td> </tr> <tr id="section-22"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-22">&#182;</a> </div> <p>Implementation of the internal algorithm 'EnqueueChangeRecord'
described in the proposal.
<a href="http://wiki.ecmascript.org/doku.php?id=harmony:observe_internals#enqueuechangerecord">Corresponding Section in ECMAScript wiki</a></p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">function</span> <span class="nx">_enqueueChangeRecord</span><span class="p">(</span><span class="nx">object</span><span class="p">,</span> <span class="nx">changeRecord</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">notifier</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">getNotifier</span><span class="p">(</span><span class="nx">object</span><span class="p">),</span>
<span class="nx">changeType</span> <span class="o">=</span> <span class="nx">changeRecord</span><span class="p">.</span><span class="nx">type</span><span class="p">,</span>
<span class="nx">activeChanges</span> <span class="o">=</span> <span class="nx">activeChangesMap</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">notifier</span><span class="p">),</span>
<span class="nx">changeObservers</span> <span class="o">=</span> <span class="nx">changeObserversMap</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">notifier</span><span class="p">);</span>
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">l</span> <span class="o">=</span> <span class="nx">changeObservers</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">l</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">observerRecord</span> <span class="o">=</span> <span class="nx">changeObservers</span><span class="p">[</span><span class="nx">i</span><span class="p">],</span>
<span class="nx">acceptList</span> <span class="o">=</span> <span class="nx">observerRecord</span><span class="p">.</span><span class="nx">accept</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">_shouldDeliverToObserver</span><span class="p">(</span><span class="nx">activeChanges</span><span class="p">,</span> <span class="nx">acceptList</span><span class="p">,</span> <span class="nx">changeType</span><span class="p">))</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">observer</span> <span class="o">=</span> <span class="nx">observerRecord</span><span class="p">.</span><span class="nx">callback</span><span class="p">,</span>
<span class="nx">pendingChangeRecords</span> <span class="o">=</span> <span class="p">[];</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">pendingChangesMap</span><span class="p">.</span><span class="nx">has</span><span class="p">(</span><span class="nx">observer</span><span class="p">))</span> <span class="p">{</span>
<span class="nx">pendingChangesMap</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="nx">observer</span><span class="p">,</span> <span class="nx">pendingChangeRecords</span><span class="p">);</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="nx">pendingChangeRecords</span> <span class="o">=</span> <span class="nx">pendingChangesMap</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">observer</span><span class="p">);</span>
<span class="p">}</span>
<span class="nx">pendingChangeRecords</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">changeRecord</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="nx">setUpChangesDelivery</span><span class="p">();</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-23"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-23">&#182;</a> </div> <p>map used to store a count of associated notifier to a function</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">attachedNotifierCountMap</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">PrivateMap</span><span class="p">();</span></pre></div> </td> </tr> <tr id="section-24"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-24">&#182;</a> </div> <p>Remove reference all reference to an observer callback,
if this one is not used anymore.
In the proposal the ObserverCallBack has a weak reference over observers,
Without this possibility we need to clean this list to avoid memory leak</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">function</span> <span class="nx">_cleanObserver</span><span class="p">(</span><span class="nx">observer</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">attachedNotifierCountMap</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">observer</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="nx">pendingChangesMap</span><span class="p">.</span><span class="nx">has</span><span class="p">(</span><span class="nx">observer</span><span class="p">))</span> <span class="p">{</span>
<span class="nx">attachedNotifierCountMap</span><span class="p">.</span><span class="k">delete</span><span class="p">(</span><span class="nx">observer</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">index</span> <span class="o">=</span> <span class="nx">observerCallbacks</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">observer</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">index</span> <span class="o">!==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">observerCallbacks</span><span class="p">.</span><span class="nx">splice</span><span class="p">(</span><span class="nx">index</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-25"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-25">&#182;</a> </div> <p>Implementation of the internal algorithm 'DeliverChangeRecords'
described in the proposal.
<a href="http://wiki.ecmascript.org/doku.php?id=harmony:observe_internals#deliverchangerecords">Corresponding Section in ECMAScript wiki</a></p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">function</span> <span class="nx">_deliverChangeRecords</span><span class="p">(</span><span class="nx">observer</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">pendingChangeRecords</span> <span class="o">=</span> <span class="nx">pendingChangesMap</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">observer</span><span class="p">);</span>
<span class="nx">pendingChangesMap</span><span class="p">.</span><span class="k">delete</span><span class="p">(</span><span class="nx">observer</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">pendingChangeRecords</span> <span class="o">||</span> <span class="nx">pendingChangeRecords</span><span class="p">.</span><span class="nx">length</span> <span class="o">===</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="kc">false</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">try</span> <span class="p">{</span>
<span class="nx">observer</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="kc">undefined</span><span class="p">,</span> <span class="nx">pendingChangeRecords</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">catch</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="p">{</span> <span class="p">}</span>
<span class="nx">_cleanObserver</span><span class="p">(</span><span class="nx">observer</span><span class="p">);</span>
<span class="k">return</span> <span class="kc">true</span><span class="p">;</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-26"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-26">&#182;</a> </div> <p>Implementation of the internal algorithm 'DeliverAllChangeRecords'
described in the proposal.
<a href="http://wiki.ecmascript.org/doku.php?id=harmony:observe_internals#deliverallchangerecords">Corresponding Section in ECMAScript wiki</a></p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">function</span> <span class="nx">_deliverAllChangeRecords</span><span class="p">()</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">observers</span> <span class="o">=</span> <span class="nx">observerCallbacks</span><span class="p">.</span><span class="nx">slice</span><span class="p">();</span>
<span class="kd">var</span> <span class="nx">anyWorkDone</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">l</span> <span class="o">=</span> <span class="nx">observers</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">l</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">observer</span> <span class="o">=</span> <span class="nx">observers</span><span class="p">[</span><span class="nx">i</span><span class="p">];</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">_deliverChangeRecords</span><span class="p">(</span><span class="nx">observer</span><span class="p">))</span> <span class="p">{</span>
<span class="nx">anyWorkDone</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nx">anyWorkDone</span><span class="p">;</span>
<span class="p">}</span>
<span class="nb">Object</span><span class="p">.</span><span class="nx">defineProperties</span><span class="p">(</span><span class="nb">Object</span><span class="p">,</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-27"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-27">&#182;</a> </div> <p>Implementation of the public api 'Object.observe'
described in the proposal.
<a href="http://wiki.ecmascript.org/doku.php?id=harmony:observe_public_api#object.observe">Corresponding Section in ECMAScript wiki</a></p> </td> <td class="code"> <div class="highlight"><pre> <span class="s1">&#39;observe&#39;</span><span class="o">:</span> <span class="p">{</span>
<span class="nx">value</span><span class="o">:</span> <span class="kd">function</span> <span class="nx">observe</span><span class="p">(</span><span class="nx">target</span><span class="p">,</span> <span class="nx">callback</span><span class="p">,</span> <span class="nx">accept</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nb">Object</span><span class="p">(</span><span class="nx">target</span><span class="p">)</span> <span class="o">!==</span> <span class="nx">target</span><span class="p">)</span> <span class="p">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nx">TypeError</span><span class="p">(</span><span class="s1">&#39;target must be an Object, given &#39;</span> <span class="o">+</span> <span class="nx">target</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">callback</span> <span class="o">!==</span> <span class="s1">&#39;function&#39;</span><span class="p">)</span> <span class="p">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nx">TypeError</span><span class="p">(</span><span class="s1">&#39;observer must be a function, given &#39;</span> <span class="o">+</span> <span class="nx">callback</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="nb">Object</span><span class="p">.</span><span class="nx">isFrozen</span><span class="p">(</span><span class="nx">callback</span><span class="p">))</span> <span class="p">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nx">TypeError</span><span class="p">(</span><span class="s1">&#39;observer cannot be frozen&#39;</span><span class="p">);</span>
<span class="p">}</span>
<span class="kd">var</span> <span class="nx">acceptList</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">accept</span> <span class="o">===</span> <span class="s1">&#39;undefined&#39;</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">acceptList</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;add&#39;</span><span class="p">,</span> <span class="s1">&#39;update&#39;</span><span class="p">,</span> <span class="s1">&#39;delete&#39;</span><span class="p">,</span> <span class="s1">&#39;reconfigure&#39;</span><span class="p">,</span> <span class="s1">&#39;setPrototype&#39;</span><span class="p">,</span> <span class="s1">&#39;preventExtensions&#39;</span><span class="p">];</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nb">Object</span><span class="p">(</span><span class="nx">accept</span><span class="p">)</span> <span class="o">!==</span> <span class="nx">accept</span><span class="p">)</span> <span class="p">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nx">TypeError</span><span class="p">(</span><span class="s1">&#39;accept must be an object, given &#39;</span> <span class="o">+</span> <span class="nx">accept</span><span class="p">);</span>
<span class="p">}</span>
<span class="kd">var</span> <span class="nx">len</span> <span class="o">=</span> <span class="nx">accept</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">len</span> <span class="o">!==</span> <span class="s1">&#39;number&#39;</span> <span class="o">||</span> <span class="nx">len</span> <span class="o">&gt;&gt;&gt;</span> <span class="mi">0</span> <span class="o">!==</span> <span class="nx">len</span> <span class="o">||</span> <span class="nx">len</span> <span class="o">&lt;</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nx">TypeError</span><span class="p">(</span><span class="s1">&#39;the \&#39;length\&#39; property of accept must be a positive integer, given &#39;</span> <span class="o">+</span> <span class="nx">len</span><span class="p">);</span>
<span class="p">}</span>
<span class="kd">var</span> <span class="nx">nextIndex</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="nx">acceptList</span> <span class="o">=</span> <span class="p">[];</span>
<span class="k">while</span> <span class="p">(</span><span class="nx">nextIndex</span> <span class="o">&lt;</span> <span class="nx">len</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">next</span> <span class="o">=</span> <span class="nx">accept</span><span class="p">[</span><span class="nx">nextIndex</span><span class="p">];</span>
<span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">next</span> <span class="o">!==</span> <span class="s1">&#39;string&#39;</span><span class="p">)</span> <span class="p">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nx">TypeError</span><span class="p">(</span><span class="s1">&#39;accept must contains only string, given&#39;</span> <span class="o">+</span> <span class="nx">next</span><span class="p">);</span>
<span class="p">}</span>
<span class="nx">acceptList</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">next</span><span class="p">);</span>
<span class="nx">nextIndex</span><span class="o">++</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="kd">var</span> <span class="nx">notifier</span> <span class="o">=</span> <span class="nx">_getNotifier</span><span class="p">(</span><span class="nx">target</span><span class="p">),</span>
<span class="nx">changeObservers</span> <span class="o">=</span> <span class="nx">changeObserversMap</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">notifier</span><span class="p">);</span>
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">l</span> <span class="o">=</span> <span class="nx">changeObservers</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">l</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">changeObservers</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">callback</span> <span class="o">===</span> <span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">changeObservers</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">accept</span> <span class="o">=</span> <span class="nx">acceptList</span><span class="p">;</span>
<span class="k">return</span> <span class="nx">target</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="nx">changeObservers</span><span class="p">.</span><span class="nx">push</span><span class="p">({</span>
<span class="nx">callback</span><span class="o">:</span> <span class="nx">callback</span><span class="p">,</span>
<span class="nx">accept</span><span class="o">:</span> <span class="nx">acceptList</span>
<span class="p">});</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">observerCallbacks</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="o">===</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">observerCallbacks</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">callback</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">attachedNotifierCountMap</span><span class="p">.</span><span class="nx">has</span><span class="p">(</span><span class="nx">callback</span><span class="p">))</span> <span class="p">{</span>
<span class="nx">attachedNotifierCountMap</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="nx">callback</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="nx">attachedNotifierCountMap</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="nx">callback</span><span class="p">,</span> <span class="nx">attachedNotifierCountMap</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nx">target</span><span class="p">;</span>
<span class="p">},</span>
<span class="nx">writable</span><span class="o">:</span> <span class="kc">true</span><span class="p">,</span>
<span class="nx">configurable</span><span class="o">:</span> <span class="kc">true</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-28"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-28">&#182;</a> </div> <p>Implementation of the public api 'Object.unobseve'
described in the proposal.
<a href="http://wiki.ecmascript.org/doku.php?id=harmony:observe_public_api#object.unobseve">Corresponding Section in ECMAScript wiki</a></p> </td> <td class="code"> <div class="highlight"><pre> <span class="s1">&#39;unobserve&#39;</span><span class="o">:</span> <span class="p">{</span>
<span class="nx">value</span><span class="o">:</span> <span class="kd">function</span> <span class="nx">unobserve</span><span class="p">(</span><span class="nx">target</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nb">Object</span><span class="p">(</span><span class="nx">target</span><span class="p">)</span> <span class="o">!==</span> <span class="nx">target</span><span class="p">)</span> <span class="p">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nx">TypeError</span><span class="p">(</span><span class="s1">&#39;target must be an Object, given &#39;</span> <span class="o">+</span> <span class="nx">target</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">callback</span> <span class="o">!==</span> <span class="s1">&#39;function&#39;</span><span class="p">)</span> <span class="p">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nx">TypeError</span><span class="p">(</span><span class="s1">&#39;observer must be a function, given &#39;</span> <span class="o">+</span> <span class="nx">callback</span><span class="p">);</span>
<span class="p">}</span>
<span class="kd">var</span> <span class="nx">notifier</span> <span class="o">=</span> <span class="nx">_getNotifier</span><span class="p">(</span><span class="nx">target</span><span class="p">),</span>
<span class="nx">changeObservers</span> <span class="o">=</span> <span class="nx">changeObserversMap</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">notifier</span><span class="p">);</span>
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">l</span> <span class="o">=</span> <span class="nx">changeObservers</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">l</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">changeObservers</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">callback</span> <span class="o">===</span> <span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">changeObservers</span><span class="p">.</span><span class="nx">splice</span><span class="p">(</span><span class="nx">i</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>
<span class="nx">attachedNotifierCountMap</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="nx">callback</span><span class="p">,</span> <span class="nx">attachedNotifierCountMap</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">);</span>
<span class="nx">_cleanObserver</span><span class="p">(</span><span class="nx">callback</span><span class="p">);</span>
<span class="k">break</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nx">target</span><span class="p">;</span>
<span class="p">},</span>
<span class="nx">writable</span><span class="o">:</span> <span class="kc">true</span><span class="p">,</span>
<span class="nx">configurable</span><span class="o">:</span> <span class="kc">true</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-29"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-29">&#182;</a> </div> <p>Implementation of the public api 'Object.deliverChangeRecords'
described in the proposal.
<a href="http://wiki.ecmascript.org/doku.php?id=harmony:observe_public_api#object.deliverchangerecords">Corresponding Section in ECMAScript wiki</a></p> </td> <td class="code"> <div class="highlight"><pre> <span class="s1">&#39;deliverChangeRecords&#39;</span><span class="o">:</span> <span class="p">{</span>
<span class="nx">value</span><span class="o">:</span> <span class="kd">function</span> <span class="nx">deliverChangeRecords</span><span class="p">(</span><span class="nx">observer</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">observer</span> <span class="o">!==</span> <span class="s1">&#39;function&#39;</span><span class="p">)</span> <span class="p">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nx">TypeError</span><span class="p">(</span><span class="s1">&#39;callback must be a function, given &#39;</span> <span class="o">+</span> <span class="nx">observer</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">while</span> <span class="p">(</span><span class="nx">_deliverChangeRecords</span><span class="p">(</span><span class="nx">observer</span><span class="p">))</span> <span class="p">{}</span>
<span class="p">},</span>
<span class="nx">writable</span><span class="o">:</span> <span class="kc">true</span><span class="p">,</span>
<span class="nx">configurable</span><span class="o">:</span> <span class="kc">true</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-30"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-30">&#182;</a> </div> <p>Implementation of the public api 'Object.getNotifier'
described in the proposal.
<a href="http://wiki.ecmascript.org/doku.php?id=harmony:observe_public_api#object.getnotifier">Corresponding Section in ECMAScript wiki</a></p> </td> <td class="code"> <div class="highlight"><pre> <span class="s1">&#39;getNotifier&#39;</span><span class="o">:</span> <span class="p">{</span>
<span class="nx">value</span><span class="o">:</span> <span class="kd">function</span> <span class="nx">getNotifier</span><span class="p">(</span><span class="nx">target</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nb">Object</span><span class="p">(</span><span class="nx">target</span><span class="p">)</span> <span class="o">!==</span> <span class="nx">target</span><span class="p">)</span> <span class="p">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nx">TypeError</span><span class="p">(</span><span class="s1">&#39;target must be an Object, given &#39;</span> <span class="o">+</span> <span class="nx">target</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="nb">Object</span><span class="p">.</span><span class="nx">isFrozen</span><span class="p">(</span><span class="nx">target</span><span class="p">))</span> <span class="p">{</span>
<span class="k">return</span> <span class="kc">null</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nx">_getNotifier</span><span class="p">(</span><span class="nx">target</span><span class="p">);</span>
<span class="p">},</span>
<span class="nx">writable</span><span class="o">:</span> <span class="kc">true</span><span class="p">,</span>
<span class="nx">configurable</span><span class="o">:</span> <span class="kc">true</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="p">})(</span><span class="k">typeof</span> <span class="nx">global</span> <span class="o">!==</span> <span class="s1">&#39;undefined&#39;</span> <span class="o">?</span> <span class="nx">global</span> <span class="o">:</span> <span class="k">this</span><span class="p">);</span>
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>

View File

@@ -0,0 +1,519 @@
// Copyright 2012 Kap IT (http://www.kapit.fr/)
//
// Licensed under the Apache License, Version 2.0 (the 'License');
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Author : François de Campredon (http://francois.de-campredon.fr/),
// Object.observe Shim
// ===================
// *See [The harmony proposal page](http://wiki.ecmascript.org/doku.php?id=harmony:observe)*
(function (global) {
'use strict';
if (typeof Object.observe === 'function') {
return;
}
// Utilities
// ---------
// setImmediate shim used to deliver changes records asynchronously
// use setImmediate if available
var setImmediate = global.setImmediate || global.msSetImmediate,
clearImmediate = global.clearImmediate || global.msClearImmediate;
if (!setImmediate) {
// fallback on setTimeout if not
setImmediate = function (func, args) {
return setTimeout(func, 0, args);
};
clearImmediate = function (id) {
clearTimeout(id);
};
}
// WeakMap
// -------
var PrivateMap;
if (typeof WeakMap !== 'undefined') {
//use weakmap if defined
PrivateMap = WeakMap;
} else {
//else use ses like shim of WeakMap
var HIDDEN_PREFIX = '__weakmap:' + (Math.random() * 1e9 >>> 0),
counter = new Date().getTime() % 1e9,
mascot = {};
PrivateMap = function () {
this.name = HIDDEN_PREFIX + (Math.random() * 1e9 >>> 0) + (counter++ + '__');
};
PrivateMap.prototype = {
has: function (key) {
return key && key.hasOwnProperty(this.name);
},
get: function (key) {
var value = key && key[this.name];
return value === mascot ? undefined : value;
},
set: function (key, value) {
Object.defineProperty(key, this.name, {
value : typeof value === 'undefined' ? mascot : value,
enumerable: false,
writable : true,
configurable: true
});
},
'delete': function (key) {
return delete key[this.name];
}
};
var getOwnPropertyName = Object.getOwnPropertyNames;
Object.defineProperty(Object, 'getOwnPropertyNames', {
value: function fakeGetOwnPropertyNames(obj) {
return getOwnPropertyName(obj).filter(function (name) {
return name.substr(0, HIDDEN_PREFIX.length) !== HIDDEN_PREFIX;
});
},
writable: true,
enumerable: false,
configurable: true
});
}
// Internal Properties
// -------------------
// An ordered list used to provide a deterministic ordering in which callbacks are called.
// [Corresponding Section in ECMAScript wiki](http://wiki.ecmascript.org/doku.php?id=harmony:observe_internals#observercallbacks)
var observerCallbacks = [];
// This object is used as the prototype of all the notifiers that are returned by Object.getNotifier(O).
// [Corresponding Section in ECMAScript wiki](http://wiki.ecmascript.org/doku.php?id=harmony:observe_internals#notifierprototype)
var NotifierPrototype = Object.create(Object.prototype);
// Used to store immediate uid reference
var changeDeliveryImmediateUid;
// Used to schedule a call to _deliverAllChangeRecords
function setUpChangesDelivery() {
clearImmediate(changeDeliveryImmediateUid);
changeDeliveryImmediateUid = setImmediate(_deliverAllChangeRecords);
}
Object.defineProperty(NotifierPrototype, 'notify', {
value: function notify(changeRecord) {
var notifier = this;
if (Object(notifier) !== notifier) {
throw new TypeError('this must be an Object, given ' + notifier);
}
if (!notifier.__target) {
return;
}
if (Object(changeRecord) !== changeRecord) {
throw new TypeError('changeRecord must be an Object, given ' + changeRecord);
}
var type = changeRecord.type;
if (typeof type !== 'string') {
throw new TypeError('changeRecord.type must be a string, given ' + type);
}
var changeObservers = changeObserversMap.get(notifier);
if (!changeObservers || changeObservers.length === 0) {
return;
}
var target = notifier.__target,
newRecord = Object.create(Object.prototype, {
'object': {
value: target,
writable : false,
enumerable : true,
configurable: false
}
});
for (var prop in changeRecord) {
if (prop !== 'object') {
var value = changeRecord[prop];
Object.defineProperty(newRecord, prop, {
value: value,
writable : false,
enumerable : true,
configurable: false
});
}
}
Object.preventExtensions(newRecord);
_enqueueChangeRecord(notifier.__target, newRecord);
},
writable: true,
enumerable: false,
configurable : true
});
Object.defineProperty(NotifierPrototype, 'performChange', {
value: function performChange(changeType, changeFn) {
var notifier = this;
if (Object(notifier) !== notifier) {
throw new TypeError('this must be an Object, given ' + notifier);
}
if (!notifier.__target) {
return;
}
if (typeof changeType !== 'string') {
throw new TypeError('changeType must be a string given ' + notifier);
}
if (typeof changeFn !== 'function') {
throw new TypeError('changeFn must be a function, given ' + changeFn);
}
_beginChange(notifier.__target, changeType);
var error, changeRecord;
try {
changeRecord = changeFn.call(undefined);
} catch (e) {
error = e;
}
_endChange(notifier.__target, changeType);
if (typeof error !== 'undefined') {
throw error;
}
var changeObservers = changeObserversMap.get(notifier);
if (changeObservers.length === 0) {
return;
}
var target = notifier.__target,
newRecord = Object.create(Object.prototype, {
'object': {
value: target,
writable : false,
enumerable : true,
configurable: false
},
'type': {
value: changeType,
writable : false,
enumerable : true,
configurable: false
}
});
if (typeof changeRecord !== 'undefined') {
for (var prop in changeRecord) {
if (prop !== 'object' && prop !== 'type') {
var value = changeRecord[prop];
Object.defineProperty(newRecord, prop, {
value: value,
writable : false,
enumerable : true,
configurable: false
});
}
}
}
Object.preventExtensions(newRecord);
_enqueueChangeRecord(notifier.__target, newRecord);
},
writable: true,
enumerable: false,
configurable : true
});
// Implementation of the internal algorithm 'BeginChange'
// described in the proposal.
// [Corresponding Section in ECMAScript wiki](http://wiki.ecmascript.org/doku.php?id=harmony:observe_internals#beginchange)
function _beginChange(object, changeType) {
var notifier = Object.getNotifier(object),
activeChanges = activeChangesMap.get(notifier),
changeCount = activeChangesMap.get(notifier)[changeType];
activeChanges[changeType] = typeof changeCount === 'undefined' ? 1 : changeCount + 1;
}
// Implementation of the internal algorithm 'EndChange'
// described in the proposal.
// [Corresponding Section in ECMAScript wiki](http://wiki.ecmascript.org/doku.php?id=harmony:observe_internals#endchange)
function _endChange(object, changeType) {
var notifier = Object.getNotifier(object),
activeChanges = activeChangesMap.get(notifier),
changeCount = activeChangesMap.get(notifier)[changeType];
activeChanges[changeType] = changeCount > 0 ? changeCount - 1 : 0;
}
// Implementation of the internal algorithm 'ShouldDeliverToObserver'
// described in the proposal.
// [Corresponding Section in ECMAScript wiki](http://wiki.ecmascript.org/doku.php?id=harmony:observe_internals#shoulddelivertoobserver)
function _shouldDeliverToObserver(activeChanges, acceptList, changeType) {
var doesAccept = false;
if (acceptList) {
for (var i = 0, l = acceptList.length; i < l; i++) {
var accept = acceptList[i];
if (activeChanges[accept] > 0) {
return false;
}
if (accept === changeType) {
doesAccept = true;
}
}
}
return doesAccept;
}
// Map used to store corresponding notifier to an object
var notifierMap = new PrivateMap(),
changeObserversMap = new PrivateMap(),
activeChangesMap = new PrivateMap();
// Implementation of the internal algorithm 'GetNotifier'
// described in the proposal.
// [Corresponding Section in ECMAScript wiki](http://wiki.ecmascript.org/doku.php?id=harmony:observe_internals#getnotifier)
function _getNotifier(target) {
if (!notifierMap.has(target)) {
var notifier = Object.create(NotifierPrototype);
// we does not really need to hide this, since anyway the host object is accessible from outside of the
// implementation. we just make it unwritable
Object.defineProperty(notifier, '__target', { value : target });
changeObserversMap.set(notifier, []);
activeChangesMap.set(notifier, {});
notifierMap.set(target, notifier);
}
return notifierMap.get(target);
}
// map used to store reference to a list of pending changeRecords
// in observer callback.
var pendingChangesMap = new PrivateMap();
// Implementation of the internal algorithm 'EnqueueChangeRecord'
// described in the proposal.
// [Corresponding Section in ECMAScript wiki](http://wiki.ecmascript.org/doku.php?id=harmony:observe_internals#enqueuechangerecord)
function _enqueueChangeRecord(object, changeRecord) {
var notifier = Object.getNotifier(object),
changeType = changeRecord.type,
activeChanges = activeChangesMap.get(notifier),
changeObservers = changeObserversMap.get(notifier);
for (var i = 0, l = changeObservers.length; i < l; i++) {
var observerRecord = changeObservers[i],
acceptList = observerRecord.accept;
if (_shouldDeliverToObserver(activeChanges, acceptList, changeType)) {
var observer = observerRecord.callback,
pendingChangeRecords = [];
if (!pendingChangesMap.has(observer)) {
pendingChangesMap.set(observer, pendingChangeRecords);
} else {
pendingChangeRecords = pendingChangesMap.get(observer);
}
pendingChangeRecords.push(changeRecord);
}
}
setUpChangesDelivery();
}
// map used to store a count of associated notifier to a function
var attachedNotifierCountMap = new PrivateMap();
// Remove reference all reference to an observer callback,
// if this one is not used anymore.
// In the proposal the ObserverCallBack has a weak reference over observers,
// Without this possibility we need to clean this list to avoid memory leak
function _cleanObserver(observer) {
if (!attachedNotifierCountMap.get(observer) && !pendingChangesMap.has(observer)) {
attachedNotifierCountMap.delete(observer);
var index = observerCallbacks.indexOf(observer);
if (index !== -1) {
observerCallbacks.splice(index, 1);
}
}
}
// Implementation of the internal algorithm 'DeliverChangeRecords'
// described in the proposal.
// [Corresponding Section in ECMAScript wiki](http://wiki.ecmascript.org/doku.php?id=harmony:observe_internals#deliverchangerecords)
function _deliverChangeRecords(observer) {
var pendingChangeRecords = pendingChangesMap.get(observer);
pendingChangesMap.delete(observer);
if (!pendingChangeRecords || pendingChangeRecords.length === 0) {
return false;
}
try {
observer.call(undefined, pendingChangeRecords);
}
catch (e) { }
_cleanObserver(observer);
return true;
}
// Implementation of the internal algorithm 'DeliverAllChangeRecords'
// described in the proposal.
// [Corresponding Section in ECMAScript wiki](http://wiki.ecmascript.org/doku.php?id=harmony:observe_internals#deliverallchangerecords)
function _deliverAllChangeRecords() {
var observers = observerCallbacks.slice();
var anyWorkDone = false;
for (var i = 0, l = observers.length; i < l; i++) {
var observer = observers[i];
if (_deliverChangeRecords(observer)) {
anyWorkDone = true;
}
}
return anyWorkDone;
}
Object.defineProperties(Object, {
// Implementation of the public api 'Object.observe'
// described in the proposal.
// [Corresponding Section in ECMAScript wiki](http://wiki.ecmascript.org/doku.php?id=harmony:observe_public_api#object.observe)
'observe': {
value: function observe(target, callback, accept) {
if (Object(target) !== target) {
throw new TypeError('target must be an Object, given ' + target);
}
if (typeof callback !== 'function') {
throw new TypeError('observer must be a function, given ' + callback);
}
if (Object.isFrozen(callback)) {
throw new TypeError('observer cannot be frozen');
}
var acceptList;
if (typeof accept === 'undefined') {
acceptList = ['add', 'update', 'delete', 'reconfigure', 'setPrototype', 'preventExtensions'];
} else {
if (Object(accept) !== accept) {
throw new TypeError('accept must be an object, given ' + accept);
}
var len = accept.length;
if (typeof len !== 'number' || len >>> 0 !== len || len < 1) {
throw new TypeError('the \'length\' property of accept must be a positive integer, given ' + len);
}
var nextIndex = 0;
acceptList = [];
while (nextIndex < len) {
var next = accept[nextIndex];
if (typeof next !== 'string') {
throw new TypeError('accept must contains only string, given' + next);
}
acceptList.push(next);
nextIndex++;
}
}
var notifier = _getNotifier(target),
changeObservers = changeObserversMap.get(notifier);
for (var i = 0, l = changeObservers.length; i < l; i++) {
if (changeObservers[i].callback === callback) {
changeObservers[i].accept = acceptList;
return target;
}
}
changeObservers.push({
callback: callback,
accept: acceptList
});
if (observerCallbacks.indexOf(callback) === -1) {
observerCallbacks.push(callback);
}
if (!attachedNotifierCountMap.has(callback)) {
attachedNotifierCountMap.set(callback, 1);
} else {
attachedNotifierCountMap.set(callback, attachedNotifierCountMap.get(callback) + 1);
}
return target;
},
writable: true,
configurable: true
},
// Implementation of the public api 'Object.unobseve'
// described in the proposal.
// [Corresponding Section in ECMAScript wiki](http://wiki.ecmascript.org/doku.php?id=harmony:observe_public_api#object.unobseve)
'unobserve': {
value: function unobserve(target, callback) {
if (Object(target) !== target) {
throw new TypeError('target must be an Object, given ' + target);
}
if (typeof callback !== 'function') {
throw new TypeError('observer must be a function, given ' + callback);
}
var notifier = _getNotifier(target),
changeObservers = changeObserversMap.get(notifier);
for (var i = 0, l = changeObservers.length; i < l; i++) {
if (changeObservers[i].callback === callback) {
changeObservers.splice(i, 1);
attachedNotifierCountMap.set(callback, attachedNotifierCountMap.get(callback) - 1);
_cleanObserver(callback);
break;
}
}
return target;
},
writable: true,
configurable: true
},
// Implementation of the public api 'Object.deliverChangeRecords'
// described in the proposal.
// [Corresponding Section in ECMAScript wiki](http://wiki.ecmascript.org/doku.php?id=harmony:observe_public_api#object.deliverchangerecords)
'deliverChangeRecords': {
value: function deliverChangeRecords(observer) {
if (typeof observer !== 'function') {
throw new TypeError('callback must be a function, given ' + observer);
}
while (_deliverChangeRecords(observer)) {}
},
writable: true,
configurable: true
},
// Implementation of the public api 'Object.getNotifier'
// described in the proposal.
// [Corresponding Section in ECMAScript wiki](http://wiki.ecmascript.org/doku.php?id=harmony:observe_public_api#object.getnotifier)
'getNotifier': {
value: function getNotifier(target) {
if (Object(target) !== target) {
throw new TypeError('target must be an Object, given ' + target);
}
if (Object.isFrozen(target)) {
return null;
}
return _getNotifier(target);
},
writable: true,
configurable: true
}
});
})(typeof global !== 'undefined' ? global : this);

View File

@@ -0,0 +1,46 @@
{
"name": "observe-shim",
"description": "An Object.observe harmony proposal shim",
"version": "0.4.2",
"keywords": [
"observe",
"data binding"
],
"author": {
"name": "Kap IT",
"email": "contact@kapit.fr",
"url": "http://www.kapit.fr/"
},
"contributors": [
{
"name": "François de Campredon",
"email": "fdecampredon@kapit.fr",
"url": "http://francois.de-campredon.fr/"
}
],
"repository": {
"type": "git",
"url": "https://github.com/kapit/observe-shim.git"
},
"main": "lib/observe-shim.js",
"scripts": {
"test": "grunt test"
},
"dependencies": {},
"devDependencies": {
"matchdep": "~0.1.1",
"grunt": "~0.4.0",
"grunt-contrib-jshint": "~0.1.1",
"grunt-mocha": "~0.4.1",
"grunt-docco": "~0.2.0",
"grunt-contrib-clean": "~0.4.0",
"mocha": "~1.12.0",
"sinon": "~1.7.3",
"expect.js": "~0.2.0",
"grunt-mocha-test": "~0.5.0"
},
"licenses": {
"type": "Apache-2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0"
}
}

View File

@@ -0,0 +1,563 @@
// Copyright 2012 Kap IT (http://www.kapit.fr/)
//
// Licensed under the Apache License, Version 2.0 (the 'License');
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Author : François de Campredon (http://francois.de-campredon.fr/),
/*global describe, it, expect , beforeEach, afterEach, sinon*/
describe('Observe.observe harmony proposal shim', function () {
'use strict';
function testIsObject(testFunc) {
expect(function () {
testFunc(5);
}).to.throwException(function (e) {
expect(e).to.be.a(TypeError);
});
expect(function () {
testFunc('string');
}).to.throwException(function (e) {
expect(e).to.be.a(TypeError);
});
expect(function () {
testFunc(NaN);
}).to.throwException(function (e) {
expect(e).to.be.a(TypeError);
});
expect(function () {
testFunc(null);
}).to.throwException(function (e) {
expect(e).to.be.a(TypeError);
});
expect(function () {
testFunc(undefined);
}).to.throwException(function (e) {
expect(e).to.be.a(TypeError);
});
expect(function () {
testFunc({});
}).to.not.throwException();
expect(function () {
testFunc(function () {});
}).to.not.throwException();
}
function testIsCallable(testFunc) {
expect(function () {
testFunc(5);
}).to.throwException(function (e) {
expect(e).to.be.a(TypeError);
});
expect(function () {
testFunc('string');
}).to.throwException(function (e) {
expect(e).to.be.a(TypeError);
});
expect(function () {
testFunc(NaN);
}).to.throwException(function (e) {
expect(e).to.be.a(TypeError);
});
expect(function () {
testFunc(null);
}).to.throwException(function (e) {
expect(e).to.be.a(TypeError);
});
expect(function () {
testFunc(undefined);
}).to.throwException(function (e) {
expect(e).to.be.a(TypeError);
});
expect(function () {
testFunc({});
}).to.throwException(function (e) {
expect(e).to.be.a(TypeError);
});
expect(function () {
//tricks for jshint
var Fn = Function;
testFunc(new Fn(''));
}).to.not.throwException();
expect(function () {
testFunc(function () {});
}).to.not.throwException();
}
describe('Object.observe', function () {
it('should throw an error when passing an non object at first parameter', function () {
testIsObject(function (target) {
Object.observe(target, function () { });
});
});
it('should throw an error when second parameter is not callable', function () {
testIsCallable(function (observer) {
Object.observe({}, observer);
});
});
it('should throw an error when second parameter is frozen', function () {
var observer = function () { };
Object.freeze(observer);
expect(function () {
Object.observe({}, observer);
}).to.throwException(function (e) {
expect(e).to.be.a(TypeError);
});
});
it('should throw an error when third parameter is defined and is not an array like of string', function () {
expect(function () {
Object.observe({}, function () {}, {});
}).to.throwException(function (e) {
expect(e).to.be.a(TypeError);
});
expect(function () {
Object.observe({}, function () {}, [5, {}]);
}).to.throwException(function (e) {
expect(e).to.be.a(TypeError);
});
expect(function () {
Object.observe({}, function () {}, ['hello', '']);
}).to.not.throwException();
});
});
describe('Object.unobserve', function () {
it('should throw an error when passing an non object at first parameter', function () {
testIsObject(function (target) {
Object.unobserve(target, function () { });
});
});
it('should throw an error when second parameter is not callable', function () {
testIsCallable(function (observer) {
Object.unobserve({}, observer);
});
});
});
describe('Object.getNotifier', function () {
it('should throw an error when passing an non object at first parameter', function () {
testIsObject(function (target) {
Object.getNotifier(target);
});
});
it('should return a notifier with a "notify" function, configurable, writable and not enumerable', function () {
var notifier = Object.getNotifier({}),
notifyDesc = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(notifier), 'notify');
expect(notifyDesc).to.be.ok();
expect(notifyDesc.value).to.be.a('function');
expect(notifyDesc.configurable).to.be.ok();
expect(notifyDesc.writable).to.be.ok();
expect(notifyDesc.enumerable).not.to.be.ok();
});
it('should return a unique notifier for a given object', function () {
var obj = {},
notifier = Object.getNotifier(obj),
notifier1 = Object.getNotifier(obj);
expect(notifier).to.be.equal(notifier1);
});
});
describe('Object.deliverChangeRecords', function () {
it('should throw an error when passing an non object at first parameter', function () {
testIsCallable(function (observer) {
Object.deliverChangeRecords(observer);
});
});
});
describe('Notifier.notify', function () {
var notifier = Object.getNotifier({});
it('should throw an error when passing an non-object as parameter', function () {
expect(function () {
notifier.notify(5);
}).to.throwException(function (e) {
expect(e).to.be.a(TypeError);
});
});
it('should throw an error when the property type of the first parameter is not a string', function () {
expect(function () {
notifier.notify({ type: 4 });
}).to.throwException(function (e) {
expect(e).to.be.a(TypeError);
});
});
});
describe('Notifier.performChange', function () {
var notifier = Object.getNotifier({});
it('should throw an error when passing a non string as first parameter', function () {
expect(function () {
notifier.performChange(null, function () {});
}).to.throwException(function (e) {
expect(e).to.be.a(TypeError);
});
});
it('should throw an error when second parameter is not callable', function () {
testIsCallable(function (observer) {
notifier.performChange('update', observer);
});
});
it('should call the changeFunction', function () {
var spy = sinon.spy();
notifier.performChange('update', spy);
expect(spy.calledOnce).to.be.ok();
});
it('should rethrow any error thrown by the changeFunction', function () {
expect(function () {
notifier.performChange('update', function () {
throw new RangeError('changeFunction exception');
});
}).to.throwException(function (e) {
expect(e).to.be.a(RangeError);
expect(e.message).to.be('changeFunction exception');
});
});
});
describe('Changes delivery', function () {
var obj, notifier, observer;
beforeEach(function () {
obj = {};
observer = sinon.spy();
Object.observe(obj, observer);
notifier = Object.getNotifier(obj);
});
afterEach(function () {
Object.unobserve(obj, observer);
obj = observer = notifier = null;
});
function getDeliveredRecords() {
return observer.args[0][0];
}
it('should call the observer when a change record is delivered', function () {
notifier.notify({
type: 'update',
name: 'foo'
});
Object.deliverChangeRecords(observer);
expect(observer.calledOnce).to.be.ok();
});
it('should call the observer only one time when multiples changes records are delivered', function () {
notifier.notify({
type: 'update',
name: 'foo'
});
notifier.notify({
type: 'update',
name: 'foo'
});
Object.deliverChangeRecords(observer);
expect(observer.calledOnce).to.be.ok();
});
it('should call the observer only one time when multiples changes records are delivered', function () {
notifier.notify({
type: 'update'
});
notifier.notify({
type: 'update'
});
Object.deliverChangeRecords(observer);
expect(observer.calledOnce).to.be.ok();
});
it('should deliver a change record with a property "object" corresponding to the observed object', function () {
notifier.notify({
type: 'update'
});
Object.deliverChangeRecords(observer);
var deliveredRecord = getDeliveredRecords()[0];
expect(deliveredRecord).to.have.property('object', obj);
});
it('should ignore an object property specified in the original change record', function () {
notifier.notify({
type: 'update',
object : 'foo'
});
Object.deliverChangeRecords(observer);
var deliveredRecord = getDeliveredRecords()[0];
expect(deliveredRecord).to.have.property('object', obj);
});
it('should deliver a change record with all other property equals to the original one', function () {
notifier.notify({
type: 'update',
foo : 1,
bar : 2
});
Object.deliverChangeRecords(observer);
var deliveredRecord = getDeliveredRecords()[0];
expect(deliveredRecord).to.have.property('foo', 1);
expect(deliveredRecord).to.have.property('bar', 2);
});
it('should call the observer function only once even in case of multiple observation', function () {
Object.observe(obj, observer);
notifier.notify({
type: 'update',
name: 'foo'
});
Object.deliverChangeRecords(observer);
expect(observer.calledOnce).to.be.ok();
});
it('should not call a function that has not been used for an observation', function () {
var observer2 = sinon.spy();
notifier.notify({
type: 'update',
name: 'foo'
});
Object.deliverChangeRecords(observer2);
expect(observer2.called).not.to.be.ok();
});
it('should not call the observer after call to Object.unobserve', function () {
Object.unobserve(obj, observer);
notifier.notify({
type: 'update',
name: 'foo'
});
Object.deliverChangeRecords(observer);
expect(observer.called).not.to.be.ok();
});
it('should not deliver change records between an unobservation and a reobservation', function () {
Object.unobserve(obj, observer);
notifier.notify({
type: 'update',
name: 'foo'
});
Object.observe(obj, observer);
notifier.notify({
type: 'update',
name: 'foo'
});
Object.deliverChangeRecords(observer);
expect(getDeliveredRecords()).to.have.length(1);
});
it('should deliver records in the order of notification', function () {
notifier.notify({
type: 'update',
order: 0
});
notifier.notify({
type: 'update',
order: 1
});
notifier.notify({
type: 'update',
order: 2
});
Object.deliverChangeRecords(observer);
var changeRecords = getDeliveredRecords();
expect(changeRecords[0]).to.have.property('order', 0);
expect(changeRecords[1]).to.have.property('order', 1);
expect(changeRecords[2]).to.have.property('order', 2);
});
it('should deliver change records asynchronously without a call to Object.deliverChangeRecords', function (done) {
this.timeout(100);
Object.observe(obj, function () {
done();
});
notifier.notify({
type: 'update'
});
});
it('should deliver change records in the order of observation', function (done) {
this.timeout(100);
var obj2 = {},
notifier2 = Object.getNotifier(obj2),
observer2 = sinon.spy(function () {
expect(observer.called).to.be.ok();
});
Object.observe(obj2, observer2);
Object.observe(obj, function () {
expect(observer2.called).to.be.ok();
done();
});
notifier.notify({
type: 'update'
});
notifier2.notify({
type: 'update'
});
});
it('should only deliver change records with type in the accept list if defined', function () {
Object.observe(obj, observer, ['bar', 'foo']);
notifier.notify({
type: 'update'
});
notifier.notify({
type: 'foo'
});
notifier.notify({
type: 'new'
});
notifier.notify({
type: 'foo'
});
notifier.notify({
type: 'bar'
});
Object.deliverChangeRecords(observer);
expect(getDeliveredRecords()).to.be.eql([
{ object : obj, type: 'foo' },
{ object : obj, type: 'foo' },
{ object : obj, type: 'bar' }
]);
});
it('should deliver a change record with \'type\' property equals to the performChange \'changeType\' ' +
'argument value and with properties of the returned value of the changeFunction', function () {
notifier.performChange('update', function () { });
notifier.performChange('delete', function () {
return {
message: 'hello world'
};
});
Object.deliverChangeRecords(observer);
expect(getDeliveredRecords()).to.be.eql([
{ object : obj, type: 'update' },
{ object : obj, type: 'delete', message: 'hello world' }
]);
});
it('should only deliver first changeType passed to performChange if part of accept list during a performChange', function () {
var notifyFoo = function () {
notifier.performChange('foo', function () {
notifier.notify({type : 'reconfigure'});
});
}, notifyBar = function () {
notifier.performChange('bar', function () {
notifier.notify({type : 'setPrototype'});
});
}, notifyFooAndBar = function () {
notifier.performChange('fooAndBar', function () {
notifyFoo();
notifyBar();
});
}, observer2 = sinon.spy();
Object.observe(obj, observer2, ['foo', 'bar', 'fooAndBar']);
notifyFoo();
notifyBar();
notifyFooAndBar();
Object.deliverChangeRecords(observer);
Object.deliverChangeRecords(observer2);
expect(getDeliveredRecords()).to.be.eql([
{ object : obj, type: 'reconfigure' },
{ object : obj, type: 'setPrototype' },
{ object : obj, type: 'reconfigure' },
{ object : obj, type: 'setPrototype' }
]);
expect(observer2.args[0][0]).to.be.eql([
{ object : obj, type: 'foo' },
{ object : obj, type: 'bar' },
{ object : obj, type: 'fooAndBar' }
]);
});
});
});

View File

@@ -0,0 +1,30 @@
// Copyright 2012 Kap IT (http://www.kapit.fr/)
//
// Licensed under the Apache License, Version 2.0 (the 'License');
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Author : François de Campredon (http://francois.de-campredon.fr/),
/*global describe, it */
describe('observer shim bugs', function () {
'use strict';
it('unobserving when there is 2 observer attached to an object, and pending changes records cause an error', function () {
var obj = {}, observer = function () {}, observer1 = function () {};
Object.observe(obj, observer);
Object.observe(obj, observer1);
Object.getNotifier(obj).notify({type : 'updated'});
Object.unobserve(obj, observer);
});
});

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="../components/mocha/mocha.css" type="text/css" >
<script type="text/javascript" src="../components/expect/expect.js" ></script>
<script type="text/javascript" src="../components/mocha/mocha.js" ></script>
<script type="text/javascript" src="../components/sinon/index.js" ></script>
<script type="text/javascript" src="../lib/observe-shim.js" ></script>
</head>
<body>
<div id="mocha"></div>
<script type="text/javascript">
mocha.setup("bdd");
</script>
<script src="./Object.observe.js" type="text/javascript" ></script>
<script src="./bugs.js" type="text/javascript"></script>
<script type="text/javascript" >
if ( typeof window.PHANTOMJS === "undefined") {
mocha.run();
}
</script>
</body>
</html>

View File

@@ -0,0 +1,7 @@
// exported expect, sinon
global.expect = require('expect.js');
global.sinon = require('sinon');
require('../lib/observe-shim.js');
require('./Object.observe');
require('./bugs');