From 397cb96c90070ad02157a27e1c34160f91376f59 Mon Sep 17 00:00:00 2001 From: machock <229235844@qq.com> Date: Sat, 16 Jun 2018 15:35:09 +0800 Subject: [PATCH 1/2] add sub/pub, add observable --- Observable.js | 12 ++++++++++++ PubSub.js | 5 +++++ 2 files changed, 17 insertions(+) diff --git a/Observable.js b/Observable.js index 03545cd..a807837 100644 --- a/Observable.js +++ b/Observable.js @@ -11,12 +11,19 @@ class ObserverList { } add(observer) { // todo add observer to list + this.observerList.push(observer) } remove(observer) { // todo remove observer from list + let i = this.observerList.indexOf(observer) + this.observerList.splice(i, 1); } count() { // return observer list size + return this.observerList.length; + } + get(index) { + return this.observerList[index]; } } @@ -26,12 +33,17 @@ class Subject { } addObserver(observer) { // todo add observer + this.observers.add(observer) } removeObserver(observer) { // todo remove observer + this.observers.remove(observer) } notify(...args) { // todo notify + this.observers.observerList.forEach(ob => { + ob.update(...args) + }); } } diff --git a/PubSub.js b/PubSub.js index 0c7999e..4b7b339 100644 --- a/PubSub.js +++ b/PubSub.js @@ -13,14 +13,19 @@ module.exports = class PubSub { subscribe(type, fn) { // todo subscribe + this.subscribers[type] ? this.subscribers[type].push(fn) : (this.subscribers[type] = [fn]) } unsubscribe(type, fn) { // todo unsubscribe + this.subscribers[type] = [] } publish(type, ...args) { // todo publish + this.subscribers[type].forEach(((fn) => { + fn.apply(this, args) + })) } } From d8c654cf57764d163a49b88055f0f81dc3d5a7f3 Mon Sep 17 00:00:00 2001 From: machock <229235844@qq.com> Date: Wed, 21 Nov 2018 20:12:28 +0800 Subject: [PATCH 2/2] pass --- Observable.js | 16 ++--- PubSub.js | 20 +++---- yarn.lock | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 176 insertions(+), 18 deletions(-) create mode 100644 yarn.lock diff --git a/Observable.js b/Observable.js index a807837..7c46736 100644 --- a/Observable.js +++ b/Observable.js @@ -1,6 +1,6 @@ /* - * @Author: kael - * @Date: 2018-02-01 17:41:25 + * @Author: kael + * @Date: 2018-02-01 17:41:25 * @Last Modified by: kael * @Last Modified time: 2018-02-02 17:38:36 */ @@ -11,11 +11,11 @@ class ObserverList { } add(observer) { // todo add observer to list - this.observerList.push(observer) + this.observerList.push(observer); } remove(observer) { // todo remove observer from list - let i = this.observerList.indexOf(observer) + let i = this.observerList.indexOf(observer); this.observerList.splice(i, 1); } count() { @@ -33,18 +33,18 @@ class Subject { } addObserver(observer) { // todo add observer - this.observers.add(observer) + this.observers.add(observer); } removeObserver(observer) { // todo remove observer - this.observers.remove(observer) + this.observers.remove(observer); } notify(...args) { // todo notify this.observers.observerList.forEach(ob => { - ob.update(...args) + ob.update(...args); }); } } -module.exports = { Subject }; \ No newline at end of file +module.exports = { Subject }; diff --git a/PubSub.js b/PubSub.js index 4b7b339..4f9fc13 100644 --- a/PubSub.js +++ b/PubSub.js @@ -1,31 +1,31 @@ /* - * @Author: kael - * @Date: 2018-02-01 17:41:25 + * @Author: kael + * @Date: 2018-02-01 17:41:25 * @Last Modified by: kael * @Last Modified time: 2018-02-02 17:39:45 */ module.exports = class PubSub { - constructor() { this.subscribers = {}; } subscribe(type, fn) { // todo subscribe - this.subscribers[type] ? this.subscribers[type].push(fn) : (this.subscribers[type] = [fn]) + this.subscribers[type] + ? this.subscribers[type].push(fn) + : (this.subscribers[type] = [fn]); } unsubscribe(type, fn) { // todo unsubscribe - this.subscribers[type] = [] + this.subscribers[type] = []; } publish(type, ...args) { // todo publish - this.subscribers[type].forEach(((fn) => { - fn.apply(this, args) - })) + this.subscribers[type].forEach(fn => { + fn.apply(this, args); + }); } - -} +}; diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..adb78a0 --- /dev/null +++ b/yarn.lock @@ -0,0 +1,158 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +balanced-match@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767" + integrity sha1-ibTRmasr7kneFk6gK4nORi1xt2c= + +brace-expansion@^1.1.7: + version "1.1.11" + resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd" + integrity sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA== + dependencies: + balanced-match "^1.0.0" + concat-map "0.0.1" + +browser-stdout@1.3.1: + version "1.3.1" + resolved "https://registry.yarnpkg.com/browser-stdout/-/browser-stdout-1.3.1.tgz#baa559ee14ced73452229bad7326467c61fabd60" + integrity sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw== + +commander@2.15.1: + version "2.15.1" + resolved "https://registry.yarnpkg.com/commander/-/commander-2.15.1.tgz#df46e867d0fc2aec66a34662b406a9ccafff5b0f" + integrity sha512-VlfT9F3V0v+jr4yxPc5gg9s62/fIVWsd2Bk2iD435um1NlGMYdVCq+MjcXnhYq2icNOizHr1kK+5TI6H0Hy0ag== + +concat-map@0.0.1: + version "0.0.1" + resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b" + integrity sha1-2Klr13/Wjfd5OnMDajug1UBdR3s= + +debug@3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261" + integrity sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g== + dependencies: + ms "2.0.0" + +diff@3.5.0: + version "3.5.0" + resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12" + integrity sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA== + +escape-string-regexp@1.0.5: + version "1.0.5" + resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4" + integrity sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ= + +fs.realpath@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" + integrity sha1-FQStJSMVjKpA20onh8sBQRmU6k8= + +glob@7.1.2: + version "7.1.2" + resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15" + integrity sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ== + dependencies: + fs.realpath "^1.0.0" + inflight "^1.0.4" + inherits "2" + minimatch "^3.0.4" + once "^1.3.0" + path-is-absolute "^1.0.0" + +growl@1.10.5: + version "1.10.5" + resolved "https://registry.yarnpkg.com/growl/-/growl-1.10.5.tgz#f2735dc2283674fa67478b10181059355c369e5e" + integrity sha512-qBr4OuELkhPenW6goKVXiv47US3clb3/IbuWF9KNKEijAy9oeHxU9IgzjvJhHkUzhaj7rOUD7+YGWqUjLp5oSA== + +has-flag@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-3.0.0.tgz#b5d454dc2199ae225699f3467e5a07f3b955bafd" + integrity sha1-tdRU3CGZriJWmfNGfloH87lVuv0= + +he@1.1.1: + version "1.1.1" + resolved "https://registry.yarnpkg.com/he/-/he-1.1.1.tgz#93410fd21b009735151f8868c2f271f3427e23fd" + integrity sha1-k0EP0hsAlzUVH4howvJx80J+I/0= + +inflight@^1.0.4: + version "1.0.6" + resolved "https://registry.yarnpkg.com/inflight/-/inflight-1.0.6.tgz#49bd6331d7d02d0c09bc910a1075ba8165b56df9" + integrity sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk= + dependencies: + once "^1.3.0" + wrappy "1" + +inherits@2: + version "2.0.3" + resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de" + integrity sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4= + +minimatch@3.0.4, minimatch@^3.0.4: + version "3.0.4" + resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083" + integrity sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA== + dependencies: + brace-expansion "^1.1.7" + +minimist@0.0.8: + version "0.0.8" + resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d" + integrity sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0= + +mkdirp@0.5.1: + version "0.5.1" + resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.1.tgz#30057438eac6cf7f8c4767f38648d6697d75c903" + integrity sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM= + dependencies: + minimist "0.0.8" + +mocha@^5.0.0: + version "5.2.0" + resolved "https://registry.yarnpkg.com/mocha/-/mocha-5.2.0.tgz#6d8ae508f59167f940f2b5b3c4a612ae50c90ae6" + integrity sha512-2IUgKDhc3J7Uug+FxMXuqIyYzH7gJjXECKe/w43IGgQHTSj3InJi+yAA7T24L9bQMRKiUEHxEX37G5JpVUGLcQ== + dependencies: + browser-stdout "1.3.1" + commander "2.15.1" + debug "3.1.0" + diff "3.5.0" + escape-string-regexp "1.0.5" + glob "7.1.2" + growl "1.10.5" + he "1.1.1" + minimatch "3.0.4" + mkdirp "0.5.1" + supports-color "5.4.0" + +ms@2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8" + integrity sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g= + +once@^1.3.0: + version "1.4.0" + resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1" + integrity sha1-WDsap3WWHUsROsF9nFC6753Xa9E= + dependencies: + wrappy "1" + +path-is-absolute@^1.0.0: + version "1.0.1" + resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f" + integrity sha1-F0uSaHNVNP+8es5r9TpanhtcX18= + +supports-color@5.4.0: + version "5.4.0" + resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.4.0.tgz#1c6b337402c2137605efe19f10fec390f6faab54" + integrity sha512-zjaXglF5nnWpsq470jSv6P9DwPvgLkuapYmfDm3JWOm0vkNTVF2tI4UrN2r6jH1qM/uc/WtxYY1hYoA2dOKj5w== + dependencies: + has-flag "^3.0.0" + +wrappy@1: + version "1.0.2" + resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f" + integrity sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=