Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: angular/zone.js
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.2.1
Choose a base ref
...
head repository: angular/zone.js
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.2.2
Choose a head ref
  • 15 commits
  • 18 files changed
  • 1 contributor

Commits on Apr 24, 2014

  1. Copy the full SHA
    818b1b4 View commit details

Commits on May 7, 2014

  1. Copy the full SHA
    21b47ae View commit details
  2. Copy the full SHA
    ba72f34 View commit details
  3. Copy the full SHA
    86328fb View commit details
  4. Copy the full SHA
    da99e15 View commit details
  5. Copy the full SHA
    c77a7a7 View commit details
  6. chore: refactor eventNames

    btford committed May 7, 2014
    Copy the full SHA
    39b1513 View commit details
  7. Copy the full SHA
    7133de0 View commit details
  8. Copy the full SHA
    93c1468 View commit details

Commits on May 8, 2014

  1. Copy the full SHA
    ad711b8 View commit details

Commits on May 22, 2014

  1. feat: support document.registerElement

    Closes #18
    btford committed May 22, 2014
    Copy the full SHA
    d3c785a View commit details
  2. Copy the full SHA
    b811d7c View commit details
  3. Copy the full SHA
    ab1d487 View commit details
  4. Copy the full SHA
    c0c316b View commit details
  5. 0.2.2

    btford committed May 22, 2014
    Copy the full SHA
    5178a3b View commit details
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -60,6 +60,13 @@ Hooks that you don't override when forking a zone are inherited from the existin
See the [API docs](#api) below for more.


## Usage

To start using Zones, you need to include the `zone.js` script in this package onto
your page. This script should appear in the `<head>` of your HTML file before any other
scripts, including shims/polyfills.


## Examples

There are two kinds of examples:
@@ -221,6 +228,16 @@ This hook will run even if the function passed to `run` throws.

This hook is called when the function passed to `run` or the `beforeTask` hook throws.

### `zone.enqueueTask`

This hook is called when a function is registered with the VM.
For instance `setTimeout` and `addEventListener`.

### `zone.dequeueTask`

This hook is called when a function is unregistered with the VM.
For instance `clearTimeout` and `removeEventListener`.

### `zone.setTimeout`, `zone.setInterval`, `zone.alert`, `zone.prompt`

These hooks allow you to change the behavior of `window.setTimeout`, `window.setInterval`, etc.
34 changes: 24 additions & 10 deletions counting-zone.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
/*
* See example/counting.html
*/

Zone.countingZone = {
'-onZoneCreated': function () {
Zone.countingZone.counter += 1;

// setTimeout
'+enqueueTask': function () {
this.data.count += 1;
},

// fires when...
// - clearTimeout
// - setTimeout finishes
'-dequeueTask': function () {
this.data.count -= 1;
},

'+afterTask': function () {
Zone.countingZone.counter -= 1;
if (Zone.countingZone.counter <= 0) {
Zone.countingZone.counter = 0;
this.onFlush();
if (this.data.count === 0 && !this.data.flushed) {
this.data.flushed = true;
this.run(this.onFlush);
}
},
'-run': function () {
Zone.countingZone.counter = 0;
},

counter: function () {
return Zone.countingZone.counter;
return this.data.count;
},

data: {
count: 0,
flushed: false
},

onFlush: function () {}
};
6 changes: 3 additions & 3 deletions example/counting.html
Original file line number Diff line number Diff line change
@@ -28,20 +28,20 @@ <h1>Counting Pending Tasks</h1>
*/
var myCountingZone = zone.fork(Zone.countingZone).fork({
'+onZoneCreated': function () {
Zone.countingZone.start || (Zone.countingZone.start = Date.now());
this.data.start || (this.data.start = Date.now());
this.print();
},
'-afterTask': function (delegate) {
this.print();
},
'+reset': function (delegate) {
Zone.countingZone.start = 0;
this.data.start = 0;
},
print: function () {
counter = this.counter();
output.innerHTML = counter ?
'pending task count: ' + counter :
' DONE! ' + (Date.now() - Zone.countingZone.start)/1000 + 's';
' DONE! ' + (Date.now() - this.data.start)/1000 + 's';
}
});

2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ module.exports = function (config) {
files: [
'zone.js',
'*-zone.js',
'test/*.spec.js'
'test/**/*.spec.js'
],

reporters: ['progress'],
25 changes: 17 additions & 8 deletions long-stack-trace-zone.js
Original file line number Diff line number Diff line change
@@ -8,13 +8,11 @@ Zone.Stacktrace = function (e) {
this._e = e;
};
Zone.Stacktrace.prototype.get = function () {
var frames = this._e.stack.split('\n');

var markerIndex;
for (var markerIndex = frames.length - 1; markerIndex >= 0; markerIndex -= 1) {
if (frames[markerIndex].indexOf('marker@') === 0) {
return frames.slice(markerIndex+1).join('\n');
}
if (zone.stackFramesFilter) {
return this._e.stack.
split('\n').
filter(zone.stackFramesFilter).
join('\n');
}
return this._e.stack;
}
@@ -46,8 +44,15 @@ Zone.getStacktrace = function () {

Zone.longStackTraceZone = {
getLongStacktrace: function (exception) {
var trace = [exception.stack];
var trace = [];
var zone = this;
if (zone.stackFramesFilter) {
trace.push(exception.stack.split('\n').
filter(zone.stackFramesFilter).
join('\n'));
} else {
trace.push(exception.stack);
}
var now = Date.now();
while (zone && zone.constructedAtException) {
trace.push(
@@ -59,6 +64,10 @@ Zone.longStackTraceZone = {
return trace.join('\n');
},

stackFramesFilter: function (line) {
return line.indexOf('zone.js') === -1;
},

onError: function (exception) {
var reporter = this.reporter || console.log.bind(console);
reporter(exception.toString());
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zone.js",
"version": "0.2.1",
"version": "0.2.2",
"description": "Zones for JavaScript",
"main": "zone.js",
"directories": {
@@ -15,7 +15,7 @@
"url": "git://github.com/btford/zone.js.git"
},
"author": "Brian Ford",
"license": "MIT",
"license": "Apache 2.0",
"bugs": {
"url": "https://github.com/btford/zone.js/issues"
},
117 changes: 103 additions & 14 deletions test/counting-zone.spec.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,123 @@
'use strict';


describe('Zone.countingZone', function () {
var flushSpy = jasmine.createSpy('flush'),
countingZone = zone.fork(Zone.countingZone).fork({
onFlush: flushSpy
});
var flushSpy, countingZone;

beforeEach(function () {
jasmine.Clock.useMock();
flushSpy.reset();
flushSpy = jasmine.createSpy('flush');
countingZone = zone.fork(Zone.longStackTraceZone).
fork(Zone.countingZone).
fork({
onFlush: flushSpy
});
});

it('should flush at the end of a run', function () {
countingZone.run(function () {});
expect(flushSpy).toHaveBeenCalled();
countingZone.run(function () {
expect(countingZone.counter()).toBe(0);
});
expect(countingZone.counter()).toBe(0);
expect(flushSpy.calls.length).toBe(1);
});

it('should work with setTimeout', function () {
var latch;

runs(function () {
countingZone.run(function () {
setTimeout(function () {
latch = true;
}, 0);
expect(countingZone.counter()).toBe(1);
});
});

waitsFor(function () {
return latch;
});

runs(function () {
expect(countingZone.counter()).toBe(0);
})
});

it('should work with clearTimeout', function () {
var latch = false;
countingZone.run(function () {
var id = setTimeout(function () {
latch = true;
}, 0);
expect(countingZone.counter()).toBe(1);
clearTimeout(id);
expect(countingZone.counter()).toBe(0);
});
});

it('should work', function () {
it('should work with setInterval', function () {
var latch = 0, id;

runs(function () {
countingZone.run(function () {
id = setInterval(function () {
latch += 1;
}, 0);
expect(countingZone.counter()).toBe(1);
});
});

waitsFor(function () {
return latch === 2;
}, 100, 'latch to increment');

runs(function () {
expect(countingZone.counter()).toBe(1);
clearInterval(id);
});
});

it('should work with clearInterval', function () {
var id;
countingZone.run(function () {
id = setInterval(function () {
latch += 1;
}, 0);
expect(countingZone.counter()).toBe(1);
clearInterval(id);
expect(countingZone.counter()).toBe(0);
});
});

it('should work with addEventListener', function () {
var elt = document.createElement('button');
var clicked = false;

setTimeout(function () {}, 0);
runs(function () {
countingZone.run(main);
});

function main () {
expect(countingZone.counter()).toBe(0);
elt.addEventListener('click', onClick);
expect(countingZone.counter()).toBe(1);

//jasmine.Clock.tick(0);
elt.click();
function onClick () {
expect(countingZone.counter()).toBe(1);
elt.removeEventListener('click', onClick);
expect(countingZone.counter()).toBe(0);
clicked = true;
}

expect(countingZone.counter()).toBe(0);
}

waitsFor(function () {
return clicked;
}, 10, 'the thing');

//expect(countingZone.counter()).toBe(0);
runs(function () {
expect(flushSpy.calls.length).toBe(1);
});

//jasmine.Clock.tick(0);
});
});
21 changes: 19 additions & 2 deletions test/long-stack-trace-zone.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';


describe('Zone.patch', function () {
describe('longStackTraceZone', function () {
var log;

var lstz = zone.fork(Zone.longStackTraceZone).fork({
@@ -29,4 +28,22 @@ describe('Zone.patch', function () {
expect(log[0]).toBe('Error: hello');
expect(log[1].split('--- ').length).toBe(4);
});


it('should filter based on stackFramesFilter', function () {
lstz.fork({
stackFramesFilter: function (line) {
return line.indexOf('jasmine.js') === -1;
}
}).run(function () {
setTimeout(function () {
setTimeout(function () {
throw new Error('hello');
}, 0);
}, 0);
});

jasmine.Clock.tick(0);
expect(log[1]).not.toContain('jasmine.js');
});
});
Loading