From 44c5edd7e8ce3c13da9c78fb4bd86d749537460c Mon Sep 17 00:00:00 2001 From: Joel Chu Date: Sat, 27 Jul 2019 22:00:44 +0800 Subject: [PATCH 1/9] update deps --- packages/web-console/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/web-console/package.json b/packages/web-console/package.json index ff27b79f..5f7f401e 100644 --- a/packages/web-console/package.json +++ b/packages/web-console/package.json @@ -11,7 +11,7 @@ }, "dependencies": { "core-js": "^3.1.4", - "jsonql-client": "^1.2.8", + "jsonql-client": "^1.2.10", "jsonql-ws-client": "^1.0.0-beta.1", "vue": "^2.6.10", "vue-async-computed": "^3.7.0", -- Gitee From f2f24263bd30ebbfd22ac57257b00ba6e99d2edb Mon Sep 17 00:00:00 2001 From: Joel Chu Date: Mon, 29 Jul 2019 12:58:38 +0800 Subject: [PATCH 2/9] start porting the suspend to evets --- packages/event/package.json | 2 +- packages/event/src/event-service.ts | 3 --- packages/event/src/suspend.ts | 1 + 3 files changed, 2 insertions(+), 4 deletions(-) create mode 100644 packages/event/src/suspend.ts diff --git a/packages/event/package.json b/packages/event/package.json index c2661d97..ac6e2624 100644 --- a/packages/event/package.json +++ b/packages/event/package.json @@ -1,6 +1,6 @@ { "name": "@jsonql/event", - "version": "1.1.0", + "version": "1.2.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/event-service.ts b/packages/event/src/event-service.ts index 65be7d87..6d624f8b 100644 --- a/packages/event/src/event-service.ts +++ b/packages/event/src/event-service.ts @@ -251,9 +251,6 @@ export class JsonqlEventService extends JsonqlStoreService { return found; } - - - /** * When using ES6 you just need this[$+type] but typescript no such luck * @param {string} type the 4 types diff --git a/packages/event/src/suspend.ts b/packages/event/src/suspend.ts new file mode 100644 index 00000000..6d37aa40 --- /dev/null +++ b/packages/event/src/suspend.ts @@ -0,0 +1 @@ +// the suspend ported from nb-event-service -- Gitee From c2aaf5fe0bcea57fcb288fc04ab5e830d7e2d89e Mon Sep 17 00:00:00 2001 From: Joelchu Date: Mon, 29 Jul 2019 16:48:24 +0800 Subject: [PATCH 3/9] The base code in place for the new suspend feature for jsonql/event --- packages/event/src/event-service.ts | 8 +++- packages/event/src/store-service.ts | 4 +- packages/event/src/suspend.ts | 69 +++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 3 deletions(-) diff --git a/packages/event/src/event-service.ts b/packages/event/src/event-service.ts index 6d624f8b..e6519ee2 100644 --- a/packages/event/src/event-service.ts +++ b/packages/event/src/event-service.ts @@ -191,9 +191,13 @@ export class JsonqlEventService extends JsonqlStoreService { let found = 0; // first check the normal store let nStore = this.normalStore; - this.logger('$trigger', nStore) + this.logger('$trigger, from normalStore: ', nStore) if (nStore.has(evt)) { - this.logger('$trigger', evt, 'found') + let added = this.$queue(evt, payload, context, type) + this.logger('$trigger', evt, 'found; add to queue: ', added) + if (added === true) { + return found; // just exit + } let nSet = this.mapToArr(nStore.get(evt)) let ctn = nSet.length; let hasOnce = false; diff --git a/packages/event/src/store-service.ts b/packages/event/src/store-service.ts index 1509457d..ddf3841d 100644 --- a/packages/event/src/store-service.ts +++ b/packages/event/src/store-service.ts @@ -1,8 +1,9 @@ // break up the store related operation here // because the class is getting way too big now import hashFnToKey from './hash-code'; +import JsonqlSuspend from './suspend'; -export default class JsonqlStoreService { +export default class JsonqlStoreService extends JsonqlSuspend { // public props keep: boolean; // protected props @@ -12,6 +13,7 @@ export default class JsonqlStoreService { protected logger: any; constructor(logger: any) { + super() // hack this.logger = typeof logger === 'function' ? logger : () => {}; // should this be WeakMap or should they be Map? diff --git a/packages/event/src/suspend.ts b/packages/event/src/suspend.ts index 6d37aa40..c25a3fab 100644 --- a/packages/event/src/suspend.ts +++ b/packages/event/src/suspend.ts @@ -1 +1,70 @@ // the suspend ported from nb-event-service +// This is different from the nb-event-service +// instead of using object.defineProperty we just use the native ES6 setter +// with Typescript private + +export default class JsonqlSuspend { + private queue: any[]; + private lastSuspendState: boolean; + private suspend: boolean; + + constructor() { + + this.queue = new Set() + } + + /** + * This is the main interface to see if anything happens + * @param {boolean} state to change the suspend state + * @return {boolean} what comes in what goes out + */ + set $suspend(state: boolean) { + this.suspend = state; + if (state !== this.lastSuspendState) { + if (this.lastSuspendState === true && this.suspend === false) { + setTimeout(() => { + this.releaseQueue() + }, 1) + } + } + this.lastSuspendState = state; + } + + /** + * getter to get back the current suspend state + * @return {boolean} the suspend state + */ + get $suspend(): boolean { + return this.suspend; + } + + /** + * Add $trigger args to the queue during suspend state + * @param {array} args from the $trigger call parameters + * @return {boolean} true when added + */ + protected $queue(...args: any[]): boolean { + let added = false; + if (this.suspend) { + this.queue.add(args) + } + return added; + } + + /** + * whenever the suspend change from true to false + * then we check the queue to see if there is anything to do + * @return {boolean} true if there is queue + */ + private releaseQueue(): boolean { + let size = this.queue.size; + if (size > 0) { + let queue = this.mapToArr(this.queue) + this.queue.clear() + queue.forEach(args => { + Reflect.apply(this.$trigger, this, args) + }) + } + return !!size; + } +} -- Gitee From ed3a44dc630faff8ebc6a7032c14d970d483a4c8 Mon Sep 17 00:00:00 2001 From: Joelchu Date: Mon, 29 Jul 2019 18:08:44 +0800 Subject: [PATCH 4/9] Finally able to build that Typescript Mother f**ker --- .../event/dist/jsonql-event-service.cjs.js | 140 ++++++++++++---- .../event/dist/jsonql-event-service.es.js | 138 +++++++++++++--- .../event/dist/jsonql-event-service.umd.js | 149 ++++++++++++++---- packages/event/index.ts | 2 +- packages/event/src/event-service.ts | 61 ++++++- packages/event/src/interfaces.ts | 1 + packages/event/src/store-service.ts | 20 +-- packages/event/src/suspend.ts | 59 ++----- 8 files changed, 417 insertions(+), 153 deletions(-) diff --git a/packages/event/dist/jsonql-event-service.cjs.js b/packages/event/dist/jsonql-event-service.cjs.js index 2a710aa1..106ada0b 100644 --- a/packages/event/dist/jsonql-event-service.cjs.js +++ b/packages/event/dist/jsonql-event-service.cjs.js @@ -1,7 +1,5 @@ 'use strict'; -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 @@ -42,18 +40,51 @@ function hashCode(fn) { return s.split("").reduce(function (a, b) { a = ((a << 5) - a) + b.charCodeAt(0); return a & a; }, 0) + ""; } -// break up the store related operation here -var JsonqlStoreService = /** @class */ (function () { +// the suspend ported from nb-event-service +// This is different from the nb-event-service +// instead of using object.defineProperty we just use the native ES6 setter +// with Typescript private +var JsonqlSuspend = /** @class */ (function () { + function JsonqlSuspend(logger) { + this.lastSuspendState = null; + this.suspend = null; + this.queue = new Set(); + } + Object.defineProperty(JsonqlSuspend.prototype, "$suspend", { + /** + * getter to get back the current suspend state + * @return {boolean} the suspend state + */ + get: function () { + return this.suspend; + }, + enumerable: true, + configurable: true + }); + /** + * @param {any} mapObj the Map object + * @return {array} transform to array to work with + */ + JsonqlSuspend.prototype.arrayFrom = function (mapObj) { + return Array.from(mapObj); + }; + return JsonqlSuspend; +}()); + +var JsonqlStoreService = /** @class */ (function (_super) { + __extends(JsonqlStoreService, _super); function JsonqlStoreService(logger) { + var _this = _super.call(this, logger) || this; // hack - this.logger = typeof logger === 'function' ? logger : function () { }; + _this.logger = typeof logger === 'function' ? logger : function () { }; // should this be WeakMap or should they be Map? - this.normalStore = new Map(); - this.lazyStore = new Map(); + _this.normalStore = new Map(); + _this.lazyStore = new Map(); // don't keep - this.keep = false; + _this.keep = false; // place holder, should this be a WeakSet? - this.result = []; + _this.result = []; + return _this; } /** * return all the listener from the event @@ -68,7 +99,7 @@ var JsonqlStoreService = /** @class */ (function () { this.logger('$get', full, this.normalStore); if (store.has(evt)) { return this - .mapToArr(store.get(evt)) + .arrayFrom(store.get(evt)) .map(function (l) { if (full) { return l; @@ -114,13 +145,6 @@ var JsonqlStoreService = /** @class */ (function () { ///////////////////////////////// // 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 @@ -208,7 +232,7 @@ var JsonqlStoreService = /** @class */ (function () { * @return {boolean} true on exist */ JsonqlStoreService.prototype.checkContentExist = function (args, fnSet) { - var list = this.mapToArr(fnSet); + var list = this.arrayFrom(fnSet); return !!list .filter(function (value, index, array) { var hash = value[0]; @@ -247,7 +271,7 @@ var JsonqlStoreService = /** @class */ (function () { var store = this.lazyStore.get(evtName); if (store) { this.logger('checkTypeInLazyStore', store); - return !!this.mapToArr(store) + return !!this.arrayFrom(store) .filter(function (value, index, array) { var t = value[2]; return t !== type; @@ -354,7 +378,7 @@ var JsonqlStoreService = /** @class */ (function () { .filter(function (t) { return type === t; }).length; }; return JsonqlStoreService; -}()); +}(JsonqlSuspend)); // main var JsonqlEventService = /** @class */ (function (_super) { @@ -434,7 +458,7 @@ var JsonqlEventService = /** @class */ (function (_super) { // 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); + var list = this.arrayFrom(lazyStoreContent); // should never have more than 1 var _a = list[0], payload = _a[0], ctx = _a[1], t = _a[2]; if (t && t !== type) { @@ -471,7 +495,7 @@ var JsonqlEventService = /** @class */ (function (_super) { 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); + var list = this.arrayFrom(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]; @@ -506,7 +530,7 @@ var JsonqlEventService = /** @class */ (function (_super) { if (lazyStoreContent !== false) { // there are data store in lazy store this.logger('$onlyOnce', lazyStoreContent); - var list = this.mapToArr(lazyStoreContent); + var list = this.arrayFrom(lazyStoreContent); // should never have more than 1 var _a = list[0], payload = _a[0], ctx = _a[1], t = _a[2]; if (t && t !== 'onlyOnce') { @@ -550,10 +574,14 @@ var JsonqlEventService = /** @class */ (function (_super) { var found = 0; // first check the normal store var nStore = this.normalStore; - this.logger('$trigger', nStore); + this.logger('$trigger, from normalStore: ', nStore); if (nStore.has(evt)) { - this.logger('$trigger', evt, 'found'); - var nSet = this.mapToArr(nStore.get(evt)); + var added = this.$queue(evt, payload, context, type); + this.logger('$trigger', evt, 'found; add to queue: ', added); + if (added === true) { + return found; // just exit + } + var nSet = this.arrayFrom(nStore.get(evt)); var ctn = nSet.length; var hasOnce = false; // let hasOnly = false; @@ -628,7 +656,63 @@ var JsonqlEventService = /** @class */ (function (_super) { throw new Error(type + " is not supported!"); } }; + /** + * Add $trigger args to the queue during suspend state + * @param {array} args from the $trigger call parameters + * @return {boolean} true when added + */ + JsonqlEventService.prototype.$queue = function () { + var args = []; + for (var _i = 0; _i < arguments.length; _i++) { + args[_i] = arguments[_i]; + } + var added = false; + if (this.suspend) { + this.queue.add(args); + added = true; + } + return added; + }; + /** + * whenever the suspend change from true to false + * then we check the queue to see if there is anything to do + * @return {boolean} true if there is queue + */ + JsonqlEventService.prototype.releaseQueue = function () { + var _this = this; + var size = this.queue.size; + if (size > 0) { + var queue = this.arrayFrom(this.queue); + this.queue.clear(); + queue.forEach(function (args) { + Reflect.apply(_this.$trigger, _this, args); + }); + } + return !!size; + }; + Object.defineProperty(JsonqlEventService.prototype, "$suspend", { + /** + * This is the main interface to see if anything happens + * @param {boolean} state to change the suspend state + * @return {boolean} what comes in what goes out + */ + set: function (state) { + var _this = this; + if (typeof state === 'boolean') { + this.suspend = state; + if (state !== this.lastSuspendState) { + if (this.lastSuspendState === true && this.suspend === false) { + setTimeout(function () { + _this.releaseQueue(); + }, 1); + } + } + this.lastSuspendState = state; + } + throw new Error("Typescript is a fucking joke! BTW we want a Boolean which fucking Typescript can't figure it out!!!!"); + }, + enumerable: true, + configurable: true + }); 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 b952283b..1698fcf5 100644 --- a/packages/event/dist/jsonql-event-service.es.js +++ b/packages/event/dist/jsonql-event-service.es.js @@ -38,18 +38,51 @@ function hashCode(fn) { return s.split("").reduce(function (a, b) { a = ((a << 5) - a) + b.charCodeAt(0); return a & a; }, 0) + ""; } -// break up the store related operation here -var JsonqlStoreService = /** @class */ (function () { +// the suspend ported from nb-event-service +// This is different from the nb-event-service +// instead of using object.defineProperty we just use the native ES6 setter +// with Typescript private +var JsonqlSuspend = /** @class */ (function () { + function JsonqlSuspend(logger) { + this.lastSuspendState = null; + this.suspend = null; + this.queue = new Set(); + } + Object.defineProperty(JsonqlSuspend.prototype, "$suspend", { + /** + * getter to get back the current suspend state + * @return {boolean} the suspend state + */ + get: function () { + return this.suspend; + }, + enumerable: true, + configurable: true + }); + /** + * @param {any} mapObj the Map object + * @return {array} transform to array to work with + */ + JsonqlSuspend.prototype.arrayFrom = function (mapObj) { + return Array.from(mapObj); + }; + return JsonqlSuspend; +}()); + +var JsonqlStoreService = /** @class */ (function (_super) { + __extends(JsonqlStoreService, _super); function JsonqlStoreService(logger) { + var _this = _super.call(this, logger) || this; // hack - this.logger = typeof logger === 'function' ? logger : function () { }; + _this.logger = typeof logger === 'function' ? logger : function () { }; // should this be WeakMap or should they be Map? - this.normalStore = new Map(); - this.lazyStore = new Map(); + _this.normalStore = new Map(); + _this.lazyStore = new Map(); // don't keep - this.keep = false; + _this.keep = false; // place holder, should this be a WeakSet? - this.result = []; + _this.result = []; + return _this; } /** * return all the listener from the event @@ -64,7 +97,7 @@ var JsonqlStoreService = /** @class */ (function () { this.logger('$get', full, this.normalStore); if (store.has(evt)) { return this - .mapToArr(store.get(evt)) + .arrayFrom(store.get(evt)) .map(function (l) { if (full) { return l; @@ -110,13 +143,6 @@ var JsonqlStoreService = /** @class */ (function () { ///////////////////////////////// // 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 @@ -204,7 +230,7 @@ var JsonqlStoreService = /** @class */ (function () { * @return {boolean} true on exist */ JsonqlStoreService.prototype.checkContentExist = function (args, fnSet) { - var list = this.mapToArr(fnSet); + var list = this.arrayFrom(fnSet); return !!list .filter(function (value, index, array) { var hash = value[0]; @@ -243,7 +269,7 @@ var JsonqlStoreService = /** @class */ (function () { var store = this.lazyStore.get(evtName); if (store) { this.logger('checkTypeInLazyStore', store); - return !!this.mapToArr(store) + return !!this.arrayFrom(store) .filter(function (value, index, array) { var t = value[2]; return t !== type; @@ -350,7 +376,7 @@ var JsonqlStoreService = /** @class */ (function () { .filter(function (t) { return type === t; }).length; }; return JsonqlStoreService; -}()); +}(JsonqlSuspend)); // main var JsonqlEventService = /** @class */ (function (_super) { @@ -430,7 +456,7 @@ var JsonqlEventService = /** @class */ (function (_super) { // 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); + var list = this.arrayFrom(lazyStoreContent); // should never have more than 1 var _a = list[0], payload = _a[0], ctx = _a[1], t = _a[2]; if (t && t !== type) { @@ -467,7 +493,7 @@ var JsonqlEventService = /** @class */ (function (_super) { 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); + var list = this.arrayFrom(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]; @@ -502,7 +528,7 @@ var JsonqlEventService = /** @class */ (function (_super) { if (lazyStoreContent !== false) { // there are data store in lazy store this.logger('$onlyOnce', lazyStoreContent); - var list = this.mapToArr(lazyStoreContent); + var list = this.arrayFrom(lazyStoreContent); // should never have more than 1 var _a = list[0], payload = _a[0], ctx = _a[1], t = _a[2]; if (t && t !== 'onlyOnce') { @@ -546,10 +572,14 @@ var JsonqlEventService = /** @class */ (function (_super) { var found = 0; // first check the normal store var nStore = this.normalStore; - this.logger('$trigger', nStore); + this.logger('$trigger, from normalStore: ', nStore); if (nStore.has(evt)) { - this.logger('$trigger', evt, 'found'); - var nSet = this.mapToArr(nStore.get(evt)); + var added = this.$queue(evt, payload, context, type); + this.logger('$trigger', evt, 'found; add to queue: ', added); + if (added === true) { + return found; // just exit + } + var nSet = this.arrayFrom(nStore.get(evt)); var ctn = nSet.length; var hasOnce = false; // let hasOnly = false; @@ -624,7 +654,63 @@ var JsonqlEventService = /** @class */ (function (_super) { throw new Error(type + " is not supported!"); } }; + /** + * Add $trigger args to the queue during suspend state + * @param {array} args from the $trigger call parameters + * @return {boolean} true when added + */ + JsonqlEventService.prototype.$queue = function () { + var args = []; + for (var _i = 0; _i < arguments.length; _i++) { + args[_i] = arguments[_i]; + } + var added = false; + if (this.suspend) { + this.queue.add(args); + added = true; + } + return added; + }; + /** + * whenever the suspend change from true to false + * then we check the queue to see if there is anything to do + * @return {boolean} true if there is queue + */ + JsonqlEventService.prototype.releaseQueue = function () { + var _this = this; + var size = this.queue.size; + if (size > 0) { + var queue = this.arrayFrom(this.queue); + this.queue.clear(); + queue.forEach(function (args) { + Reflect.apply(_this.$trigger, _this, args); + }); + } + return !!size; + }; + Object.defineProperty(JsonqlEventService.prototype, "$suspend", { + /** + * This is the main interface to see if anything happens + * @param {boolean} state to change the suspend state + * @return {boolean} what comes in what goes out + */ + set: function (state) { + var _this = this; + if (typeof state === 'boolean') { + this.suspend = state; + if (state !== this.lastSuspendState) { + if (this.lastSuspendState === true && this.suspend === false) { + setTimeout(function () { + _this.releaseQueue(); + }, 1); + } + } + this.lastSuspendState = state; + } + throw new Error("Typescript is a fucking joke! BTW we want a Boolean which fucking Typescript can't figure it out!!!!"); + }, + enumerable: true, + configurable: true + }); 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 654592e2..d6038a0e 100644 --- a/packages/event/dist/jsonql-event-service.umd.js +++ b/packages/event/dist/jsonql-event-service.umd.js @@ -1,8 +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.JsonqlEventService = {})); -}(this, function (exports) { 'use strict'; +(function (factory) { + typeof define === 'function' && define.amd ? define(factory) : + factory(); +}(function () { 'use strict'; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. All rights reserved. @@ -44,18 +43,51 @@ return s.split("").reduce(function (a, b) { a = ((a << 5) - a) + b.charCodeAt(0); return a & a; }, 0) + ""; } - // break up the store related operation here - var JsonqlStoreService = /** @class */ (function () { + // the suspend ported from nb-event-service + // This is different from the nb-event-service + // instead of using object.defineProperty we just use the native ES6 setter + // with Typescript private + var JsonqlSuspend = /** @class */ (function () { + function JsonqlSuspend(logger) { + this.lastSuspendState = null; + this.suspend = null; + this.queue = new Set(); + } + Object.defineProperty(JsonqlSuspend.prototype, "$suspend", { + /** + * getter to get back the current suspend state + * @return {boolean} the suspend state + */ + get: function () { + return this.suspend; + }, + enumerable: true, + configurable: true + }); + /** + * @param {any} mapObj the Map object + * @return {array} transform to array to work with + */ + JsonqlSuspend.prototype.arrayFrom = function (mapObj) { + return Array.from(mapObj); + }; + return JsonqlSuspend; + }()); + + var JsonqlStoreService = /** @class */ (function (_super) { + __extends(JsonqlStoreService, _super); function JsonqlStoreService(logger) { + var _this = _super.call(this, logger) || this; // hack - this.logger = typeof logger === 'function' ? logger : function () { }; + _this.logger = typeof logger === 'function' ? logger : function () { }; // should this be WeakMap or should they be Map? - this.normalStore = new Map(); - this.lazyStore = new Map(); + _this.normalStore = new Map(); + _this.lazyStore = new Map(); // don't keep - this.keep = false; + _this.keep = false; // place holder, should this be a WeakSet? - this.result = []; + _this.result = []; + return _this; } /** * return all the listener from the event @@ -70,7 +102,7 @@ this.logger('$get', full, this.normalStore); if (store.has(evt)) { return this - .mapToArr(store.get(evt)) + .arrayFrom(store.get(evt)) .map(function (l) { if (full) { return l; @@ -116,13 +148,6 @@ ///////////////////////////////// // 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 @@ -210,7 +235,7 @@ * @return {boolean} true on exist */ JsonqlStoreService.prototype.checkContentExist = function (args, fnSet) { - var list = this.mapToArr(fnSet); + var list = this.arrayFrom(fnSet); return !!list .filter(function (value, index, array) { var hash = value[0]; @@ -249,7 +274,7 @@ var store = this.lazyStore.get(evtName); if (store) { this.logger('checkTypeInLazyStore', store); - return !!this.mapToArr(store) + return !!this.arrayFrom(store) .filter(function (value, index, array) { var t = value[2]; return t !== type; @@ -356,7 +381,7 @@ .filter(function (t) { return type === t; }).length; }; return JsonqlStoreService; - }()); + }(JsonqlSuspend)); // main var JsonqlEventService = /** @class */ (function (_super) { @@ -436,7 +461,7 @@ // 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); + var list = this.arrayFrom(lazyStoreContent); // should never have more than 1 var _a = list[0], payload = _a[0], ctx = _a[1], t = _a[2]; if (t && t !== type) { @@ -473,7 +498,7 @@ 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); + var list = this.arrayFrom(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]; @@ -508,7 +533,7 @@ if (lazyStoreContent !== false) { // there are data store in lazy store this.logger('$onlyOnce', lazyStoreContent); - var list = this.mapToArr(lazyStoreContent); + var list = this.arrayFrom(lazyStoreContent); // should never have more than 1 var _a = list[0], payload = _a[0], ctx = _a[1], t = _a[2]; if (t && t !== 'onlyOnce') { @@ -552,10 +577,14 @@ var found = 0; // first check the normal store var nStore = this.normalStore; - this.logger('$trigger', nStore); + this.logger('$trigger, from normalStore: ', nStore); if (nStore.has(evt)) { - this.logger('$trigger', evt, 'found'); - var nSet = this.mapToArr(nStore.get(evt)); + var added = this.$queue(evt, payload, context, type); + this.logger('$trigger', evt, 'found; add to queue: ', added); + if (added === true) { + return found; // just exit + } + var nSet = this.arrayFrom(nStore.get(evt)); var ctn = nSet.length; var hasOnce = false; // let hasOnly = false; @@ -630,11 +659,65 @@ throw new Error(type + " is not supported!"); } }; + /** + * Add $trigger args to the queue during suspend state + * @param {array} args from the $trigger call parameters + * @return {boolean} true when added + */ + JsonqlEventService.prototype.$queue = function () { + var args = []; + for (var _i = 0; _i < arguments.length; _i++) { + args[_i] = arguments[_i]; + } + var added = false; + if (this.suspend) { + this.queue.add(args); + added = true; + } + return added; + }; + /** + * whenever the suspend change from true to false + * then we check the queue to see if there is anything to do + * @return {boolean} true if there is queue + */ + JsonqlEventService.prototype.releaseQueue = function () { + var _this = this; + var size = this.queue.size; + if (size > 0) { + var queue = this.arrayFrom(this.queue); + this.queue.clear(); + queue.forEach(function (args) { + Reflect.apply(_this.$trigger, _this, args); + }); + } + return !!size; + }; + Object.defineProperty(JsonqlEventService.prototype, "$suspend", { + /** + * This is the main interface to see if anything happens + * @param {boolean} state to change the suspend state + * @return {boolean} what comes in what goes out + */ + set: function (state) { + var _this = this; + if (typeof state === 'boolean') { + this.suspend = state; + if (state !== this.lastSuspendState) { + if (this.lastSuspendState === true && this.suspend === false) { + setTimeout(function () { + _this.releaseQueue(); + }, 1); + } + } + this.lastSuspendState = state; + } + throw new Error("Typescript is a fucking joke! BTW we want a Boolean which fucking Typescript can't figure it out!!!!"); + }, + enumerable: true, + configurable: true + }); return JsonqlEventService; }(JsonqlStoreService)); - exports.JsonqlEventService = JsonqlEventService; - - Object.defineProperty(exports, '__esModule', { value: true }); - })); diff --git a/packages/event/index.ts b/packages/event/index.ts index 879d21b2..8317dbc9 100644 --- a/packages/event/index.ts +++ b/packages/event/index.ts @@ -1,5 +1,5 @@ // The top level for @jsonql/event -// The class file has no default export therefore here you will ended up with named export +// The class file has no default export therefore here you will ended up with named export export * from './src/event-service'; // what a mess! you can just do a direct export! if you use default you ended up with a stupid .default name diff --git a/packages/event/src/event-service.ts b/packages/event/src/event-service.ts index e6519ee2..6102281a 100644 --- a/packages/event/src/event-service.ts +++ b/packages/event/src/event-service.ts @@ -3,7 +3,7 @@ import './interfaces.ts'; import JsonqlStoreService from './store-service'; // main -export class JsonqlEventService extends JsonqlStoreService { +export default class JsonqlEventService extends JsonqlStoreService { /** * Create EventService instance @@ -79,7 +79,7 @@ export class JsonqlEventService extends JsonqlStoreService { // 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) - const list = this.mapToArr(lazyStoreContent) + const list = this.arrayFrom(lazyStoreContent) // should never have more than 1 const [ payload, ctx, t ] = list[0] if (t && t !== type) { @@ -115,7 +115,7 @@ export class JsonqlEventService extends JsonqlStoreService { if (lazyStoreContent !== false) { // there are data store in lazy store this.logger('$only', `${evt} found data in lazy store to execute`) - const list = this.mapToArr(lazyStoreContent) + const list = this.arrayFrom(lazyStoreContent) // $only allow to trigger this multiple time on the single handler list.forEach( l => { const [ payload, ctx, t ] = l; @@ -150,7 +150,7 @@ export class JsonqlEventService extends JsonqlStoreService { if (lazyStoreContent !== false) { // there are data store in lazy store this.logger('$onlyOnce', lazyStoreContent) - const list = this.mapToArr(lazyStoreContent) + const list = this.arrayFrom(lazyStoreContent) // should never have more than 1 const [ payload, ctx, t ] = list[0] if (t && t !== 'onlyOnce') { @@ -198,7 +198,7 @@ export class JsonqlEventService extends JsonqlStoreService { if (added === true) { return found; // just exit } - let nSet = this.mapToArr(nStore.get(evt)) + let nSet = this.arrayFrom(nStore.get(evt)) let ctn = nSet.length; let hasOnce = false; // let hasOnly = false; @@ -274,5 +274,56 @@ export class JsonqlEventService extends JsonqlStoreService { throw new Error(`${type} is not supported!`) } } + /** + * Add $trigger args to the queue during suspend state + * @param {array} args from the $trigger call parameters + * @return {boolean} true when added + */ + private $queue(...args: any[]): boolean { + let added = false; + if (this.suspend) { + this.queue.add(args) + added = true; + } + return added; + } + + /** + * whenever the suspend change from true to false + * then we check the queue to see if there is anything to do + * @return {boolean} true if there is queue + */ + private releaseQueue(): boolean { + let size = this.queue.size; + if (size > 0) { + let queue = this.arrayFrom(this.queue) + this.queue.clear() + queue.forEach(args => { + Reflect.apply(this.$trigger, this, args) + }) + } + return !!size; + } + + /** + * This is the main interface to see if anything happens + * @param {boolean} state to change the suspend state + * @return {boolean} what comes in what goes out + */ + public set $suspend(state: boolean) { + if (typeof state === 'boolean') { + this.suspend = state; + if (state !== this.lastSuspendState) { + if (this.lastSuspendState === true && this.suspend === false) { + setTimeout(() => { + this.releaseQueue() + }, 1) + } + } + this.lastSuspendState = state; + } + throw new Error(`Typescript is a fucking joke! BTW we want a Boolean which fucking Typescript can't figure it out!!!!`) + } + } diff --git a/packages/event/src/interfaces.ts b/packages/event/src/interfaces.ts index 82154f01..6000128f 100644 --- a/packages/event/src/interfaces.ts +++ b/packages/event/src/interfaces.ts @@ -4,6 +4,7 @@ interface myCallbackType { (myArgument: string): void } + /* @TODO interface configObj { diff --git a/packages/event/src/store-service.ts b/packages/event/src/store-service.ts index ddf3841d..71ea27e2 100644 --- a/packages/event/src/store-service.ts +++ b/packages/event/src/store-service.ts @@ -1,7 +1,7 @@ // break up the store related operation here // because the class is getting way too big now -import hashFnToKey from './hash-code'; -import JsonqlSuspend from './suspend'; +import hashFnToKey from './hash-code' +import JsonqlSuspend from './suspend' export default class JsonqlStoreService extends JsonqlSuspend { // public props @@ -13,7 +13,7 @@ export default class JsonqlStoreService extends JsonqlSuspend { protected logger: any; constructor(logger: any) { - super() + super(logger) // hack this.logger = typeof logger === 'function' ? logger : () => {}; // should this be WeakMap or should they be Map? @@ -38,7 +38,7 @@ export default class JsonqlStoreService extends JsonqlSuspend { this.logger('$get', full, this.normalStore) if (store.has(evt)) { return this - .mapToArr(store.get(evt)) + .arrayFrom(store.get(evt)) .map( l => { if (full) { return l; @@ -85,14 +85,6 @@ export default class JsonqlStoreService extends JsonqlSuspend { // 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 @@ -177,7 +169,7 @@ export default class JsonqlStoreService extends JsonqlSuspend { * @return {boolean} true on exist */ protected checkContentExist(args: any[], fnSet: Map): boolean { - let list = this.mapToArr(fnSet) + let list = this.arrayFrom(fnSet) return !!list .filter((value: any, index: number, array: any[]): boolean => { let [hash,] = value; @@ -218,7 +210,7 @@ export default class JsonqlStoreService extends JsonqlSuspend { let store = this.lazyStore.get(evtName) if (store) { this.logger('checkTypeInLazyStore', store) - return !!this.mapToArr(store) + return !!this.arrayFrom(store) .filter((value: any, index: number, array: any[]): boolean => { let [,,t] = value; return t !== type; diff --git a/packages/event/src/suspend.ts b/packages/event/src/suspend.ts index c25a3fab..290244a2 100644 --- a/packages/event/src/suspend.ts +++ b/packages/event/src/suspend.ts @@ -4,67 +4,34 @@ // with Typescript private export default class JsonqlSuspend { - private queue: any[]; - private lastSuspendState: boolean; - private suspend: boolean; + protected queue: Set; + protected suspend?: any; // Typescript is a fucking joke + protected lastSuspendState?: any; // Typescript is a fucking joke - constructor() { + constructor(logger: any) { + this.lastSuspendState = null; + this.suspend = null; this.queue = new Set() } - /** - * This is the main interface to see if anything happens - * @param {boolean} state to change the suspend state - * @return {boolean} what comes in what goes out - */ - set $suspend(state: boolean) { - this.suspend = state; - if (state !== this.lastSuspendState) { - if (this.lastSuspendState === true && this.suspend === false) { - setTimeout(() => { - this.releaseQueue() - }, 1) - } - } - this.lastSuspendState = state; - } + /** * getter to get back the current suspend state * @return {boolean} the suspend state */ - get $suspend(): boolean { + public get $suspend(): boolean { return this.suspend; } /** - * Add $trigger args to the queue during suspend state - * @param {array} args from the $trigger call parameters - * @return {boolean} true when added + * @param {any} mapObj the Map object + * @return {array} transform to array to work with */ - protected $queue(...args: any[]): boolean { - let added = false; - if (this.suspend) { - this.queue.add(args) - } - return added; + protected arrayFrom(mapObj: any): any[] { + return Array.from(mapObj) } - /** - * whenever the suspend change from true to false - * then we check the queue to see if there is anything to do - * @return {boolean} true if there is queue - */ - private releaseQueue(): boolean { - let size = this.queue.size; - if (size > 0) { - let queue = this.mapToArr(this.queue) - this.queue.clear() - queue.forEach(args => { - Reflect.apply(this.$trigger, this, args) - }) - } - return !!size; - } + } -- Gitee From 9803efac9b6397d0bd7ed11126142f061bfa019a Mon Sep 17 00:00:00 2001 From: Joelchu Date: Mon, 29 Jul 2019 18:12:19 +0800 Subject: [PATCH 5/9] rename all the export files and setup the types file --- .../{jsonql-event-service.cjs.js => jsonql-event.cjs.js} | 0 packages/event/dist/jsonql-event.d.ts | 0 .../{jsonql-event-service.es.js => jsonql-event.es.js} | 0 .../{jsonql-event-service.umd.js => jsonql-event.umd.js} | 0 packages/event/package.json | 8 ++++---- 5 files changed, 4 insertions(+), 4 deletions(-) rename packages/event/dist/{jsonql-event-service.cjs.js => jsonql-event.cjs.js} (100%) create mode 100644 packages/event/dist/jsonql-event.d.ts rename packages/event/dist/{jsonql-event-service.es.js => jsonql-event.es.js} (100%) rename packages/event/dist/{jsonql-event-service.umd.js => jsonql-event.umd.js} (100%) diff --git a/packages/event/dist/jsonql-event-service.cjs.js b/packages/event/dist/jsonql-event.cjs.js similarity index 100% rename from packages/event/dist/jsonql-event-service.cjs.js rename to packages/event/dist/jsonql-event.cjs.js diff --git a/packages/event/dist/jsonql-event.d.ts b/packages/event/dist/jsonql-event.d.ts new file mode 100644 index 00000000..e69de29b diff --git a/packages/event/dist/jsonql-event-service.es.js b/packages/event/dist/jsonql-event.es.js similarity index 100% rename from packages/event/dist/jsonql-event-service.es.js rename to packages/event/dist/jsonql-event.es.js diff --git a/packages/event/dist/jsonql-event-service.umd.js b/packages/event/dist/jsonql-event.umd.js similarity index 100% rename from packages/event/dist/jsonql-event-service.umd.js rename to packages/event/dist/jsonql-event.umd.js diff --git a/packages/event/package.json b/packages/event/package.json index ac6e2624..9784b699 100644 --- a/packages/event/package.json +++ b/packages/event/package.json @@ -2,10 +2,10 @@ "name": "@jsonql/event", "version": "1.2.0", "description": "Ported from nb-event-service rewritten with Typescript", - "main": "dist/jsonql-event-service.cjs.js", - "browser": "dist/jsonql-event-service.umd.js", - "module": "dist/jsonql-event-service.es.js", - "types": "dist/index.d.ts", + "main": "dist/jsonql-event.cjs.js", + "browser": "dist/jsonql-event.umd.js", + "module": "dist/jsonql-event.es.js", + "types": "dist/jsonql-event.d.ts", "files": [ "dist", "index.ts" -- Gitee From 589ec46523275fb3f420acc31c1791bee591a7d1 Mon Sep 17 00:00:00 2001 From: Joelchu Date: Mon, 29 Jul 2019 18:18:55 +0800 Subject: [PATCH 6/9] use the tsc --declaration method is f**k as usual like anything typescript --- packages/event/dist/jsonql-event.d.ts | 1 + packages/event/src/hash-code.d.ts | 7 +++++++ packages/event/src/suspend.d.ts | 16 ++++++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 packages/event/src/hash-code.d.ts create mode 100644 packages/event/src/suspend.d.ts diff --git a/packages/event/dist/jsonql-event.d.ts b/packages/event/dist/jsonql-event.d.ts index e69de29b..dffd0e60 100644 --- a/packages/event/dist/jsonql-event.d.ts +++ b/packages/event/dist/jsonql-event.d.ts @@ -0,0 +1 @@ +declare module "@jsonql/event" diff --git a/packages/event/src/hash-code.d.ts b/packages/event/src/hash-code.d.ts new file mode 100644 index 00000000..6c687411 --- /dev/null +++ b/packages/event/src/hash-code.d.ts @@ -0,0 +1,7 @@ +/** + * 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 + */ +export default function hashCode(fn: Function): String; diff --git a/packages/event/src/suspend.d.ts b/packages/event/src/suspend.d.ts new file mode 100644 index 00000000..1169f903 --- /dev/null +++ b/packages/event/src/suspend.d.ts @@ -0,0 +1,16 @@ +export default class JsonqlSuspend { + protected queue: Set; + protected suspend?: any; + protected lastSuspendState?: any; + constructor(logger: any); + /** + * getter to get back the current suspend state + * @return {boolean} the suspend state + */ + readonly $suspend: boolean; + /** + * @param {any} mapObj the Map object + * @return {array} transform to array to work with + */ + protected arrayFrom(mapObj: any): any[]; +} -- Gitee From 87c91c0ef87c3377f65c9ef58c3f51a1004c4237 Mon Sep 17 00:00:00 2001 From: Joelchu Date: Mon, 29 Jul 2019 18:25:22 +0800 Subject: [PATCH 7/9] Change the jsonql/event module completely --- packages/event/dist/jsonql-event.cjs.js | 36 ++++++++++--------- packages/event/dist/jsonql-event.es.js | 36 ++++++++++--------- packages/event/dist/jsonql-event.umd.js | 43 +++++++++++++---------- packages/event/index.ts | 5 +-- packages/event/rollup.config.js | 2 +- packages/event/src/event-service.ts | 2 +- packages/event/tests/basic.test.js | 6 ++-- packages/event/tests/lazy.test.js | 2 +- packages/event/tests/once-problem.test.js | 2 +- packages/event/tests/only-once.test.js | 2 +- packages/event/tests/only.test.js | 2 +- packages/event/tests/replace.test.js | 2 +- 12 files changed, 77 insertions(+), 63 deletions(-) diff --git a/packages/event/dist/jsonql-event.cjs.js b/packages/event/dist/jsonql-event.cjs.js index 106ada0b..18d85eaf 100644 --- a/packages/event/dist/jsonql-event.cjs.js +++ b/packages/event/dist/jsonql-event.cjs.js @@ -381,14 +381,14 @@ var JsonqlStoreService = /** @class */ (function (_super) { }(JsonqlSuspend)); // main -var JsonqlEventService = /** @class */ (function (_super) { - __extends(JsonqlEventService, _super); +var JsonqlEvent = /** @class */ (function (_super) { + __extends(JsonqlEvent, _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) { + function JsonqlEvent(logger) { return _super.call(this, logger) || this; } /** @@ -399,7 +399,7 @@ var JsonqlEventService = /** @class */ (function (_super) { * @param {object} [context=null] to execute this call in * @return {number} the size of the store */ - JsonqlEventService.prototype.$on = function (evt, callback, context) { + JsonqlEvent.prototype.$on = function (evt, callback, context) { var _this = this; if (context === void 0) { context = null; } this.validate(callback, evt); @@ -439,7 +439,7 @@ var JsonqlEventService = /** @class */ (function (_super) { * @param {object} [context=null] the handler execute in * @return {number|boolean} result */ - JsonqlEventService.prototype.$once = function (evt, callback, context) { + JsonqlEvent.prototype.$once = function (evt, callback, context) { if (context === void 0) { context = null; } this.validate(callback, evt); var type = 'once'; @@ -477,7 +477,7 @@ var JsonqlEventService = /** @class */ (function (_super) { * @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) { + JsonqlEvent.prototype.$only = function (evt, callback, context) { var _this = this; if (context === void 0) { context = null; } this.validate(callback, evt); @@ -515,7 +515,7 @@ var JsonqlEventService = /** @class */ (function (_super) { * @param {object} [context=null] exeucte context * @return {boolean} same as above */ - JsonqlEventService.prototype.$onlyOnce = function (evt, callback, context) { + JsonqlEvent.prototype.$onlyOnce = function (evt, callback, context) { if (context === void 0) { context = null; } this.validate(callback, evt); var type = 'onlyOnce'; @@ -550,7 +550,7 @@ var JsonqlEventService = /** @class */ (function (_super) { * @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) { + JsonqlEvent.prototype.$replace = function (evt, callback, context, type) { if (context === void 0) { context = null; } if (type === void 0) { type = 'on'; } this.$off(evt); @@ -566,7 +566,7 @@ var JsonqlEventService = /** @class */ (function (_super) { * @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) { + JsonqlEvent.prototype.$trigger = function (evt, payload, context, type) { if (payload === void 0) { payload = []; } if (context === void 0) { context = null; } if (type === void 0) { type = false; } @@ -612,7 +612,7 @@ var JsonqlEventService = /** @class */ (function (_super) { * @param {object} context what context callback execute in * @return {*} from $trigger */ - JsonqlEventService.prototype.$call = function (evt, params, type, context) { + JsonqlEvent.prototype.$call = function (evt, params, type, context) { if (type === void 0) { type = false; } if (context === void 0) { context = null; } this.validateEvt(evt); @@ -625,7 +625,7 @@ var JsonqlEventService = /** @class */ (function (_super) { * @param {string} evt name * @return {boolean} true actually delete something */ - JsonqlEventService.prototype.$off = function (evt) { + JsonqlEvent.prototype.$off = function (evt) { this.validateEvt(evt); var stores = [this.lazyStore, this.normalStore]; var found = false; @@ -642,7 +642,7 @@ var JsonqlEventService = /** @class */ (function (_super) { * @param {string} type the 4 types * @return {*} the method */ - JsonqlEventService.prototype.getMethodBy = function (type) { + JsonqlEvent.prototype.getMethodBy = function (type) { switch (type) { case 'on': return this.$on; @@ -661,7 +661,7 @@ var JsonqlEventService = /** @class */ (function (_super) { * @param {array} args from the $trigger call parameters * @return {boolean} true when added */ - JsonqlEventService.prototype.$queue = function () { + JsonqlEvent.prototype.$queue = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; @@ -678,7 +678,7 @@ var JsonqlEventService = /** @class */ (function (_super) { * then we check the queue to see if there is anything to do * @return {boolean} true if there is queue */ - JsonqlEventService.prototype.releaseQueue = function () { + JsonqlEvent.prototype.releaseQueue = function () { var _this = this; var size = this.queue.size; if (size > 0) { @@ -690,7 +690,7 @@ var JsonqlEventService = /** @class */ (function (_super) { } return !!size; }; - Object.defineProperty(JsonqlEventService.prototype, "$suspend", { + Object.defineProperty(JsonqlEvent.prototype, "$suspend", { /** * This is the main interface to see if anything happens * @param {boolean} state to change the suspend state @@ -714,5 +714,9 @@ var JsonqlEventService = /** @class */ (function (_super) { enumerable: true, configurable: true }); - return JsonqlEventService; + return JsonqlEvent; }(JsonqlStoreService)); + +// The top level for @jsonql/event + +module.exports = JsonqlEvent; diff --git a/packages/event/dist/jsonql-event.es.js b/packages/event/dist/jsonql-event.es.js index 1698fcf5..624e1ff3 100644 --- a/packages/event/dist/jsonql-event.es.js +++ b/packages/event/dist/jsonql-event.es.js @@ -379,14 +379,14 @@ var JsonqlStoreService = /** @class */ (function (_super) { }(JsonqlSuspend)); // main -var JsonqlEventService = /** @class */ (function (_super) { - __extends(JsonqlEventService, _super); +var JsonqlEvent = /** @class */ (function (_super) { + __extends(JsonqlEvent, _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) { + function JsonqlEvent(logger) { return _super.call(this, logger) || this; } /** @@ -397,7 +397,7 @@ var JsonqlEventService = /** @class */ (function (_super) { * @param {object} [context=null] to execute this call in * @return {number} the size of the store */ - JsonqlEventService.prototype.$on = function (evt, callback, context) { + JsonqlEvent.prototype.$on = function (evt, callback, context) { var _this = this; if (context === void 0) { context = null; } this.validate(callback, evt); @@ -437,7 +437,7 @@ var JsonqlEventService = /** @class */ (function (_super) { * @param {object} [context=null] the handler execute in * @return {number|boolean} result */ - JsonqlEventService.prototype.$once = function (evt, callback, context) { + JsonqlEvent.prototype.$once = function (evt, callback, context) { if (context === void 0) { context = null; } this.validate(callback, evt); var type = 'once'; @@ -475,7 +475,7 @@ var JsonqlEventService = /** @class */ (function (_super) { * @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) { + JsonqlEvent.prototype.$only = function (evt, callback, context) { var _this = this; if (context === void 0) { context = null; } this.validate(callback, evt); @@ -513,7 +513,7 @@ var JsonqlEventService = /** @class */ (function (_super) { * @param {object} [context=null] exeucte context * @return {boolean} same as above */ - JsonqlEventService.prototype.$onlyOnce = function (evt, callback, context) { + JsonqlEvent.prototype.$onlyOnce = function (evt, callback, context) { if (context === void 0) { context = null; } this.validate(callback, evt); var type = 'onlyOnce'; @@ -548,7 +548,7 @@ var JsonqlEventService = /** @class */ (function (_super) { * @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) { + JsonqlEvent.prototype.$replace = function (evt, callback, context, type) { if (context === void 0) { context = null; } if (type === void 0) { type = 'on'; } this.$off(evt); @@ -564,7 +564,7 @@ var JsonqlEventService = /** @class */ (function (_super) { * @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) { + JsonqlEvent.prototype.$trigger = function (evt, payload, context, type) { if (payload === void 0) { payload = []; } if (context === void 0) { context = null; } if (type === void 0) { type = false; } @@ -610,7 +610,7 @@ var JsonqlEventService = /** @class */ (function (_super) { * @param {object} context what context callback execute in * @return {*} from $trigger */ - JsonqlEventService.prototype.$call = function (evt, params, type, context) { + JsonqlEvent.prototype.$call = function (evt, params, type, context) { if (type === void 0) { type = false; } if (context === void 0) { context = null; } this.validateEvt(evt); @@ -623,7 +623,7 @@ var JsonqlEventService = /** @class */ (function (_super) { * @param {string} evt name * @return {boolean} true actually delete something */ - JsonqlEventService.prototype.$off = function (evt) { + JsonqlEvent.prototype.$off = function (evt) { this.validateEvt(evt); var stores = [this.lazyStore, this.normalStore]; var found = false; @@ -640,7 +640,7 @@ var JsonqlEventService = /** @class */ (function (_super) { * @param {string} type the 4 types * @return {*} the method */ - JsonqlEventService.prototype.getMethodBy = function (type) { + JsonqlEvent.prototype.getMethodBy = function (type) { switch (type) { case 'on': return this.$on; @@ -659,7 +659,7 @@ var JsonqlEventService = /** @class */ (function (_super) { * @param {array} args from the $trigger call parameters * @return {boolean} true when added */ - JsonqlEventService.prototype.$queue = function () { + JsonqlEvent.prototype.$queue = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; @@ -676,7 +676,7 @@ var JsonqlEventService = /** @class */ (function (_super) { * then we check the queue to see if there is anything to do * @return {boolean} true if there is queue */ - JsonqlEventService.prototype.releaseQueue = function () { + JsonqlEvent.prototype.releaseQueue = function () { var _this = this; var size = this.queue.size; if (size > 0) { @@ -688,7 +688,7 @@ var JsonqlEventService = /** @class */ (function (_super) { } return !!size; }; - Object.defineProperty(JsonqlEventService.prototype, "$suspend", { + Object.defineProperty(JsonqlEvent.prototype, "$suspend", { /** * This is the main interface to see if anything happens * @param {boolean} state to change the suspend state @@ -712,5 +712,9 @@ var JsonqlEventService = /** @class */ (function (_super) { enumerable: true, configurable: true }); - return JsonqlEventService; + return JsonqlEvent; }(JsonqlStoreService)); + +// The top level for @jsonql/event + +export default JsonqlEvent; diff --git a/packages/event/dist/jsonql-event.umd.js b/packages/event/dist/jsonql-event.umd.js index d6038a0e..69189b0f 100644 --- a/packages/event/dist/jsonql-event.umd.js +++ b/packages/event/dist/jsonql-event.umd.js @@ -1,7 +1,8 @@ -(function (factory) { +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : - factory(); -}(function () { 'use strict'; + (global = global || self, global.JsonqlEvent = factory()); +}(this, function () { 'use strict'; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. All rights reserved. @@ -384,14 +385,14 @@ }(JsonqlSuspend)); // main - var JsonqlEventService = /** @class */ (function (_super) { - __extends(JsonqlEventService, _super); + var JsonqlEvent = /** @class */ (function (_super) { + __extends(JsonqlEvent, _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) { + function JsonqlEvent(logger) { return _super.call(this, logger) || this; } /** @@ -402,7 +403,7 @@ * @param {object} [context=null] to execute this call in * @return {number} the size of the store */ - JsonqlEventService.prototype.$on = function (evt, callback, context) { + JsonqlEvent.prototype.$on = function (evt, callback, context) { var _this = this; if (context === void 0) { context = null; } this.validate(callback, evt); @@ -442,7 +443,7 @@ * @param {object} [context=null] the handler execute in * @return {number|boolean} result */ - JsonqlEventService.prototype.$once = function (evt, callback, context) { + JsonqlEvent.prototype.$once = function (evt, callback, context) { if (context === void 0) { context = null; } this.validate(callback, evt); var type = 'once'; @@ -480,7 +481,7 @@ * @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) { + JsonqlEvent.prototype.$only = function (evt, callback, context) { var _this = this; if (context === void 0) { context = null; } this.validate(callback, evt); @@ -518,7 +519,7 @@ * @param {object} [context=null] exeucte context * @return {boolean} same as above */ - JsonqlEventService.prototype.$onlyOnce = function (evt, callback, context) { + JsonqlEvent.prototype.$onlyOnce = function (evt, callback, context) { if (context === void 0) { context = null; } this.validate(callback, evt); var type = 'onlyOnce'; @@ -553,7 +554,7 @@ * @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) { + JsonqlEvent.prototype.$replace = function (evt, callback, context, type) { if (context === void 0) { context = null; } if (type === void 0) { type = 'on'; } this.$off(evt); @@ -569,7 +570,7 @@ * @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) { + JsonqlEvent.prototype.$trigger = function (evt, payload, context, type) { if (payload === void 0) { payload = []; } if (context === void 0) { context = null; } if (type === void 0) { type = false; } @@ -615,7 +616,7 @@ * @param {object} context what context callback execute in * @return {*} from $trigger */ - JsonqlEventService.prototype.$call = function (evt, params, type, context) { + JsonqlEvent.prototype.$call = function (evt, params, type, context) { if (type === void 0) { type = false; } if (context === void 0) { context = null; } this.validateEvt(evt); @@ -628,7 +629,7 @@ * @param {string} evt name * @return {boolean} true actually delete something */ - JsonqlEventService.prototype.$off = function (evt) { + JsonqlEvent.prototype.$off = function (evt) { this.validateEvt(evt); var stores = [this.lazyStore, this.normalStore]; var found = false; @@ -645,7 +646,7 @@ * @param {string} type the 4 types * @return {*} the method */ - JsonqlEventService.prototype.getMethodBy = function (type) { + JsonqlEvent.prototype.getMethodBy = function (type) { switch (type) { case 'on': return this.$on; @@ -664,7 +665,7 @@ * @param {array} args from the $trigger call parameters * @return {boolean} true when added */ - JsonqlEventService.prototype.$queue = function () { + JsonqlEvent.prototype.$queue = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; @@ -681,7 +682,7 @@ * then we check the queue to see if there is anything to do * @return {boolean} true if there is queue */ - JsonqlEventService.prototype.releaseQueue = function () { + JsonqlEvent.prototype.releaseQueue = function () { var _this = this; var size = this.queue.size; if (size > 0) { @@ -693,7 +694,7 @@ } return !!size; }; - Object.defineProperty(JsonqlEventService.prototype, "$suspend", { + Object.defineProperty(JsonqlEvent.prototype, "$suspend", { /** * This is the main interface to see if anything happens * @param {boolean} state to change the suspend state @@ -717,7 +718,11 @@ enumerable: true, configurable: true }); - return JsonqlEventService; + return JsonqlEvent; }(JsonqlStoreService)); + // The top level for @jsonql/event + + return JsonqlEvent; + })); diff --git a/packages/event/index.ts b/packages/event/index.ts index 8317dbc9..25ae74ef 100644 --- a/packages/event/index.ts +++ b/packages/event/index.ts @@ -1,5 +1,6 @@ // The top level for @jsonql/event // The class file has no default export therefore here you will ended up with named export -export * from './src/event-service'; -// what a mess! you can just do a direct export! if you use default you ended up with a stupid .default name +import JsonqlEvent from './src/event-service'; +// You have to do this in 2 steps to get rip of the .default from cjs! +export default JsonqlEvent; diff --git a/packages/event/rollup.config.js b/packages/event/rollup.config.js index eed25546..e82b1dfd 100644 --- a/packages/event/rollup.config.js +++ b/packages/event/rollup.config.js @@ -15,7 +15,7 @@ export default { { file: pkg.browser, format: 'umd', - name: 'JsonqlEventService' + name: 'JsonqlEvent' } ], external: [ diff --git a/packages/event/src/event-service.ts b/packages/event/src/event-service.ts index 6102281a..a3c3ed8f 100644 --- a/packages/event/src/event-service.ts +++ b/packages/event/src/event-service.ts @@ -3,7 +3,7 @@ import './interfaces.ts'; import JsonqlStoreService from './store-service'; // main -export default class JsonqlEventService extends JsonqlStoreService { +export default class JsonqlEvent extends JsonqlStoreService { /** * Create EventService instance diff --git a/packages/event/tests/basic.test.js b/packages/event/tests/basic.test.js index 139c4e02..fca5db4a 100644 --- a/packages/event/tests/basic.test.js +++ b/packages/event/tests/basic.test.js @@ -1,14 +1,14 @@ const test = require('ava') // import the cjs version for testing -const { JsonqlEventService } = require('../dist/jsonql-event-service.cjs') +const JsonqlEvent = require('../dist/jsonql-event.cjs') const logger = require('debug')('nb-event-service') const debug = require('debug')('nb-event-service:test:basic') let value = 1000; -debug(JsonqlEventService) +debug(JsonqlEvent) test.before( t => { - t.context.evtSrv = new JsonqlEventService(logger) + t.context.evtSrv = new JsonqlEvent(logger) }) test('It should able to validate the evt', t => { diff --git a/packages/event/tests/lazy.test.js b/packages/event/tests/lazy.test.js index a38f8cef..017c8d99 100644 --- a/packages/event/tests/lazy.test.js +++ b/packages/event/tests/lazy.test.js @@ -1,7 +1,7 @@ // testing the lazy store with a type using the context parameter const test = require('ava') -const { JsonqlEventService } = require('../dist/jsonql-event-service.cjs') +const { JsonqlEventService } = require('../dist/jsonql-event.cjs') const logger = require('debug')('nb-event-service') const debug = require('debug')('nb-event-service:test:lazy') diff --git a/packages/event/tests/once-problem.test.js b/packages/event/tests/once-problem.test.js index 84145629..69c89df7 100644 --- a/packages/event/tests/once-problem.test.js +++ b/packages/event/tests/once-problem.test.js @@ -1,6 +1,6 @@ const test = require('ava') -const { JsonqlEventService } = require('../dist/jsonql-event-service.cjs') +const { JsonqlEventService } = require('../dist/jsonql-event.cjs') const logger = require('debug')('nb-event-service') const debug = require('debug')('nb-event-service:test:only-problem') let value = 1000; diff --git a/packages/event/tests/only-once.test.js b/packages/event/tests/only-once.test.js index 390548b1..a741ed46 100644 --- a/packages/event/tests/only-once.test.js +++ b/packages/event/tests/only-once.test.js @@ -1,6 +1,6 @@ const test = require('ava') -const { JsonqlEventService } = require('../dist/jsonql-event-service.cjs') +const { JsonqlEventService } = require('../dist/jsonql-event.cjs') const logger = require('debug')('nb-event-service') const debug = require('debug')('nb-event-service:test:only-once') let value = 2000; diff --git a/packages/event/tests/only.test.js b/packages/event/tests/only.test.js index 34ac6498..d68a4f99 100644 --- a/packages/event/tests/only.test.js +++ b/packages/event/tests/only.test.js @@ -1,6 +1,6 @@ const test = require('ava') -const { JsonqlEventService } = require('../dist/jsonql-event-service.cjs') +const { JsonqlEventService } = require('../dist/jsonql-event.cjs') const logger = require('debug')('nb-event-service') const debug = require('debug')('nb-event-service:test:only-problem') let value = 1000; diff --git a/packages/event/tests/replace.test.js b/packages/event/tests/replace.test.js index a6354015..9e691066 100644 --- a/packages/event/tests/replace.test.js +++ b/packages/event/tests/replace.test.js @@ -1,6 +1,6 @@ const test = require('ava') -const { JsonqlEventService } = require('../dist/jsonql-event-service.cjs') +const { JsonqlEventService } = require('../dist/jsonql-event.cjs') const logger = require('debug')('nb-event-service') const debug = require('debug')('nb-event-service:test:replace') -- Gitee From 0caf61152aae76856786fec5ec360aacada8ad98 Mon Sep 17 00:00:00 2001 From: Joelchu Date: Mon, 29 Jul 2019 18:31:01 +0800 Subject: [PATCH 8/9] all test passed --- packages/event/tests/lazy.test.js | 2 +- packages/event/tests/once-problem.test.js | 2 +- packages/event/tests/only-once.test.js | 2 +- packages/event/tests/only.test.js | 2 +- packages/event/tests/replace.test.js | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/event/tests/lazy.test.js b/packages/event/tests/lazy.test.js index 017c8d99..c672ee15 100644 --- a/packages/event/tests/lazy.test.js +++ b/packages/event/tests/lazy.test.js @@ -1,7 +1,7 @@ // testing the lazy store with a type using the context parameter const test = require('ava') -const { JsonqlEventService } = require('../dist/jsonql-event.cjs') +const JsonqlEventService = require('../dist/jsonql-event.cjs') const logger = require('debug')('nb-event-service') const debug = require('debug')('nb-event-service:test:lazy') diff --git a/packages/event/tests/once-problem.test.js b/packages/event/tests/once-problem.test.js index 69c89df7..1fa55262 100644 --- a/packages/event/tests/once-problem.test.js +++ b/packages/event/tests/once-problem.test.js @@ -1,6 +1,6 @@ const test = require('ava') -const { JsonqlEventService } = require('../dist/jsonql-event.cjs') +const JsonqlEventService = require('../dist/jsonql-event.cjs') const logger = require('debug')('nb-event-service') const debug = require('debug')('nb-event-service:test:only-problem') let value = 1000; diff --git a/packages/event/tests/only-once.test.js b/packages/event/tests/only-once.test.js index a741ed46..4b5f15b3 100644 --- a/packages/event/tests/only-once.test.js +++ b/packages/event/tests/only-once.test.js @@ -1,6 +1,6 @@ const test = require('ava') -const { JsonqlEventService } = require('../dist/jsonql-event.cjs') +const JsonqlEventService = require('../dist/jsonql-event.cjs') const logger = require('debug')('nb-event-service') const debug = require('debug')('nb-event-service:test:only-once') let value = 2000; diff --git a/packages/event/tests/only.test.js b/packages/event/tests/only.test.js index d68a4f99..f96f8c07 100644 --- a/packages/event/tests/only.test.js +++ b/packages/event/tests/only.test.js @@ -1,6 +1,6 @@ const test = require('ava') -const { JsonqlEventService } = require('../dist/jsonql-event.cjs') +const JsonqlEventService = require('../dist/jsonql-event.cjs') const logger = require('debug')('nb-event-service') const debug = require('debug')('nb-event-service:test:only-problem') let value = 1000; diff --git a/packages/event/tests/replace.test.js b/packages/event/tests/replace.test.js index 9e691066..200bc89f 100644 --- a/packages/event/tests/replace.test.js +++ b/packages/event/tests/replace.test.js @@ -1,6 +1,6 @@ const test = require('ava') -const { JsonqlEventService } = require('../dist/jsonql-event.cjs') +const JsonqlEventService = require('../dist/jsonql-event.cjs') const logger = require('debug')('nb-event-service') const debug = require('debug')('nb-event-service:test:replace') -- Gitee From e79f0847c6285569e369d1e3945ae04b63acb698 Mon Sep 17 00:00:00 2001 From: Joelchu Date: Mon, 29 Jul 2019 18:48:53 +0800 Subject: [PATCH 9/9] suspend test passed --- packages/event/README.md | 34 +++++++++++++++++++----- packages/event/dist/jsonql-event.cjs.js | 20 +++++++------- packages/event/dist/jsonql-event.es.js | 20 +++++++------- packages/event/dist/jsonql-event.umd.js | 20 +++++++------- packages/event/package.json | 9 ++++--- packages/event/src/event-service.ts | 20 +++++++------- packages/event/tests/fixtures/index.html | 6 ++--- packages/event/tests/suspend.test.js | 25 +++++++++++++++++ 8 files changed, 100 insertions(+), 54 deletions(-) create mode 100644 packages/event/tests/suspend.test.js diff --git a/packages/event/README.md b/packages/event/README.md index 4cd91028..ed56f226 100644 --- a/packages/event/README.md +++ b/packages/event/README.md @@ -17,21 +17,18 @@ $ npm i @jsonql/event When using it directly in browser you have to do this: ```js -var eventService = new JsonqlEventService.JsonqlEventService() +var eventService = new JsonqlEvent() // the rest of the code ``` -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 magical as most keep saying it is); -you should use [nb-event-service](https://npmjs.org/package/nb-event-service) instead. +you should use [nb-event-service](https://npmjs.org/package/nb-event-service) instead, if you only use this one module. +If you are using Typescript, then suck it up then ... For the other that use node build tool, just do this: ```js -import { JsonqlEventService } from '@jsonql/event' +import JsonqlEvent from '@jsonql/event' ``` ## API @@ -169,6 +166,29 @@ es.$on('some-event', function(num) { Or it will return `false` if there is nothing +#### $suspend + +This is a setter inside the class; When you set this to `true` it suspend all the `$trigger` and `call` operation. +They will be store inside a queue, and release when you set it to `false` + +```js +const evtSrv = new JsonqlEvent() + +evtSrv.$on('add-something', function(value) { + return value + 1; +}) +evtSrv.$suspend = true; + +evtSrv.$trigger('add-something', 100) + +evtSrv.$done === 101; // false + +evtSrv.$suspend = false; + +evtSrv.$done === 101; // true + +``` + --- diff --git a/packages/event/dist/jsonql-event.cjs.js b/packages/event/dist/jsonql-event.cjs.js index 18d85eaf..a26e9b3c 100644 --- a/packages/event/dist/jsonql-event.cjs.js +++ b/packages/event/dist/jsonql-event.cjs.js @@ -698,18 +698,18 @@ var JsonqlEvent = /** @class */ (function (_super) { */ set: function (state) { var _this = this; - if (typeof state === 'boolean') { - this.suspend = state; - if (state !== this.lastSuspendState) { - if (this.lastSuspendState === true && this.suspend === false) { - setTimeout(function () { - _this.releaseQueue(); - }, 1); - } + if (typeof state !== 'boolean') { + throw new Error("Typescript is a fucking joke! BTW we want a Boolean which fucking Typescript can't figure it out!!!! " + typeof state); + } + this.suspend = state; + if (state !== this.lastSuspendState) { + if (this.lastSuspendState === true && this.suspend === false) { + setTimeout(function () { + _this.releaseQueue(); + }, 1); } - this.lastSuspendState = state; } - throw new Error("Typescript is a fucking joke! BTW we want a Boolean which fucking Typescript can't figure it out!!!!"); + this.lastSuspendState = state; }, enumerable: true, configurable: true diff --git a/packages/event/dist/jsonql-event.es.js b/packages/event/dist/jsonql-event.es.js index 624e1ff3..df8db154 100644 --- a/packages/event/dist/jsonql-event.es.js +++ b/packages/event/dist/jsonql-event.es.js @@ -696,18 +696,18 @@ var JsonqlEvent = /** @class */ (function (_super) { */ set: function (state) { var _this = this; - if (typeof state === 'boolean') { - this.suspend = state; - if (state !== this.lastSuspendState) { - if (this.lastSuspendState === true && this.suspend === false) { - setTimeout(function () { - _this.releaseQueue(); - }, 1); - } + if (typeof state !== 'boolean') { + throw new Error("Typescript is a fucking joke! BTW we want a Boolean which fucking Typescript can't figure it out!!!! " + typeof state); + } + this.suspend = state; + if (state !== this.lastSuspendState) { + if (this.lastSuspendState === true && this.suspend === false) { + setTimeout(function () { + _this.releaseQueue(); + }, 1); } - this.lastSuspendState = state; } - throw new Error("Typescript is a fucking joke! BTW we want a Boolean which fucking Typescript can't figure it out!!!!"); + this.lastSuspendState = state; }, enumerable: true, configurable: true diff --git a/packages/event/dist/jsonql-event.umd.js b/packages/event/dist/jsonql-event.umd.js index 69189b0f..9b24f5d1 100644 --- a/packages/event/dist/jsonql-event.umd.js +++ b/packages/event/dist/jsonql-event.umd.js @@ -702,18 +702,18 @@ */ set: function (state) { var _this = this; - if (typeof state === 'boolean') { - this.suspend = state; - if (state !== this.lastSuspendState) { - if (this.lastSuspendState === true && this.suspend === false) { - setTimeout(function () { - _this.releaseQueue(); - }, 1); - } + if (typeof state !== 'boolean') { + throw new Error("Typescript is a fucking joke! BTW we want a Boolean which fucking Typescript can't figure it out!!!! " + typeof state); + } + this.suspend = state; + if (state !== this.lastSuspendState) { + if (this.lastSuspendState === true && this.suspend === false) { + setTimeout(function () { + _this.releaseQueue(); + }, 1); } - this.lastSuspendState = state; } - throw new Error("Typescript is a fucking joke! BTW we want a Boolean which fucking Typescript can't figure it out!!!!"); + this.lastSuspendState = state; }, enumerable: true, configurable: true diff --git a/packages/event/package.json b/packages/event/package.json index 9784b699..2e9b868b 100644 --- a/packages/event/package.json +++ b/packages/event/package.json @@ -14,9 +14,10 @@ "test": "DEBUG=nb-event-service* ava --verbose", "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:once-problem": "DEBUG=nb-event-service* ava ./tests/once-problem.test.js" + "test:basic": "DEBUG=nb-event-service* ava ./tests/basic.test.js", + "test:once-problem": "DEBUG=nb-event-service* ava ./tests/once-problem.test.js", + "test:suspend": "DEBUG=nb-event-service* ava ./tests/suspend.test.js" }, "keywords": [ "jsonql", @@ -37,8 +38,8 @@ "devDependencies": { "ava": "^2.2.0", "debug": "^4.1.1", - "rollup": "^1.16.7", - "rollup-plugin-typescript2": "^0.22.0", + "rollup": "^1.17.0", + "rollup-plugin-typescript2": "^0.22.1", "server-io-core": "^1.2.0-beta.2", "ts-node": "^8.3.0", "typescript": "^3.5.3" diff --git a/packages/event/src/event-service.ts b/packages/event/src/event-service.ts index a3c3ed8f..d7b8c8e2 100644 --- a/packages/event/src/event-service.ts +++ b/packages/event/src/event-service.ts @@ -311,18 +311,18 @@ export default class JsonqlEvent extends JsonqlStoreService { * @return {boolean} what comes in what goes out */ public set $suspend(state: boolean) { - if (typeof state === 'boolean') { - this.suspend = state; - if (state !== this.lastSuspendState) { - if (this.lastSuspendState === true && this.suspend === false) { - setTimeout(() => { - this.releaseQueue() - }, 1) - } + if (typeof state !== 'boolean') { + throw new Error(`Typescript is a fucking joke! BTW we want a Boolean which fucking Typescript can't figure it out!!!! ${typeof state}`) + } + this.suspend = state; + if (state !== this.lastSuspendState) { + if (this.lastSuspendState === true && this.suspend === false) { + setTimeout(() => { + this.releaseQueue() + }, 1) } - this.lastSuspendState = state; } - throw new Error(`Typescript is a fucking joke! BTW we want a Boolean which fucking Typescript can't figure it out!!!!`) + this.lastSuspendState = state; } diff --git a/packages/event/tests/fixtures/index.html b/packages/event/tests/fixtures/index.html index 1006f42d..ec6323c9 100644 --- a/packages/event/tests/fixtures/index.html +++ b/packages/event/tests/fixtures/index.html @@ -8,10 +8,10 @@

Just look at the console log

- +