2019年底

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// 简易版emit/on
function Pubsub(){
     //存放事件和对应的处理方法
    this .handles = {};
 }
 Pubsub.prototype={
     //传入事件类型type和事件处理handle
     on:   function (type, handle) {
         if (!  this .handles[type]){
             this .handles[type] = [];
         }
         this .handles[type].push(handle);
     },
     emit:   function () {
         //通过传入参数获取事件类型
        var type = Array.prototype.shift.call(arguments);
        console.log(type,   'type' );
         if (!  this .handles[type]){
             return false ;
         }
 for var i = 0; i <   this .handles[type].length; i++) {
             var handle =   this .handles[type][i];
             //执行事件
            console.log(handle,   'handle' , arguments);
            handle.apply(  this , arguments);
         }
     },
     off:   function (type, handle) {
         handles =   this .handles[type];
         if (handles){
             if (!handle){
                 handles.length = 0;  //清空数组
            else {
 for var i = 0; i < handles.length; i++) {
                     var _handle = handles[i];
                     if (_handle === handle){
                         handles.splice(i,1);
                     }
                 }
             }
         }
     }
 }
 
 var p1 =   new Pubsub();
 p1.on(  'mm' ,   function (name) {
     console.log(  'mm: ' + name);
 });
 p1.emit(  'mm' '哈哈哈哈' );
 
 
// 洋葱圈模型
// 实现方式一
function compose (middleware) {
   return async   function () {
      let args = arguments
      await dispatch(0)
      function async dispatch (i) {
         const fn = middleware[i]
         if (!fn)   return null
         await fn(  function next () {
            dispatch(i + 1)
         }, ...args)
      }
   }
}
// 实现方式二
function compose(middlewares=[fn1,fn2,fn3])
{
    function dispatch(i)
    {
        let fn=middlewares[i]
        if (!fn){
            return Promise.resolve()
        }
        else
        {
            return new Promise((resolve)=>{
                resolve(fn(  function next () {
                    return dispatch(i + 1)
                }));
            });
        }
    }
    return dispatch(0);
}

  

 


__EOF__

本文作者Paxster
本文链接https://www.cnblogs.com/paxster/p/12088040.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   Paxster  阅读(227)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
点击右上角即可分享
微信分享提示