]> git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/vim-mode/node_modules/grim/node_modules/emissary/node_modules/es6-weak-map/node_modules/es6-iterator/index.js
10fd08958f68c7b016cf6f31f8c951e024c8b870
[rbdr/dotfiles] / atom / packages / vim-mode / node_modules / grim / node_modules / emissary / node_modules / es6-weak-map / node_modules / es6-iterator / index.js
1 'use strict';
2
3 var clear = require('es5-ext/array/#/clear')
4 , assign = require('es5-ext/object/assign')
5 , callable = require('es5-ext/object/valid-callable')
6 , value = require('es5-ext/object/valid-value')
7 , d = require('d')
8 , autoBind = require('d/auto-bind')
9 , Symbol = require('es6-symbol')
10
11 , defineProperty = Object.defineProperty
12 , defineProperties = Object.defineProperties
13 , Iterator;
14
15 module.exports = Iterator = function (list, context) {
16 if (!(this instanceof Iterator)) return new Iterator(list, context);
17 defineProperties(this, {
18 __list__: d('w', value(list)),
19 __context__: d('w', context),
20 __nextIndex__: d('w', 0)
21 });
22 if (!context) return;
23 callable(context.on);
24 context.on('_add', this._onAdd);
25 context.on('_delete', this._onDelete);
26 context.on('_clear', this._onClear);
27 };
28
29 defineProperties(Iterator.prototype, assign({
30 constructor: d(Iterator),
31 _next: d(function () {
32 var i;
33 if (!this.__list__) return;
34 if (this.__redo__) {
35 i = this.__redo__.shift();
36 if (i !== undefined) return i;
37 }
38 if (this.__nextIndex__ < this.__list__.length) return this.__nextIndex__++;
39 this._unBind();
40 }),
41 next: d(function () { return this._createResult(this._next()); }),
42 _createResult: d(function (i) {
43 if (i === undefined) return { done: true, value: undefined };
44 return { done: false, value: this._resolve(i) };
45 }),
46 _resolve: d(function (i) { return this.__list__[i]; }),
47 _unBind: d(function () {
48 this.__list__ = null;
49 delete this.__redo__;
50 if (!this.__context__) return;
51 this.__context__.off('_add', this._onAdd);
52 this.__context__.off('_delete', this._onDelete);
53 this.__context__.off('_clear', this._onClear);
54 this.__context__ = null;
55 }),
56 toString: d(function () { return '[object Iterator]'; })
57 }, autoBind({
58 _onAdd: d(function (index) {
59 if (index >= this.__nextIndex__) return;
60 ++this.__nextIndex__;
61 if (!this.__redo__) {
62 defineProperty(this, '__redo__', d('c', [index]));
63 return;
64 }
65 this.__redo__.forEach(function (redo, i) {
66 if (redo >= index) this.__redo__[i] = ++redo;
67 }, this);
68 this.__redo__.push(index);
69 }),
70 _onDelete: d(function (index) {
71 var i;
72 if (index >= this.__nextIndex__) return;
73 --this.__nextIndex__;
74 if (!this.__redo__) return;
75 i = this.__redo__.indexOf(index);
76 if (i !== -1) this.__redo__.splice(i, 1);
77 this.__redo__.forEach(function (redo, i) {
78 if (redo > index) this.__redo__[i] = --redo;
79 }, this);
80 }),
81 _onClear: d(function () {
82 if (this.__redo__) clear.call(this.__redo__);
83 this.__nextIndex__ = 0;
84 })
85 })));
86
87 defineProperty(Iterator.prototype, Symbol.iterator, d(function () {
88 return this;
89 }));
90 defineProperty(Iterator.prototype, Symbol.toStringTag, d('', 'Iterator'));