-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
function add() { | ||
return arguments.reduce((prev, cur) => prev + cur, 0); | ||
} | ||
|
||
|
||
function currying(fn) { | ||
let args = []; | ||
return function temp() { | ||
const context = this; | ||
if (!arguments.length) { | ||
let ans = fn.apply(context, args); | ||
args = []; | ||
return ans; | ||
} else { | ||
args = [ | ||
...args, | ||
...arguments, | ||
]; | ||
return temp; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
function debounce(fn, wait) { | ||
let timer; | ||
return function () { | ||
if (timer) { | ||
clearTimeout(timer); | ||
} | ||
timer = setTimeout(() => { | ||
fn.apply(this, arguments); | ||
}, wait); | ||
} | ||
} | ||
|
||
function debounce_immediate(fn, wait, immediate) { | ||
let timer; | ||
return function () { | ||
let context = this; | ||
let args = arguments; | ||
|
||
if (timer) { | ||
clearTimeout(timer); | ||
} | ||
|
||
if (!immediate) { | ||
timer = setTimeout(() => { | ||
fn.apply(context, args); | ||
}, wait); | ||
} else { | ||
const callNow = !timer; | ||
timer = setTimeout(() => { | ||
clearTimeout(timer); | ||
}, wait); | ||
if (callNow) { | ||
fn.apply(context, args); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
class EventBus { | ||
static listener = new Map(); | ||
|
||
static on = (name, cb) => { | ||
if (this.listener.has(name)) { | ||
this.listener.get(name).push({ once: false, fn: cb }); | ||
} else { | ||
this.listener.set(name, [{ once: false, fn: cb }]); | ||
} | ||
} | ||
|
||
static once = (name, cb) => { | ||
if (this.listener.has(name)) { | ||
this.listener.get(name).push({ once: true, fn: cb }); | ||
} else { | ||
this.listener.set(name, [{ once: true, fn: cb }]); | ||
} | ||
} | ||
|
||
static off = (name, cb) => { | ||
if (this.listener.has(name)) { | ||
const cbs = this.listener.get(name).filter(item => item.fn !== cb); | ||
this.listener.set(name, cbs); | ||
} | ||
} | ||
|
||
static removeAllListener = (name) => { | ||
if (name) { | ||
this.listener.delete(name); | ||
} else { | ||
this.listener.clear(); | ||
} | ||
} | ||
|
||
static emit = (name, ...rest) => { | ||
if (this.listener.has(name)) { | ||
const cache = this.listener.get(name); | ||
cache.forEach(item => { | ||
item.fn.call(this, rest); | ||
}); | ||
const newCache = cache.filter(item => !item.once); | ||
this.listener.set(name, newCache); | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
function mySetInterVal(fn, a, b) { | ||
this.a = a; | ||
this.b = b; | ||
this.count = 0; | ||
this.timer = null; | ||
this.start = () => { | ||
this.timer = setTimeout(() => { | ||
fn(); | ||
this.count++; | ||
this.start(); | ||
}, this.a + this.b * this.count); | ||
} | ||
this.stop = () => { | ||
clearTimeout(this.timer); | ||
this.count = 0; | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
class MyPromise { | ||
status = 'pending'; | ||
data = undefined; | ||
callbacks = []; | ||
constructor(excutor) { | ||
|
||
const resolve = (value) => { | ||
if (this.status !== 'pending') { | ||
return; | ||
} | ||
this.status = 'resolved'; | ||
this.data = value; | ||
if (this.callbacks.length) { | ||
this.callbacks.forEach(cb => { | ||
cb.onResolved(value); | ||
}) | ||
} | ||
} | ||
|
||
const reject = (reason) => { | ||
if (this.status !== 'pending') { | ||
return; | ||
} | ||
this.status = 'rejected'; | ||
this.data = reason; | ||
if (this.callbacks.length) { | ||
this.callbacks.forEach(cb => { | ||
cb.onRejected(reason); | ||
}) | ||
} | ||
} | ||
|
||
|
||
try { | ||
excutor(resolve, reject) | ||
} catch (error) {// 如果执行器抛出异常将失败 | ||
console.error('🙅 error', error); | ||
reject(error) | ||
} | ||
} | ||
|
||
|
||
then(onResolved, onRejected) { | ||
return new MyPromise((resolve, reject) => { | ||
if (this.status === 'pending') { | ||
this.callbacks.push({ onResolved, onRejected }); | ||
} else if (this.status === 'resolved') { | ||
try { | ||
const res = onResolved(this.data); | ||
if (res instanceof MyPromise) { | ||
res.then( | ||
value => resolve(value), | ||
reason => reject(reason), | ||
); | ||
} else { | ||
resolve(res); | ||
} | ||
} catch (reason) { | ||
reject(resolve); | ||
} | ||
} else { | ||
onRejected(this.data) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
|
||
|
||
var p = new MyPromise(function (resolve, reject) { resolve(1) }); | ||
p.then( | ||
res => console.log(res), | ||
reason => console.error(reason) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
function throttle(fn, wait) { | ||
let timer; | ||
return function () { | ||
const context = this; | ||
const args = arguments; | ||
if (timer) { | ||
return; | ||
} | ||
timer = setTimeout(() => { | ||
fn.apply(context, args); | ||
clearTimeout(timer); | ||
}, wait); | ||
} | ||
} | ||
|
||
|