扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
这篇文章主要介绍“如何用promsie实现观察者模式”,在日常操作中,相信很多人在如何用promsie实现观察者模式问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何用promsie实现观察者模式”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
创新互联-专业网站定制、快速模板网站建设、高性价比即墨网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式即墨网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖即墨地区。费用合理售后完善,10余年实体公司更值得信赖。
下面我们用一个示例来演示一下什么是观察者模式,有这样一个场景,在一个院子里,有一个小偷,和若干条狗,小偷只要一行动,狗就会叫,这个场景如果用图来展示的话如图:
// 第一版
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()
// 第一版-新增dog4
class 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()
// 第二版
// 1、thief增加了list属性,是一个数组
// 2、subscrible方法,追加方法
// 3、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
// 1、thief增加了list属性,是一个数组
// 2、subscrible方法,追加方法
// 3、publish 发布消息
class Thief {
constructor() {
this.list = []
}
//
subscrible(call){
this.list.push(call)
}
// publish遍历数组,调用所有方法。
publish(){
for(let i= 0 ;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()
// 第二版,新增thief
class Thief {
constructor() {
this.list = []
}
//
subscrible(call){
this.list.push(call)
}
// publish遍历数组,调用所有方法。
publish(){
for(let i= 0 ;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
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')// 1、获取CancelTokenvar CancelToken = axios.CancelToken;// 2、生成sourcevar source = CancelToken.source();console.log(source.token)axios.get('/user/12345', {//get请求在第二个参数 // 3、注入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)});// 4、调用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;});
到此,关于“如何用promsie实现观察者模式”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流