diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..aec0949 --- /dev/null +++ b/.npmignore @@ -0,0 +1,4 @@ +/src/ +/scripts/ +/test/ +/webpack/ \ No newline at end of file diff --git a/README.md b/README.md index fa81d5b..7ad33ac 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,74 @@ -# eventra -A lightweight, but powerful JavaScript event manger that will work in both in the browser and node.js. +

+ Eventra
+ + + + + + + +

+ +Eventra is a lightweight, but powerful JavaScript/TypeScript event manager that comes with built in Type Support and browser/node.js compatibility. Eventra is designed to closely resemble Node.JS Event Emitters so that you can very easily and quickly switch between the two. + +# Type Safety +With the ability to use this library with in TypeScript, we have given you the ability to add type definitions directly into the Event Emitter. Once you have added your types, you can very quickly and very easily begin using your type safe events. + +### Type Safe Implementation +```ts +// Declare your events interface +interface MyEvents { + 'greeting': (message: string) => void; +} + +const eventra = new Eventra(); + +// Create your event listener +eventra.on('greeting', (msg) => { + console.log("Received a greeting message:", msg); +}); + +// Emit your new event +eventra.emit('greeting', "Hello User"); +``` + +# JavaScript Implementation +To implement this into your JavaScript application, it is a very similar process to the one listed above. Simply declare your event emitter and start using it. + +### Plain JavaScript example +```js +const eventra = new Eventra(); + +// Create your event listener +eventra.on('greeting', (msg) => { + console.log("Received a greeting message:", msg); +}); + +// Emit your new event +eventra.emit('greeting', "Hello User"); +``` + +# Browswer Implementation +When we ship this library, we also ship it with a webpacked version so that you may simply slot it into the browser. To do so you can use the `Eventra` namespace and call the class from that. Here is an example as such: + +### Browswer Example +```html + + +``` \ No newline at end of file diff --git a/dist/eventra.js b/dist/eventra.js index 11b1752..0b2a430 100644 --- a/dist/eventra.js +++ b/dist/eventra.js @@ -80,10 +80,6 @@ class Eventra { } } exports.Eventra = Eventra; -const ev = new Eventra(); -ev.on("hello", (msg) => { - console.log("Recieved greeting:", msg); -}); /***/ }), diff --git a/dist/eventra.min.js b/dist/eventra.min.js index 45cb471..ec1d74a 100644 --- a/dist/eventra.min.js +++ b/dist/eventra.min.js @@ -1 +1 @@ -var Eventra;(()=>{"use strict";var t={568:function(t,e,r){var s=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0}),e.Eventra=void 0;const n=s(r(351));class i{constructor(){this._listeners=new n.default({mode:"recurring"}),this._singularListeners=new n.default({mode:"once"}),this.addListener=this.on,this.off=this.removeListener}emit(t,...e){this._listeners.executeEvent(t,...e),this._singularListeners.executeEvent(t,...e)}eventNames(){let t=[];return this._listeners.storage.map(((e,r)=>{t.includes(r)||t.push(r)})),this._singularListeners.storage.map(((e,r)=>{t.includes(r)||t.push(r)})),t}listenerCount(t){return this._listeners.countListeners(t)+this._singularListeners.countListeners(t)}listeners(t){return{recurring:this._listeners.fetchListeners(t),singular:this._singularListeners.fetchListeners(t)}}on(t,e){this._listeners.add(t,e)}once(t,e){this._singularListeners.add(t,e)}prependListener(t,e){this._listeners.prepend(t,e)}prependOnceListener(t,e){this._singularListeners.prepend(t,e)}removeAllListeners(...t){return[...t].map((t=>{this._listeners.removeEvent(t),this._singularListeners.removeEvent(t)})),this}removeListener(t,e){return this._listeners.removeListener(t,e),this._singularListeners.removeListener(t,e),this}}e.Eventra=i,(new i).on("hello",(t=>{console.log("Recieved greeting:",t)}))},351:function(t,e,r){var s=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0});const n=s(r(286));e.default=class{constructor(t){this._internalStorage=new n.default,this._options=t||{mode:"recurring"}}get mode(){return this._options.mode}get storage(){return this._internalStorage}_updateInternalStorage(t){t&&(this._internalStorage=t)}_cloneInternalStorage(){return this._internalStorage.clone()}add(t,e){let r=this._cloneInternalStorage(),s=r.get(t);return s?(s.push(e),this._updateInternalStorage(r),this):(r.set(t,[e]),this._updateInternalStorage(r),this)}prepend(t,e){let r=this._cloneInternalStorage(),s=r.get(t);return s?(s.unshift(e),this._updateInternalStorage(r),this):(r.set(t,[e]),this._updateInternalStorage(r),this)}removeListener(t,e){let r=this._cloneInternalStorage(),s=r.get(t);if(!s)return this;if(!s.includes(e))return this;if(1==s.length)return r.delete(t),this._updateInternalStorage(r),this;const n=s.indexOf(e);return n>-1&&s.splice(n,1),this._updateInternalStorage(r),this}removeEvent(t){let e=this._cloneInternalStorage();return e.get(t)?(e.delete(t),this._updateInternalStorage(e),this):this}countListeners(t){const e=this._internalStorage.get(t);return e?e.length:0}fetchListeners(t){return this._internalStorage.get(t)||[]}executeEvent(t,...e){let r=this._cloneInternalStorage(),s=r.get(t);return s?(s.map(((t,r)=>{t(...e)})),"once"==this.mode&&r.delete(t),this._updateInternalStorage(r),this):this}}},286:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.Collection=void 0;class r extends Map{get(t){return super.get(t)}set(t,e){return super.set(t,e)}has(t){return super.has(t)}delete(t){return super.delete(t)}clear(){return super.clear()}hasAll(...t){return t.every((t=>super.has(t)))}hasAny(...t){return t.some((t=>super.has(t)))}first(t){if(void 0===t)return this.values().next().value;if(t<0)return this.last(-1*t);t=Math.min(this.size,t);const e=this.values();return Array.from({length:t},(()=>e.next().value))}firstKey(t){if(void 0===t)return this.keys().next().value;if(t<0)return this.lastKey(-1*t);t=Math.min(this.size,t);const e=this.keys();return Array.from({length:t},(()=>e.next().value))}last(t){const e=[...this.values()];return void 0===t?e[e.length-1]:t<0?this.first(-1*t):t?e.slice(-t):[]}lastKey(t){const e=[...this.keys()];return void 0===t?e[e.length-1]:t<0?this.firstKey(-1*t):t?e.slice(-t):[]}random(t){const e=[...this.values()];return void 0===t?e[Math.floor(Math.random()*e.length)]:e.length&&t?Array.from({length:Math.min(t,e.length)},(()=>e.splice(Math.floor(Math.random()*e.length),1)[0])):[]}randomKey(t){const e=[...this.keys()];return void 0===t?e[Math.floor(Math.random()*e.length)]:e.length&&t?Array.from({length:Math.min(t,e.length)},(()=>e.splice(Math.floor(Math.random()*e.length),1)[0])):[]}find(t,e){void 0!==e&&(t=t.bind(e));for(const[e,r]of this)if(t(r,e,this))return r}findKey(t,e){void 0!==e&&(t=t.bind(e));for(const[e,r]of this)if(t(r,e,this))return e}sweep(t,e){void 0!==e&&(t=t.bind(e));const r=this.size;for(const[e,r]of this)t(r,e,this)&&this.delete(e);return r-this.size}filter(t,e){void 0!==e&&(t=t.bind(e));const r=new this.constructor[Symbol.species];for(const[e,s]of this)t(s,e,this)&&r.set(e,s);return r}partition(t,e){void 0!==e&&(t=t.bind(e));const r=[new this.constructor[Symbol.species],new this.constructor[Symbol.species]];for(const[e,s]of this)t(s,e,this)?r[0].set(e,s):r[1].set(e,s);return r}flatMap(t,e){const r=this.map(t,e);return(new this.constructor[Symbol.species]).concat(...r)}map(t,e){void 0!==e&&(t=t.bind(e));const r=this.entries();return Array.from({length:this.size},(()=>{const[e,s]=r.next().value;return t(s,e,this)}))}mapValues(t,e){void 0!==e&&(t=t.bind(e));const r=new this.constructor[Symbol.species];for(const[e,s]of this)r.set(e,t(s,e,this));return r}some(t,e){void 0!==e&&(t=t.bind(e));for(const[e,r]of this)if(t(r,e,this))return!0;return!1}every(t,e){void 0!==e&&(t=t.bind(e));for(const[e,r]of this)if(!t(r,e,this))return!1;return!0}reduce(t,e){let r;if(void 0!==e){r=e;for(const[e,s]of this)r=t(r,s,e,this);return r}let s=!0;for(const[e,n]of this)s?(r=n,s=!1):r=t(r,n,e,this);if(s)throw new TypeError("Reduce of empty collection with no initial value");return r}each(t,e){return this.forEach(t,e),this}tap(t,e){return void 0!==e&&(t=t.bind(e)),t(this),this}clone(){return new this.constructor[Symbol.species](this)}concat(...t){const e=this.clone();for(const r of t)for(const[t,s]of r)e.set(t,s);return e}equals(t){if(!t)return!1;if(this===t)return!0;if(this.size!==t.size)return!1;for(const[e,r]of this)if(!t.has(e)||r!==t.get(e))return!1;return!0}sort(t=r.defaultSort){const e=[...this.entries()];e.sort(((e,r)=>t(e[1],r[1],e[0],r[0]))),super.clear();for(const[t,r]of e)super.set(t,r);return this}intersect(t){const e=new this.constructor[Symbol.species];for(const[r,s]of t)this.has(r)&&e.set(r,s);return e}difference(t){const e=new this.constructor[Symbol.species];for(const[r,s]of t)this.has(r)||e.set(r,s);for(const[r,s]of this)t.has(r)||e.set(r,s);return e}sorted(t=r.defaultSort){return new this.constructor[Symbol.species](this).sort(((e,r,s,n)=>t(e,r,s,n)))}static defaultSort(t,e){return Number(t>e)||Number(t===e)-1}}e.Collection=r,r.default=r,e.default=r}},e={},r=function r(s){var n=e[s];if(void 0!==n)return n.exports;var i=e[s]={exports:{}};return t[s].call(i.exports,i,i.exports,r),i.exports}(568);Eventra=r})(); \ No newline at end of file +var Eventra;(()=>{"use strict";var t={568:function(t,e,r){var s=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0}),e.Eventra=void 0;const n=s(r(351));e.Eventra=class{constructor(){this._listeners=new n.default({mode:"recurring"}),this._singularListeners=new n.default({mode:"once"}),this.addListener=this.on,this.off=this.removeListener}emit(t,...e){this._listeners.executeEvent(t,...e),this._singularListeners.executeEvent(t,...e)}eventNames(){let t=[];return this._listeners.storage.map(((e,r)=>{t.includes(r)||t.push(r)})),this._singularListeners.storage.map(((e,r)=>{t.includes(r)||t.push(r)})),t}listenerCount(t){return this._listeners.countListeners(t)+this._singularListeners.countListeners(t)}listeners(t){return{recurring:this._listeners.fetchListeners(t),singular:this._singularListeners.fetchListeners(t)}}on(t,e){this._listeners.add(t,e)}once(t,e){this._singularListeners.add(t,e)}prependListener(t,e){this._listeners.prepend(t,e)}prependOnceListener(t,e){this._singularListeners.prepend(t,e)}removeAllListeners(...t){return[...t].map((t=>{this._listeners.removeEvent(t),this._singularListeners.removeEvent(t)})),this}removeListener(t,e){return this._listeners.removeListener(t,e),this._singularListeners.removeListener(t,e),this}}},351:function(t,e,r){var s=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0});const n=s(r(286));e.default=class{constructor(t){this._internalStorage=new n.default,this._options=t||{mode:"recurring"}}get mode(){return this._options.mode}get storage(){return this._internalStorage}_updateInternalStorage(t){t&&(this._internalStorage=t)}_cloneInternalStorage(){return this._internalStorage.clone()}add(t,e){let r=this._cloneInternalStorage(),s=r.get(t);return s?(s.push(e),this._updateInternalStorage(r),this):(r.set(t,[e]),this._updateInternalStorage(r),this)}prepend(t,e){let r=this._cloneInternalStorage(),s=r.get(t);return s?(s.unshift(e),this._updateInternalStorage(r),this):(r.set(t,[e]),this._updateInternalStorage(r),this)}removeListener(t,e){let r=this._cloneInternalStorage(),s=r.get(t);if(!s)return this;if(!s.includes(e))return this;if(1==s.length)return r.delete(t),this._updateInternalStorage(r),this;const n=s.indexOf(e);return n>-1&&s.splice(n,1),this._updateInternalStorage(r),this}removeEvent(t){let e=this._cloneInternalStorage();return e.get(t)?(e.delete(t),this._updateInternalStorage(e),this):this}countListeners(t){const e=this._internalStorage.get(t);return e?e.length:0}fetchListeners(t){return this._internalStorage.get(t)||[]}executeEvent(t,...e){let r=this._cloneInternalStorage(),s=r.get(t);return s?(s.map(((t,r)=>{t(...e)})),"once"==this.mode&&r.delete(t),this._updateInternalStorage(r),this):this}}},286:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.Collection=void 0;class r extends Map{get(t){return super.get(t)}set(t,e){return super.set(t,e)}has(t){return super.has(t)}delete(t){return super.delete(t)}clear(){return super.clear()}hasAll(...t){return t.every((t=>super.has(t)))}hasAny(...t){return t.some((t=>super.has(t)))}first(t){if(void 0===t)return this.values().next().value;if(t<0)return this.last(-1*t);t=Math.min(this.size,t);const e=this.values();return Array.from({length:t},(()=>e.next().value))}firstKey(t){if(void 0===t)return this.keys().next().value;if(t<0)return this.lastKey(-1*t);t=Math.min(this.size,t);const e=this.keys();return Array.from({length:t},(()=>e.next().value))}last(t){const e=[...this.values()];return void 0===t?e[e.length-1]:t<0?this.first(-1*t):t?e.slice(-t):[]}lastKey(t){const e=[...this.keys()];return void 0===t?e[e.length-1]:t<0?this.firstKey(-1*t):t?e.slice(-t):[]}random(t){const e=[...this.values()];return void 0===t?e[Math.floor(Math.random()*e.length)]:e.length&&t?Array.from({length:Math.min(t,e.length)},(()=>e.splice(Math.floor(Math.random()*e.length),1)[0])):[]}randomKey(t){const e=[...this.keys()];return void 0===t?e[Math.floor(Math.random()*e.length)]:e.length&&t?Array.from({length:Math.min(t,e.length)},(()=>e.splice(Math.floor(Math.random()*e.length),1)[0])):[]}find(t,e){void 0!==e&&(t=t.bind(e));for(const[e,r]of this)if(t(r,e,this))return r}findKey(t,e){void 0!==e&&(t=t.bind(e));for(const[e,r]of this)if(t(r,e,this))return e}sweep(t,e){void 0!==e&&(t=t.bind(e));const r=this.size;for(const[e,r]of this)t(r,e,this)&&this.delete(e);return r-this.size}filter(t,e){void 0!==e&&(t=t.bind(e));const r=new this.constructor[Symbol.species];for(const[e,s]of this)t(s,e,this)&&r.set(e,s);return r}partition(t,e){void 0!==e&&(t=t.bind(e));const r=[new this.constructor[Symbol.species],new this.constructor[Symbol.species]];for(const[e,s]of this)t(s,e,this)?r[0].set(e,s):r[1].set(e,s);return r}flatMap(t,e){const r=this.map(t,e);return(new this.constructor[Symbol.species]).concat(...r)}map(t,e){void 0!==e&&(t=t.bind(e));const r=this.entries();return Array.from({length:this.size},(()=>{const[e,s]=r.next().value;return t(s,e,this)}))}mapValues(t,e){void 0!==e&&(t=t.bind(e));const r=new this.constructor[Symbol.species];for(const[e,s]of this)r.set(e,t(s,e,this));return r}some(t,e){void 0!==e&&(t=t.bind(e));for(const[e,r]of this)if(t(r,e,this))return!0;return!1}every(t,e){void 0!==e&&(t=t.bind(e));for(const[e,r]of this)if(!t(r,e,this))return!1;return!0}reduce(t,e){let r;if(void 0!==e){r=e;for(const[e,s]of this)r=t(r,s,e,this);return r}let s=!0;for(const[e,n]of this)s?(r=n,s=!1):r=t(r,n,e,this);if(s)throw new TypeError("Reduce of empty collection with no initial value");return r}each(t,e){return this.forEach(t,e),this}tap(t,e){return void 0!==e&&(t=t.bind(e)),t(this),this}clone(){return new this.constructor[Symbol.species](this)}concat(...t){const e=this.clone();for(const r of t)for(const[t,s]of r)e.set(t,s);return e}equals(t){if(!t)return!1;if(this===t)return!0;if(this.size!==t.size)return!1;for(const[e,r]of this)if(!t.has(e)||r!==t.get(e))return!1;return!0}sort(t=r.defaultSort){const e=[...this.entries()];e.sort(((e,r)=>t(e[1],r[1],e[0],r[0]))),super.clear();for(const[t,r]of e)super.set(t,r);return this}intersect(t){const e=new this.constructor[Symbol.species];for(const[r,s]of t)this.has(r)&&e.set(r,s);return e}difference(t){const e=new this.constructor[Symbol.species];for(const[r,s]of t)this.has(r)||e.set(r,s);for(const[r,s]of this)t.has(r)||e.set(r,s);return e}sorted(t=r.defaultSort){return new this.constructor[Symbol.species](this).sort(((e,r,s,n)=>t(e,r,s,n)))}static defaultSort(t,e){return Number(t>e)||Number(t===e)-1}}e.Collection=r,r.default=r,e.default=r}},e={},r=function r(s){var n=e[s];if(void 0!==n)return n.exports;var i=e[s]={exports:{}};return t[s].call(i.exports,i,i.exports,r),i.exports}(568);Eventra=r})(); \ No newline at end of file diff --git a/package.json b/package.json index 7ca2173..612c92e 100644 --- a/package.json +++ b/package.json @@ -6,9 +6,9 @@ "repository": "https://github.com/duxcore/eventra.git", "author": "Jackson Roberts ", "license": "GPL-3.0", - "private": false, "scripts": { - "build": "zx scripts/build.mjs" + "build": "zx scripts/build.mjs", + "deploy": "yarn build -wp; yarn publish;" }, "devDependencies": { "@vercel/ncc": "^0.29.0",