Skip to content

Commit 51fac19

Browse files
author
zhangyue0503
committed
20181219
1 parent 34bf218 commit 51fac19

File tree

136 files changed

+8497
-460
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

136 files changed

+8497
-460
lines changed

.DS_Store

0 Bytes
Binary file not shown.
1.29 MB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Dispatcher Class
3+
* @version 1.0
4+
* @author genify(caijf@corp.netease.com)
5+
*/
6+
import { Event } from './Event.js';
7+
8+
// private name
9+
const middleware = Symbol('middleware');
10+
11+
// class definition
12+
export class Dispatcher extends Event {
13+
/**
14+
* Dispatcher Constructor
15+
* @param options
16+
*/
17+
constructor(options) {
18+
super(options);
19+
20+
this[middleware] = [];
21+
}
22+
23+
/**
24+
* add one or more middle ware
25+
* @param {MiddleWare} MiddleWare - MiddleWare Constructor
26+
*/
27+
add() {
28+
this[middleware].push.apply(
29+
this[middleware], arguments
30+
);
31+
}
32+
33+
/**
34+
* redirect to url
35+
* @param url
36+
*/
37+
redirect(url) {
38+
this.emit('redirect', { url: url });
39+
}
40+
41+
/**
42+
* dispatch middle ware
43+
* @param {Object} context - exec context
44+
*/
45+
dispatch(context) {
46+
let index = 0;
47+
let list = this[middleware];
48+
49+
let next = () => {
50+
let MiddleWare = list[index++];
51+
if (MiddleWare){
52+
let mw = new MiddleWare(next, {
53+
context: context,
54+
redirect: this.redirect.bind(this)
55+
});
56+
return mw.exec(context);
57+
}
58+
};
59+
60+
return next();
61+
}
62+
}
63+
64+
65+
66+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Base Event Class
3+
* @version 1.0
4+
* @author genify(caijf@corp.netease.com)
5+
*/
6+
// private name
7+
const evtname = Symbol('events');
8+
9+
// class definition
10+
export class Event {
11+
/**
12+
* Event Constructor
13+
*/
14+
constructor(options = {}) {
15+
this[evtname] = {};
16+
// auto add event for function config
17+
Object.keys(options).forEach((key) => {
18+
let it = options[key];
19+
if (typeof it === 'function'){
20+
this.on(key, it);
21+
}
22+
},this);
23+
}
24+
25+
/**
26+
* add a listener with event type
27+
* @param {String} type - event type
28+
* @param {Function} listener - event listener
29+
*/
30+
on(type, listener) {
31+
let list = this[evtname][type];
32+
if (!list){
33+
list = [];
34+
this[evtname][type] = list;
35+
}
36+
if (typeof listener === 'function'){
37+
list.push(listener);
38+
}
39+
}
40+
41+
/**
42+
* remove a listener with event type
43+
* @param {String} type - event type
44+
* @param {Function} listener - event listener
45+
*/
46+
off(type, listener) {
47+
let list = this[evtname][type];
48+
if (!list||!list.length){
49+
return;
50+
}
51+
let index = list.indexOf(listener);
52+
if (index>=0){
53+
list.splice(index, 1);
54+
}
55+
}
56+
57+
/**
58+
* emit an event with type
59+
* @param {String} type - event type
60+
* @param {Variable} args - parameters for event
61+
*/
62+
emit(type, ...args) {
63+
let list = this[evtname][type];
64+
if (list&&list.length>0){
65+
list.forEach((it) => {
66+
it.apply(null, args);
67+
});
68+
}
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Monitor Class
3+
* @version 1.0
4+
* @author genify(caijf@corp.netease.com)
5+
*/
6+
import { Event } from './Event.js';
7+
8+
// private name
9+
const key = Symbol('key');
10+
const last = Symbol('last');
11+
const check = Symbol('check');
12+
const timer = Symbol('timer');
13+
const source = Symbol('source');
14+
const interval = Symbol('interval');
15+
16+
// class definition
17+
export class Monitor extends Event{
18+
/**
19+
* Monitor Constructor
20+
* @param {Object} options - Monitor config object
21+
* @param {Number} options.interval - check interval
22+
* @param {Object} options.source - watch object
23+
* @param {Object} options.key - watch key
24+
*/
25+
constructor(options = {}) {
26+
super(options);
27+
28+
this[last];
29+
this[key] = options.key;
30+
this[source] = options.source||{};
31+
this[interval] = options.interval||100;
32+
}
33+
34+
/**
35+
* check prop changes
36+
*/
37+
[check]() {
38+
let event = {
39+
oldValue: this[last],
40+
newValue: this[source][this[key]]
41+
};
42+
if (event.oldValue!==event.newValue){
43+
this[last] = event.newValue;
44+
this.emit('change', event);
45+
}
46+
}
47+
48+
/**
49+
* start monitor
50+
*/
51+
start() {
52+
if (!this[timer]){
53+
this[timer] = setInterval(
54+
this[check].bind(this),
55+
this[interval]
56+
);
57+
this[check]();
58+
}
59+
}
60+
61+
/**
62+
* stop monitor
63+
*/
64+
stop() {
65+
if (this[timer]){
66+
this[timer] = clearInterval(
67+
this[timer]
68+
);
69+
}
70+
}
71+
}
72+
73+
74+
75+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Base MiddleWare Class
3+
* @version 1.0
4+
* @author genify(caijf@corp.netease.com)
5+
*/
6+
import { Event } from '../disp/Event.js';
7+
// private name
8+
const nxt = Symbol('next');
9+
const context = Symbol('context');
10+
11+
// class definition
12+
export class MiddleWare extends Event {
13+
/**
14+
* MiddleWare Constructor
15+
* @param {Function} next - next middle ware action
16+
* @param {Object} options - middle ware config
17+
* @param {Object} options.context - middle ware context
18+
*/
19+
constructor(next, options = {}) {
20+
super(options);
21+
22+
this.name = 'MIDDLEWARE';
23+
this[nxt] = next;
24+
this[context] = options.context;
25+
}
26+
27+
/**
28+
* get middle ware context
29+
* @returns {*}
30+
*/
31+
getContext() {
32+
return this[context];
33+
}
34+
35+
/**
36+
* run next middle ware
37+
*/
38+
next() {
39+
if (this[nxt]){
40+
this[nxt]();
41+
}
42+
}
43+
44+
/**
45+
* redirect to url
46+
* @param {Object} config - redirect config
47+
*/
48+
redirect(config) {
49+
this.emit('redirect', config);
50+
}
51+
52+
/**
53+
* execute middle ware
54+
* @param {Object} context - middle ware context
55+
*/
56+
exec(context) {
57+
console.log('exec middle ware '+this.name);
58+
// overwrite by sub class
59+
// call next middleware use this.next()
60+
}
61+
}
62+

0 commit comments

Comments
 (0)