From 1d33b7d75237eccc079e9105e4faa370af348d25 Mon Sep 17 00:00:00 2001 From: Joel Chu Date: Mon, 15 Jul 2019 14:41:46 +0800 Subject: [PATCH 01/14] remove lerna since we dont actually use it --- README.md | 2 ++ lerna.json | 7 ------- package.json | 6 ------ 3 files changed, 2 insertions(+), 13 deletions(-) delete mode 100755 lerna.json delete mode 100755 package.json diff --git a/README.md b/README.md index f397acfc..c97eabda 100755 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ ![](public/jsonql-logo.png) +> What is JSON:QL? JSON Query Load :) + **IMPORTANT NOTE: This repo is currently under heavy development and many of the existing module will get transfer to our new `@jsonql` npm namespace Please check out website http://jsonql.org for update to date information** diff --git a/lerna.json b/lerna.json deleted file mode 100755 index 9b535f1d..00000000 --- a/lerna.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "lerna": "2.11.0", - "packages": [ - "packages/*" - ], - "version": "0.0.0" -} diff --git a/package.json b/package.json deleted file mode 100755 index bbf8292d..00000000 --- a/package.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "devDependencies": { - "lerna": "^3.15.0" - }, - "dependencies": {} -} -- Gitee From e54cc2767cbf4bd97c11e264540ebca8df46c77e Mon Sep 17 00:00:00 2001 From: Joel Chu Date: Mon, 15 Jul 2019 14:52:13 +0800 Subject: [PATCH 02/14] update the information on package.json for npmjs to pick up --- packages/event/README.md | 2 +- packages/event/package.json | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/event/README.md b/packages/event/README.md index 72daea5f..c71b3042 100644 --- a/packages/event/README.md +++ b/packages/event/README.md @@ -1,6 +1,6 @@ # @jsonql/event -This is ported from [nb-event-service]() and rewritten with Typescript +This is ported from [nb-event-service](https://npmjs.com/package/nb-event-service) and rewritten with Typescript The different is - no alias options diff --git a/packages/event/package.json b/packages/event/package.json index 83876085..600d49a8 100644 --- a/packages/event/package.json +++ b/packages/event/package.json @@ -1,6 +1,6 @@ { "name": "@jsonql/event", - "version": "1.0.0", + "version": "1.0.1", "description": "Ported from nb-event-service rewritten with Typescript", "main": "dist/jsonql-event-service.cjs.js", "browser": "dist/jsonql-event-service.umd.js", @@ -28,6 +28,10 @@ "type": "git", "url": "git+ssh://git@gitee.com:to1source/jsonql.git" }, + "bugs": { + "url": "https://gitee.com:to1source/jsonql/issues" + }, + "homepage": "https://gitee.com:to1source/jsonql#readme", "license": "ISC", "devDependencies": { "ava": "^2.2.0", -- Gitee From 994dcc823f67d0aca2262b0219ed0e10eb96f11f Mon Sep 17 00:00:00 2001 From: Joel Chu Date: Mon, 15 Jul 2019 14:55:25 +0800 Subject: [PATCH 03/14] oops --- packages/event/package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/event/package.json b/packages/event/package.json index 600d49a8..e19fe8df 100644 --- a/packages/event/package.json +++ b/packages/event/package.json @@ -1,6 +1,6 @@ { "name": "@jsonql/event", - "version": "1.0.1", + "version": "1.0.2", "description": "Ported from nb-event-service rewritten with Typescript", "main": "dist/jsonql-event-service.cjs.js", "browser": "dist/jsonql-event-service.umd.js", @@ -29,9 +29,9 @@ "url": "git+ssh://git@gitee.com:to1source/jsonql.git" }, "bugs": { - "url": "https://gitee.com:to1source/jsonql/issues" + "url": "https://gitee.com/to1source/jsonql/issues" }, - "homepage": "https://gitee.com:to1source/jsonql#readme", + "homepage": "https://gitee.com/to1source/jsonql#readme", "license": "ISC", "devDependencies": { "ava": "^2.2.0", -- Gitee From 54a35749248d3fd984cabccf44a16792cdca2a29 Mon Sep 17 00:00:00 2001 From: Joel Chu Date: Mon, 15 Jul 2019 22:50:34 +0800 Subject: [PATCH 04/14] add back all the custom validation because typescript is a completely useless without any run time checking --- .../event/dist/jsonql-event-service.cjs.js | 68 +++++++++++++---- .../event/dist/jsonql-event-service.es.js | 68 +++++++++++++---- .../event/dist/jsonql-event-service.umd.js | 70 ++++++++++++----- packages/event/rollup.config.js | 2 +- packages/event/src/event-service.ts | 75 ++++++++++++++----- packages/event/tsconfig.json | 2 +- 6 files changed, 217 insertions(+), 68 deletions(-) diff --git a/packages/event/dist/jsonql-event-service.cjs.js b/packages/event/dist/jsonql-event-service.cjs.js index 65d55bfd..ca2a4cc0 100644 --- a/packages/event/dist/jsonql-event-service.cjs.js +++ b/packages/event/dist/jsonql-event-service.cjs.js @@ -40,6 +40,7 @@ var JsonqlEventService = /** @class */ (function () { JsonqlEventService.prototype.$on = function (evt, callback, context) { var _this = this; if (context === void 0) { context = null; } + this.validate(evt, callback); var type = 'on'; // first need to check if this evt is in lazy store var lazyStoreContent = this.takeFromStore(evt); @@ -78,6 +79,7 @@ var JsonqlEventService = /** @class */ (function () { */ JsonqlEventService.prototype.$once = function (evt, callback, context) { if (context === void 0) { context = null; } + this.validate(evt, callback); var type = 'once'; var lazyStoreContent = this.takeFromStore(evt); // this is normal register before call $trigger @@ -116,6 +118,7 @@ var JsonqlEventService = /** @class */ (function () { JsonqlEventService.prototype.$only = function (evt, callback, context) { var _this = this; if (context === void 0) { context = null; } + this.validate(evt, callback); var type = 'only'; // keep getting TS232, this is why typescript is a joke // it will just ended up with everything is any type and back to square one @@ -152,6 +155,7 @@ var JsonqlEventService = /** @class */ (function () { */ JsonqlEventService.prototype.$onlyOnce = function (evt, callback, context) { if (context === void 0) { context = null; } + this.validate(evt, callback); var type = 'onlyOnce'; var added = false; var lazyStoreContent = this.takeFromStore(evt); @@ -204,6 +208,7 @@ var JsonqlEventService = /** @class */ (function () { if (payload === void 0) { payload = []; } if (context === void 0) { context = null; } if (type === void 0) { type = false; } + this.validateEvt(evt); var found = 0; // first check the normal store var nStore = this.normalStore; @@ -243,6 +248,7 @@ var JsonqlEventService = /** @class */ (function () { JsonqlEventService.prototype.$call = function (evt, params, type, context) { if (type === void 0) { type = false; } if (context === void 0) { context = null; } + this.validateEvt(evt); var args = [evt, params]; args.push(context, type); return Reflect.apply(this.$trigger, this, args); @@ -253,6 +259,7 @@ var JsonqlEventService = /** @class */ (function () { * @return {boolean} true actually delete something */ JsonqlEventService.prototype.$off = function (evt) { + this.validateEvt(evt); var stores = [this.lazyStore, this.normalStore]; var found = false; stores.forEach(function (store) { @@ -271,6 +278,7 @@ var JsonqlEventService = /** @class */ (function () { */ JsonqlEventService.prototype.$get = function (evt, full) { if (full === void 0) { full = false; } + this.validateEvt(evt); var store = this.normalStore; if (store.has(evt)) { return this @@ -440,21 +448,6 @@ var JsonqlEventService = /** @class */ (function () { return hash === args[0]; }).length; }; - /** - * return all the listener from the event - * @param {string} evtName event name - * @param {boolean} [full=false] if true then return the entire content - * @return {array|boolean} listerner(s) or false when not found - */ - JsonqlEventService.prototype.getByEvt = function (evtName) { - var store = this.normalStore; - if (store.has(evtName)) { - return Array - .from(store.get(evtName)) - .map(function (l) { return l; }); - } - return false; - }; /** * get the existing type to make sure no mix type add to the same store * @param {string} evtName event name @@ -462,7 +455,9 @@ var JsonqlEventService = /** @class */ (function () { * @return {boolean} true you can add, false then you can't add this type */ JsonqlEventService.prototype.checkTypeInStore = function (evtName, type) { - var all = this.getByEvt(evtName); + this.validateEvt(evtName); + this.validateEvt(type); + var all = this.$get(evtName); if (all === false) { // pristine it means you can add return true; @@ -482,6 +477,8 @@ var JsonqlEventService = /** @class */ (function () { * @return {boolean} true OK */ JsonqlEventService.prototype.checkTypeInLazyStore = function (evtName, type) { + this.validateEvt(evtName); + this.validateEvt(type); var store = this.lazyStore.get(evtName); this.logger('checkTypeInLazyStore', store); if (store) { @@ -541,6 +538,45 @@ var JsonqlEventService = /** @class */ (function () { this.lazyStore = _store; return size; }; + // validators + // This is another JOKE about Typescript, after you spend all this time on + // fixing the non-error (aka typing) it doesn't do anything during run time + // so what the fuck? + /** + * validate the event name + * @param {string} evt event name + * @return {boolean} true when OK + */ + JsonqlEventService.prototype.validateEvt = function (evt) { + if (typeof evt === 'string') { + return true; + } + throw new Error("event name must be string type!"); + }; + /** + * Simple quick check on the two main parameters + * @param {string} evt event name + * @param {function} callback function to call + * @return {boolean} true when OK + */ + JsonqlEventService.prototype.validate = function (evt, callback) { + if (this.validateEvt(evt)) { + if (typeof callback === 'function') { + return true; + } + } + throw new Error("callback required to be function type!"); + }; + /** + * Check if this type is correct or not added in V1.5.0 + * @param {string} type for checking + * @return {boolean} true on OK + */ + JsonqlEventService.prototype.validateType = function (type) { + var types = ['on', 'only', 'once', 'onlyOnce']; + return !!types + .filter(function (t) { return type === t; }).length; + }; return JsonqlEventService; }()); diff --git a/packages/event/dist/jsonql-event-service.es.js b/packages/event/dist/jsonql-event-service.es.js index 9c96af46..03c499f3 100644 --- a/packages/event/dist/jsonql-event-service.es.js +++ b/packages/event/dist/jsonql-event-service.es.js @@ -36,6 +36,7 @@ var JsonqlEventService = /** @class */ (function () { JsonqlEventService.prototype.$on = function (evt, callback, context) { var _this = this; if (context === void 0) { context = null; } + this.validate(evt, callback); var type = 'on'; // first need to check if this evt is in lazy store var lazyStoreContent = this.takeFromStore(evt); @@ -74,6 +75,7 @@ var JsonqlEventService = /** @class */ (function () { */ JsonqlEventService.prototype.$once = function (evt, callback, context) { if (context === void 0) { context = null; } + this.validate(evt, callback); var type = 'once'; var lazyStoreContent = this.takeFromStore(evt); // this is normal register before call $trigger @@ -112,6 +114,7 @@ var JsonqlEventService = /** @class */ (function () { JsonqlEventService.prototype.$only = function (evt, callback, context) { var _this = this; if (context === void 0) { context = null; } + this.validate(evt, callback); var type = 'only'; // keep getting TS232, this is why typescript is a joke // it will just ended up with everything is any type and back to square one @@ -148,6 +151,7 @@ var JsonqlEventService = /** @class */ (function () { */ JsonqlEventService.prototype.$onlyOnce = function (evt, callback, context) { if (context === void 0) { context = null; } + this.validate(evt, callback); var type = 'onlyOnce'; var added = false; var lazyStoreContent = this.takeFromStore(evt); @@ -200,6 +204,7 @@ var JsonqlEventService = /** @class */ (function () { if (payload === void 0) { payload = []; } if (context === void 0) { context = null; } if (type === void 0) { type = false; } + this.validateEvt(evt); var found = 0; // first check the normal store var nStore = this.normalStore; @@ -239,6 +244,7 @@ var JsonqlEventService = /** @class */ (function () { JsonqlEventService.prototype.$call = function (evt, params, type, context) { if (type === void 0) { type = false; } if (context === void 0) { context = null; } + this.validateEvt(evt); var args = [evt, params]; args.push(context, type); return Reflect.apply(this.$trigger, this, args); @@ -249,6 +255,7 @@ var JsonqlEventService = /** @class */ (function () { * @return {boolean} true actually delete something */ JsonqlEventService.prototype.$off = function (evt) { + this.validateEvt(evt); var stores = [this.lazyStore, this.normalStore]; var found = false; stores.forEach(function (store) { @@ -267,6 +274,7 @@ var JsonqlEventService = /** @class */ (function () { */ JsonqlEventService.prototype.$get = function (evt, full) { if (full === void 0) { full = false; } + this.validateEvt(evt); var store = this.normalStore; if (store.has(evt)) { return this @@ -436,21 +444,6 @@ var JsonqlEventService = /** @class */ (function () { return hash === args[0]; }).length; }; - /** - * return all the listener from the event - * @param {string} evtName event name - * @param {boolean} [full=false] if true then return the entire content - * @return {array|boolean} listerner(s) or false when not found - */ - JsonqlEventService.prototype.getByEvt = function (evtName) { - var store = this.normalStore; - if (store.has(evtName)) { - return Array - .from(store.get(evtName)) - .map(function (l) { return l; }); - } - return false; - }; /** * get the existing type to make sure no mix type add to the same store * @param {string} evtName event name @@ -458,7 +451,9 @@ var JsonqlEventService = /** @class */ (function () { * @return {boolean} true you can add, false then you can't add this type */ JsonqlEventService.prototype.checkTypeInStore = function (evtName, type) { - var all = this.getByEvt(evtName); + this.validateEvt(evtName); + this.validateEvt(type); + var all = this.$get(evtName); if (all === false) { // pristine it means you can add return true; @@ -478,6 +473,8 @@ var JsonqlEventService = /** @class */ (function () { * @return {boolean} true OK */ JsonqlEventService.prototype.checkTypeInLazyStore = function (evtName, type) { + this.validateEvt(evtName); + this.validateEvt(type); var store = this.lazyStore.get(evtName); this.logger('checkTypeInLazyStore', store); if (store) { @@ -537,6 +534,45 @@ var JsonqlEventService = /** @class */ (function () { this.lazyStore = _store; return size; }; + // validators + // This is another JOKE about Typescript, after you spend all this time on + // fixing the non-error (aka typing) it doesn't do anything during run time + // so what the fuck? + /** + * validate the event name + * @param {string} evt event name + * @return {boolean} true when OK + */ + JsonqlEventService.prototype.validateEvt = function (evt) { + if (typeof evt === 'string') { + return true; + } + throw new Error("event name must be string type!"); + }; + /** + * Simple quick check on the two main parameters + * @param {string} evt event name + * @param {function} callback function to call + * @return {boolean} true when OK + */ + JsonqlEventService.prototype.validate = function (evt, callback) { + if (this.validateEvt(evt)) { + if (typeof callback === 'function') { + return true; + } + } + throw new Error("callback required to be function type!"); + }; + /** + * Check if this type is correct or not added in V1.5.0 + * @param {string} type for checking + * @return {boolean} true on OK + */ + JsonqlEventService.prototype.validateType = function (type) { + var types = ['on', 'only', 'once', 'onlyOnce']; + return !!types + .filter(function (t) { return type === t; }).length; + }; return JsonqlEventService; }()); diff --git a/packages/event/dist/jsonql-event-service.umd.js b/packages/event/dist/jsonql-event-service.umd.js index a1629268..155cd846 100644 --- a/packages/event/dist/jsonql-event-service.umd.js +++ b/packages/event/dist/jsonql-event-service.umd.js @@ -1,7 +1,7 @@ (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : - (global = global || self, factory(global.Jsonql = {})); + (global = global || self, factory(global.JsonqlEventService = {})); }(this, function (exports) { 'use strict'; /** @@ -42,6 +42,7 @@ JsonqlEventService.prototype.$on = function (evt, callback, context) { var _this = this; if (context === void 0) { context = null; } + this.validate(evt, callback); var type = 'on'; // first need to check if this evt is in lazy store var lazyStoreContent = this.takeFromStore(evt); @@ -80,6 +81,7 @@ */ JsonqlEventService.prototype.$once = function (evt, callback, context) { if (context === void 0) { context = null; } + this.validate(evt, callback); var type = 'once'; var lazyStoreContent = this.takeFromStore(evt); // this is normal register before call $trigger @@ -118,6 +120,7 @@ JsonqlEventService.prototype.$only = function (evt, callback, context) { var _this = this; if (context === void 0) { context = null; } + this.validate(evt, callback); var type = 'only'; // keep getting TS232, this is why typescript is a joke // it will just ended up with everything is any type and back to square one @@ -154,6 +157,7 @@ */ JsonqlEventService.prototype.$onlyOnce = function (evt, callback, context) { if (context === void 0) { context = null; } + this.validate(evt, callback); var type = 'onlyOnce'; var added = false; var lazyStoreContent = this.takeFromStore(evt); @@ -206,6 +210,7 @@ if (payload === void 0) { payload = []; } if (context === void 0) { context = null; } if (type === void 0) { type = false; } + this.validateEvt(evt); var found = 0; // first check the normal store var nStore = this.normalStore; @@ -245,6 +250,7 @@ JsonqlEventService.prototype.$call = function (evt, params, type, context) { if (type === void 0) { type = false; } if (context === void 0) { context = null; } + this.validateEvt(evt); var args = [evt, params]; args.push(context, type); return Reflect.apply(this.$trigger, this, args); @@ -255,6 +261,7 @@ * @return {boolean} true actually delete something */ JsonqlEventService.prototype.$off = function (evt) { + this.validateEvt(evt); var stores = [this.lazyStore, this.normalStore]; var found = false; stores.forEach(function (store) { @@ -273,6 +280,7 @@ */ JsonqlEventService.prototype.$get = function (evt, full) { if (full === void 0) { full = false; } + this.validateEvt(evt); var store = this.normalStore; if (store.has(evt)) { return this @@ -442,21 +450,6 @@ return hash === args[0]; }).length; }; - /** - * return all the listener from the event - * @param {string} evtName event name - * @param {boolean} [full=false] if true then return the entire content - * @return {array|boolean} listerner(s) or false when not found - */ - JsonqlEventService.prototype.getByEvt = function (evtName) { - var store = this.normalStore; - if (store.has(evtName)) { - return Array - .from(store.get(evtName)) - .map(function (l) { return l; }); - } - return false; - }; /** * get the existing type to make sure no mix type add to the same store * @param {string} evtName event name @@ -464,7 +457,9 @@ * @return {boolean} true you can add, false then you can't add this type */ JsonqlEventService.prototype.checkTypeInStore = function (evtName, type) { - var all = this.getByEvt(evtName); + this.validateEvt(evtName); + this.validateEvt(type); + var all = this.$get(evtName); if (all === false) { // pristine it means you can add return true; @@ -484,6 +479,8 @@ * @return {boolean} true OK */ JsonqlEventService.prototype.checkTypeInLazyStore = function (evtName, type) { + this.validateEvt(evtName); + this.validateEvt(type); var store = this.lazyStore.get(evtName); this.logger('checkTypeInLazyStore', store); if (store) { @@ -543,6 +540,45 @@ this.lazyStore = _store; return size; }; + // validators + // This is another JOKE about Typescript, after you spend all this time on + // fixing the non-error (aka typing) it doesn't do anything during run time + // so what the fuck? + /** + * validate the event name + * @param {string} evt event name + * @return {boolean} true when OK + */ + JsonqlEventService.prototype.validateEvt = function (evt) { + if (typeof evt === 'string') { + return true; + } + throw new Error("event name must be string type!"); + }; + /** + * Simple quick check on the two main parameters + * @param {string} evt event name + * @param {function} callback function to call + * @return {boolean} true when OK + */ + JsonqlEventService.prototype.validate = function (evt, callback) { + if (this.validateEvt(evt)) { + if (typeof callback === 'function') { + return true; + } + } + throw new Error("callback required to be function type!"); + }; + /** + * Check if this type is correct or not added in V1.5.0 + * @param {string} type for checking + * @return {boolean} true on OK + */ + JsonqlEventService.prototype.validateType = function (type) { + var types = ['on', 'only', 'once', 'onlyOnce']; + return !!types + .filter(function (t) { return type === t; }).length; + }; return JsonqlEventService; }()); diff --git a/packages/event/rollup.config.js b/packages/event/rollup.config.js index 7220fd53..eed25546 100644 --- a/packages/event/rollup.config.js +++ b/packages/event/rollup.config.js @@ -19,7 +19,7 @@ export default { } ], external: [ - ...Object.keys(pkg.depedencies || {}), + ...Object.keys(pkg.dependencies || {}), ...Object.keys(pkg.peerDependencies || {}) ], plugins: [ diff --git a/packages/event/src/event-service.ts b/packages/event/src/event-service.ts index 679ec3f7..0b110074 100644 --- a/packages/event/src/event-service.ts +++ b/packages/event/src/event-service.ts @@ -36,6 +36,7 @@ export class JsonqlEventService { * @return {number} the size of the store */ public $on(evt: string, callback: myCallbackType, context: any = null): any { + this.validate(evt, callback) const type = 'on'; // first need to check if this evt is in lazy store let lazyStoreContent = this.takeFromStore(evt) @@ -75,6 +76,7 @@ export class JsonqlEventService { * @return {number|boolean} result */ public $once(evt: string , callback: myCallbackType , context: any = null): any { + this.validate(evt, callback) const type = 'once'; let lazyStoreContent = this.takeFromStore(evt) // this is normal register before call $trigger @@ -111,6 +113,7 @@ export class JsonqlEventService { * @return {boolean} true bind for first time, false already existed */ public $only(evt: string, callback: myCallbackType, context: any = null): boolean { + this.validate(evt, callback) const type = 'only'; // keep getting TS232, this is why typescript is a joke // it will just ended up with everything is any type and back to square one @@ -147,6 +150,7 @@ export class JsonqlEventService { * @return {boolean} same as above */ public $onlyOnce(evt: string, callback: myCallbackType, context: any = null): boolean { + this.validate(evt, callback) const type = 'onlyOnce'; let added: any = false; let lazyStoreContent = this.takeFromStore(evt) @@ -196,6 +200,7 @@ export class JsonqlEventService { * @return {number} if it has been execute how many times */ public $trigger(evt: string , payload: any[] = [] , context: any = null, type: string|boolean = false): number { + this.validateEvt(evt) let found = 0; // first check the normal store let nStore = this.normalStore; @@ -235,6 +240,7 @@ export class JsonqlEventService { * @return {*} from $trigger */ public $call(evt: string, params: any[], type: any = false, context: any = null): any { + this.validateEvt(evt) let args = [evt, params] args.push(context, type) return Reflect.apply(this.$trigger, this, args) @@ -246,6 +252,7 @@ export class JsonqlEventService { * @return {boolean} true actually delete something */ public $off(evt: string): boolean { + this.validateEvt(evt) let stores = [ this.lazyStore, this.normalStore ] let found = false; stores.forEach(store => { @@ -264,6 +271,7 @@ export class JsonqlEventService { * @return {array|boolean} listerner(s) or false when not found */ public $get(evt: string, full: boolean = false): any { + this.validateEvt(evt) let store = this.normalStore; if (store.has(evt)) { return this @@ -435,22 +443,6 @@ export class JsonqlEventService { }).length; } - /** - * return all the listener from the event - * @param {string} evtName event name - * @param {boolean} [full=false] if true then return the entire content - * @return {array|boolean} listerner(s) or false when not found - */ - private getByEvt(evtName: string): any { - let store = this.normalStore; - if (store.has(evtName)) { - return Array - .from(store.get(evtName)) - .map((l: any): any[] => l) - } - return false; - } - /** * get the existing type to make sure no mix type add to the same store * @param {string} evtName event name @@ -458,7 +450,9 @@ export class JsonqlEventService { * @return {boolean} true you can add, false then you can't add this type */ private checkTypeInStore(evtName: string, type: string): boolean { - let all = this.getByEvt(evtName) + this.validateEvt(evtName) + this.validateEvt(type) + let all = this.$get(evtName) if (all === false) { // pristine it means you can add return true; @@ -479,6 +473,8 @@ export class JsonqlEventService { * @return {boolean} true OK */ private checkTypeInLazyStore(evtName: string, type: string): boolean { + this.validateEvt(evtName) + this.validateEvt(type) let store = this.lazyStore.get(evtName) this.logger('checkTypeInLazyStore', store) if (store) { @@ -537,4 +533,49 @@ export class JsonqlEventService { this.lazyStore = _store; return size; } + + // validators + // This is another JOKE about Typescript, after you spend all this time on + // fixing the non-error (aka typing) it doesn't do anything during run time + // so what the fuck? + /** + * validate the event name + * @param {string} evt event name + * @return {boolean} true when OK + */ + private validateEvt(evt: string): boolean { + if (typeof evt === 'string') { + return true; + } + throw new Error(`event name must be string type!`) + } + + /** + * Simple quick check on the two main parameters + * @param {string} evt event name + * @param {function} callback function to call + * @return {boolean} true when OK + */ + private validate(evt: string, callback: myCallbackType): boolean { + if (this.validateEvt(evt)) { + if (typeof callback === 'function') { + return true; + } + } + throw new Error(`callback required to be function type!`) + } + + /** + * Check if this type is correct or not added in V1.5.0 + * @param {string} type for checking + * @return {boolean} true on OK + */ + private validateType(type: string): boolean { + const types = ['on', 'only', 'once', 'onlyOnce'] + return !!types + .filter( + (t: any): boolean => type === t + ).length; + } + } diff --git a/packages/event/tsconfig.json b/packages/event/tsconfig.json index 5909d0cb..37a8428c 100644 --- a/packages/event/tsconfig.json +++ b/packages/event/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { /* Basic Options */ // "incremental": true, /* Enable incremental compilation */ - "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */ + "target": "ES5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */ "module": "es2015", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ // "lib": [], /* Specify library files to be included in the compilation. */ // "allowJs": true, /* Allow javascript files to be compiled. */ -- Gitee From 6a52e94d8f59c14857bd120e5a31d507ff9ca082 Mon Sep 17 00:00:00 2001 From: Joel Chu Date: Mon, 15 Jul 2019 23:07:59 +0800 Subject: [PATCH 05/14] somehow adding back the validation create a bug? --- packages/event/README.md | 10 +++++----- packages/event/package.json | 3 ++- packages/event/tests/once-problem.test.js | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/event/README.md b/packages/event/README.md index c71b3042..b2c360de 100644 --- a/packages/event/README.md +++ b/packages/event/README.md @@ -8,14 +8,12 @@ The different is - no alias options You don't usually use this directly, this is part of the `@jsonql` modules. -But it's design to work standalone: +But it can work standalone: ```sh $ npm i @jsonql/event ``` -For directly use in browser, you can use the `dist/jsonql-event-service.umd.js` - And to use it directly in browser you have to do this ```js @@ -25,8 +23,10 @@ var eventService = new JsonqlEventService.JsonqlEventService() I know it sucks big time, but that's the way how Typescript export when using name ... might change in the future when I figure out a better way, such as using a `jsonql` namespace -to encapsulate all the browser modules. But for now you have to kinda suck it up, if you want -to use it directly. I would recommend that you don't and use [nb-event-service](https://npmjs.org/package/nb-event-service) instead. +to encapsulate all the browser modules. But for now you have to kinda suck it up. + +I would recommend that you don't use this module (Typescript is not as great as most keep blowing it to be); +you should use [nb-event-service](https://npmjs.org/package/nb-event-service) instead. For the other that use node build tool, just do this: diff --git a/packages/event/package.json b/packages/event/package.json index e19fe8df..17b0b4b1 100644 --- a/packages/event/package.json +++ b/packages/event/package.json @@ -15,7 +15,8 @@ "build": "rollup -c", "dev": "rollup -cw", "test:basic": "DEBUG=nb-event-service* ava ./tests/basic.test.js", - "test:browser": "node ./tests/fixtures/browser.js" + "test:browser": "node ./tests/fixtures/browser.js", + "test:once-problem": "DEBUG=nb-event-service* ava ./tests/once-problem.test.js" }, "keywords": [ "jsonql", diff --git a/packages/event/tests/once-problem.test.js b/packages/event/tests/once-problem.test.js index 88cb5f0e..8b23c07e 100644 --- a/packages/event/tests/once-problem.test.js +++ b/packages/event/tests/once-problem.test.js @@ -77,7 +77,7 @@ test.only('It should be fixed with the check type before adding to the store, bu t.is(evtSrv.$done, 1200) evtSrv.$trigger(evtName, 2000) - // but this work as expecte because the order of adding to it + // but this work as expected because the order of adding to it // where the last one was taken out from the lazyStore t.is(evtSrv.$done, 2600) -- Gitee From 30a2dfa818b4b2000ea6d6708cc2cdc4572abb5f Mon Sep 17 00:00:00 2001 From: Joel Chu Date: Tue, 16 Jul 2019 09:24:35 +0800 Subject: [PATCH 06/14] update the validation methods --- packages/event/README.md | 7 ++++--- packages/event/src/event-service.ts | 24 ++++++++++++------------ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/packages/event/README.md b/packages/event/README.md index b2c360de..4cd91028 100644 --- a/packages/event/README.md +++ b/packages/event/README.md @@ -14,18 +14,18 @@ But it can work standalone: $ npm i @jsonql/event ``` -And to use it directly in browser you have to do this +When using it directly in browser you have to do this: ```js var eventService = new JsonqlEventService.JsonqlEventService() // the rest of the code ``` -I know it sucks big time, but that's the way how Typescript export when using name ... +I know it sucks big time, but that's the way how Typescript export when using name option with UMD ... might change in the future when I figure out a better way, such as using a `jsonql` namespace to encapsulate all the browser modules. But for now you have to kinda suck it up. -I would recommend that you don't use this module (Typescript is not as great as most keep blowing it to be); +I would recommend that you don't use this module (Typescript is not as magical as most keep saying it is); you should use [nb-event-service](https://npmjs.org/package/nb-event-service) instead. For the other that use node build tool, just do this: @@ -36,6 +36,7 @@ import { JsonqlEventService } from '@jsonql/event' ## API +Same as `nb-event-service` only without the alias. #### $on(eventName, callback, context) diff --git a/packages/event/src/event-service.ts b/packages/event/src/event-service.ts index 0b110074..e7665e49 100644 --- a/packages/event/src/event-service.ts +++ b/packages/event/src/event-service.ts @@ -543,26 +543,26 @@ export class JsonqlEventService { * @param {string} evt event name * @return {boolean} true when OK */ - private validateEvt(evt: string): boolean { - if (typeof evt === 'string') { - return true; - } - throw new Error(`event name must be string type!`) + private validateEvt(...args: string[]): boolean { + args.forEach((arg: string) => { + if (typeof evt !== 'string') { + throw new Error(`event name must be string type! ${evt}`) + } + }) + return true; } /** * Simple quick check on the two main parameters - * @param {string} evt event name * @param {function} callback function to call + * @param {string} evt event name * @return {boolean} true when OK */ - private validate(evt: string, callback: myCallbackType): boolean { - if (this.validateEvt(evt)) { - if (typeof callback === 'function') { - return true; - } + private validate(callback: myCallbackType, ...args: string[]): boolean { + if (typeof callback === 'function') { + return Reflect.apply(this.validateEvt, this, args) } - throw new Error(`callback required to be function type!`) + throw new Error(`callback required to be function type! ${callback}`) } /** -- Gitee From fb95e5807e162927602a9186da08160f5c98f524 Mon Sep 17 00:00:00 2001 From: Joel Chu Date: Tue, 16 Jul 2019 09:54:37 +0800 Subject: [PATCH 07/14] update all the validation interface params --- packages/event/src/event-service.ts | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/packages/event/src/event-service.ts b/packages/event/src/event-service.ts index e7665e49..73c1618a 100644 --- a/packages/event/src/event-service.ts +++ b/packages/event/src/event-service.ts @@ -36,7 +36,7 @@ export class JsonqlEventService { * @return {number} the size of the store */ public $on(evt: string, callback: myCallbackType, context: any = null): any { - this.validate(evt, callback) + this.validate(callback, evt) const type = 'on'; // first need to check if this evt is in lazy store let lazyStoreContent = this.takeFromStore(evt) @@ -76,7 +76,7 @@ export class JsonqlEventService { * @return {number|boolean} result */ public $once(evt: string , callback: myCallbackType , context: any = null): any { - this.validate(evt, callback) + this.validate(callback, evt) const type = 'once'; let lazyStoreContent = this.takeFromStore(evt) // this is normal register before call $trigger @@ -113,7 +113,7 @@ export class JsonqlEventService { * @return {boolean} true bind for first time, false already existed */ public $only(evt: string, callback: myCallbackType, context: any = null): boolean { - this.validate(evt, callback) + this.validate(callback, evt) const type = 'only'; // keep getting TS232, this is why typescript is a joke // it will just ended up with everything is any type and back to square one @@ -150,7 +150,7 @@ export class JsonqlEventService { * @return {boolean} same as above */ public $onlyOnce(evt: string, callback: myCallbackType, context: any = null): boolean { - this.validate(evt, callback) + this.validate(callback, evt) const type = 'onlyOnce'; let added: any = false; let lazyStoreContent = this.takeFromStore(evt) @@ -379,14 +379,13 @@ export class JsonqlEventService { */ private takeFromStore(evt: string) { const store = this.lazyStore; - // let store = this[storeName]; // it could be empty at this point - this.logger('takeFromStore', store) if (store.has(evt)) { let content = store.get(evt) this.logger('takeFromStore', content) store.delete(evt) return content; } + this.logger(`lazyStore doesn't have ${evt}`) return false; } @@ -450,8 +449,7 @@ export class JsonqlEventService { * @return {boolean} true you can add, false then you can't add this type */ private checkTypeInStore(evtName: string, type: string): boolean { - this.validateEvt(evtName) - this.validateEvt(type) + this.validateEvt(evtName, type) let all = this.$get(evtName) if (all === false) { // pristine it means you can add @@ -473,18 +471,17 @@ export class JsonqlEventService { * @return {boolean} true OK */ private checkTypeInLazyStore(evtName: string, type: string): boolean { - this.validateEvt(evtName) - this.validateEvt(type) + this.validateEvt(evtName, type) let store = this.lazyStore.get(evtName) - this.logger('checkTypeInLazyStore', store) if (store) { - return !!Array - .from(store) + this.logger('checkTypeInLazyStore', store) + return !!this.mapToArr(store) .filter((value: any, index: number, array: any[]): boolean => { let [,,t] = value; return t !== type; }).length } + this.logger(`Store ${evtName} is empty`) return false; } @@ -498,7 +495,6 @@ export class JsonqlEventService { * @return {number|boolean} size of the store, or false when the check type failed */ private addToNormalStore(evt: string, type: any, callback: myCallbackType, context: any = null): number | boolean { - this.logger('addToNormalStore', evt, type, 'add to normal store') // @TODO we need to check the existing store for the type first! if (this.checkTypeInStore(evt, type)) { this.logger(`${type} can add to ${evt} store`) @@ -508,6 +504,7 @@ export class JsonqlEventService { this.normalStore = _store; return size; } + this.logger('addToNormalStore', evt, type, 'NOT add to normal store') return false; } -- Gitee From 60a61e8103c125ba3727bd98d96134fc8f0d1276 Mon Sep 17 00:00:00 2001 From: Joel Chu Date: Tue, 16 Jul 2019 09:55:46 +0800 Subject: [PATCH 08/14] rebuild successful --- .../event/dist/jsonql-event-service.cjs.js | 55 ++++++++++--------- .../event/dist/jsonql-event-service.es.js | 55 ++++++++++--------- .../event/dist/jsonql-event-service.umd.js | 55 ++++++++++--------- packages/event/src/event-service.ts | 4 +- 4 files changed, 92 insertions(+), 77 deletions(-) diff --git a/packages/event/dist/jsonql-event-service.cjs.js b/packages/event/dist/jsonql-event-service.cjs.js index ca2a4cc0..61e47732 100644 --- a/packages/event/dist/jsonql-event-service.cjs.js +++ b/packages/event/dist/jsonql-event-service.cjs.js @@ -40,7 +40,7 @@ var JsonqlEventService = /** @class */ (function () { JsonqlEventService.prototype.$on = function (evt, callback, context) { var _this = this; if (context === void 0) { context = null; } - this.validate(evt, callback); + this.validate(callback, evt); var type = 'on'; // first need to check if this evt is in lazy store var lazyStoreContent = this.takeFromStore(evt); @@ -79,7 +79,7 @@ var JsonqlEventService = /** @class */ (function () { */ JsonqlEventService.prototype.$once = function (evt, callback, context) { if (context === void 0) { context = null; } - this.validate(evt, callback); + this.validate(callback, evt); var type = 'once'; var lazyStoreContent = this.takeFromStore(evt); // this is normal register before call $trigger @@ -118,7 +118,7 @@ var JsonqlEventService = /** @class */ (function () { JsonqlEventService.prototype.$only = function (evt, callback, context) { var _this = this; if (context === void 0) { context = null; } - this.validate(evt, callback); + this.validate(callback, evt); var type = 'only'; // keep getting TS232, this is why typescript is a joke // it will just ended up with everything is any type and back to square one @@ -155,7 +155,7 @@ var JsonqlEventService = /** @class */ (function () { */ JsonqlEventService.prototype.$onlyOnce = function (evt, callback, context) { if (context === void 0) { context = null; } - this.validate(evt, callback); + this.validate(callback, evt); var type = 'onlyOnce'; var added = false; var lazyStoreContent = this.takeFromStore(evt); @@ -380,14 +380,13 @@ var JsonqlEventService = /** @class */ (function () { */ JsonqlEventService.prototype.takeFromStore = function (evt) { var store = this.lazyStore; - // let store = this[storeName]; // it could be empty at this point - this.logger('takeFromStore', store); if (store.has(evt)) { var content = store.get(evt); this.logger('takeFromStore', content); store.delete(evt); return content; } + this.logger("lazyStore doesn't have " + evt); return false; }; /** @@ -455,8 +454,7 @@ var JsonqlEventService = /** @class */ (function () { * @return {boolean} true you can add, false then you can't add this type */ JsonqlEventService.prototype.checkTypeInStore = function (evtName, type) { - this.validateEvt(evtName); - this.validateEvt(type); + this.validateEvt(evtName, type); var all = this.$get(evtName); if (all === false) { // pristine it means you can add @@ -477,18 +475,17 @@ var JsonqlEventService = /** @class */ (function () { * @return {boolean} true OK */ JsonqlEventService.prototype.checkTypeInLazyStore = function (evtName, type) { - this.validateEvt(evtName); - this.validateEvt(type); + this.validateEvt(evtName, type); var store = this.lazyStore.get(evtName); - this.logger('checkTypeInLazyStore', store); if (store) { - return !!Array - .from(store) + this.logger('checkTypeInLazyStore', store); + return !!this.mapToArr(store) .filter(function (value, index, array) { var t = value[2]; return t !== type; }).length; } + this.logger("Store " + evtName + " is empty"); return false; }; /** @@ -502,7 +499,6 @@ var JsonqlEventService = /** @class */ (function () { */ JsonqlEventService.prototype.addToNormalStore = function (evt, type, callback, context) { if (context === void 0) { context = null; } - this.logger('addToNormalStore', evt, type, 'add to normal store'); // @TODO we need to check the existing store for the type first! if (this.checkTypeInStore(evt, type)) { this.logger(type + " can add to " + evt + " store"); @@ -512,6 +508,7 @@ var JsonqlEventService = /** @class */ (function () { this.normalStore = _store; return size; } + this.logger('addToNormalStore', evt, type, 'NOT add to normal store'); return false; }; /** @@ -547,25 +544,33 @@ var JsonqlEventService = /** @class */ (function () { * @param {string} evt event name * @return {boolean} true when OK */ - JsonqlEventService.prototype.validateEvt = function (evt) { - if (typeof evt === 'string') { - return true; + JsonqlEventService.prototype.validateEvt = function () { + var args = []; + for (var _i = 0; _i < arguments.length; _i++) { + args[_i] = arguments[_i]; } - throw new Error("event name must be string type!"); + args.forEach(function (arg) { + if (typeof arg !== 'string') { + throw new Error("event name must be string type! " + arg); + } + }); + return true; }; /** * Simple quick check on the two main parameters - * @param {string} evt event name * @param {function} callback function to call + * @param {string} evt event name * @return {boolean} true when OK */ - JsonqlEventService.prototype.validate = function (evt, callback) { - if (this.validateEvt(evt)) { - if (typeof callback === 'function') { - return true; - } + JsonqlEventService.prototype.validate = function (callback) { + var args = []; + for (var _i = 1; _i < arguments.length; _i++) { + args[_i - 1] = arguments[_i]; + } + if (typeof callback === 'function') { + return Reflect.apply(this.validateEvt, this, args); } - throw new Error("callback required to be function type!"); + throw new Error("callback required to be function type! " + callback); }; /** * Check if this type is correct or not added in V1.5.0 diff --git a/packages/event/dist/jsonql-event-service.es.js b/packages/event/dist/jsonql-event-service.es.js index 03c499f3..6cec814d 100644 --- a/packages/event/dist/jsonql-event-service.es.js +++ b/packages/event/dist/jsonql-event-service.es.js @@ -36,7 +36,7 @@ var JsonqlEventService = /** @class */ (function () { JsonqlEventService.prototype.$on = function (evt, callback, context) { var _this = this; if (context === void 0) { context = null; } - this.validate(evt, callback); + this.validate(callback, evt); var type = 'on'; // first need to check if this evt is in lazy store var lazyStoreContent = this.takeFromStore(evt); @@ -75,7 +75,7 @@ var JsonqlEventService = /** @class */ (function () { */ JsonqlEventService.prototype.$once = function (evt, callback, context) { if (context === void 0) { context = null; } - this.validate(evt, callback); + this.validate(callback, evt); var type = 'once'; var lazyStoreContent = this.takeFromStore(evt); // this is normal register before call $trigger @@ -114,7 +114,7 @@ var JsonqlEventService = /** @class */ (function () { JsonqlEventService.prototype.$only = function (evt, callback, context) { var _this = this; if (context === void 0) { context = null; } - this.validate(evt, callback); + this.validate(callback, evt); var type = 'only'; // keep getting TS232, this is why typescript is a joke // it will just ended up with everything is any type and back to square one @@ -151,7 +151,7 @@ var JsonqlEventService = /** @class */ (function () { */ JsonqlEventService.prototype.$onlyOnce = function (evt, callback, context) { if (context === void 0) { context = null; } - this.validate(evt, callback); + this.validate(callback, evt); var type = 'onlyOnce'; var added = false; var lazyStoreContent = this.takeFromStore(evt); @@ -376,14 +376,13 @@ var JsonqlEventService = /** @class */ (function () { */ JsonqlEventService.prototype.takeFromStore = function (evt) { var store = this.lazyStore; - // let store = this[storeName]; // it could be empty at this point - this.logger('takeFromStore', store); if (store.has(evt)) { var content = store.get(evt); this.logger('takeFromStore', content); store.delete(evt); return content; } + this.logger("lazyStore doesn't have " + evt); return false; }; /** @@ -451,8 +450,7 @@ var JsonqlEventService = /** @class */ (function () { * @return {boolean} true you can add, false then you can't add this type */ JsonqlEventService.prototype.checkTypeInStore = function (evtName, type) { - this.validateEvt(evtName); - this.validateEvt(type); + this.validateEvt(evtName, type); var all = this.$get(evtName); if (all === false) { // pristine it means you can add @@ -473,18 +471,17 @@ var JsonqlEventService = /** @class */ (function () { * @return {boolean} true OK */ JsonqlEventService.prototype.checkTypeInLazyStore = function (evtName, type) { - this.validateEvt(evtName); - this.validateEvt(type); + this.validateEvt(evtName, type); var store = this.lazyStore.get(evtName); - this.logger('checkTypeInLazyStore', store); if (store) { - return !!Array - .from(store) + this.logger('checkTypeInLazyStore', store); + return !!this.mapToArr(store) .filter(function (value, index, array) { var t = value[2]; return t !== type; }).length; } + this.logger("Store " + evtName + " is empty"); return false; }; /** @@ -498,7 +495,6 @@ var JsonqlEventService = /** @class */ (function () { */ JsonqlEventService.prototype.addToNormalStore = function (evt, type, callback, context) { if (context === void 0) { context = null; } - this.logger('addToNormalStore', evt, type, 'add to normal store'); // @TODO we need to check the existing store for the type first! if (this.checkTypeInStore(evt, type)) { this.logger(type + " can add to " + evt + " store"); @@ -508,6 +504,7 @@ var JsonqlEventService = /** @class */ (function () { this.normalStore = _store; return size; } + this.logger('addToNormalStore', evt, type, 'NOT add to normal store'); return false; }; /** @@ -543,25 +540,33 @@ var JsonqlEventService = /** @class */ (function () { * @param {string} evt event name * @return {boolean} true when OK */ - JsonqlEventService.prototype.validateEvt = function (evt) { - if (typeof evt === 'string') { - return true; + JsonqlEventService.prototype.validateEvt = function () { + var args = []; + for (var _i = 0; _i < arguments.length; _i++) { + args[_i] = arguments[_i]; } - throw new Error("event name must be string type!"); + args.forEach(function (arg) { + if (typeof arg !== 'string') { + throw new Error("event name must be string type! " + arg); + } + }); + return true; }; /** * Simple quick check on the two main parameters - * @param {string} evt event name * @param {function} callback function to call + * @param {string} evt event name * @return {boolean} true when OK */ - JsonqlEventService.prototype.validate = function (evt, callback) { - if (this.validateEvt(evt)) { - if (typeof callback === 'function') { - return true; - } + JsonqlEventService.prototype.validate = function (callback) { + var args = []; + for (var _i = 1; _i < arguments.length; _i++) { + args[_i - 1] = arguments[_i]; + } + if (typeof callback === 'function') { + return Reflect.apply(this.validateEvt, this, args); } - throw new Error("callback required to be function type!"); + throw new Error("callback required to be function type! " + callback); }; /** * Check if this type is correct or not added in V1.5.0 diff --git a/packages/event/dist/jsonql-event-service.umd.js b/packages/event/dist/jsonql-event-service.umd.js index 155cd846..d82e8bb1 100644 --- a/packages/event/dist/jsonql-event-service.umd.js +++ b/packages/event/dist/jsonql-event-service.umd.js @@ -42,7 +42,7 @@ JsonqlEventService.prototype.$on = function (evt, callback, context) { var _this = this; if (context === void 0) { context = null; } - this.validate(evt, callback); + this.validate(callback, evt); var type = 'on'; // first need to check if this evt is in lazy store var lazyStoreContent = this.takeFromStore(evt); @@ -81,7 +81,7 @@ */ JsonqlEventService.prototype.$once = function (evt, callback, context) { if (context === void 0) { context = null; } - this.validate(evt, callback); + this.validate(callback, evt); var type = 'once'; var lazyStoreContent = this.takeFromStore(evt); // this is normal register before call $trigger @@ -120,7 +120,7 @@ JsonqlEventService.prototype.$only = function (evt, callback, context) { var _this = this; if (context === void 0) { context = null; } - this.validate(evt, callback); + this.validate(callback, evt); var type = 'only'; // keep getting TS232, this is why typescript is a joke // it will just ended up with everything is any type and back to square one @@ -157,7 +157,7 @@ */ JsonqlEventService.prototype.$onlyOnce = function (evt, callback, context) { if (context === void 0) { context = null; } - this.validate(evt, callback); + this.validate(callback, evt); var type = 'onlyOnce'; var added = false; var lazyStoreContent = this.takeFromStore(evt); @@ -382,14 +382,13 @@ */ JsonqlEventService.prototype.takeFromStore = function (evt) { var store = this.lazyStore; - // let store = this[storeName]; // it could be empty at this point - this.logger('takeFromStore', store); if (store.has(evt)) { var content = store.get(evt); this.logger('takeFromStore', content); store.delete(evt); return content; } + this.logger("lazyStore doesn't have " + evt); return false; }; /** @@ -457,8 +456,7 @@ * @return {boolean} true you can add, false then you can't add this type */ JsonqlEventService.prototype.checkTypeInStore = function (evtName, type) { - this.validateEvt(evtName); - this.validateEvt(type); + this.validateEvt(evtName, type); var all = this.$get(evtName); if (all === false) { // pristine it means you can add @@ -479,18 +477,17 @@ * @return {boolean} true OK */ JsonqlEventService.prototype.checkTypeInLazyStore = function (evtName, type) { - this.validateEvt(evtName); - this.validateEvt(type); + this.validateEvt(evtName, type); var store = this.lazyStore.get(evtName); - this.logger('checkTypeInLazyStore', store); if (store) { - return !!Array - .from(store) + this.logger('checkTypeInLazyStore', store); + return !!this.mapToArr(store) .filter(function (value, index, array) { var t = value[2]; return t !== type; }).length; } + this.logger("Store " + evtName + " is empty"); return false; }; /** @@ -504,7 +501,6 @@ */ JsonqlEventService.prototype.addToNormalStore = function (evt, type, callback, context) { if (context === void 0) { context = null; } - this.logger('addToNormalStore', evt, type, 'add to normal store'); // @TODO we need to check the existing store for the type first! if (this.checkTypeInStore(evt, type)) { this.logger(type + " can add to " + evt + " store"); @@ -514,6 +510,7 @@ this.normalStore = _store; return size; } + this.logger('addToNormalStore', evt, type, 'NOT add to normal store'); return false; }; /** @@ -549,25 +546,33 @@ * @param {string} evt event name * @return {boolean} true when OK */ - JsonqlEventService.prototype.validateEvt = function (evt) { - if (typeof evt === 'string') { - return true; + JsonqlEventService.prototype.validateEvt = function () { + var args = []; + for (var _i = 0; _i < arguments.length; _i++) { + args[_i] = arguments[_i]; } - throw new Error("event name must be string type!"); + args.forEach(function (arg) { + if (typeof arg !== 'string') { + throw new Error("event name must be string type! " + arg); + } + }); + return true; }; /** * Simple quick check on the two main parameters - * @param {string} evt event name * @param {function} callback function to call + * @param {string} evt event name * @return {boolean} true when OK */ - JsonqlEventService.prototype.validate = function (evt, callback) { - if (this.validateEvt(evt)) { - if (typeof callback === 'function') { - return true; - } + JsonqlEventService.prototype.validate = function (callback) { + var args = []; + for (var _i = 1; _i < arguments.length; _i++) { + args[_i - 1] = arguments[_i]; + } + if (typeof callback === 'function') { + return Reflect.apply(this.validateEvt, this, args); } - throw new Error("callback required to be function type!"); + throw new Error("callback required to be function type! " + callback); }; /** * Check if this type is correct or not added in V1.5.0 diff --git a/packages/event/src/event-service.ts b/packages/event/src/event-service.ts index 73c1618a..b1795605 100644 --- a/packages/event/src/event-service.ts +++ b/packages/event/src/event-service.ts @@ -542,8 +542,8 @@ export class JsonqlEventService { */ private validateEvt(...args: string[]): boolean { args.forEach((arg: string) => { - if (typeof evt !== 'string') { - throw new Error(`event name must be string type! ${evt}`) + if (typeof arg !== 'string') { + throw new Error(`event name must be string type! ${arg}`) } }) return true; -- Gitee From e149dc469f0a447803a4e954b808762189595098 Mon Sep 17 00:00:00 2001 From: Joel Chu Date: Tue, 16 Jul 2019 10:22:29 +0800 Subject: [PATCH 09/14] break up the classes --- packages/event/src/event-service.ts | 272 +-------------------------- packages/event/src/store-service.ts | 281 ++++++++++++++++++++++++++++ 2 files changed, 286 insertions(+), 267 deletions(-) create mode 100644 packages/event/src/store-service.ts diff --git a/packages/event/src/event-service.ts b/packages/event/src/event-service.ts index b1795605..f1ed6046 100644 --- a/packages/event/src/event-service.ts +++ b/packages/event/src/event-service.ts @@ -1,15 +1,11 @@ // The main class import './interfaces.ts'; -import hashFnToKey from './hash-code'; +import JsonqlStoreService from './store-service'; + // main -export class JsonqlEventService { +export class JsonqlEventService extends JsonqlStoreService { // public props keep: boolean; - // private props - private normalStore: Map; - private lazyStore: Map; - private logger: any; - private result: any[]; /** * Create EventService instance @@ -17,11 +13,8 @@ export class JsonqlEventService { * @return {void} nothing - don't do :void this fuck typescript up what a lot of shit */ constructor(logger: any) { - // hack - this.logger = typeof logger === 'function' ? logger : () => {}; - this.normalStore = new Map() - this.lazyStore = new Map() + super(logger) this.keep = false; this.result = []; @@ -319,260 +312,5 @@ export class JsonqlEventService { return this.result; } - ///////////////////////////////// - // PRIVATE / PROTECTED METHODS // - ///////////////////////////////// - - /** - * @param {any} mapObj the Map object - * @return {array} transform to array to work with - */ - protected mapToArr(mapObj: any): any[] { - return Array.from(mapObj) - } - - /** - * make sure we store the argument correctly - * @param {*} arg could be array - * @return {array} make sured - */ - protected toArray(arg: any) { - return Array.isArray(arg) ? arg : [arg]; - } - - /** - * When using ES6 you just need this[$+type] but typescript no such luck - * @param {string} type the 4 types - * @return {*} the method - */ - private getMethodBy(type: string): any { - switch(type) { - case 'on': - return this.$on; - case 'only': - return this.$only; - case 'once': - return this.$once; - case 'onlyOnce': - return this.$onlyOnce; - default: - throw new Error(`${type} is not supported!`) - } - } - - /** - * Run the callback - * @param {function} callback function to execute - * @param {array} payload for callback - * @param {object} ctx context or null - * @return {void} the result store in $done - */ - private run(callback: myCallbackType, payload: any[], ctx: any): void { - this.logger('run', callback, payload, ctx) - this.$done = Reflect.apply(callback, ctx, this.toArray(payload)) - } - - /** - * Take the content out and remove it from store id by the name - * @param {string} evt event name - * @return {object|boolean} content or false on not found - */ - private takeFromStore(evt: string) { - const store = this.lazyStore; - if (store.has(evt)) { - let content = store.get(evt) - this.logger('takeFromStore', content) - store.delete(evt) - return content; - } - this.logger(`lazyStore doesn't have ${evt}`) - return false; - } - - /** - * The add to store step is similar so make it generic for resuse - * @param {object} store which store to use - * @param {string} evt event name - * @param {spread} args because the lazy store and normal store store different things - * @return {array} store and the size of the store (cheated @TODO) - */ - private addToStore(store: any, evt: string, ...args: any[]): any[] { - let fnSet; - if (store.has(evt)) { - this.logger('addToStore', `${evt} existed`) - fnSet = store.get(evt) - } else { - this.logger('addToStore', `create new Set for ${evt}`) - // this is new - fnSet = new Set() - } - // lazy only store 2 items - this is not the case in V1.6.0 anymore - // we need to check the first parameter is string or not - if (args.length > 2) { - if (Array.isArray(args[0])) { // lazy store - // check if this type of this event already register in the lazy store - let [,,t] = args; - if (!this.checkTypeInLazyStore(evt, t)) { - fnSet.add(args) - } - } else { - if (!this.checkContentExist(args, fnSet)) { - this.logger('addToStore', `insert new`, args) - fnSet.add(args) - } - } - } else { // add straight to lazy store - fnSet.add(args) - } - store.set(evt, fnSet) - return [store, fnSet.size] - } - - /** - * @param {array} args for compare - * @param {object} fnSet A Set to search from - * @return {boolean} true on exist - */ - private checkContentExist(args: any[], fnSet: Map): boolean { - let list = this.mapToArr(fnSet) - return !!list - .filter((value: any, index: number, array: any[]): boolean => { - let [hash,] = value; - return hash === args[0]; - }).length; - } - - /** - * get the existing type to make sure no mix type add to the same store - * @param {string} evtName event name - * @param {string} type the type to check - * @return {boolean} true you can add, false then you can't add this type - */ - private checkTypeInStore(evtName: string, type: string): boolean { - this.validateEvt(evtName, type) - let all = this.$get(evtName) - if (all === false) { - // pristine it means you can add - return true; - } - // it should only have ONE type in ONE event store - return !all - .filter( (value: any, index: number, array: any[]): boolean => { - let [ ,,,t ] = value; - return type !== t; - }).length; - } - - /** - * This is checking just the lazy store because the structure is different - * therefore we need to use a new method to check it - * @param {string} evtName event name - * @param {string} type the types of allow event - * @return {boolean} true OK - */ - private checkTypeInLazyStore(evtName: string, type: string): boolean { - this.validateEvt(evtName, type) - let store = this.lazyStore.get(evtName) - if (store) { - this.logger('checkTypeInLazyStore', store) - return !!this.mapToArr(store) - .filter((value: any, index: number, array: any[]): boolean => { - let [,,t] = value; - return t !== type; - }).length - } - this.logger(`Store ${evtName} is empty`) - return false; - } - - /** - * wrapper to re-use the addToStore, - * V1.3.0 add extra check to see if this type can add to this evt - * @param {string} evt event name - * @param {string} type on or once - * @param {function} callback function - * @param {object} context the context the function execute in or null - * @return {number|boolean} size of the store, or false when the check type failed - */ - private addToNormalStore(evt: string, type: any, callback: myCallbackType, context: any = null): number | boolean { - // @TODO we need to check the existing store for the type first! - if (this.checkTypeInStore(evt, type)) { - this.logger(`${type} can add to ${evt} store`) - let key = hashFnToKey(callback) - let args = [this.normalStore, evt, key, callback, context, type] - let [_store, size] = Reflect.apply(this.addToStore, this, args) - this.normalStore = _store; - return size; - } - this.logger('addToNormalStore', evt, type, 'NOT add to normal store') - return false; - } - - /** - * Add to lazy store this get calls when the callback is not register yet - * so we only get a payload object or even nothing - * @param {string} evt event name - * @param {array} payload of arguments or empty if there is none - * @param {object} [ctx=null] the context the callback execute in - * @param {string|boolean} [type=false] register a type so no other type can add to this evt - * @return {number} size of the store - */ - private addToLazyStore(evt: string, payload: any[] = [], ctx: any = null, type: string|boolean): number { - // this is add in V1.6.0 - // when there is type then we will need to check if this already added in lazy store - // and no other type can add to this lazy store - let args = []; - args.push(this.lazyStore, evt, this.toArray(payload), ctx) - if (type) { - args.push(type) - } - let [_store, size] = Reflect.apply(this.addToStore, this, args) - this.lazyStore = _store; - return size; - } - - // validators - // This is another JOKE about Typescript, after you spend all this time on - // fixing the non-error (aka typing) it doesn't do anything during run time - // so what the fuck? - /** - * validate the event name - * @param {string} evt event name - * @return {boolean} true when OK - */ - private validateEvt(...args: string[]): boolean { - args.forEach((arg: string) => { - if (typeof arg !== 'string') { - throw new Error(`event name must be string type! ${arg}`) - } - }) - return true; - } - - /** - * Simple quick check on the two main parameters - * @param {function} callback function to call - * @param {string} evt event name - * @return {boolean} true when OK - */ - private validate(callback: myCallbackType, ...args: string[]): boolean { - if (typeof callback === 'function') { - return Reflect.apply(this.validateEvt, this, args) - } - throw new Error(`callback required to be function type! ${callback}`) - } - - /** - * Check if this type is correct or not added in V1.5.0 - * @param {string} type for checking - * @return {boolean} true on OK - */ - private validateType(type: string): boolean { - const types = ['on', 'only', 'once', 'onlyOnce'] - return !!types - .filter( - (t: any): boolean => type === t - ).length; - } - + } diff --git a/packages/event/src/store-service.ts b/packages/event/src/store-service.ts new file mode 100644 index 00000000..84d5d5ca --- /dev/null +++ b/packages/event/src/store-service.ts @@ -0,0 +1,281 @@ +// break up the store related operation here +// because the class is getting way too big now +import hashFnToKey from './hash-code'; + +export default class JsonqlStoreService { + // protected props + protected normalStore: Map; + protected lazyStore: Map; + protected logger: any; + protected result: any[]; + + constructor(logger: any) { + // hack + this.logger = typeof logger === 'function' ? logger : () => {}; + // should this be WeakMap or should they be Map? + this.normalStore = new Map() + this.lazyStore = new Map() + } + + ///////////////////////////////// + // protected / PROTECTED METHODS // + ///////////////////////////////// + + /** + * @param {any} mapObj the Map object + * @return {array} transform to array to work with + */ + protected mapToArr(mapObj: any): any[] { + return Array.from(mapObj) + } + + /** + * make sure we store the argument correctly + * @param {*} arg could be array + * @return {array} make sured + */ + protected toArray(arg: any) { + return Array.isArray(arg) ? arg : [arg]; + } + + /** + * When using ES6 you just need this[$+type] but typescript no such luck + * @param {string} type the 4 types + * @return {*} the method + */ + protected getMethodBy(type: string): any { + switch(type) { + case 'on': + return this.$on; + case 'only': + return this.$only; + case 'once': + return this.$once; + case 'onlyOnce': + return this.$onlyOnce; + default: + throw new Error(`${type} is not supported!`) + } + } + + /** + * Run the callback + * @param {function} callback function to execute + * @param {array} payload for callback + * @param {object} ctx context or null + * @return {void} the result store in $done + */ + protected run(callback: myCallbackType, payload: any[], ctx: any): void { + this.logger('run', callback, payload, ctx) + this.$done = Reflect.apply(callback, ctx, this.toArray(payload)) + } + + /** + * Take the content out and remove it from store id by the name + * @param {string} evt event name + * @return {object|boolean} content or false on not found + */ + protected takeFromStore(evt: string) { + const store = this.lazyStore; + if (store.has(evt)) { + let content = store.get(evt) + this.logger('takeFromStore', content) + store.delete(evt) + return content; + } + this.logger(`lazyStore doesn't have ${evt}`) + return false; + } + + /** + * The add to store step is similar so make it generic for resuse + * @param {object} store which store to use + * @param {string} evt event name + * @param {spread} args because the lazy store and normal store store different things + * @return {array} store and the size of the store (cheated @TODO) + */ + protected addToStore(store: any, evt: string, ...args: any[]): any[] { + let fnSet; + if (store.has(evt)) { + this.logger('addToStore', `${evt} existed`) + fnSet = store.get(evt) + } else { + this.logger('addToStore', `create new Set for ${evt}`) + // this is new + fnSet = new Set() + } + // lazy only store 2 items - this is not the case in V1.6.0 anymore + // we need to check the first parameter is string or not + if (args.length > 2) { + if (Array.isArray(args[0])) { // lazy store + // check if this type of this event already register in the lazy store + let [,,t] = args; + if (!this.checkTypeInLazyStore(evt, t)) { + fnSet.add(args) + } + } else { + if (!this.checkContentExist(args, fnSet)) { + this.logger('addToStore', `insert new`, args) + fnSet.add(args) + } + } + } else { // add straight to lazy store + fnSet.add(args) + } + store.set(evt, fnSet) + return [store, fnSet.size] + } + + /** + * @param {array} args for compare + * @param {object} fnSet A Set to search from + * @return {boolean} true on exist + */ + protected checkContentExist(args: any[], fnSet: Map): boolean { + let list = this.mapToArr(fnSet) + return !!list + .filter((value: any, index: number, array: any[]): boolean => { + let [hash,] = value; + return hash === args[0]; + }).length; + } + + /** + * get the existing type to make sure no mix type add to the same store + * @param {string} evtName event name + * @param {string} type the type to check + * @return {boolean} true you can add, false then you can't add this type + */ + protected checkTypeInStore(evtName: string, type: string): boolean { + this.validateEvt(evtName, type) + let all = this.$get(evtName) + if (all === false) { + // pristine it means you can add + return true; + } + // it should only have ONE type in ONE event store + return !all + .filter( (value: any, index: number, array: any[]): boolean => { + let [ ,,,t ] = value; + return type !== t; + }).length; + } + + /** + * This is checking just the lazy store because the structure is different + * therefore we need to use a new method to check it + * @param {string} evtName event name + * @param {string} type the types of allow event + * @return {boolean} true OK + */ + protected checkTypeInLazyStore(evtName: string, type: string): boolean { + this.validateEvt(evtName, type) + let store = this.lazyStore.get(evtName) + if (store) { + this.logger('checkTypeInLazyStore', store) + return !!this.mapToArr(store) + .filter((value: any, index: number, array: any[]): boolean => { + let [,,t] = value; + return t !== type; + }).length + } + this.logger(`Store ${evtName} is empty`) + return false; + } + + /** + * wrapper to re-use the addToStore, + * V1.3.0 add extra check to see if this type can add to this evt + * @param {string} evt event name + * @param {string} type on or once + * @param {function} callback function + * @param {object} context the context the function execute in or null + * @return {number|boolean} size of the store, or false when the check type failed + */ + protected addToNormalStore(evt: string, type: any, callback: myCallbackType, context: any = null): number | boolean { + // @TODO we need to check the existing store for the type first! + if (this.checkTypeInStore(evt, type)) { + this.logger(`addToNormalStore: ${type} can add to ${evt} store`) + let key = hashFnToKey(callback) + let args = [this.normalStore, evt, key, callback, context, type] + let [_store, size] = Reflect.apply(this.addToStore, this, args) + // @BUG? is this the problem because it was a setter before + this.normalStore = _store; + + return size; + } + this.logger('addToNormalStore:', evt, type, 'NOT add to normal store') + return false; + } + + /** + * Add to lazy store this get calls when the callback is not register yet + * so we only get a payload object or even nothing + * @param {string} evt event name + * @param {array} payload of arguments or empty if there is none + * @param {object} [ctx=null] the context the callback execute in + * @param {string|boolean} [type=false] register a type so no other type can add to this evt + * @return {number} size of the store + */ + protected addToLazyStore(evt: string, payload: any[] = [], ctx: any = null, type: string|boolean): number { + // this is add in V1.6.0 + // when there is type then we will need to check if this already added in lazy store + // and no other type can add to this lazy store + let args = []; + args.push(this.lazyStore, evt, this.toArray(payload), ctx) + if (type) { + args.push(type) + } + let [_store, size] = Reflect.apply(this.addToStore, this, args) + // @BUG ? this was a setter + this.lazyStore = _store; + return size; + } + + // VALIDATORS // + // This is another JOKE about Typescript, after you spend all this time on + // fixing the non-error (aka typing) it doesn't do anything during run time + // so what the fuck? + + /** + * validate the event name + * @param {string} evt event name + * @return {boolean} true when OK + */ + protected validateEvt(...args: string[]): boolean { + args.forEach((arg: string) => { + if (typeof arg !== 'string') { + throw new Error(`event name must be string type! ${arg}`) + } + }) + return true; + } + + /** + * Simple quick check on the two main parameters + * @param {function} callback function to call + * @param {string} evt event name + * @return {boolean} true when OK + */ + protected validate(callback: myCallbackType, ...args: string[]): boolean { + if (typeof callback === 'function') { + return Reflect.apply(this.validateEvt, this, args) + } + throw new Error(`callback required to be function type! ${callback}`) + } + + /** + * Check if this type is correct or not added in V1.5.0 + * @param {string} type for checking + * @return {boolean} true on OK + */ + protected validateType(type: string): boolean { + const types = ['on', 'only', 'once', 'onlyOnce'] + return !!types + .filter( + (t: any): boolean => type === t + ).length; + } + + +} -- Gitee From 80ce7c2f3b616d64e9d2ffda7bdaa2684a2a015f Mon Sep 17 00:00:00 2001 From: Joelchu Date: Tue, 16 Jul 2019 18:31:25 +0800 Subject: [PATCH 10/14] still couldn't see why the add failed --- packages/event/src/event-service.js | 10 +++++----- packages/event/src/event-service.ts | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/event/src/event-service.js b/packages/event/src/event-service.js index a75d6c05..fe6f2cc6 100644 --- a/packages/event/src/event-service.js +++ b/packages/event/src/event-service.js @@ -274,13 +274,13 @@ export default class EventService { this.validateEvt(evt) let store = this.normalStore; if (store.has(evt)) { - return Array - .from(store.get(evt)) - .map( l => { + return this + .mapToArr(store.get(evt)) + .map((value: any, index: number, arr: any[]) => { if (full) { - return l; + return value; } - let [key, callback, ] = l; + let [key, callback, ] = value; return callback; }) } diff --git a/packages/event/src/event-service.ts b/packages/event/src/event-service.ts index f1ed6046..74121092 100644 --- a/packages/event/src/event-service.ts +++ b/packages/event/src/event-service.ts @@ -203,7 +203,7 @@ export class JsonqlEventService extends JsonqlStoreService { let nSet = this.mapToArr(nStore.get(evt)) let ctn = nSet.length; let hasOnce = false; - let hasOnly = false; + // let hasOnly = false; for (let i=0; i < ctn; ++i) { ++found; // this.logger('found', found) @@ -312,5 +312,5 @@ export class JsonqlEventService extends JsonqlStoreService { return this.result; } - + } -- Gitee From cb643ad5c901e9a334e5663af453d064af78dae5 Mon Sep 17 00:00:00 2001 From: Joelchu Date: Tue, 16 Jul 2019 18:39:34 +0800 Subject: [PATCH 11/14] sorting out the new class structure and able to build --- .../event/dist/jsonql-event-service.cjs.js | 621 +++++---- .../event/dist/jsonql-event-service.es.js | 621 +++++---- .../event/dist/jsonql-event-service.umd.js | 1215 +++++++++-------- packages/event/src/event-service.js | 2 + packages/event/src/event-service.ts | 69 +- packages/event/src/store-service.ts | 85 +- 6 files changed, 1373 insertions(+), 1240 deletions(-) diff --git a/packages/event/dist/jsonql-event-service.cjs.js b/packages/event/dist/jsonql-event-service.cjs.js index 61e47732..f7c33769 100644 --- a/packages/event/dist/jsonql-event-service.cjs.js +++ b/packages/event/dist/jsonql-event-service.cjs.js @@ -2,6 +2,35 @@ Object.defineProperty(exports, '__esModule', { value: true }); +/*! ***************************************************************************** +Copyright (c) Microsoft Corporation. All rights reserved. +Licensed under the Apache License, Version 2.0 (the "License"); you may not use +this file except in compliance with the License. You may obtain a copy of the +License at http://www.apache.org/licenses/LICENSE-2.0 + +THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED +WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, +MERCHANTABLITY OR NON-INFRINGEMENT. + +See the Apache Version 2.0 License for specific language governing permissions +and limitations under the License. +***************************************************************************** */ +/* global Reflect, Promise */ + +var extendStatics = function(d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; + return extendStatics(d, b); +}; + +function __extends(d, b) { + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +} + /** * generate a 32bit hash based on the function.toString() * _from http://stackoverflow.com/questions/7616461/generate-a-hash-_from-string-in-javascript-jquery @@ -13,270 +42,26 @@ function hashCode(fn) { return s.split("").reduce(function (a, b) { a = ((a << 5) - a) + b.charCodeAt(0); return a & a; }, 0) + ""; } -// The main class -// main -var JsonqlEventService = /** @class */ (function () { - /** - * Create EventService instance - * @param {configObj} config configuration object - * @return {void} nothing - don't do :void this fuck typescript up what a lot of shit - */ - function JsonqlEventService(logger) { +// break up the store related operation here +var JsonqlStoreService = /** @class */ (function () { + function JsonqlStoreService(logger) { // hack this.logger = typeof logger === 'function' ? logger : function () { }; + // should this be WeakMap or should they be Map? this.normalStore = new Map(); this.lazyStore = new Map(); + // don't keep this.keep = false; + // place holder, should this be a WeakSet? this.result = []; } - /** - * Register your evt handler, note we don't check the type here, - * we expect you to be sensible and know what you are doing. - * @param {string} evt name of event - * @param {function} callback bind method --> if it's array or not - * @param {object} [context=null] to execute this call in - * @return {number} the size of the store - */ - JsonqlEventService.prototype.$on = function (evt, callback, context) { - var _this = this; - if (context === void 0) { context = null; } - this.validate(callback, evt); - var type = 'on'; - // first need to check if this evt is in lazy store - var lazyStoreContent = this.takeFromStore(evt); - // this is normal register first then call later - if (lazyStoreContent === false) { - this.logger('$on', evt + " callback is not in lazy store"); - // @TODO we need to check if there was other listener to this - // event and are they the same type then we could solve that - // register the different type to the same event name - return this.addToNormalStore(evt, type, callback, context); - } - this.logger('$on', evt + " found in lazy store"); - // this is when they call $trigger before register this callback - var size = 0; - lazyStoreContent.forEach(function (content) { - var payload = content[0], ctx = content[1], t = content[2]; - if (t && t !== type) { - throw new Error("You are trying to register an event already been taken by other type: " + t); - } - _this.run(callback, payload, context || ctx); - var result = _this.addToNormalStore(evt, type, callback, context || ctx); - if (typeof result !== 'boolean') { - size += result; - } - }); - return size; - }; - /** - * once only registered it once, there is no overwrite option here - * @NOTE change in v1.3.0 $once can add multiple listeners - * but once the event fired, it will remove this event (see $only) - * @param {string} evt name - * @param {function} callback to execute - * @param {object} [context=null] the handler execute in - * @return {number|boolean} result - */ - JsonqlEventService.prototype.$once = function (evt, callback, context) { - if (context === void 0) { context = null; } - this.validate(callback, evt); - var type = 'once'; - var lazyStoreContent = this.takeFromStore(evt); - // this is normal register before call $trigger - var nStore = this.normalStore; - if (lazyStoreContent === false) { - this.logger('$once', evt + " not in the lazy store"); - // v1.3.0 $once now allow to add multiple listeners - return this.addToNormalStore(evt, type, callback, context); - } - else { - // now this is the tricky bit - // there is a potential bug here that cause by the developer - // if they call $trigger first, the lazy won't know it's a once call - // so if in the middle they register any call with the same evt name - // then this $once call will be fucked - add this to the documentation - this.logger('$once', lazyStoreContent); - var list = this.mapToArr(lazyStoreContent); - // should never have more than 1 - var _a = list[0], payload = _a[0], ctx = _a[1], t = _a[2]; - if (t && t !== type) { - throw new Error("You are trying to register an event already been taken by other type: " + t); - } - this.run(callback, payload, context || ctx); - // remove this evt from store - this.$off(evt); - } - return false; - }; - /** - * This one event can only bind one callbackback - * @param {string} evt event name - * @param {function} callback event handler - * @param {object} [context=null] the context the event handler execute in - * @return {boolean} true bind for first time, false already existed - */ - JsonqlEventService.prototype.$only = function (evt, callback, context) { - var _this = this; - if (context === void 0) { context = null; } - this.validate(callback, evt); - var type = 'only'; - // keep getting TS232, this is why typescript is a joke - // it will just ended up with everything is any type and back to square one - var added = false; - var lazyStoreContent = this.takeFromStore(evt); - // this is normal register before call $trigger - var nStore = this.normalStore; - if (!nStore.has(evt)) { - this.logger("$only", evt + " add to store"); - added = this.addToNormalStore(evt, type, callback, context); - } - if (lazyStoreContent !== false) { - // there are data store in lazy store - this.logger('$only', evt + " found data in lazy store to execute"); - var list = this.mapToArr(lazyStoreContent); - // $only allow to trigger this multiple time on the single handler - list.forEach(function (l) { - var payload = l[0], ctx = l[1], t = l[2]; - if (t && t !== type) { - throw new Error("You are trying to register an event already been taken by other type: " + t); - } - _this.run(callback, payload, context || ctx); - }); - } - return added; - }; - /** - * $only + $once this is because I found a very subtile bug when we pass a - * resolver, rejecter - and it never fire because that's OLD adeed in v1.4.0 - * @param {string} evt event name - * @param {function} callback to call later - * @param {object} [context=null] exeucte context - * @return {boolean} same as above - */ - JsonqlEventService.prototype.$onlyOnce = function (evt, callback, context) { - if (context === void 0) { context = null; } - this.validate(callback, evt); - var type = 'onlyOnce'; - var added = false; - var lazyStoreContent = this.takeFromStore(evt); - // this is normal register before call $trigger - var nStore = this.normalStore; - if (!nStore.has(evt)) { - this.logger("$onlyOnce", evt + " add to store"); - added = this.addToNormalStore(evt, type, callback, context); - } - if (lazyStoreContent !== false) { - // there are data store in lazy store - this.logger('$onlyOnce', lazyStoreContent); - var list = this.mapToArr(lazyStoreContent); - // should never have more than 1 - var _a = list[0], payload = _a[0], ctx = _a[1], t = _a[2]; - if (t && t !== 'onlyOnce') { - throw new Error("You are trying to register an event already been taken by other type: " + t); - } - this.run(callback, payload, context || ctx); - // remove this evt from store - this.$off(evt); - } - return added; - }; - /** - * This is a shorthand of $off + $on added in V1.5.0 - * @param {string} evt event name - * @param {function} callback to exeucte - * @param {object} [context = null] or pass a string as type - * @param {string} [type=on] what type of method to replace - * @return {any} whatever the call method return - */ - JsonqlEventService.prototype.$replace = function (evt, callback, context, type) { - if (context === void 0) { context = null; } - if (type === void 0) { type = 'on'; } - this.$off(evt); - // this will become a problem - var method = this.getMethodBy(type); - return Reflect.apply(method, this, [evt, callback, context]); - }; - /** - * trigger the event - * @param {string} evt name NOT allow array anymore! - * @param {mixed} [payload = []] pass to fn - * @param {object|string} [context = null] overwrite what stored - * @param {string} [type=false] if pass this then we need to add type to store too - * @return {number} if it has been execute how many times - */ - JsonqlEventService.prototype.$trigger = function (evt, payload, context, type) { - if (payload === void 0) { payload = []; } - if (context === void 0) { context = null; } - if (type === void 0) { type = false; } - this.validateEvt(evt); - var found = 0; - // first check the normal store - var nStore = this.normalStore; - this.logger('$trigger', nStore); - if (nStore.has(evt)) { - this.logger('$trigger', evt, 'found'); - var nSet = this.mapToArr(nStore.get(evt)); - var ctn = nSet.length; - var hasOnce = false; - for (var i = 0; i < ctn; ++i) { - ++found; - // this.logger('found', found) - var _a = nSet[i], _ = _a[0], callback = _a[1], ctx = _a[2], type_1 = _a[3]; - this.run(callback, payload, context || ctx); - if (type_1 === 'once' || type_1 === 'onlyOnce') { - hasOnce = true; - } - } - if (hasOnce) { - nStore.delete(evt); - } - return found; - } - // now this is not register yet - this.addToLazyStore(evt, payload, context, type); - return found; - }; - /** - * this is an alias to the $trigger - * @NOTE breaking change in V1.6.0 we swap the parameter around - * @param {string} evt event name - * @param {*} params pass to the callback - * @param {string} type of call - * @param {object} context what context callback execute in - * @return {*} from $trigger - */ - JsonqlEventService.prototype.$call = function (evt, params, type, context) { - if (type === void 0) { type = false; } - if (context === void 0) { context = null; } - this.validateEvt(evt); - var args = [evt, params]; - args.push(context, type); - return Reflect.apply(this.$trigger, this, args); - }; - /** - * remove the evt from all the stores - * @param {string} evt name - * @return {boolean} true actually delete something - */ - JsonqlEventService.prototype.$off = function (evt) { - this.validateEvt(evt); - var stores = [this.lazyStore, this.normalStore]; - var found = false; - stores.forEach(function (store) { - if (store.has(evt)) { - found = true; - store.delete(evt); - } - }); - return found; - }; /** * return all the listener from the event * @param {string} evtName event name * @param {boolean} [full=false] if true then return the entire content * @return {array|boolean} listerner(s) or false when not found */ - JsonqlEventService.prototype.$get = function (evt, full) { + JsonqlStoreService.prototype.$get = function (evt, full) { if (full === void 0) { full = false; } this.validateEvt(evt); var store = this.normalStore; @@ -293,7 +78,7 @@ var JsonqlEventService = /** @class */ (function () { } return false; }; - Object.defineProperty(JsonqlEventService.prototype, "$done", { + Object.defineProperty(JsonqlStoreService.prototype, "$done", { /** * @TODO is there any real use with the keep prop? * getter for $done @@ -326,13 +111,13 @@ var JsonqlEventService = /** @class */ (function () { configurable: true }); ///////////////////////////////// - // PRIVATE / PROTECTED METHODS // + // protected / PROTECTED METHODS // ///////////////////////////////// /** * @param {any} mapObj the Map object * @return {array} transform to array to work with */ - JsonqlEventService.prototype.mapToArr = function (mapObj) { + JsonqlStoreService.prototype.mapToArr = function (mapObj) { return Array.from(mapObj); }; /** @@ -340,28 +125,9 @@ var JsonqlEventService = /** @class */ (function () { * @param {*} arg could be array * @return {array} make sured */ - JsonqlEventService.prototype.toArray = function (arg) { + JsonqlStoreService.prototype.toArray = function (arg) { return Array.isArray(arg) ? arg : [arg]; }; - /** - * When using ES6 you just need this[$+type] but typescript no such luck - * @param {string} type the 4 types - * @return {*} the method - */ - JsonqlEventService.prototype.getMethodBy = function (type) { - switch (type) { - case 'on': - return this.$on; - case 'only': - return this.$only; - case 'once': - return this.$once; - case 'onlyOnce': - return this.$onlyOnce; - default: - throw new Error(type + " is not supported!"); - } - }; /** * Run the callback * @param {function} callback function to execute @@ -369,7 +135,7 @@ var JsonqlEventService = /** @class */ (function () { * @param {object} ctx context or null * @return {void} the result store in $done */ - JsonqlEventService.prototype.run = function (callback, payload, ctx) { + JsonqlStoreService.prototype.run = function (callback, payload, ctx) { this.logger('run', callback, payload, ctx); this.$done = Reflect.apply(callback, ctx, this.toArray(payload)); }; @@ -378,7 +144,7 @@ var JsonqlEventService = /** @class */ (function () { * @param {string} evt event name * @return {object|boolean} content or false on not found */ - JsonqlEventService.prototype.takeFromStore = function (evt) { + JsonqlStoreService.prototype.takeFromStore = function (evt) { var store = this.lazyStore; if (store.has(evt)) { var content = store.get(evt); @@ -396,7 +162,7 @@ var JsonqlEventService = /** @class */ (function () { * @param {spread} args because the lazy store and normal store store different things * @return {array} store and the size of the store (cheated @TODO) */ - JsonqlEventService.prototype.addToStore = function (store, evt) { + JsonqlStoreService.prototype.addToStore = function (store, evt) { var args = []; for (var _i = 2; _i < arguments.length; _i++) { args[_i - 2] = arguments[_i]; @@ -439,7 +205,7 @@ var JsonqlEventService = /** @class */ (function () { * @param {object} fnSet A Set to search from * @return {boolean} true on exist */ - JsonqlEventService.prototype.checkContentExist = function (args, fnSet) { + JsonqlStoreService.prototype.checkContentExist = function (args, fnSet) { var list = this.mapToArr(fnSet); return !!list .filter(function (value, index, array) { @@ -453,7 +219,7 @@ var JsonqlEventService = /** @class */ (function () { * @param {string} type the type to check * @return {boolean} true you can add, false then you can't add this type */ - JsonqlEventService.prototype.checkTypeInStore = function (evtName, type) { + JsonqlStoreService.prototype.checkTypeInStore = function (evtName, type) { this.validateEvt(evtName, type); var all = this.$get(evtName); if (all === false) { @@ -474,7 +240,7 @@ var JsonqlEventService = /** @class */ (function () { * @param {string} type the types of allow event * @return {boolean} true OK */ - JsonqlEventService.prototype.checkTypeInLazyStore = function (evtName, type) { + JsonqlStoreService.prototype.checkTypeInLazyStore = function (evtName, type) { this.validateEvt(evtName, type); var store = this.lazyStore.get(evtName); if (store) { @@ -497,18 +263,19 @@ var JsonqlEventService = /** @class */ (function () { * @param {object} context the context the function execute in or null * @return {number|boolean} size of the store, or false when the check type failed */ - JsonqlEventService.prototype.addToNormalStore = function (evt, type, callback, context) { + JsonqlStoreService.prototype.addToNormalStore = function (evt, type, callback, context) { if (context === void 0) { context = null; } // @TODO we need to check the existing store for the type first! if (this.checkTypeInStore(evt, type)) { - this.logger(type + " can add to " + evt + " store"); + this.logger("addToNormalStore: " + type + " can add to " + evt + " store"); var key = hashCode(callback); var args = [this.normalStore, evt, key, callback, context, type]; var _a = Reflect.apply(this.addToStore, this, args), _store = _a[0], size = _a[1]; + // @BUG? is this the problem because it was a setter before this.normalStore = _store; return size; } - this.logger('addToNormalStore', evt, type, 'NOT add to normal store'); + this.logger('addToNormalStore:', evt, type, 'NOT add to normal store'); return false; }; /** @@ -520,7 +287,7 @@ var JsonqlEventService = /** @class */ (function () { * @param {string|boolean} [type=false] register a type so no other type can add to this evt * @return {number} size of the store */ - JsonqlEventService.prototype.addToLazyStore = function (evt, payload, ctx, type) { + JsonqlStoreService.prototype.addToLazyStore = function (evt, payload, ctx, type) { if (payload === void 0) { payload = []; } if (ctx === void 0) { ctx = null; } // this is add in V1.6.0 @@ -532,10 +299,11 @@ var JsonqlEventService = /** @class */ (function () { args.push(type); } var _a = Reflect.apply(this.addToStore, this, args), _store = _a[0], size = _a[1]; + // @BUG ? this was a setter this.lazyStore = _store; return size; }; - // validators + // VALIDATORS // // This is another JOKE about Typescript, after you spend all this time on // fixing the non-error (aka typing) it doesn't do anything during run time // so what the fuck? @@ -544,7 +312,7 @@ var JsonqlEventService = /** @class */ (function () { * @param {string} evt event name * @return {boolean} true when OK */ - JsonqlEventService.prototype.validateEvt = function () { + JsonqlStoreService.prototype.validateEvt = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; @@ -562,7 +330,7 @@ var JsonqlEventService = /** @class */ (function () { * @param {string} evt event name * @return {boolean} true when OK */ - JsonqlEventService.prototype.validate = function (callback) { + JsonqlStoreService.prototype.validate = function (callback) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; @@ -577,12 +345,287 @@ var JsonqlEventService = /** @class */ (function () { * @param {string} type for checking * @return {boolean} true on OK */ - JsonqlEventService.prototype.validateType = function (type) { + JsonqlStoreService.prototype.validateType = function (type) { var types = ['on', 'only', 'once', 'onlyOnce']; return !!types .filter(function (t) { return type === t; }).length; }; - return JsonqlEventService; + return JsonqlStoreService; }()); +// main +var JsonqlEventService = /** @class */ (function (_super) { + __extends(JsonqlEventService, _super); + /** + * Create EventService instance + * @param {configObj} config configuration object + * @return {void} nothing - don't do :void this fuck typescript up what a lot of shit + */ + function JsonqlEventService(logger) { + return _super.call(this, logger) || this; + } + /** + * Register your evt handler, note we don't check the type here, + * we expect you to be sensible and know what you are doing. + * @param {string} evt name of event + * @param {function} callback bind method --> if it's array or not + * @param {object} [context=null] to execute this call in + * @return {number} the size of the store + */ + JsonqlEventService.prototype.$on = function (evt, callback, context) { + var _this = this; + if (context === void 0) { context = null; } + this.validate(callback, evt); + var type = 'on'; + // first need to check if this evt is in lazy store + var lazyStoreContent = this.takeFromStore(evt); + // this is normal register first then call later + if (lazyStoreContent === false) { + this.logger('$on', evt + " callback is not in lazy store"); + // @TODO we need to check if there was other listener to this + // event and are they the same type then we could solve that + // register the different type to the same event name + return this.addToNormalStore(evt, type, callback, context); + } + this.logger('$on', evt + " found in lazy store"); + // this is when they call $trigger before register this callback + var size = 0; + lazyStoreContent.forEach(function (content) { + var payload = content[0], ctx = content[1], t = content[2]; + if (t && t !== type) { + throw new Error("You are trying to register an event already been taken by other type: " + t); + } + _this.run(callback, payload, context || ctx); + var result = _this.addToNormalStore(evt, type, callback, context || ctx); + if (typeof result !== 'boolean') { + size += result; + } + }); + return size; + }; + /** + * once only registered it once, there is no overwrite option here + * @NOTE change in v1.3.0 $once can add multiple listeners + * but once the event fired, it will remove this event (see $only) + * @param {string} evt name + * @param {function} callback to execute + * @param {object} [context=null] the handler execute in + * @return {number|boolean} result + */ + JsonqlEventService.prototype.$once = function (evt, callback, context) { + if (context === void 0) { context = null; } + this.validate(callback, evt); + var type = 'once'; + var lazyStoreContent = this.takeFromStore(evt); + // this is normal register before call $trigger + var nStore = this.normalStore; + if (lazyStoreContent === false) { + this.logger('$once', evt + " not in the lazy store"); + // v1.3.0 $once now allow to add multiple listeners + return this.addToNormalStore(evt, type, callback, context); + } + else { + // now this is the tricky bit + // there is a potential bug here that cause by the developer + // if they call $trigger first, the lazy won't know it's a once call + // so if in the middle they register any call with the same evt name + // then this $once call will be fucked - add this to the documentation + this.logger('$once', lazyStoreContent); + var list = this.mapToArr(lazyStoreContent); + // should never have more than 1 + var _a = list[0], payload = _a[0], ctx = _a[1], t = _a[2]; + if (t && t !== type) { + throw new Error("You are trying to register an event already been taken by other type: " + t); + } + this.run(callback, payload, context || ctx); + // remove this evt from store + this.$off(evt); + } + return false; + }; + /** + * This one event can only bind one callbackback + * @param {string} evt event name + * @param {function} callback event handler + * @param {object} [context=null] the context the event handler execute in + * @return {boolean} true bind for first time, false already existed + */ + JsonqlEventService.prototype.$only = function (evt, callback, context) { + var _this = this; + if (context === void 0) { context = null; } + this.validate(callback, evt); + var type = 'only'; + // keep getting TS232, this is why typescript is a joke + // it will just ended up with everything is any type and back to square one + var added = false; + var lazyStoreContent = this.takeFromStore(evt); + // this is normal register before call $trigger + var nStore = this.normalStore; + if (!nStore.has(evt)) { + this.logger("$only", evt + " add to store"); + added = this.addToNormalStore(evt, type, callback, context); + } + if (lazyStoreContent !== false) { + // there are data store in lazy store + this.logger('$only', evt + " found data in lazy store to execute"); + var list = this.mapToArr(lazyStoreContent); + // $only allow to trigger this multiple time on the single handler + list.forEach(function (l) { + var payload = l[0], ctx = l[1], t = l[2]; + if (t && t !== type) { + throw new Error("You are trying to register an event already been taken by other type: " + t); + } + _this.run(callback, payload, context || ctx); + }); + } + return added; + }; + /** + * $only + $once this is because I found a very subtile bug when we pass a + * resolver, rejecter - and it never fire because that's OLD adeed in v1.4.0 + * @param {string} evt event name + * @param {function} callback to call later + * @param {object} [context=null] exeucte context + * @return {boolean} same as above + */ + JsonqlEventService.prototype.$onlyOnce = function (evt, callback, context) { + if (context === void 0) { context = null; } + this.validate(callback, evt); + var type = 'onlyOnce'; + var added = false; + var lazyStoreContent = this.takeFromStore(evt); + // this is normal register before call $trigger + var nStore = this.normalStore; + if (!nStore.has(evt)) { + this.logger("$onlyOnce", evt + " add to store"); + added = this.addToNormalStore(evt, type, callback, context); + } + if (lazyStoreContent !== false) { + // there are data store in lazy store + this.logger('$onlyOnce', lazyStoreContent); + var list = this.mapToArr(lazyStoreContent); + // should never have more than 1 + var _a = list[0], payload = _a[0], ctx = _a[1], t = _a[2]; + if (t && t !== 'onlyOnce') { + throw new Error("You are trying to register an event already been taken by other type: " + t); + } + this.run(callback, payload, context || ctx); + // remove this evt from store + this.$off(evt); + } + return added; + }; + /** + * This is a shorthand of $off + $on added in V1.5.0 + * @param {string} evt event name + * @param {function} callback to exeucte + * @param {object} [context = null] or pass a string as type + * @param {string} [type=on] what type of method to replace + * @return {any} whatever the call method return + */ + JsonqlEventService.prototype.$replace = function (evt, callback, context, type) { + if (context === void 0) { context = null; } + if (type === void 0) { type = 'on'; } + this.$off(evt); + // this will become a problem + var method = this.getMethodBy(type); + return Reflect.apply(method, this, [evt, callback, context]); + }; + /** + * trigger the event + * @param {string} evt name NOT allow array anymore! + * @param {mixed} [payload = []] pass to fn + * @param {object|string} [context = null] overwrite what stored + * @param {string} [type=false] if pass this then we need to add type to store too + * @return {number} if it has been execute how many times + */ + JsonqlEventService.prototype.$trigger = function (evt, payload, context, type) { + if (payload === void 0) { payload = []; } + if (context === void 0) { context = null; } + if (type === void 0) { type = false; } + this.validateEvt(evt); + var found = 0; + // first check the normal store + var nStore = this.normalStore; + this.logger('$trigger', nStore); + if (nStore.has(evt)) { + this.logger('$trigger', evt, 'found'); + var nSet = this.mapToArr(nStore.get(evt)); + var ctn = nSet.length; + var hasOnce = false; + // let hasOnly = false; + for (var i = 0; i < ctn; ++i) { + ++found; + // this.logger('found', found) + var _a = nSet[i], _ = _a[0], callback = _a[1], ctx = _a[2], type_1 = _a[3]; + this.run(callback, payload, context || ctx); + if (type_1 === 'once' || type_1 === 'onlyOnce') { + hasOnce = true; + } + } + if (hasOnce) { + nStore.delete(evt); + } + return found; + } + // now this is not register yet + this.addToLazyStore(evt, payload, context, type); + return found; + }; + /** + * this is an alias to the $trigger + * @NOTE breaking change in V1.6.0 we swap the parameter around + * @param {string} evt event name + * @param {*} params pass to the callback + * @param {string} type of call + * @param {object} context what context callback execute in + * @return {*} from $trigger + */ + JsonqlEventService.prototype.$call = function (evt, params, type, context) { + if (type === void 0) { type = false; } + if (context === void 0) { context = null; } + this.validateEvt(evt); + var args = [evt, params]; + args.push(context, type); + return Reflect.apply(this.$trigger, this, args); + }; + /** + * remove the evt from all the stores + * @param {string} evt name + * @return {boolean} true actually delete something + */ + JsonqlEventService.prototype.$off = function (evt) { + this.validateEvt(evt); + var stores = [this.lazyStore, this.normalStore]; + var found = false; + stores.forEach(function (store) { + if (store.has(evt)) { + found = true; + store.delete(evt); + } + }); + return found; + }; + /** + * When using ES6 you just need this[$+type] but typescript no such luck + * @param {string} type the 4 types + * @return {*} the method + */ + JsonqlEventService.prototype.getMethodBy = function (type) { + switch (type) { + case 'on': + return this.$on; + case 'only': + return this.$only; + case 'once': + return this.$once; + case 'onlyOnce': + return this.$onlyOnce; + default: + throw new Error(type + " is not supported!"); + } + }; + return JsonqlEventService; +}(JsonqlStoreService)); + exports.JsonqlEventService = JsonqlEventService; diff --git a/packages/event/dist/jsonql-event-service.es.js b/packages/event/dist/jsonql-event-service.es.js index 6cec814d..a8ef75e1 100644 --- a/packages/event/dist/jsonql-event-service.es.js +++ b/packages/event/dist/jsonql-event-service.es.js @@ -1,3 +1,32 @@ +/*! ***************************************************************************** +Copyright (c) Microsoft Corporation. All rights reserved. +Licensed under the Apache License, Version 2.0 (the "License"); you may not use +this file except in compliance with the License. You may obtain a copy of the +License at http://www.apache.org/licenses/LICENSE-2.0 + +THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED +WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, +MERCHANTABLITY OR NON-INFRINGEMENT. + +See the Apache Version 2.0 License for specific language governing permissions +and limitations under the License. +***************************************************************************** */ +/* global Reflect, Promise */ + +var extendStatics = function(d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; + return extendStatics(d, b); +}; + +function __extends(d, b) { + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +} + /** * generate a 32bit hash based on the function.toString() * _from http://stackoverflow.com/questions/7616461/generate-a-hash-_from-string-in-javascript-jquery @@ -9,270 +38,26 @@ function hashCode(fn) { return s.split("").reduce(function (a, b) { a = ((a << 5) - a) + b.charCodeAt(0); return a & a; }, 0) + ""; } -// The main class -// main -var JsonqlEventService = /** @class */ (function () { - /** - * Create EventService instance - * @param {configObj} config configuration object - * @return {void} nothing - don't do :void this fuck typescript up what a lot of shit - */ - function JsonqlEventService(logger) { +// break up the store related operation here +var JsonqlStoreService = /** @class */ (function () { + function JsonqlStoreService(logger) { // hack this.logger = typeof logger === 'function' ? logger : function () { }; + // should this be WeakMap or should they be Map? this.normalStore = new Map(); this.lazyStore = new Map(); + // don't keep this.keep = false; + // place holder, should this be a WeakSet? this.result = []; } - /** - * Register your evt handler, note we don't check the type here, - * we expect you to be sensible and know what you are doing. - * @param {string} evt name of event - * @param {function} callback bind method --> if it's array or not - * @param {object} [context=null] to execute this call in - * @return {number} the size of the store - */ - JsonqlEventService.prototype.$on = function (evt, callback, context) { - var _this = this; - if (context === void 0) { context = null; } - this.validate(callback, evt); - var type = 'on'; - // first need to check if this evt is in lazy store - var lazyStoreContent = this.takeFromStore(evt); - // this is normal register first then call later - if (lazyStoreContent === false) { - this.logger('$on', evt + " callback is not in lazy store"); - // @TODO we need to check if there was other listener to this - // event and are they the same type then we could solve that - // register the different type to the same event name - return this.addToNormalStore(evt, type, callback, context); - } - this.logger('$on', evt + " found in lazy store"); - // this is when they call $trigger before register this callback - var size = 0; - lazyStoreContent.forEach(function (content) { - var payload = content[0], ctx = content[1], t = content[2]; - if (t && t !== type) { - throw new Error("You are trying to register an event already been taken by other type: " + t); - } - _this.run(callback, payload, context || ctx); - var result = _this.addToNormalStore(evt, type, callback, context || ctx); - if (typeof result !== 'boolean') { - size += result; - } - }); - return size; - }; - /** - * once only registered it once, there is no overwrite option here - * @NOTE change in v1.3.0 $once can add multiple listeners - * but once the event fired, it will remove this event (see $only) - * @param {string} evt name - * @param {function} callback to execute - * @param {object} [context=null] the handler execute in - * @return {number|boolean} result - */ - JsonqlEventService.prototype.$once = function (evt, callback, context) { - if (context === void 0) { context = null; } - this.validate(callback, evt); - var type = 'once'; - var lazyStoreContent = this.takeFromStore(evt); - // this is normal register before call $trigger - var nStore = this.normalStore; - if (lazyStoreContent === false) { - this.logger('$once', evt + " not in the lazy store"); - // v1.3.0 $once now allow to add multiple listeners - return this.addToNormalStore(evt, type, callback, context); - } - else { - // now this is the tricky bit - // there is a potential bug here that cause by the developer - // if they call $trigger first, the lazy won't know it's a once call - // so if in the middle they register any call with the same evt name - // then this $once call will be fucked - add this to the documentation - this.logger('$once', lazyStoreContent); - var list = this.mapToArr(lazyStoreContent); - // should never have more than 1 - var _a = list[0], payload = _a[0], ctx = _a[1], t = _a[2]; - if (t && t !== type) { - throw new Error("You are trying to register an event already been taken by other type: " + t); - } - this.run(callback, payload, context || ctx); - // remove this evt from store - this.$off(evt); - } - return false; - }; - /** - * This one event can only bind one callbackback - * @param {string} evt event name - * @param {function} callback event handler - * @param {object} [context=null] the context the event handler execute in - * @return {boolean} true bind for first time, false already existed - */ - JsonqlEventService.prototype.$only = function (evt, callback, context) { - var _this = this; - if (context === void 0) { context = null; } - this.validate(callback, evt); - var type = 'only'; - // keep getting TS232, this is why typescript is a joke - // it will just ended up with everything is any type and back to square one - var added = false; - var lazyStoreContent = this.takeFromStore(evt); - // this is normal register before call $trigger - var nStore = this.normalStore; - if (!nStore.has(evt)) { - this.logger("$only", evt + " add to store"); - added = this.addToNormalStore(evt, type, callback, context); - } - if (lazyStoreContent !== false) { - // there are data store in lazy store - this.logger('$only', evt + " found data in lazy store to execute"); - var list = this.mapToArr(lazyStoreContent); - // $only allow to trigger this multiple time on the single handler - list.forEach(function (l) { - var payload = l[0], ctx = l[1], t = l[2]; - if (t && t !== type) { - throw new Error("You are trying to register an event already been taken by other type: " + t); - } - _this.run(callback, payload, context || ctx); - }); - } - return added; - }; - /** - * $only + $once this is because I found a very subtile bug when we pass a - * resolver, rejecter - and it never fire because that's OLD adeed in v1.4.0 - * @param {string} evt event name - * @param {function} callback to call later - * @param {object} [context=null] exeucte context - * @return {boolean} same as above - */ - JsonqlEventService.prototype.$onlyOnce = function (evt, callback, context) { - if (context === void 0) { context = null; } - this.validate(callback, evt); - var type = 'onlyOnce'; - var added = false; - var lazyStoreContent = this.takeFromStore(evt); - // this is normal register before call $trigger - var nStore = this.normalStore; - if (!nStore.has(evt)) { - this.logger("$onlyOnce", evt + " add to store"); - added = this.addToNormalStore(evt, type, callback, context); - } - if (lazyStoreContent !== false) { - // there are data store in lazy store - this.logger('$onlyOnce', lazyStoreContent); - var list = this.mapToArr(lazyStoreContent); - // should never have more than 1 - var _a = list[0], payload = _a[0], ctx = _a[1], t = _a[2]; - if (t && t !== 'onlyOnce') { - throw new Error("You are trying to register an event already been taken by other type: " + t); - } - this.run(callback, payload, context || ctx); - // remove this evt from store - this.$off(evt); - } - return added; - }; - /** - * This is a shorthand of $off + $on added in V1.5.0 - * @param {string} evt event name - * @param {function} callback to exeucte - * @param {object} [context = null] or pass a string as type - * @param {string} [type=on] what type of method to replace - * @return {any} whatever the call method return - */ - JsonqlEventService.prototype.$replace = function (evt, callback, context, type) { - if (context === void 0) { context = null; } - if (type === void 0) { type = 'on'; } - this.$off(evt); - // this will become a problem - var method = this.getMethodBy(type); - return Reflect.apply(method, this, [evt, callback, context]); - }; - /** - * trigger the event - * @param {string} evt name NOT allow array anymore! - * @param {mixed} [payload = []] pass to fn - * @param {object|string} [context = null] overwrite what stored - * @param {string} [type=false] if pass this then we need to add type to store too - * @return {number} if it has been execute how many times - */ - JsonqlEventService.prototype.$trigger = function (evt, payload, context, type) { - if (payload === void 0) { payload = []; } - if (context === void 0) { context = null; } - if (type === void 0) { type = false; } - this.validateEvt(evt); - var found = 0; - // first check the normal store - var nStore = this.normalStore; - this.logger('$trigger', nStore); - if (nStore.has(evt)) { - this.logger('$trigger', evt, 'found'); - var nSet = this.mapToArr(nStore.get(evt)); - var ctn = nSet.length; - var hasOnce = false; - for (var i = 0; i < ctn; ++i) { - ++found; - // this.logger('found', found) - var _a = nSet[i], _ = _a[0], callback = _a[1], ctx = _a[2], type_1 = _a[3]; - this.run(callback, payload, context || ctx); - if (type_1 === 'once' || type_1 === 'onlyOnce') { - hasOnce = true; - } - } - if (hasOnce) { - nStore.delete(evt); - } - return found; - } - // now this is not register yet - this.addToLazyStore(evt, payload, context, type); - return found; - }; - /** - * this is an alias to the $trigger - * @NOTE breaking change in V1.6.0 we swap the parameter around - * @param {string} evt event name - * @param {*} params pass to the callback - * @param {string} type of call - * @param {object} context what context callback execute in - * @return {*} from $trigger - */ - JsonqlEventService.prototype.$call = function (evt, params, type, context) { - if (type === void 0) { type = false; } - if (context === void 0) { context = null; } - this.validateEvt(evt); - var args = [evt, params]; - args.push(context, type); - return Reflect.apply(this.$trigger, this, args); - }; - /** - * remove the evt from all the stores - * @param {string} evt name - * @return {boolean} true actually delete something - */ - JsonqlEventService.prototype.$off = function (evt) { - this.validateEvt(evt); - var stores = [this.lazyStore, this.normalStore]; - var found = false; - stores.forEach(function (store) { - if (store.has(evt)) { - found = true; - store.delete(evt); - } - }); - return found; - }; /** * return all the listener from the event * @param {string} evtName event name * @param {boolean} [full=false] if true then return the entire content * @return {array|boolean} listerner(s) or false when not found */ - JsonqlEventService.prototype.$get = function (evt, full) { + JsonqlStoreService.prototype.$get = function (evt, full) { if (full === void 0) { full = false; } this.validateEvt(evt); var store = this.normalStore; @@ -289,7 +74,7 @@ var JsonqlEventService = /** @class */ (function () { } return false; }; - Object.defineProperty(JsonqlEventService.prototype, "$done", { + Object.defineProperty(JsonqlStoreService.prototype, "$done", { /** * @TODO is there any real use with the keep prop? * getter for $done @@ -322,13 +107,13 @@ var JsonqlEventService = /** @class */ (function () { configurable: true }); ///////////////////////////////// - // PRIVATE / PROTECTED METHODS // + // protected / PROTECTED METHODS // ///////////////////////////////// /** * @param {any} mapObj the Map object * @return {array} transform to array to work with */ - JsonqlEventService.prototype.mapToArr = function (mapObj) { + JsonqlStoreService.prototype.mapToArr = function (mapObj) { return Array.from(mapObj); }; /** @@ -336,28 +121,9 @@ var JsonqlEventService = /** @class */ (function () { * @param {*} arg could be array * @return {array} make sured */ - JsonqlEventService.prototype.toArray = function (arg) { + JsonqlStoreService.prototype.toArray = function (arg) { return Array.isArray(arg) ? arg : [arg]; }; - /** - * When using ES6 you just need this[$+type] but typescript no such luck - * @param {string} type the 4 types - * @return {*} the method - */ - JsonqlEventService.prototype.getMethodBy = function (type) { - switch (type) { - case 'on': - return this.$on; - case 'only': - return this.$only; - case 'once': - return this.$once; - case 'onlyOnce': - return this.$onlyOnce; - default: - throw new Error(type + " is not supported!"); - } - }; /** * Run the callback * @param {function} callback function to execute @@ -365,7 +131,7 @@ var JsonqlEventService = /** @class */ (function () { * @param {object} ctx context or null * @return {void} the result store in $done */ - JsonqlEventService.prototype.run = function (callback, payload, ctx) { + JsonqlStoreService.prototype.run = function (callback, payload, ctx) { this.logger('run', callback, payload, ctx); this.$done = Reflect.apply(callback, ctx, this.toArray(payload)); }; @@ -374,7 +140,7 @@ var JsonqlEventService = /** @class */ (function () { * @param {string} evt event name * @return {object|boolean} content or false on not found */ - JsonqlEventService.prototype.takeFromStore = function (evt) { + JsonqlStoreService.prototype.takeFromStore = function (evt) { var store = this.lazyStore; if (store.has(evt)) { var content = store.get(evt); @@ -392,7 +158,7 @@ var JsonqlEventService = /** @class */ (function () { * @param {spread} args because the lazy store and normal store store different things * @return {array} store and the size of the store (cheated @TODO) */ - JsonqlEventService.prototype.addToStore = function (store, evt) { + JsonqlStoreService.prototype.addToStore = function (store, evt) { var args = []; for (var _i = 2; _i < arguments.length; _i++) { args[_i - 2] = arguments[_i]; @@ -435,7 +201,7 @@ var JsonqlEventService = /** @class */ (function () { * @param {object} fnSet A Set to search from * @return {boolean} true on exist */ - JsonqlEventService.prototype.checkContentExist = function (args, fnSet) { + JsonqlStoreService.prototype.checkContentExist = function (args, fnSet) { var list = this.mapToArr(fnSet); return !!list .filter(function (value, index, array) { @@ -449,7 +215,7 @@ var JsonqlEventService = /** @class */ (function () { * @param {string} type the type to check * @return {boolean} true you can add, false then you can't add this type */ - JsonqlEventService.prototype.checkTypeInStore = function (evtName, type) { + JsonqlStoreService.prototype.checkTypeInStore = function (evtName, type) { this.validateEvt(evtName, type); var all = this.$get(evtName); if (all === false) { @@ -470,7 +236,7 @@ var JsonqlEventService = /** @class */ (function () { * @param {string} type the types of allow event * @return {boolean} true OK */ - JsonqlEventService.prototype.checkTypeInLazyStore = function (evtName, type) { + JsonqlStoreService.prototype.checkTypeInLazyStore = function (evtName, type) { this.validateEvt(evtName, type); var store = this.lazyStore.get(evtName); if (store) { @@ -493,18 +259,19 @@ var JsonqlEventService = /** @class */ (function () { * @param {object} context the context the function execute in or null * @return {number|boolean} size of the store, or false when the check type failed */ - JsonqlEventService.prototype.addToNormalStore = function (evt, type, callback, context) { + JsonqlStoreService.prototype.addToNormalStore = function (evt, type, callback, context) { if (context === void 0) { context = null; } // @TODO we need to check the existing store for the type first! if (this.checkTypeInStore(evt, type)) { - this.logger(type + " can add to " + evt + " store"); + this.logger("addToNormalStore: " + type + " can add to " + evt + " store"); var key = hashCode(callback); var args = [this.normalStore, evt, key, callback, context, type]; var _a = Reflect.apply(this.addToStore, this, args), _store = _a[0], size = _a[1]; + // @BUG? is this the problem because it was a setter before this.normalStore = _store; return size; } - this.logger('addToNormalStore', evt, type, 'NOT add to normal store'); + this.logger('addToNormalStore:', evt, type, 'NOT add to normal store'); return false; }; /** @@ -516,7 +283,7 @@ var JsonqlEventService = /** @class */ (function () { * @param {string|boolean} [type=false] register a type so no other type can add to this evt * @return {number} size of the store */ - JsonqlEventService.prototype.addToLazyStore = function (evt, payload, ctx, type) { + JsonqlStoreService.prototype.addToLazyStore = function (evt, payload, ctx, type) { if (payload === void 0) { payload = []; } if (ctx === void 0) { ctx = null; } // this is add in V1.6.0 @@ -528,10 +295,11 @@ var JsonqlEventService = /** @class */ (function () { args.push(type); } var _a = Reflect.apply(this.addToStore, this, args), _store = _a[0], size = _a[1]; + // @BUG ? this was a setter this.lazyStore = _store; return size; }; - // validators + // VALIDATORS // // This is another JOKE about Typescript, after you spend all this time on // fixing the non-error (aka typing) it doesn't do anything during run time // so what the fuck? @@ -540,7 +308,7 @@ var JsonqlEventService = /** @class */ (function () { * @param {string} evt event name * @return {boolean} true when OK */ - JsonqlEventService.prototype.validateEvt = function () { + JsonqlStoreService.prototype.validateEvt = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; @@ -558,7 +326,7 @@ var JsonqlEventService = /** @class */ (function () { * @param {string} evt event name * @return {boolean} true when OK */ - JsonqlEventService.prototype.validate = function (callback) { + JsonqlStoreService.prototype.validate = function (callback) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; @@ -573,12 +341,287 @@ var JsonqlEventService = /** @class */ (function () { * @param {string} type for checking * @return {boolean} true on OK */ - JsonqlEventService.prototype.validateType = function (type) { + JsonqlStoreService.prototype.validateType = function (type) { var types = ['on', 'only', 'once', 'onlyOnce']; return !!types .filter(function (t) { return type === t; }).length; }; - return JsonqlEventService; + return JsonqlStoreService; }()); +// main +var JsonqlEventService = /** @class */ (function (_super) { + __extends(JsonqlEventService, _super); + /** + * Create EventService instance + * @param {configObj} config configuration object + * @return {void} nothing - don't do :void this fuck typescript up what a lot of shit + */ + function JsonqlEventService(logger) { + return _super.call(this, logger) || this; + } + /** + * Register your evt handler, note we don't check the type here, + * we expect you to be sensible and know what you are doing. + * @param {string} evt name of event + * @param {function} callback bind method --> if it's array or not + * @param {object} [context=null] to execute this call in + * @return {number} the size of the store + */ + JsonqlEventService.prototype.$on = function (evt, callback, context) { + var _this = this; + if (context === void 0) { context = null; } + this.validate(callback, evt); + var type = 'on'; + // first need to check if this evt is in lazy store + var lazyStoreContent = this.takeFromStore(evt); + // this is normal register first then call later + if (lazyStoreContent === false) { + this.logger('$on', evt + " callback is not in lazy store"); + // @TODO we need to check if there was other listener to this + // event and are they the same type then we could solve that + // register the different type to the same event name + return this.addToNormalStore(evt, type, callback, context); + } + this.logger('$on', evt + " found in lazy store"); + // this is when they call $trigger before register this callback + var size = 0; + lazyStoreContent.forEach(function (content) { + var payload = content[0], ctx = content[1], t = content[2]; + if (t && t !== type) { + throw new Error("You are trying to register an event already been taken by other type: " + t); + } + _this.run(callback, payload, context || ctx); + var result = _this.addToNormalStore(evt, type, callback, context || ctx); + if (typeof result !== 'boolean') { + size += result; + } + }); + return size; + }; + /** + * once only registered it once, there is no overwrite option here + * @NOTE change in v1.3.0 $once can add multiple listeners + * but once the event fired, it will remove this event (see $only) + * @param {string} evt name + * @param {function} callback to execute + * @param {object} [context=null] the handler execute in + * @return {number|boolean} result + */ + JsonqlEventService.prototype.$once = function (evt, callback, context) { + if (context === void 0) { context = null; } + this.validate(callback, evt); + var type = 'once'; + var lazyStoreContent = this.takeFromStore(evt); + // this is normal register before call $trigger + var nStore = this.normalStore; + if (lazyStoreContent === false) { + this.logger('$once', evt + " not in the lazy store"); + // v1.3.0 $once now allow to add multiple listeners + return this.addToNormalStore(evt, type, callback, context); + } + else { + // now this is the tricky bit + // there is a potential bug here that cause by the developer + // if they call $trigger first, the lazy won't know it's a once call + // so if in the middle they register any call with the same evt name + // then this $once call will be fucked - add this to the documentation + this.logger('$once', lazyStoreContent); + var list = this.mapToArr(lazyStoreContent); + // should never have more than 1 + var _a = list[0], payload = _a[0], ctx = _a[1], t = _a[2]; + if (t && t !== type) { + throw new Error("You are trying to register an event already been taken by other type: " + t); + } + this.run(callback, payload, context || ctx); + // remove this evt from store + this.$off(evt); + } + return false; + }; + /** + * This one event can only bind one callbackback + * @param {string} evt event name + * @param {function} callback event handler + * @param {object} [context=null] the context the event handler execute in + * @return {boolean} true bind for first time, false already existed + */ + JsonqlEventService.prototype.$only = function (evt, callback, context) { + var _this = this; + if (context === void 0) { context = null; } + this.validate(callback, evt); + var type = 'only'; + // keep getting TS232, this is why typescript is a joke + // it will just ended up with everything is any type and back to square one + var added = false; + var lazyStoreContent = this.takeFromStore(evt); + // this is normal register before call $trigger + var nStore = this.normalStore; + if (!nStore.has(evt)) { + this.logger("$only", evt + " add to store"); + added = this.addToNormalStore(evt, type, callback, context); + } + if (lazyStoreContent !== false) { + // there are data store in lazy store + this.logger('$only', evt + " found data in lazy store to execute"); + var list = this.mapToArr(lazyStoreContent); + // $only allow to trigger this multiple time on the single handler + list.forEach(function (l) { + var payload = l[0], ctx = l[1], t = l[2]; + if (t && t !== type) { + throw new Error("You are trying to register an event already been taken by other type: " + t); + } + _this.run(callback, payload, context || ctx); + }); + } + return added; + }; + /** + * $only + $once this is because I found a very subtile bug when we pass a + * resolver, rejecter - and it never fire because that's OLD adeed in v1.4.0 + * @param {string} evt event name + * @param {function} callback to call later + * @param {object} [context=null] exeucte context + * @return {boolean} same as above + */ + JsonqlEventService.prototype.$onlyOnce = function (evt, callback, context) { + if (context === void 0) { context = null; } + this.validate(callback, evt); + var type = 'onlyOnce'; + var added = false; + var lazyStoreContent = this.takeFromStore(evt); + // this is normal register before call $trigger + var nStore = this.normalStore; + if (!nStore.has(evt)) { + this.logger("$onlyOnce", evt + " add to store"); + added = this.addToNormalStore(evt, type, callback, context); + } + if (lazyStoreContent !== false) { + // there are data store in lazy store + this.logger('$onlyOnce', lazyStoreContent); + var list = this.mapToArr(lazyStoreContent); + // should never have more than 1 + var _a = list[0], payload = _a[0], ctx = _a[1], t = _a[2]; + if (t && t !== 'onlyOnce') { + throw new Error("You are trying to register an event already been taken by other type: " + t); + } + this.run(callback, payload, context || ctx); + // remove this evt from store + this.$off(evt); + } + return added; + }; + /** + * This is a shorthand of $off + $on added in V1.5.0 + * @param {string} evt event name + * @param {function} callback to exeucte + * @param {object} [context = null] or pass a string as type + * @param {string} [type=on] what type of method to replace + * @return {any} whatever the call method return + */ + JsonqlEventService.prototype.$replace = function (evt, callback, context, type) { + if (context === void 0) { context = null; } + if (type === void 0) { type = 'on'; } + this.$off(evt); + // this will become a problem + var method = this.getMethodBy(type); + return Reflect.apply(method, this, [evt, callback, context]); + }; + /** + * trigger the event + * @param {string} evt name NOT allow array anymore! + * @param {mixed} [payload = []] pass to fn + * @param {object|string} [context = null] overwrite what stored + * @param {string} [type=false] if pass this then we need to add type to store too + * @return {number} if it has been execute how many times + */ + JsonqlEventService.prototype.$trigger = function (evt, payload, context, type) { + if (payload === void 0) { payload = []; } + if (context === void 0) { context = null; } + if (type === void 0) { type = false; } + this.validateEvt(evt); + var found = 0; + // first check the normal store + var nStore = this.normalStore; + this.logger('$trigger', nStore); + if (nStore.has(evt)) { + this.logger('$trigger', evt, 'found'); + var nSet = this.mapToArr(nStore.get(evt)); + var ctn = nSet.length; + var hasOnce = false; + // let hasOnly = false; + for (var i = 0; i < ctn; ++i) { + ++found; + // this.logger('found', found) + var _a = nSet[i], _ = _a[0], callback = _a[1], ctx = _a[2], type_1 = _a[3]; + this.run(callback, payload, context || ctx); + if (type_1 === 'once' || type_1 === 'onlyOnce') { + hasOnce = true; + } + } + if (hasOnce) { + nStore.delete(evt); + } + return found; + } + // now this is not register yet + this.addToLazyStore(evt, payload, context, type); + return found; + }; + /** + * this is an alias to the $trigger + * @NOTE breaking change in V1.6.0 we swap the parameter around + * @param {string} evt event name + * @param {*} params pass to the callback + * @param {string} type of call + * @param {object} context what context callback execute in + * @return {*} from $trigger + */ + JsonqlEventService.prototype.$call = function (evt, params, type, context) { + if (type === void 0) { type = false; } + if (context === void 0) { context = null; } + this.validateEvt(evt); + var args = [evt, params]; + args.push(context, type); + return Reflect.apply(this.$trigger, this, args); + }; + /** + * remove the evt from all the stores + * @param {string} evt name + * @return {boolean} true actually delete something + */ + JsonqlEventService.prototype.$off = function (evt) { + this.validateEvt(evt); + var stores = [this.lazyStore, this.normalStore]; + var found = false; + stores.forEach(function (store) { + if (store.has(evt)) { + found = true; + store.delete(evt); + } + }); + return found; + }; + /** + * When using ES6 you just need this[$+type] but typescript no such luck + * @param {string} type the 4 types + * @return {*} the method + */ + JsonqlEventService.prototype.getMethodBy = function (type) { + switch (type) { + case 'on': + return this.$on; + case 'only': + return this.$only; + case 'once': + return this.$once; + case 'onlyOnce': + return this.$onlyOnce; + default: + throw new Error(type + " is not supported!"); + } + }; + return JsonqlEventService; +}(JsonqlStoreService)); + export { JsonqlEventService }; diff --git a/packages/event/dist/jsonql-event-service.umd.js b/packages/event/dist/jsonql-event-service.umd.js index d82e8bb1..212aff44 100644 --- a/packages/event/dist/jsonql-event-service.umd.js +++ b/packages/event/dist/jsonql-event-service.umd.js @@ -1,594 +1,637 @@ (function (global, factory) { - typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : - typeof define === 'function' && define.amd ? define(['exports'], factory) : - (global = global || self, factory(global.JsonqlEventService = {})); + typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : + typeof define === 'function' && define.amd ? define(['exports'], factory) : + (global = global || self, factory(global.JsonqlEventService = {})); }(this, function (exports) { 'use strict'; - /** - * generate a 32bit hash based on the function.toString() - * _from http://stackoverflow.com/questions/7616461/generate-a-hash-_from-string-in-javascript-jquery - * @param {function} fn the converted to string function - * @return {string} the hashed function string - */ - function hashCode(fn) { - var s = fn.toString(); - return s.split("").reduce(function (a, b) { a = ((a << 5) - a) + b.charCodeAt(0); return a & a; }, 0) + ""; - } + /*! ***************************************************************************** + Copyright (c) Microsoft Corporation. All rights reserved. + Licensed under the Apache License, Version 2.0 (the "License"); you may not use + this file except in compliance with the License. You may obtain a copy of the + License at http://www.apache.org/licenses/LICENSE-2.0 + + THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED + WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, + MERCHANTABLITY OR NON-INFRINGEMENT. + + See the Apache Version 2.0 License for specific language governing permissions + and limitations under the License. + ***************************************************************************** */ + /* global Reflect, Promise */ + + var extendStatics = function(d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + + function __extends(d, b) { + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + } - // The main class - // main - var JsonqlEventService = /** @class */ (function () { - /** - * Create EventService instance - * @param {configObj} config configuration object - * @return {void} nothing - don't do :void this fuck typescript up what a lot of shit - */ - function JsonqlEventService(logger) { - // hack - this.logger = typeof logger === 'function' ? logger : function () { }; - this.normalStore = new Map(); - this.lazyStore = new Map(); - this.keep = false; - this.result = []; - } - /** - * Register your evt handler, note we don't check the type here, - * we expect you to be sensible and know what you are doing. - * @param {string} evt name of event - * @param {function} callback bind method --> if it's array or not - * @param {object} [context=null] to execute this call in - * @return {number} the size of the store - */ - JsonqlEventService.prototype.$on = function (evt, callback, context) { - var _this = this; - if (context === void 0) { context = null; } - this.validate(callback, evt); - var type = 'on'; - // first need to check if this evt is in lazy store - var lazyStoreContent = this.takeFromStore(evt); - // this is normal register first then call later - if (lazyStoreContent === false) { - this.logger('$on', evt + " callback is not in lazy store"); - // @TODO we need to check if there was other listener to this - // event and are they the same type then we could solve that - // register the different type to the same event name - return this.addToNormalStore(evt, type, callback, context); - } - this.logger('$on', evt + " found in lazy store"); - // this is when they call $trigger before register this callback - var size = 0; - lazyStoreContent.forEach(function (content) { - var payload = content[0], ctx = content[1], t = content[2]; - if (t && t !== type) { - throw new Error("You are trying to register an event already been taken by other type: " + t); - } - _this.run(callback, payload, context || ctx); - var result = _this.addToNormalStore(evt, type, callback, context || ctx); - if (typeof result !== 'boolean') { - size += result; - } - }); - return size; - }; - /** - * once only registered it once, there is no overwrite option here - * @NOTE change in v1.3.0 $once can add multiple listeners - * but once the event fired, it will remove this event (see $only) - * @param {string} evt name - * @param {function} callback to execute - * @param {object} [context=null] the handler execute in - * @return {number|boolean} result - */ - JsonqlEventService.prototype.$once = function (evt, callback, context) { - if (context === void 0) { context = null; } - this.validate(callback, evt); - var type = 'once'; - var lazyStoreContent = this.takeFromStore(evt); - // this is normal register before call $trigger - var nStore = this.normalStore; - if (lazyStoreContent === false) { - this.logger('$once', evt + " not in the lazy store"); - // v1.3.0 $once now allow to add multiple listeners - return this.addToNormalStore(evt, type, callback, context); - } - else { - // now this is the tricky bit - // there is a potential bug here that cause by the developer - // if they call $trigger first, the lazy won't know it's a once call - // so if in the middle they register any call with the same evt name - // then this $once call will be fucked - add this to the documentation - this.logger('$once', lazyStoreContent); - var list = this.mapToArr(lazyStoreContent); - // should never have more than 1 - var _a = list[0], payload = _a[0], ctx = _a[1], t = _a[2]; - if (t && t !== type) { - throw new Error("You are trying to register an event already been taken by other type: " + t); - } - this.run(callback, payload, context || ctx); - // remove this evt from store - this.$off(evt); - } - return false; - }; - /** - * This one event can only bind one callbackback - * @param {string} evt event name - * @param {function} callback event handler - * @param {object} [context=null] the context the event handler execute in - * @return {boolean} true bind for first time, false already existed - */ - JsonqlEventService.prototype.$only = function (evt, callback, context) { - var _this = this; - if (context === void 0) { context = null; } - this.validate(callback, evt); - var type = 'only'; - // keep getting TS232, this is why typescript is a joke - // it will just ended up with everything is any type and back to square one - var added = false; - var lazyStoreContent = this.takeFromStore(evt); - // this is normal register before call $trigger - var nStore = this.normalStore; - if (!nStore.has(evt)) { - this.logger("$only", evt + " add to store"); - added = this.addToNormalStore(evt, type, callback, context); - } - if (lazyStoreContent !== false) { - // there are data store in lazy store - this.logger('$only', evt + " found data in lazy store to execute"); - var list = this.mapToArr(lazyStoreContent); - // $only allow to trigger this multiple time on the single handler - list.forEach(function (l) { - var payload = l[0], ctx = l[1], t = l[2]; - if (t && t !== type) { - throw new Error("You are trying to register an event already been taken by other type: " + t); - } - _this.run(callback, payload, context || ctx); - }); - } - return added; - }; - /** - * $only + $once this is because I found a very subtile bug when we pass a - * resolver, rejecter - and it never fire because that's OLD adeed in v1.4.0 - * @param {string} evt event name - * @param {function} callback to call later - * @param {object} [context=null] exeucte context - * @return {boolean} same as above - */ - JsonqlEventService.prototype.$onlyOnce = function (evt, callback, context) { - if (context === void 0) { context = null; } - this.validate(callback, evt); - var type = 'onlyOnce'; - var added = false; - var lazyStoreContent = this.takeFromStore(evt); - // this is normal register before call $trigger - var nStore = this.normalStore; - if (!nStore.has(evt)) { - this.logger("$onlyOnce", evt + " add to store"); - added = this.addToNormalStore(evt, type, callback, context); - } - if (lazyStoreContent !== false) { - // there are data store in lazy store - this.logger('$onlyOnce', lazyStoreContent); - var list = this.mapToArr(lazyStoreContent); - // should never have more than 1 - var _a = list[0], payload = _a[0], ctx = _a[1], t = _a[2]; - if (t && t !== 'onlyOnce') { - throw new Error("You are trying to register an event already been taken by other type: " + t); - } - this.run(callback, payload, context || ctx); - // remove this evt from store - this.$off(evt); - } - return added; - }; - /** - * This is a shorthand of $off + $on added in V1.5.0 - * @param {string} evt event name - * @param {function} callback to exeucte - * @param {object} [context = null] or pass a string as type - * @param {string} [type=on] what type of method to replace - * @return {any} whatever the call method return - */ - JsonqlEventService.prototype.$replace = function (evt, callback, context, type) { - if (context === void 0) { context = null; } - if (type === void 0) { type = 'on'; } - this.$off(evt); - // this will become a problem - var method = this.getMethodBy(type); - return Reflect.apply(method, this, [evt, callback, context]); - }; - /** - * trigger the event - * @param {string} evt name NOT allow array anymore! - * @param {mixed} [payload = []] pass to fn - * @param {object|string} [context = null] overwrite what stored - * @param {string} [type=false] if pass this then we need to add type to store too - * @return {number} if it has been execute how many times - */ - JsonqlEventService.prototype.$trigger = function (evt, payload, context, type) { - if (payload === void 0) { payload = []; } - if (context === void 0) { context = null; } - if (type === void 0) { type = false; } - this.validateEvt(evt); - var found = 0; - // first check the normal store - var nStore = this.normalStore; - this.logger('$trigger', nStore); - if (nStore.has(evt)) { - this.logger('$trigger', evt, 'found'); - var nSet = this.mapToArr(nStore.get(evt)); - var ctn = nSet.length; - var hasOnce = false; - for (var i = 0; i < ctn; ++i) { - ++found; - // this.logger('found', found) - var _a = nSet[i], _ = _a[0], callback = _a[1], ctx = _a[2], type_1 = _a[3]; - this.run(callback, payload, context || ctx); - if (type_1 === 'once' || type_1 === 'onlyOnce') { - hasOnce = true; - } - } - if (hasOnce) { - nStore.delete(evt); - } - return found; - } - // now this is not register yet - this.addToLazyStore(evt, payload, context, type); - return found; - }; - /** - * this is an alias to the $trigger - * @NOTE breaking change in V1.6.0 we swap the parameter around - * @param {string} evt event name - * @param {*} params pass to the callback - * @param {string} type of call - * @param {object} context what context callback execute in - * @return {*} from $trigger - */ - JsonqlEventService.prototype.$call = function (evt, params, type, context) { - if (type === void 0) { type = false; } - if (context === void 0) { context = null; } - this.validateEvt(evt); - var args = [evt, params]; - args.push(context, type); - return Reflect.apply(this.$trigger, this, args); - }; - /** - * remove the evt from all the stores - * @param {string} evt name - * @return {boolean} true actually delete something - */ - JsonqlEventService.prototype.$off = function (evt) { - this.validateEvt(evt); - var stores = [this.lazyStore, this.normalStore]; - var found = false; - stores.forEach(function (store) { - if (store.has(evt)) { - found = true; - store.delete(evt); - } - }); - return found; - }; - /** - * return all the listener from the event - * @param {string} evtName event name - * @param {boolean} [full=false] if true then return the entire content - * @return {array|boolean} listerner(s) or false when not found - */ - JsonqlEventService.prototype.$get = function (evt, full) { - if (full === void 0) { full = false; } - this.validateEvt(evt); - var store = this.normalStore; - if (store.has(evt)) { - return this - .mapToArr(store.get(evt)) - .map(function (l) { - if (full) { - return l; - } - var key = l[0], callback = l[1]; - return callback; - }); - } - return false; - }; - Object.defineProperty(JsonqlEventService.prototype, "$done", { - /** - * @TODO is there any real use with the keep prop? - * getter for $done - * @return {*} whatever last store result - */ - get: function () { - if (this.keep) { - this.logger(this.result); - return this.result[this.result.length - 1]; - } - return this.result; - }, - //////////////////////////////// - // SETTER / GETTER // - //////////////////////////////// - /** - * store the return result from the run - * @param {*} value whatever return from callback - */ - set: function (value) { - this.logger('set $done', value); - if (this.keep) { - this.result.push(value); - } - else { - this.result = value; - } - }, - enumerable: true, - configurable: true - }); - ///////////////////////////////// - // PRIVATE / PROTECTED METHODS // - ///////////////////////////////// - /** - * @param {any} mapObj the Map object - * @return {array} transform to array to work with - */ - JsonqlEventService.prototype.mapToArr = function (mapObj) { - return Array.from(mapObj); - }; - /** - * make sure we store the argument correctly - * @param {*} arg could be array - * @return {array} make sured - */ - JsonqlEventService.prototype.toArray = function (arg) { - return Array.isArray(arg) ? arg : [arg]; - }; - /** - * When using ES6 you just need this[$+type] but typescript no such luck - * @param {string} type the 4 types - * @return {*} the method - */ - JsonqlEventService.prototype.getMethodBy = function (type) { - switch (type) { - case 'on': - return this.$on; - case 'only': - return this.$only; - case 'once': - return this.$once; - case 'onlyOnce': - return this.$onlyOnce; - default: - throw new Error(type + " is not supported!"); - } - }; - /** - * Run the callback - * @param {function} callback function to execute - * @param {array} payload for callback - * @param {object} ctx context or null - * @return {void} the result store in $done - */ - JsonqlEventService.prototype.run = function (callback, payload, ctx) { - this.logger('run', callback, payload, ctx); - this.$done = Reflect.apply(callback, ctx, this.toArray(payload)); - }; - /** - * Take the content out and remove it from store id by the name - * @param {string} evt event name - * @return {object|boolean} content or false on not found - */ - JsonqlEventService.prototype.takeFromStore = function (evt) { - var store = this.lazyStore; - if (store.has(evt)) { - var content = store.get(evt); - this.logger('takeFromStore', content); - store.delete(evt); - return content; - } - this.logger("lazyStore doesn't have " + evt); - return false; - }; - /** - * The add to store step is similar so make it generic for resuse - * @param {object} store which store to use - * @param {string} evt event name - * @param {spread} args because the lazy store and normal store store different things - * @return {array} store and the size of the store (cheated @TODO) - */ - JsonqlEventService.prototype.addToStore = function (store, evt) { - var args = []; - for (var _i = 2; _i < arguments.length; _i++) { - args[_i - 2] = arguments[_i]; - } - var fnSet; - if (store.has(evt)) { - this.logger('addToStore', evt + " existed"); - fnSet = store.get(evt); - } - else { - this.logger('addToStore', "create new Set for " + evt); - // this is new - fnSet = new Set(); - } - // lazy only store 2 items - this is not the case in V1.6.0 anymore - // we need to check the first parameter is string or not - if (args.length > 2) { - if (Array.isArray(args[0])) { // lazy store - // check if this type of this event already register in the lazy store - var t = args[2]; - if (!this.checkTypeInLazyStore(evt, t)) { - fnSet.add(args); - } - } - else { - if (!this.checkContentExist(args, fnSet)) { - this.logger('addToStore', "insert new", args); - fnSet.add(args); - } - } - } - else { // add straight to lazy store - fnSet.add(args); - } - store.set(evt, fnSet); - return [store, fnSet.size]; - }; - /** - * @param {array} args for compare - * @param {object} fnSet A Set to search from - * @return {boolean} true on exist - */ - JsonqlEventService.prototype.checkContentExist = function (args, fnSet) { - var list = this.mapToArr(fnSet); - return !!list - .filter(function (value, index, array) { - var hash = value[0]; - return hash === args[0]; - }).length; - }; - /** - * get the existing type to make sure no mix type add to the same store - * @param {string} evtName event name - * @param {string} type the type to check - * @return {boolean} true you can add, false then you can't add this type - */ - JsonqlEventService.prototype.checkTypeInStore = function (evtName, type) { - this.validateEvt(evtName, type); - var all = this.$get(evtName); - if (all === false) { - // pristine it means you can add - return true; - } - // it should only have ONE type in ONE event store - return !all - .filter(function (value, index, array) { - var t = value[3]; - return type !== t; - }).length; - }; - /** - * This is checking just the lazy store because the structure is different - * therefore we need to use a new method to check it - * @param {string} evtName event name - * @param {string} type the types of allow event - * @return {boolean} true OK - */ - JsonqlEventService.prototype.checkTypeInLazyStore = function (evtName, type) { - this.validateEvt(evtName, type); - var store = this.lazyStore.get(evtName); - if (store) { - this.logger('checkTypeInLazyStore', store); - return !!this.mapToArr(store) - .filter(function (value, index, array) { - var t = value[2]; - return t !== type; - }).length; - } - this.logger("Store " + evtName + " is empty"); - return false; - }; - /** - * wrapper to re-use the addToStore, - * V1.3.0 add extra check to see if this type can add to this evt - * @param {string} evt event name - * @param {string} type on or once - * @param {function} callback function - * @param {object} context the context the function execute in or null - * @return {number|boolean} size of the store, or false when the check type failed - */ - JsonqlEventService.prototype.addToNormalStore = function (evt, type, callback, context) { - if (context === void 0) { context = null; } - // @TODO we need to check the existing store for the type first! - if (this.checkTypeInStore(evt, type)) { - this.logger(type + " can add to " + evt + " store"); - var key = hashCode(callback); - var args = [this.normalStore, evt, key, callback, context, type]; - var _a = Reflect.apply(this.addToStore, this, args), _store = _a[0], size = _a[1]; - this.normalStore = _store; - return size; - } - this.logger('addToNormalStore', evt, type, 'NOT add to normal store'); - return false; - }; - /** - * Add to lazy store this get calls when the callback is not register yet - * so we only get a payload object or even nothing - * @param {string} evt event name - * @param {array} payload of arguments or empty if there is none - * @param {object} [ctx=null] the context the callback execute in - * @param {string|boolean} [type=false] register a type so no other type can add to this evt - * @return {number} size of the store - */ - JsonqlEventService.prototype.addToLazyStore = function (evt, payload, ctx, type) { - if (payload === void 0) { payload = []; } - if (ctx === void 0) { ctx = null; } - // this is add in V1.6.0 - // when there is type then we will need to check if this already added in lazy store - // and no other type can add to this lazy store - var args = []; - args.push(this.lazyStore, evt, this.toArray(payload), ctx); - if (type) { - args.push(type); - } - var _a = Reflect.apply(this.addToStore, this, args), _store = _a[0], size = _a[1]; - this.lazyStore = _store; - return size; - }; - // validators - // This is another JOKE about Typescript, after you spend all this time on - // fixing the non-error (aka typing) it doesn't do anything during run time - // so what the fuck? - /** - * validate the event name - * @param {string} evt event name - * @return {boolean} true when OK - */ - JsonqlEventService.prototype.validateEvt = function () { - var args = []; - for (var _i = 0; _i < arguments.length; _i++) { - args[_i] = arguments[_i]; - } - args.forEach(function (arg) { - if (typeof arg !== 'string') { - throw new Error("event name must be string type! " + arg); - } - }); - return true; - }; - /** - * Simple quick check on the two main parameters - * @param {function} callback function to call - * @param {string} evt event name - * @return {boolean} true when OK - */ - JsonqlEventService.prototype.validate = function (callback) { - var args = []; - for (var _i = 1; _i < arguments.length; _i++) { - args[_i - 1] = arguments[_i]; - } - if (typeof callback === 'function') { - return Reflect.apply(this.validateEvt, this, args); - } - throw new Error("callback required to be function type! " + callback); - }; - /** - * Check if this type is correct or not added in V1.5.0 - * @param {string} type for checking - * @return {boolean} true on OK - */ - JsonqlEventService.prototype.validateType = function (type) { - var types = ['on', 'only', 'once', 'onlyOnce']; - return !!types - .filter(function (t) { return type === t; }).length; - }; - return JsonqlEventService; - }()); + /** + * generate a 32bit hash based on the function.toString() + * _from http://stackoverflow.com/questions/7616461/generate-a-hash-_from-string-in-javascript-jquery + * @param {function} fn the converted to string function + * @return {string} the hashed function string + */ + function hashCode(fn) { + var s = fn.toString(); + return s.split("").reduce(function (a, b) { a = ((a << 5) - a) + b.charCodeAt(0); return a & a; }, 0) + ""; + } - exports.JsonqlEventService = JsonqlEventService; + // break up the store related operation here + var JsonqlStoreService = /** @class */ (function () { + function JsonqlStoreService(logger) { + // hack + this.logger = typeof logger === 'function' ? logger : function () { }; + // should this be WeakMap or should they be Map? + this.normalStore = new Map(); + this.lazyStore = new Map(); + // don't keep + this.keep = false; + // place holder, should this be a WeakSet? + this.result = []; + } + /** + * return all the listener from the event + * @param {string} evtName event name + * @param {boolean} [full=false] if true then return the entire content + * @return {array|boolean} listerner(s) or false when not found + */ + JsonqlStoreService.prototype.$get = function (evt, full) { + if (full === void 0) { full = false; } + this.validateEvt(evt); + var store = this.normalStore; + if (store.has(evt)) { + return this + .mapToArr(store.get(evt)) + .map(function (l) { + if (full) { + return l; + } + var key = l[0], callback = l[1]; + return callback; + }); + } + return false; + }; + Object.defineProperty(JsonqlStoreService.prototype, "$done", { + /** + * @TODO is there any real use with the keep prop? + * getter for $done + * @return {*} whatever last store result + */ + get: function () { + if (this.keep) { + this.logger(this.result); + return this.result[this.result.length - 1]; + } + return this.result; + }, + //////////////////////////////// + // SETTER / GETTER // + //////////////////////////////// + /** + * store the return result from the run + * @param {*} value whatever return from callback + */ + set: function (value) { + this.logger('set $done', value); + if (this.keep) { + this.result.push(value); + } + else { + this.result = value; + } + }, + enumerable: true, + configurable: true + }); + ///////////////////////////////// + // protected / PROTECTED METHODS // + ///////////////////////////////// + /** + * @param {any} mapObj the Map object + * @return {array} transform to array to work with + */ + JsonqlStoreService.prototype.mapToArr = function (mapObj) { + return Array.from(mapObj); + }; + /** + * make sure we store the argument correctly + * @param {*} arg could be array + * @return {array} make sured + */ + JsonqlStoreService.prototype.toArray = function (arg) { + return Array.isArray(arg) ? arg : [arg]; + }; + /** + * Run the callback + * @param {function} callback function to execute + * @param {array} payload for callback + * @param {object} ctx context or null + * @return {void} the result store in $done + */ + JsonqlStoreService.prototype.run = function (callback, payload, ctx) { + this.logger('run', callback, payload, ctx); + this.$done = Reflect.apply(callback, ctx, this.toArray(payload)); + }; + /** + * Take the content out and remove it from store id by the name + * @param {string} evt event name + * @return {object|boolean} content or false on not found + */ + JsonqlStoreService.prototype.takeFromStore = function (evt) { + var store = this.lazyStore; + if (store.has(evt)) { + var content = store.get(evt); + this.logger('takeFromStore', content); + store.delete(evt); + return content; + } + this.logger("lazyStore doesn't have " + evt); + return false; + }; + /** + * The add to store step is similar so make it generic for resuse + * @param {object} store which store to use + * @param {string} evt event name + * @param {spread} args because the lazy store and normal store store different things + * @return {array} store and the size of the store (cheated @TODO) + */ + JsonqlStoreService.prototype.addToStore = function (store, evt) { + var args = []; + for (var _i = 2; _i < arguments.length; _i++) { + args[_i - 2] = arguments[_i]; + } + var fnSet; + if (store.has(evt)) { + this.logger('addToStore', evt + " existed"); + fnSet = store.get(evt); + } + else { + this.logger('addToStore', "create new Set for " + evt); + // this is new + fnSet = new Set(); + } + // lazy only store 2 items - this is not the case in V1.6.0 anymore + // we need to check the first parameter is string or not + if (args.length > 2) { + if (Array.isArray(args[0])) { // lazy store + // check if this type of this event already register in the lazy store + var t = args[2]; + if (!this.checkTypeInLazyStore(evt, t)) { + fnSet.add(args); + } + } + else { + if (!this.checkContentExist(args, fnSet)) { + this.logger('addToStore', "insert new", args); + fnSet.add(args); + } + } + } + else { // add straight to lazy store + fnSet.add(args); + } + store.set(evt, fnSet); + return [store, fnSet.size]; + }; + /** + * @param {array} args for compare + * @param {object} fnSet A Set to search from + * @return {boolean} true on exist + */ + JsonqlStoreService.prototype.checkContentExist = function (args, fnSet) { + var list = this.mapToArr(fnSet); + return !!list + .filter(function (value, index, array) { + var hash = value[0]; + return hash === args[0]; + }).length; + }; + /** + * get the existing type to make sure no mix type add to the same store + * @param {string} evtName event name + * @param {string} type the type to check + * @return {boolean} true you can add, false then you can't add this type + */ + JsonqlStoreService.prototype.checkTypeInStore = function (evtName, type) { + this.validateEvt(evtName, type); + var all = this.$get(evtName); + if (all === false) { + // pristine it means you can add + return true; + } + // it should only have ONE type in ONE event store + return !all + .filter(function (value, index, array) { + var t = value[3]; + return type !== t; + }).length; + }; + /** + * This is checking just the lazy store because the structure is different + * therefore we need to use a new method to check it + * @param {string} evtName event name + * @param {string} type the types of allow event + * @return {boolean} true OK + */ + JsonqlStoreService.prototype.checkTypeInLazyStore = function (evtName, type) { + this.validateEvt(evtName, type); + var store = this.lazyStore.get(evtName); + if (store) { + this.logger('checkTypeInLazyStore', store); + return !!this.mapToArr(store) + .filter(function (value, index, array) { + var t = value[2]; + return t !== type; + }).length; + } + this.logger("Store " + evtName + " is empty"); + return false; + }; + /** + * wrapper to re-use the addToStore, + * V1.3.0 add extra check to see if this type can add to this evt + * @param {string} evt event name + * @param {string} type on or once + * @param {function} callback function + * @param {object} context the context the function execute in or null + * @return {number|boolean} size of the store, or false when the check type failed + */ + JsonqlStoreService.prototype.addToNormalStore = function (evt, type, callback, context) { + if (context === void 0) { context = null; } + // @TODO we need to check the existing store for the type first! + if (this.checkTypeInStore(evt, type)) { + this.logger("addToNormalStore: " + type + " can add to " + evt + " store"); + var key = hashCode(callback); + var args = [this.normalStore, evt, key, callback, context, type]; + var _a = Reflect.apply(this.addToStore, this, args), _store = _a[0], size = _a[1]; + // @BUG? is this the problem because it was a setter before + this.normalStore = _store; + return size; + } + this.logger('addToNormalStore:', evt, type, 'NOT add to normal store'); + return false; + }; + /** + * Add to lazy store this get calls when the callback is not register yet + * so we only get a payload object or even nothing + * @param {string} evt event name + * @param {array} payload of arguments or empty if there is none + * @param {object} [ctx=null] the context the callback execute in + * @param {string|boolean} [type=false] register a type so no other type can add to this evt + * @return {number} size of the store + */ + JsonqlStoreService.prototype.addToLazyStore = function (evt, payload, ctx, type) { + if (payload === void 0) { payload = []; } + if (ctx === void 0) { ctx = null; } + // this is add in V1.6.0 + // when there is type then we will need to check if this already added in lazy store + // and no other type can add to this lazy store + var args = []; + args.push(this.lazyStore, evt, this.toArray(payload), ctx); + if (type) { + args.push(type); + } + var _a = Reflect.apply(this.addToStore, this, args), _store = _a[0], size = _a[1]; + // @BUG ? this was a setter + this.lazyStore = _store; + return size; + }; + // VALIDATORS // + // This is another JOKE about Typescript, after you spend all this time on + // fixing the non-error (aka typing) it doesn't do anything during run time + // so what the fuck? + /** + * validate the event name + * @param {string} evt event name + * @return {boolean} true when OK + */ + JsonqlStoreService.prototype.validateEvt = function () { + var args = []; + for (var _i = 0; _i < arguments.length; _i++) { + args[_i] = arguments[_i]; + } + args.forEach(function (arg) { + if (typeof arg !== 'string') { + throw new Error("event name must be string type! " + arg); + } + }); + return true; + }; + /** + * Simple quick check on the two main parameters + * @param {function} callback function to call + * @param {string} evt event name + * @return {boolean} true when OK + */ + JsonqlStoreService.prototype.validate = function (callback) { + var args = []; + for (var _i = 1; _i < arguments.length; _i++) { + args[_i - 1] = arguments[_i]; + } + if (typeof callback === 'function') { + return Reflect.apply(this.validateEvt, this, args); + } + throw new Error("callback required to be function type! " + callback); + }; + /** + * Check if this type is correct or not added in V1.5.0 + * @param {string} type for checking + * @return {boolean} true on OK + */ + JsonqlStoreService.prototype.validateType = function (type) { + var types = ['on', 'only', 'once', 'onlyOnce']; + return !!types + .filter(function (t) { return type === t; }).length; + }; + return JsonqlStoreService; + }()); - Object.defineProperty(exports, '__esModule', { value: true }); + // main + var JsonqlEventService = /** @class */ (function (_super) { + __extends(JsonqlEventService, _super); + /** + * Create EventService instance + * @param {configObj} config configuration object + * @return {void} nothing - don't do :void this fuck typescript up what a lot of shit + */ + function JsonqlEventService(logger) { + return _super.call(this, logger) || this; + } + /** + * Register your evt handler, note we don't check the type here, + * we expect you to be sensible and know what you are doing. + * @param {string} evt name of event + * @param {function} callback bind method --> if it's array or not + * @param {object} [context=null] to execute this call in + * @return {number} the size of the store + */ + JsonqlEventService.prototype.$on = function (evt, callback, context) { + var _this = this; + if (context === void 0) { context = null; } + this.validate(callback, evt); + var type = 'on'; + // first need to check if this evt is in lazy store + var lazyStoreContent = this.takeFromStore(evt); + // this is normal register first then call later + if (lazyStoreContent === false) { + this.logger('$on', evt + " callback is not in lazy store"); + // @TODO we need to check if there was other listener to this + // event and are they the same type then we could solve that + // register the different type to the same event name + return this.addToNormalStore(evt, type, callback, context); + } + this.logger('$on', evt + " found in lazy store"); + // this is when they call $trigger before register this callback + var size = 0; + lazyStoreContent.forEach(function (content) { + var payload = content[0], ctx = content[1], t = content[2]; + if (t && t !== type) { + throw new Error("You are trying to register an event already been taken by other type: " + t); + } + _this.run(callback, payload, context || ctx); + var result = _this.addToNormalStore(evt, type, callback, context || ctx); + if (typeof result !== 'boolean') { + size += result; + } + }); + return size; + }; + /** + * once only registered it once, there is no overwrite option here + * @NOTE change in v1.3.0 $once can add multiple listeners + * but once the event fired, it will remove this event (see $only) + * @param {string} evt name + * @param {function} callback to execute + * @param {object} [context=null] the handler execute in + * @return {number|boolean} result + */ + JsonqlEventService.prototype.$once = function (evt, callback, context) { + if (context === void 0) { context = null; } + this.validate(callback, evt); + var type = 'once'; + var lazyStoreContent = this.takeFromStore(evt); + // this is normal register before call $trigger + var nStore = this.normalStore; + if (lazyStoreContent === false) { + this.logger('$once', evt + " not in the lazy store"); + // v1.3.0 $once now allow to add multiple listeners + return this.addToNormalStore(evt, type, callback, context); + } + else { + // now this is the tricky bit + // there is a potential bug here that cause by the developer + // if they call $trigger first, the lazy won't know it's a once call + // so if in the middle they register any call with the same evt name + // then this $once call will be fucked - add this to the documentation + this.logger('$once', lazyStoreContent); + var list = this.mapToArr(lazyStoreContent); + // should never have more than 1 + var _a = list[0], payload = _a[0], ctx = _a[1], t = _a[2]; + if (t && t !== type) { + throw new Error("You are trying to register an event already been taken by other type: " + t); + } + this.run(callback, payload, context || ctx); + // remove this evt from store + this.$off(evt); + } + return false; + }; + /** + * This one event can only bind one callbackback + * @param {string} evt event name + * @param {function} callback event handler + * @param {object} [context=null] the context the event handler execute in + * @return {boolean} true bind for first time, false already existed + */ + JsonqlEventService.prototype.$only = function (evt, callback, context) { + var _this = this; + if (context === void 0) { context = null; } + this.validate(callback, evt); + var type = 'only'; + // keep getting TS232, this is why typescript is a joke + // it will just ended up with everything is any type and back to square one + var added = false; + var lazyStoreContent = this.takeFromStore(evt); + // this is normal register before call $trigger + var nStore = this.normalStore; + if (!nStore.has(evt)) { + this.logger("$only", evt + " add to store"); + added = this.addToNormalStore(evt, type, callback, context); + } + if (lazyStoreContent !== false) { + // there are data store in lazy store + this.logger('$only', evt + " found data in lazy store to execute"); + var list = this.mapToArr(lazyStoreContent); + // $only allow to trigger this multiple time on the single handler + list.forEach(function (l) { + var payload = l[0], ctx = l[1], t = l[2]; + if (t && t !== type) { + throw new Error("You are trying to register an event already been taken by other type: " + t); + } + _this.run(callback, payload, context || ctx); + }); + } + return added; + }; + /** + * $only + $once this is because I found a very subtile bug when we pass a + * resolver, rejecter - and it never fire because that's OLD adeed in v1.4.0 + * @param {string} evt event name + * @param {function} callback to call later + * @param {object} [context=null] exeucte context + * @return {boolean} same as above + */ + JsonqlEventService.prototype.$onlyOnce = function (evt, callback, context) { + if (context === void 0) { context = null; } + this.validate(callback, evt); + var type = 'onlyOnce'; + var added = false; + var lazyStoreContent = this.takeFromStore(evt); + // this is normal register before call $trigger + var nStore = this.normalStore; + if (!nStore.has(evt)) { + this.logger("$onlyOnce", evt + " add to store"); + added = this.addToNormalStore(evt, type, callback, context); + } + if (lazyStoreContent !== false) { + // there are data store in lazy store + this.logger('$onlyOnce', lazyStoreContent); + var list = this.mapToArr(lazyStoreContent); + // should never have more than 1 + var _a = list[0], payload = _a[0], ctx = _a[1], t = _a[2]; + if (t && t !== 'onlyOnce') { + throw new Error("You are trying to register an event already been taken by other type: " + t); + } + this.run(callback, payload, context || ctx); + // remove this evt from store + this.$off(evt); + } + return added; + }; + /** + * This is a shorthand of $off + $on added in V1.5.0 + * @param {string} evt event name + * @param {function} callback to exeucte + * @param {object} [context = null] or pass a string as type + * @param {string} [type=on] what type of method to replace + * @return {any} whatever the call method return + */ + JsonqlEventService.prototype.$replace = function (evt, callback, context, type) { + if (context === void 0) { context = null; } + if (type === void 0) { type = 'on'; } + this.$off(evt); + // this will become a problem + var method = this.getMethodBy(type); + return Reflect.apply(method, this, [evt, callback, context]); + }; + /** + * trigger the event + * @param {string} evt name NOT allow array anymore! + * @param {mixed} [payload = []] pass to fn + * @param {object|string} [context = null] overwrite what stored + * @param {string} [type=false] if pass this then we need to add type to store too + * @return {number} if it has been execute how many times + */ + JsonqlEventService.prototype.$trigger = function (evt, payload, context, type) { + if (payload === void 0) { payload = []; } + if (context === void 0) { context = null; } + if (type === void 0) { type = false; } + this.validateEvt(evt); + var found = 0; + // first check the normal store + var nStore = this.normalStore; + this.logger('$trigger', nStore); + if (nStore.has(evt)) { + this.logger('$trigger', evt, 'found'); + var nSet = this.mapToArr(nStore.get(evt)); + var ctn = nSet.length; + var hasOnce = false; + // let hasOnly = false; + for (var i = 0; i < ctn; ++i) { + ++found; + // this.logger('found', found) + var _a = nSet[i], _ = _a[0], callback = _a[1], ctx = _a[2], type_1 = _a[3]; + this.run(callback, payload, context || ctx); + if (type_1 === 'once' || type_1 === 'onlyOnce') { + hasOnce = true; + } + } + if (hasOnce) { + nStore.delete(evt); + } + return found; + } + // now this is not register yet + this.addToLazyStore(evt, payload, context, type); + return found; + }; + /** + * this is an alias to the $trigger + * @NOTE breaking change in V1.6.0 we swap the parameter around + * @param {string} evt event name + * @param {*} params pass to the callback + * @param {string} type of call + * @param {object} context what context callback execute in + * @return {*} from $trigger + */ + JsonqlEventService.prototype.$call = function (evt, params, type, context) { + if (type === void 0) { type = false; } + if (context === void 0) { context = null; } + this.validateEvt(evt); + var args = [evt, params]; + args.push(context, type); + return Reflect.apply(this.$trigger, this, args); + }; + /** + * remove the evt from all the stores + * @param {string} evt name + * @return {boolean} true actually delete something + */ + JsonqlEventService.prototype.$off = function (evt) { + this.validateEvt(evt); + var stores = [this.lazyStore, this.normalStore]; + var found = false; + stores.forEach(function (store) { + if (store.has(evt)) { + found = true; + store.delete(evt); + } + }); + return found; + }; + /** + * When using ES6 you just need this[$+type] but typescript no such luck + * @param {string} type the 4 types + * @return {*} the method + */ + JsonqlEventService.prototype.getMethodBy = function (type) { + switch (type) { + case 'on': + return this.$on; + case 'only': + return this.$only; + case 'once': + return this.$once; + case 'onlyOnce': + return this.$onlyOnce; + default: + throw new Error(type + " is not supported!"); + } + }; + return JsonqlEventService; + }(JsonqlStoreService)); + + exports.JsonqlEventService = JsonqlEventService; + + Object.defineProperty(exports, '__esModule', { value: true }); })); diff --git a/packages/event/src/event-service.js b/packages/event/src/event-service.js index fe6f2cc6..3f26fcef 100644 --- a/packages/event/src/event-service.js +++ b/packages/event/src/event-service.js @@ -7,6 +7,8 @@ import { import genHaskKey from './hash-code.js' // export export default class EventService { + protected logger: any; + /** * class constructor */ diff --git a/packages/event/src/event-service.ts b/packages/event/src/event-service.ts index 74121092..65be7d87 100644 --- a/packages/event/src/event-service.ts +++ b/packages/event/src/event-service.ts @@ -4,8 +4,6 @@ import JsonqlStoreService from './store-service'; // main export class JsonqlEventService extends JsonqlStoreService { - // public props - keep: boolean; /** * Create EventService instance @@ -13,11 +11,7 @@ export class JsonqlEventService extends JsonqlStoreService { * @return {void} nothing - don't do :void this fuck typescript up what a lot of shit */ constructor(logger: any) { - super(logger) - - this.keep = false; - this.result = []; } /** @@ -257,60 +251,27 @@ export class JsonqlEventService extends JsonqlStoreService { return found; } - /** - * return all the listener from the event - * @param {string} evtName event name - * @param {boolean} [full=false] if true then return the entire content - * @return {array|boolean} listerner(s) or false when not found - */ - public $get(evt: string, full: boolean = false): any { - this.validateEvt(evt) - let store = this.normalStore; - if (store.has(evt)) { - return this - .mapToArr(store.get(evt)) - .map( l => { - if (full) { - return l; - } - let [key, callback, ] = l; - return callback; - }) - } - return false; - } - - - //////////////////////////////// - // SETTER / GETTER // - //////////////////////////////// - /** - * store the return result from the run - * @param {*} value whatever return from callback - */ - set $done(value: any) { - this.logger('set $done', value) - if (this.keep) { - this.result.push(value) - } else { - this.result = value; - } - } /** - * @TODO is there any real use with the keep prop? - * getter for $done - * @return {*} whatever last store result + * When using ES6 you just need this[$+type] but typescript no such luck + * @param {string} type the 4 types + * @return {*} the method */ - get $done(): any { - if (this.keep) { - this.logger(this.result) - return this.result[this.result.length - 1] + protected getMethodBy(type: string): any { + switch(type) { + case 'on': + return this.$on; + case 'only': + return this.$only; + case 'once': + return this.$once; + case 'onlyOnce': + return this.$onlyOnce; + default: + throw new Error(`${type} is not supported!`) } - return this.result; } - } diff --git a/packages/event/src/store-service.ts b/packages/event/src/store-service.ts index 84d5d5ca..3bd1a59b 100644 --- a/packages/event/src/store-service.ts +++ b/packages/event/src/store-service.ts @@ -3,11 +3,13 @@ import hashFnToKey from './hash-code'; export default class JsonqlStoreService { + // public props + keep: boolean; // protected props protected normalStore: Map; protected lazyStore: Map; - protected logger: any; protected result: any[]; + protected logger: any; constructor(logger: any) { // hack @@ -15,8 +17,67 @@ export default class JsonqlStoreService { // should this be WeakMap or should they be Map? this.normalStore = new Map() this.lazyStore = new Map() + // don't keep + this.keep = false; + // place holder, should this be a WeakSet? + this.result = []; } + + /** + * return all the listener from the event + * @param {string} evtName event name + * @param {boolean} [full=false] if true then return the entire content + * @return {array|boolean} listerner(s) or false when not found + */ + public $get(evt: string, full: boolean = false): any { + this.validateEvt(evt) + let store = this.normalStore; + if (store.has(evt)) { + return this + .mapToArr(store.get(evt)) + .map( l => { + if (full) { + return l; + } + let [key, callback, ] = l; + return callback; + }) + } + return false; + } + + + //////////////////////////////// + // SETTER / GETTER // + //////////////////////////////// + + /** + * store the return result from the run + * @param {*} value whatever return from callback + */ + set $done(value: any) { + this.logger('set $done', value) + if (this.keep) { + this.result.push(value) + } else { + this.result = value; + } + } + + /** + * @TODO is there any real use with the keep prop? + * getter for $done + * @return {*} whatever last store result + */ + get $done(): any { + if (this.keep) { + this.logger(this.result) + return this.result[this.result.length - 1] + } + return this.result; + } + ///////////////////////////////// // protected / PROTECTED METHODS // ///////////////////////////////// @@ -38,26 +99,6 @@ export default class JsonqlStoreService { return Array.isArray(arg) ? arg : [arg]; } - /** - * When using ES6 you just need this[$+type] but typescript no such luck - * @param {string} type the 4 types - * @return {*} the method - */ - protected getMethodBy(type: string): any { - switch(type) { - case 'on': - return this.$on; - case 'only': - return this.$only; - case 'once': - return this.$once; - case 'onlyOnce': - return this.$onlyOnce; - default: - throw new Error(`${type} is not supported!`) - } - } - /** * Run the callback * @param {function} callback function to execute @@ -227,7 +268,7 @@ export default class JsonqlStoreService { args.push(type) } let [_store, size] = Reflect.apply(this.addToStore, this, args) - // @BUG ? this was a setter + // @BUG ? this was a setter this.lazyStore = _store; return size; } -- Gitee From 0b245d8078e1963829206df728c79f77679c771c Mon Sep 17 00:00:00 2001 From: Joelchu Date: Tue, 16 Jul 2019 21:07:23 +0800 Subject: [PATCH 12/14] found the bug --- packages/event/dist/jsonql-event-service.cjs.js | 9 ++++++--- packages/event/dist/jsonql-event-service.es.js | 9 ++++++--- packages/event/dist/jsonql-event-service.umd.js | 9 ++++++--- packages/event/src/store-service.ts | 13 +++++++++---- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/packages/event/dist/jsonql-event-service.cjs.js b/packages/event/dist/jsonql-event-service.cjs.js index f7c33769..34ae5350 100644 --- a/packages/event/dist/jsonql-event-service.cjs.js +++ b/packages/event/dist/jsonql-event-service.cjs.js @@ -173,7 +173,7 @@ var JsonqlStoreService = /** @class */ (function () { fnSet = store.get(evt); } else { - this.logger('addToStore', "create new Set for " + evt); + this.logger('addToStore', "create new Set for \"" + evt + "\""); // this is new fnSet = new Set(); } @@ -220,8 +220,10 @@ var JsonqlStoreService = /** @class */ (function () { * @return {boolean} true you can add, false then you can't add this type */ JsonqlStoreService.prototype.checkTypeInStore = function (evtName, type) { + var _this = this; this.validateEvt(evtName, type); var all = this.$get(evtName); + this.logger('all: ', all); if (all === false) { // pristine it means you can add return true; @@ -230,6 +232,7 @@ var JsonqlStoreService = /** @class */ (function () { return !all .filter(function (value, index, array) { var t = value[3]; + _this.logger('checkTypeInStore', t, value); return type !== t; }).length; }; @@ -267,7 +270,7 @@ var JsonqlStoreService = /** @class */ (function () { if (context === void 0) { context = null; } // @TODO we need to check the existing store for the type first! if (this.checkTypeInStore(evt, type)) { - this.logger("addToNormalStore: " + type + " can add to " + evt + " store"); + this.logger("addToNormalStore: " + type + " can add to \"" + evt + "\" store"); var key = hashCode(callback); var args = [this.normalStore, evt, key, callback, context, type]; var _a = Reflect.apply(this.addToStore, this, args), _store = _a[0], size = _a[1]; @@ -275,7 +278,7 @@ var JsonqlStoreService = /** @class */ (function () { this.normalStore = _store; return size; } - this.logger('addToNormalStore:', evt, type, 'NOT add to normal store'); + this.logger('addToNormalStore:', evt, type, 'NOT add to normal store', callback.toString()); return false; }; /** diff --git a/packages/event/dist/jsonql-event-service.es.js b/packages/event/dist/jsonql-event-service.es.js index a8ef75e1..d96e7f2e 100644 --- a/packages/event/dist/jsonql-event-service.es.js +++ b/packages/event/dist/jsonql-event-service.es.js @@ -169,7 +169,7 @@ var JsonqlStoreService = /** @class */ (function () { fnSet = store.get(evt); } else { - this.logger('addToStore', "create new Set for " + evt); + this.logger('addToStore', "create new Set for \"" + evt + "\""); // this is new fnSet = new Set(); } @@ -216,8 +216,10 @@ var JsonqlStoreService = /** @class */ (function () { * @return {boolean} true you can add, false then you can't add this type */ JsonqlStoreService.prototype.checkTypeInStore = function (evtName, type) { + var _this = this; this.validateEvt(evtName, type); var all = this.$get(evtName); + this.logger('all: ', all); if (all === false) { // pristine it means you can add return true; @@ -226,6 +228,7 @@ var JsonqlStoreService = /** @class */ (function () { return !all .filter(function (value, index, array) { var t = value[3]; + _this.logger('checkTypeInStore', t, value); return type !== t; }).length; }; @@ -263,7 +266,7 @@ var JsonqlStoreService = /** @class */ (function () { if (context === void 0) { context = null; } // @TODO we need to check the existing store for the type first! if (this.checkTypeInStore(evt, type)) { - this.logger("addToNormalStore: " + type + " can add to " + evt + " store"); + this.logger("addToNormalStore: " + type + " can add to \"" + evt + "\" store"); var key = hashCode(callback); var args = [this.normalStore, evt, key, callback, context, type]; var _a = Reflect.apply(this.addToStore, this, args), _store = _a[0], size = _a[1]; @@ -271,7 +274,7 @@ var JsonqlStoreService = /** @class */ (function () { this.normalStore = _store; return size; } - this.logger('addToNormalStore:', evt, type, 'NOT add to normal store'); + this.logger('addToNormalStore:', evt, type, 'NOT add to normal store', callback.toString()); return false; }; /** diff --git a/packages/event/dist/jsonql-event-service.umd.js b/packages/event/dist/jsonql-event-service.umd.js index 212aff44..cc3d01e7 100644 --- a/packages/event/dist/jsonql-event-service.umd.js +++ b/packages/event/dist/jsonql-event-service.umd.js @@ -175,7 +175,7 @@ fnSet = store.get(evt); } else { - this.logger('addToStore', "create new Set for " + evt); + this.logger('addToStore', "create new Set for \"" + evt + "\""); // this is new fnSet = new Set(); } @@ -222,8 +222,10 @@ * @return {boolean} true you can add, false then you can't add this type */ JsonqlStoreService.prototype.checkTypeInStore = function (evtName, type) { + var _this = this; this.validateEvt(evtName, type); var all = this.$get(evtName); + this.logger('all: ', all); if (all === false) { // pristine it means you can add return true; @@ -232,6 +234,7 @@ return !all .filter(function (value, index, array) { var t = value[3]; + _this.logger('checkTypeInStore', t, value); return type !== t; }).length; }; @@ -269,7 +272,7 @@ if (context === void 0) { context = null; } // @TODO we need to check the existing store for the type first! if (this.checkTypeInStore(evt, type)) { - this.logger("addToNormalStore: " + type + " can add to " + evt + " store"); + this.logger("addToNormalStore: " + type + " can add to \"" + evt + "\" store"); var key = hashCode(callback); var args = [this.normalStore, evt, key, callback, context, type]; var _a = Reflect.apply(this.addToStore, this, args), _store = _a[0], size = _a[1]; @@ -277,7 +280,7 @@ this.normalStore = _store; return size; } - this.logger('addToNormalStore:', evt, type, 'NOT add to normal store'); + this.logger('addToNormalStore:', evt, type, 'NOT add to normal store', callback.toString()); return false; }; /** diff --git a/packages/event/src/store-service.ts b/packages/event/src/store-service.ts index 3bd1a59b..1cf218bf 100644 --- a/packages/event/src/store-service.ts +++ b/packages/event/src/store-service.ts @@ -141,7 +141,7 @@ export default class JsonqlStoreService { this.logger('addToStore', `${evt} existed`) fnSet = store.get(evt) } else { - this.logger('addToStore', `create new Set for ${evt}`) + this.logger('addToStore', `create new Set for "${evt}"`) // this is new fnSet = new Set() } @@ -189,7 +189,8 @@ export default class JsonqlStoreService { */ protected checkTypeInStore(evtName: string, type: string): boolean { this.validateEvt(evtName, type) - let all = this.$get(evtName) + let all = this.$get(evtName) // @BUG the return result is wrong! + this.logger('all: ', all) if (all === false) { // pristine it means you can add return true; @@ -197,7 +198,11 @@ export default class JsonqlStoreService { // it should only have ONE type in ONE event store return !all .filter( (value: any, index: number, array: any[]): boolean => { + let [ ,,,t ] = value; + + this.logger('checkTypeInStore', t, value) + return type !== t; }).length; } @@ -236,7 +241,7 @@ export default class JsonqlStoreService { protected addToNormalStore(evt: string, type: any, callback: myCallbackType, context: any = null): number | boolean { // @TODO we need to check the existing store for the type first! if (this.checkTypeInStore(evt, type)) { - this.logger(`addToNormalStore: ${type} can add to ${evt} store`) + this.logger(`addToNormalStore: ${type} can add to "${evt}" store`) let key = hashFnToKey(callback) let args = [this.normalStore, evt, key, callback, context, type] let [_store, size] = Reflect.apply(this.addToStore, this, args) @@ -245,7 +250,7 @@ export default class JsonqlStoreService { return size; } - this.logger('addToNormalStore:', evt, type, 'NOT add to normal store') + this.logger('addToNormalStore:', evt, type, 'NOT add to normal store', callback.toString()) return false; } -- Gitee From 3d4ddc99e4b267f55942c4721a0cc56d5855131b Mon Sep 17 00:00:00 2001 From: Joel Chu Date: Tue, 16 Jul 2019 22:45:47 +0800 Subject: [PATCH 13/14] Fix the once-problem --- packages/event/dist/jsonql-event-service.cjs.js | 8 ++++---- packages/event/dist/jsonql-event-service.es.js | 8 ++++---- packages/event/dist/jsonql-event-service.umd.js | 8 ++++---- packages/event/package.json | 2 +- packages/event/src/store-service.ts | 13 +++++-------- packages/event/tests/once-problem.test.js | 2 +- 6 files changed, 19 insertions(+), 22 deletions(-) diff --git a/packages/event/dist/jsonql-event-service.cjs.js b/packages/event/dist/jsonql-event-service.cjs.js index 34ae5350..2a710aa1 100644 --- a/packages/event/dist/jsonql-event-service.cjs.js +++ b/packages/event/dist/jsonql-event-service.cjs.js @@ -65,6 +65,7 @@ var JsonqlStoreService = /** @class */ (function () { if (full === void 0) { full = false; } this.validateEvt(evt); var store = this.normalStore; + this.logger('$get', full, this.normalStore); if (store.has(evt)) { return this .mapToArr(store.get(evt)) @@ -198,6 +199,7 @@ var JsonqlStoreService = /** @class */ (function () { fnSet.add(args); } store.set(evt, fnSet); + // this.logger('fnSet', fnSet, store) return [store, fnSet.size]; }; /** @@ -220,10 +222,8 @@ var JsonqlStoreService = /** @class */ (function () { * @return {boolean} true you can add, false then you can't add this type */ JsonqlStoreService.prototype.checkTypeInStore = function (evtName, type) { - var _this = this; this.validateEvt(evtName, type); - var all = this.$get(evtName); - this.logger('all: ', all); + var all = this.$get(evtName, true); if (all === false) { // pristine it means you can add return true; @@ -232,7 +232,6 @@ var JsonqlStoreService = /** @class */ (function () { return !all .filter(function (value, index, array) { var t = value[3]; - _this.logger('checkTypeInStore', t, value); return type !== t; }).length; }; @@ -276,6 +275,7 @@ var JsonqlStoreService = /** @class */ (function () { var _a = Reflect.apply(this.addToStore, this, args), _store = _a[0], size = _a[1]; // @BUG? is this the problem because it was a setter before this.normalStore = _store; + // this.logger('after add to store', this.normalStore) return size; } this.logger('addToNormalStore:', evt, type, 'NOT add to normal store', callback.toString()); diff --git a/packages/event/dist/jsonql-event-service.es.js b/packages/event/dist/jsonql-event-service.es.js index d96e7f2e..b952283b 100644 --- a/packages/event/dist/jsonql-event-service.es.js +++ b/packages/event/dist/jsonql-event-service.es.js @@ -61,6 +61,7 @@ var JsonqlStoreService = /** @class */ (function () { if (full === void 0) { full = false; } this.validateEvt(evt); var store = this.normalStore; + this.logger('$get', full, this.normalStore); if (store.has(evt)) { return this .mapToArr(store.get(evt)) @@ -194,6 +195,7 @@ var JsonqlStoreService = /** @class */ (function () { fnSet.add(args); } store.set(evt, fnSet); + // this.logger('fnSet', fnSet, store) return [store, fnSet.size]; }; /** @@ -216,10 +218,8 @@ var JsonqlStoreService = /** @class */ (function () { * @return {boolean} true you can add, false then you can't add this type */ JsonqlStoreService.prototype.checkTypeInStore = function (evtName, type) { - var _this = this; this.validateEvt(evtName, type); - var all = this.$get(evtName); - this.logger('all: ', all); + var all = this.$get(evtName, true); if (all === false) { // pristine it means you can add return true; @@ -228,7 +228,6 @@ var JsonqlStoreService = /** @class */ (function () { return !all .filter(function (value, index, array) { var t = value[3]; - _this.logger('checkTypeInStore', t, value); return type !== t; }).length; }; @@ -272,6 +271,7 @@ var JsonqlStoreService = /** @class */ (function () { var _a = Reflect.apply(this.addToStore, this, args), _store = _a[0], size = _a[1]; // @BUG? is this the problem because it was a setter before this.normalStore = _store; + // this.logger('after add to store', this.normalStore) return size; } this.logger('addToNormalStore:', evt, type, 'NOT add to normal store', callback.toString()); diff --git a/packages/event/dist/jsonql-event-service.umd.js b/packages/event/dist/jsonql-event-service.umd.js index cc3d01e7..654592e2 100644 --- a/packages/event/dist/jsonql-event-service.umd.js +++ b/packages/event/dist/jsonql-event-service.umd.js @@ -67,6 +67,7 @@ if (full === void 0) { full = false; } this.validateEvt(evt); var store = this.normalStore; + this.logger('$get', full, this.normalStore); if (store.has(evt)) { return this .mapToArr(store.get(evt)) @@ -200,6 +201,7 @@ fnSet.add(args); } store.set(evt, fnSet); + // this.logger('fnSet', fnSet, store) return [store, fnSet.size]; }; /** @@ -222,10 +224,8 @@ * @return {boolean} true you can add, false then you can't add this type */ JsonqlStoreService.prototype.checkTypeInStore = function (evtName, type) { - var _this = this; this.validateEvt(evtName, type); - var all = this.$get(evtName); - this.logger('all: ', all); + var all = this.$get(evtName, true); if (all === false) { // pristine it means you can add return true; @@ -234,7 +234,6 @@ return !all .filter(function (value, index, array) { var t = value[3]; - _this.logger('checkTypeInStore', t, value); return type !== t; }).length; }; @@ -278,6 +277,7 @@ var _a = Reflect.apply(this.addToStore, this, args), _store = _a[0], size = _a[1]; // @BUG? is this the problem because it was a setter before this.normalStore = _store; + // this.logger('after add to store', this.normalStore) return size; } this.logger('addToNormalStore:', evt, type, 'NOT add to normal store', callback.toString()); diff --git a/packages/event/package.json b/packages/event/package.json index 17b0b4b1..c2661d97 100644 --- a/packages/event/package.json +++ b/packages/event/package.json @@ -1,6 +1,6 @@ { "name": "@jsonql/event", - "version": "1.0.2", + "version": "1.1.0", "description": "Ported from nb-event-service rewritten with Typescript", "main": "dist/jsonql-event-service.cjs.js", "browser": "dist/jsonql-event-service.umd.js", diff --git a/packages/event/src/store-service.ts b/packages/event/src/store-service.ts index 1cf218bf..1509457d 100644 --- a/packages/event/src/store-service.ts +++ b/packages/event/src/store-service.ts @@ -33,6 +33,7 @@ export default class JsonqlStoreService { public $get(evt: string, full: boolean = false): any { this.validateEvt(evt) let store = this.normalStore; + this.logger('$get', full, this.normalStore) if (store.has(evt)) { return this .mapToArr(store.get(evt)) @@ -164,6 +165,7 @@ export default class JsonqlStoreService { fnSet.add(args) } store.set(evt, fnSet) + // this.logger('fnSet', fnSet, store) return [store, fnSet.size] } @@ -189,20 +191,15 @@ export default class JsonqlStoreService { */ protected checkTypeInStore(evtName: string, type: string): boolean { this.validateEvt(evtName, type) - let all = this.$get(evtName) // @BUG the return result is wrong! - this.logger('all: ', all) + let all = this.$get(evtName, true) if (all === false) { // pristine it means you can add return true; } // it should only have ONE type in ONE event store return !all - .filter( (value: any, index: number, array: any[]): boolean => { - + .filter((value: any, index: number, array: any[]): boolean => { let [ ,,,t ] = value; - - this.logger('checkTypeInStore', t, value) - return type !== t; }).length; } @@ -247,7 +244,7 @@ export default class JsonqlStoreService { let [_store, size] = Reflect.apply(this.addToStore, this, args) // @BUG? is this the problem because it was a setter before this.normalStore = _store; - + // this.logger('after add to store', this.normalStore) return size; } this.logger('addToNormalStore:', evt, type, 'NOT add to normal store', callback.toString()) diff --git a/packages/event/tests/once-problem.test.js b/packages/event/tests/once-problem.test.js index 8b23c07e..84145629 100644 --- a/packages/event/tests/once-problem.test.js +++ b/packages/event/tests/once-problem.test.js @@ -53,7 +53,7 @@ test.cb('$once should allow to add more than one listner', t => { }) -test.only('It should be fixed with the check type before adding to the store, but the $done value can be unpredictable', t => { +test('It should be fixed with the check type before adding to the store, but the $done value can be unpredictable', t => { let evtName = 'once-problem'; let evtSrv = t.context.evtSrv; -- Gitee From dbc653201aedf340ae8a7921627e504f6e332632 Mon Sep 17 00:00:00 2001 From: Joel Chu Date: Tue, 16 Jul 2019 22:46:50 +0800 Subject: [PATCH 14/14] fix the basic test with throw error --- packages/event/tests/basic.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/event/tests/basic.test.js b/packages/event/tests/basic.test.js index d7f37a60..139c4e02 100644 --- a/packages/event/tests/basic.test.js +++ b/packages/event/tests/basic.test.js @@ -11,7 +11,7 @@ test.before( t => { t.context.evtSrv = new JsonqlEventService(logger) }) -test.skip('It should able to validate the evt', t => { +test('It should able to validate the evt', t => { let evtSrv = t.context.evtSrv; let fn = (...args) => Reflect.apply(evtSrv.$on, evtSrv, args) -- Gitee