Skip to content

Commit

Permalink
【新增】$.throttle() 函数执行节流器(基于此优化了 GalleryView);
Browse files Browse the repository at this point in the history
【优化】基于 Promise/A+ 精简 $.dataHash() 的实现
  • Loading branch information
TechQuery committed Dec 15, 2016
1 parent 7eebdb0 commit fd3a0a3
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 157 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
以下扩展已收录在本项目的 [**jQuery+.js**](/master/jQuery+.js) 中,方便配合其它 jQuery API 实现 ——
- 内置一个改进且向下兼容的 **$.browser 对象**,能通过 **直接比较版本号**来区分浏览器
- 新增 **计时相关方法(秒基准)**—— `$.every()``$.wait()``$.start()``$.end()`
- 新增 **函数执行节流器** —— `$.throttle()`
- 新增 **唯一串号生成器** —— `$.uuid()`
- 新增 **类数组对象**判断方法 —— `$.likeArray()`
- `$.unique()` 方法不局限于 DOM 元素数组
Expand Down
2 changes: 1 addition & 1 deletion build/jQuery+_Wrap.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// >>> jQuery+ <<<
//
//
// [Version] v8.6 (2016-12-15)
// [Version] v8.7 (2016-12-15)
//
// [Require] jQuery v1.9+
//
Expand Down
108 changes: 42 additions & 66 deletions iQuery+.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,14 @@

return $.ListView = $.inherit($.CommonView, ListView, {
findView: function ($_View, Init_Instance) {
$_View = $($_View).find('*:list, *[multiple]')
.not('input[type="file"]');
$_View = $( $_View ).find(':list, :data("' + this.getClass() + '")');

if (Init_Instance === true) {
for (var i = 0; i < $_View.length; i++)

for (var i = 0; $_View[i]; i++)
if (! this.instanceOf($_View[i], false))
this( $_View[i] );

} else if (Init_Instance === false)
$_View.data(this.getClass(), null);

Expand Down Expand Up @@ -363,8 +364,6 @@
if ((_This_ !== this) || (! _This_.$_View.children()[0]))
return _This_;

_This_.viewPort = [0, 1];

_This_.on('insert', function ($_Item, _, Index) {
var $_Prev = _This_[--Index];

Expand All @@ -373,35 +372,17 @@
return;
}

_This_.viewPort[1] = Index;

_Self_.toggle( $_Item ).filter('[data-src]').one('load', function () {

this.width = $(this).css('width');

this.height = $(this).css('height');
});
}).$_View.add( document ).scroll(function () {
}).$_View.add( document ).scroll($.throttle(function () {

var View_Port = [ ];

for (var i = _This_.viewPort[0]; _This_[i]; i++)
if ( _This_[i].inViewport() ) {
if (View_Port[0] === undefined) View_Port[0] = i;

_Self_.toggle(_This_[i], true);
} else {
if (View_Port[1] === undefined) View_Port[1] = i;

_Self_.toggle( _This_[i] );
}

if (View_Port[0] != null)
_This_.viewPort[0] = View_Port[0];

if (View_Port[1] != null)
_This_.viewPort[1] = View_Port[1];
});
for (var i = 0; _This_[i]; i++)
_Self_.toggle(_This_[i], _This_[i].inViewport());
}));

return _This_;
}
Expand Down Expand Up @@ -635,12 +616,12 @@
iType = iString[1];
iString = iString[2];
}
iString = BOM.atob(iString);
iString = BOM.atob( iString );

var iBuffer = new ArrayBuffer(iString.length);
var uBuffer = new Uint8Array(iBuffer);
var iBuffer = new ArrayBuffer( iString.length );
var uBuffer = new Uint8Array( iBuffer );

for (var i = 0; i < iString.length; i++)
for (var i = 0; iString[i]; i++)
uBuffer[i] = iString.charCodeAt(i);

var BlobBuilder = BOM.WebKitBlobBuilder || BOM.MozBlobBuilder;
Expand All @@ -649,70 +630,65 @@
return new BOM.Blob([iBuffer], {type: iType});

var iBuilder = new BlobBuilder();
iBuilder.append(iBuffer);
return iBuilder.getBlob(iType);
iBuilder.append( iBuffer );

