From 6f01886089d13a193e3509ced80d644abfa21482 Mon Sep 17 00:00:00 2001 From: Leonardo Matos Date: Thu, 29 Aug 2019 19:25:47 -0300 Subject: [PATCH] feat(emitter): add events emmiter to constructor --- src/index.js | 12 +++++++++++- src/lib/emitter.js | 3 +++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 src/lib/emitter.js diff --git a/src/index.js b/src/index.js index a1ac23c..dbfff84 100644 --- a/src/index.js +++ b/src/index.js @@ -5,6 +5,7 @@ */ import { _config } from '@ecomplus/utils' +import emitter from './lib/emitter' import addItem from './methods/add-item' import addPoduct from './methods/add-product' @@ -34,7 +35,7 @@ import clear from './methods/clear' const _key = 'ecomShoppingCart' const _storage = typeof window === 'object' && window.localStorage -export default function (storeId, storageKey = _key, localStorage = _storage) { +const EcomCart = function (storeId, storageKey = _key, localStorage = _storage) { const self = this /** @@ -95,6 +96,15 @@ export default function (storeId, storageKey = _key, localStorage = _storage) { } } +// events emitter +;['on', 'off', 'once'].forEach(method => { + EcomCart[method] = (ev, fn) => { + emitter[method](ev, fn) + } +}) + +export default EcomCart + /** * Construct a new shopping cart instance object. * @class EcomCart diff --git a/src/lib/emitter.js b/src/lib/emitter.js new file mode 100644 index 0000000..e4aa502 --- /dev/null +++ b/src/lib/emitter.js @@ -0,0 +1,3 @@ +import EventEmitter from 'eventemitter3' +const emitter = new EventEmitter() +export default emitter