下面咱们用一个示例来演示一下什么是观察者模式,有这样一个场景,在一个院子里,有一个小偷,和若干条狗,小偷只要一行动,狗就会叫,这个场景若是用图来展现的话如图:javascript

// 初版class Thief { constructor(){
} // thief的方法,调用dog的方法; action(){ dog1.call() dog2.call() dog3.call() }}
class Dog { call(){ console.log("狗叫") }}
let dog1 = new Dog()let dog2 = new Dog()let dog3 = new Dog()let thief = new Thief();thief.action()
// 初版-新增dog4class Thief { constructor() {
} // thief的方法,调用dog的方法; action() { dog1.call() dog2.call() dog3.call() // 新增代码 dog4.call() }}
class Dog { call() { console.log("狗叫") }}
let dog1 = new Dog()let dog2 = new Dog()let dog3 = new Dog()// 新增代码:let dog4 = new Dog()
let thief = new Thief();thief.action()
// 第二版// 一、thief增长了list属性,是一个数组// 二、subscrible方法,追加方法// 三、publish 发布消息class Thief { constructor() { this.list = [] } // subscrible(call) { this.list.push(call) } // publish遍历数组,调用全部方法。 publish() { for (let i = 0; i < this.list.length; i++) { this.list[i]() } } // thief的方法内部不会直接调用dog的方法了, // 而是调用publish action() { this.publish() }}class Dog { call() { console.log("狗叫") }}
let thief = new Thief();let dog1 = new Dog()thief.subscrible(dog1.call)// 每增长一条狗就将狗的call方法追加到list
let dog2 = new Dog()thief.subscrible(dog2.call)let dog3 = new Dog()thief.subscrible(dog3.call)thief.action()
// 第二版,新增dog4// 一、thief增长了list属性,是一个数组// 二、subscrible方法,追加方法// 三、publish 发布消息class Thief { constructor() { this.list = [] } // subscrible(call){ this.list.push(call) } // publish遍历数组,调用全部方法。 publish(){ for(let i= 0 ;i<this.list.length;i++){ this.list[i]() } } // thief的方法内部不会直接调用dog的方法了, // 而是调用publish action() { this.publish() }}class Dog { call() { console.log("狗叫") }}
let thief = new Thief();let dog1 = new Dog()thief.subscrible(dog1.call)// 每增长一条狗就将狗的call方法追加到list
let dog2 = new Dog()thief.subscrible(dog2.call)let dog3 = new Dog()thief.subscrible(dog3.call)// 增长代码:let dog4 = new Dog()thief.subscrible(dog4.call)thief.action()
// 第二版,新增thiefclass Thief { constructor() { this.list = [] } // subscrible(call){ this.list.push(call) } // publish遍历数组,调用全部方法。 publish(){ for(let i= 0 ;i<this.list.length;i++){ this.list[i]() } } // thief的方法内部不会直接调用dog的方法了, // 而是调用publish action() { this.publish() }}class Dog { call() { console.log("狗叫") }}
let thief = new Thief();// 新增thief代码let thief1 = new Thief()
let dog1 = new Dog()thief.subscrible(dog1.call)// 新增代码thief1.subscrible(dog1.call)let dog2 = new Dog()thief.subscrible(dog2.call)// 新增代码thief1.subscrible(dog2.call)let dog3 = new Dog()thief.subscrible(dog3.call)// 新增代码thief1.subscrible(dog3.call)
thief.action()// 新增代码thief1.action()
class Pubsub{ constructor(){ this.list = [] } subscrible(call){ this.list.push(call) } publish(){ for(let i= 0 ;i<this.list.length;i++){ this.list[i]() } }}
let pubsub = new Pubsub();class Dog { call() { console.log("狗叫") }}
class Thief { constructor() {
} action() { pubsub.publish() }}
let thief = new Thief();let dog1 = new Dog()pubsub.subscrible(dog1.call)let dog2 = new Dog()pubsub.subscrible(dog2.call)let dog3 = new Dog()pubsub.subscrible(dog3.call)
thief.action()
let pubsub = new Pubsub();class Dog { call() { console.log("狗叫") }}
class Thief { constructor() {
} action() { pubsub.publish() }}
let thief = new Thief();
// 新增thief1代码let thief1 = new Thief();
let dog1 = new Dog()pubsub.subscrible(dog1.call)let dog2 = new Dog()pubsub.subscrible(dog2.call)let dog3 = new Dog()pubsub.subscrible(dog3.call)
// 新增dog4代码let dog4 = new Dog()pubsub.subscrible(dog4.call)
thief.action()
class Pubsub { constructor() { let promise = new Promise((resolve,reject)=>{ this.resolve = resolve; }) this.promise = promise; } subscrible(call) { this.promise.then(call) } publish() { this.resolve(); }}
class Pubsub { constructor() { let promise = new Promise((resolve,reject)=>{ this.resolve = resolve; }) this.promise = promise; } subscrible(call) { this.promise.then(call) } publish() { this.resolve(); }}
let pubsub = new Pubsub();class Dog { call() { console.log("狗叫") }}
class Thief { constructor() {
} action() { pubsub.publish() }}
let thief = new Thief();
// 新增thief1代码let thief1 = new Thief();
let dog1 = new Dog()pubsub.subscrible(dog1.call)let dog2 = new Dog()pubsub.subscrible(dog2.call)let dog3 = new Dog()pubsub.subscrible(dog3.call)
// 新增dog4代码let dog4 = new Dog()pubsub.subscrible(dog4.call)
thief.action()
const axios = require('axios')// 一、获取CancelTokenvar CancelToken = axios.CancelToken;// 二、生成sourcevar source = CancelToken.source();console.log(source.token)axios.get('/user/12345', {//get请求在第二个参数 // 三、注入source.token cancelToken: source.token}).catch(function (thrown) { console.log(thrown)});axios.post('/user/12345', {//post请求在第三个参数 name: 'new name'}, { cancelToken: source.token}).catch(e => { console.log(e)});// 四、调用source.cancel("缘由"),终止注入了source.token的请求source.cancel('不想请求了');
'use strict';var Cancel = require('./Cancel');/** * A `CancelToken` is an object that can be used to request cancellation of an operation. * * @class * @param {Function} executor The executor function. */function CancelToken(executor) { if (typeof executor !== 'function') { throw new TypeError('executor must be a function.'); } var resolvePromise; this.promise = new Promise(function promiseExecutor(resolve) { resolvePromise = resolve; }); var token = this; executor(function cancel(message) { if (token.reason) { // Cancellation has already been requested return; } token.reason = new Cancel(message); resolvePromise(token.reason); });}/** * Throws a `Cancel` if cancellation has been requested. */CancelToken.prototype.throwIfRequested = function throwIfRequested() { if (this.reason) { throw this.reason; }};/** * Returns an object that contains a new `CancelToken` and a function that, when called, * cancels the `CancelToken`. */CancelToken.source = function source() { var cancel; var token = new CancelToken(function executor(c) { cancel = c; }); return { token: token, cancel: cancel };};module.exports = CancelToken;
function CancelToken(executor) {
var resolvePromise; this.promise = new Promise(function promiseExecutor(resolve) { resolvePromise = resolve; }); var token = this; executor(function cancel(message) { if (token.reason) { return; } token.reason = message resolvePromise(token.reason); });}
CancelToken.source = function source() { var cancel; var token = new CancelToken(function executor(c) { cancel = c; }); return { token: token, cancel: cancel };};
if (config.cancelToken) { // Handle cancellation config.cancelToken.promise.then(function onCanceled(cancel) { if (!request) { return; } request.abort(); reject(cancel); // Clean up request request = null; });}
config.cancelToken.promise.then(function onCanceled(cancel) { if (!request) { return; } request.abort(); reject(cancel); // Clean up request request = null;});
本文分享自微信公众号 - nodejs全栈开发(geekclass)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。前端