return iBuilder.getBlob( iType );
};

/* ---------- Hash Algorithm (Crypto API Wrapper) v0.1 ---------- */

// Thanks "emu" --- http://blog.csdn.net/emu/article/details/39618297

function BufferToString(iBuffer){
var iDataView = new DataView(iBuffer),
iResult = [ ];
var iDataView = new DataView(iBuffer), iResult = '';

for (var i = 0, iTemp; i < iBuffer.byteLength; i += 4) {
iTemp = iDataView.getUint32(i).toString(16);
iResult.push(
((iTemp.length == 8) ? '' : '0') + iTemp
);

iResult += ((iTemp.length == 8) ? '' : 0) + iTemp;
}
return iResult.join('');

return iResult;
}

$.dataHash = function (iAlgorithm, iData, iCallback, iFailback) {
$.dataHash = function (iAlgorithm, iData) {
if (arguments.length < 2) {
iData = iAlgorithm;
iAlgorithm = 'SHA-512';
}
var iCrypto = BOM.crypto || BOM.msCrypto;
var iSubtle = iCrypto.subtle || iCrypto.webkitSubtle;

iAlgorithm = iAlgorithm || 'SHA-512';
iFailback = iFailback || iCallback;

try {
iData = iData.split('');
for (var i = 0; i < iData.length; i++)
iData[i] = iData[i].charCodeAt(0);

var iPromise = iSubtle.digest(
var iPromise = (iCrypto.subtle || iCrypto.webkitSubtle).digest(
{name: iAlgorithm},
new Uint8Array(iData)
);
new Uint8Array(
Array.prototype.map.call(String( iData ), function () {

if(typeof iPromise.then == 'function')
iPromise.then(
function () {
iCallback.call(this, BufferToString(arguments[0]));
},
iFailback
return arguments[0].charCodeAt(0);
})
)
);
else
iPromise.oncomplete = function () {
iCallback.call(
this, BufferToString( arguments[0].target.result )
);
};
return ((typeof iPromise.then == 'function') ?
iPromise : new Promise(function (iResolve) {

iPromise.oncomplete = function () {
iResolve( arguments[0].target.result );
};
})
).then( BufferToString );

} catch (iError) {
iFailback(iError);
return Promise.reject( iError );
}
};

})(self, self.document, self.jQuery);


//
// >>> iQuery+ <<<
//
//
// [Version] v1.6 (2016-12-13) Stable
// [Version] v1.6 (2016-12-15) Stable
//
// [Require] iQuery || jQuery with jQuery+
//
Expand Down
33 changes: 26 additions & 7 deletions iQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -885,9 +885,9 @@
var _Timer_ = { };

