下面咱們用一個示例來演示一下什麼是觀察者模式,有這樣一個場景,在一個院子裏,有一個小偷,和若干條狗,小偷只要一行動,狗就會叫,這個場景若是用圖來展現的話如圖: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源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。前端