$.extend({
_Root_: BOM,
now: Date.now,
every: function (iSecond, iCallback) {
_Root_: BOM,
now: Date.now,
every: function (iSecond, iCallback) {
var _BOM_ = this._Root_,
iTimeOut = (iSecond || 0.01) * 1000,
iStart = this.now(),
Expand All @@ -902,19 +902,38 @@
_BOM_.setTimeout(arguments.callee, iTimeOut);
}, iTimeOut);
},
wait: function (iSecond, iCallback) {
wait: function (iSecond, iCallback) {
return this.every(iSecond, function () {
iCallback.apply(this, arguments);
return false;
});
},
start: function (iName) {
start: function (iName) {
return (_Timer_[iName] = this.now());
},
end: function (iName) {
end: function (iName) {
return (this.now() - _Timer_[iName]) / 1000;
},
uuid: function () {
throttle: function (iSecond, iOrigin) {
if (typeof iSecond != 'number') {
iOrigin = iSecond;
iSecond = 0;
}
iSecond = (iSecond || 0.25) * 1000;

var Last_Exec = 0;

return function () {
var iNow = Date.now();

if (Last_Exec + iSecond <= iNow) {
Last_Exec = iNow;

return iOrigin.apply(this, arguments);
}
};
},
uuid: function () {
return (arguments[0] || 'uuid') + '_' +
(this.now() + Math.random()).toString(36)
.replace('.', '').toUpperCase();
Expand Down
2 changes: 1 addition & 1 deletion iQuery.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion iQuery.min.map

Large diffs are not rendered by default.

35 changes: 27 additions & 8 deletions jQuery+.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// >>> jQuery+ <<<
//
//
// [Version] v8.6 (2016-12-15)
// [Version] v8.7 (2016-12-15)
//
// [Require] jQuery v1.9+
//
Expand Down Expand Up @@ -645,9 +645,9 @@
var _Timer_ = { };

$.extend({
_Root_: BOM,
now: Date.now,
every: function (iSecond, iCallback) {
_Root_: BOM,
now: Date.now,
every: function (iSecond, iCallback) {
var _BOM_ = this._Root_,
iTimeOut = (iSecond || 0.01) * 1000,
iStart = this.now(),
Expand All @@ -662,19 +662,38 @@
_BOM_.setTimeout(arguments.callee, iTimeOut);
}, iTimeOut);
},
wait: function (iSecond, iCallback) {
wait: function (iSecond, iCallback) {
return this.every(iSecond, function () {
iCallback.apply(this, arguments);
return false;
});
},
start: function (iName) {
start: function (iName) {
return (_Timer_[iName] = this.now());
},
end: function (iName) {
end: function (iName) {
return (this.now() - _Timer_[iName]) / 1000;
},
uuid: function () {
throttle: function (iSecond, iOrigin) {
if (typeof iSecond != 'number') {
iOrigin = iSecond;
iSecond = 0;
}
iSecond = (iSecond || 0.25) * 1000;

var Last_Exec = 0;

return function () {
var iNow = Date.now();

if (Last_Exec + iSecond <= iNow) {
Last_Exec = iNow;

return iOrigin.apply(this, arguments);
}
};
},
uuid: function () {
return (arguments[0] || 'uuid') + '_' +
(this.now() + Math.random()).toString(36)
.replace('.', '').toUpperCase();
Expand Down
28 changes: 4 additions & 24 deletions source/advanced/GalleryView.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ define(['jquery', 'ListView'], function ($) {
if ((_This_ !== this) || (! _This_.$_View.children()[0]))
return _This_;

_This_.viewPort = [0, 1];

_This_.on('insert', function ($_Item, _, Index) {
var $_Prev = _This_[--Index];

Expand All @@ -25,35 +23,17 @@ define(['jquery', 'ListView'], function ($) {
return;
}

_This_.viewPort[1] = Index;

_Self_.toggle( $_Item ).filter('[data-src]').one('load', function () {

this.width = $(this).css('width');

this.height = $(this).css('height');
});
}).$_View.add( document ).scroll(function () {

var View_Port = [ ];

for (var i = _This_.viewPort[0]; _This_[i]; i++)
if ( _This_[i].inViewport() ) {
if (View_Port[0] === undefined) View_Port[0] = i;

_Self_.toggle(_This_[i], true);
} else {
if (View_Port[1] === undefined) View_Port[1] = i;

_Self_.toggle( _This_[i] );
}

if (View_Port[0] != null)
_This_.viewPort[0] = View_Port[0];
}).$_View.add( document ).scroll($.throttle(function () {

if (View_Port[1] != null)
_This_.viewPort[1] = View_Port[1];
});
for (var i = 0; _This_[i]; i++)
_Self_.toggle(_This_[i], _This_[i].inViewport());
}));

return _This_;
}
Expand Down
7 changes: 4 additions & 3 deletions source/advanced/ListView.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ define(['jquery', 'CommonView'], function ($) {

return $.ListView = $.inherit($.CommonView, ListView, {
findView: function ($_View, Init_Instance) {
$_View = $($_View).find('*:list, *[multiple]')
.not('input[type="file"]');
$_View = $( $_View ).find(':list, :data("' + this.getClass() + '")');

if (Init_Instance === true) {
for (var i = 0; i < $_View.length; i++)

for (var i = 0; $_View[i]; i++)
if (! this.instanceOf($_View[i], false))
this( $_View[i] );

} else if (Init_Instance === false)
$_View.data(this.getClass(), null);

Expand Down
Loading

0 comments on commit fd3a0a3

Please sign in to comment.