From 5a2030f9df24618acecb4890c53ef0917b51f5d7 Mon Sep 17 00:00:00 2001 From: Joelchu Date: Sat, 13 Jul 2019 11:13:29 +0800 Subject: [PATCH 01/23] move client to http client and setup scope jsonql/client and jsonql/http-client --- packages/client/package.json | 15 +-- packages/{client => http-client}/README.md | 0 .../dist/jsonql-client.cls.js | 0 .../dist/jsonql-client.cls.js.map | 0 .../dist/jsonql-client.umd.js | 0 .../dist/jsonql-client.umd.js.map | 0 .../dist/jsonql-stores.js | 0 .../dist/jsonql-stores.js.map | 0 packages/{client => http-client}/index.js | 0 packages/{client => http-client}/index.ts | 0 packages/http-client/package.json | 105 ++++++++++++++++++ .../{client => http-client}/rollup.config.js | 0 .../rollup.oop.config.js | 0 packages/{client => http-client}/src/index.js | 0 .../{client => http-client}/src/jsonql-api.js | 0 .../src/jsonql-client-cls.js | 0 .../src/lib/base/auth-cls.js | 0 .../src/lib/base/base-cls.js | 0 .../src/lib/base/contract-cls.js | 0 .../src/lib/base/flyio.js | 0 .../src/lib/base/http-cls.js | 0 .../src/lib/base/index.js | 0 .../{client => http-client}/src/lib/ee.js | 0 .../src/lib/jsonql-api-generator.js | 0 .../src/lib/options/base-options.js | 0 .../src/lib/options/check-options.js | 0 .../src/lib/options/index.js | 0 .../src/lib/stores/index.js | 0 .../src/lib/stores/local-store.js | 0 .../src/lib/stores/session-store.js | 0 .../{client => http-client}/src/lib/utils.js | 0 .../{client => http-client}/src/ts/README.md | 0 .../tests/contract-lock.test.js | 0 .../tests/fixtures/contract.js | 0 .../tests/fixtures/dev.js | 0 .../tests/fixtures/favicon.ico | Bin .../tests/fixtures/helpers/setup-browser.js | 0 .../tests/fixtures/index.html | 0 .../tests/fixtures/obj.js | 0 .../tests/fixtures/options.json | 0 .../tests/fixtures/resolvers/auth/issuer.js | 0 .../fixtures/resolvers/auth/validator.js | 0 .../tests/fixtures/resolvers/mutation/plus.js | 0 .../fixtures/resolvers/query/get-something.js | 0 .../fixtures/resolvers/query/test-list.js | 0 .../tests/fixtures/run.js | 0 .../tests/fixtures/server.js | 0 .../{client => http-client}/tests/fn.test.js | 0 .../tests/main.test.js | 0 .../tests/qunit/files/base-test.js | 0 .../tests/qunit/files/stores-test.js | 0 .../tests/qunit/run-qunit-setup.js | 0 .../tests/qunit/run-qunit.js | 0 .../tests/qunit/webroot/index.html | 0 .../tests/qunit/webroot/nb-qunit-helper.js | 0 .../tests/validation.test.js | 0 56 files changed, 111 insertions(+), 9 deletions(-) mode change 100755 => 100644 packages/client/package.json rename packages/{client => http-client}/README.md (100%) rename packages/{client => http-client}/dist/jsonql-client.cls.js (100%) rename packages/{client => http-client}/dist/jsonql-client.cls.js.map (100%) rename packages/{client => http-client}/dist/jsonql-client.umd.js (100%) rename packages/{client => http-client}/dist/jsonql-client.umd.js.map (100%) rename packages/{client => http-client}/dist/jsonql-stores.js (100%) rename packages/{client => http-client}/dist/jsonql-stores.js.map (100%) rename packages/{client => http-client}/index.js (100%) rename packages/{client => http-client}/index.ts (100%) create mode 100755 packages/http-client/package.json rename packages/{client => http-client}/rollup.config.js (100%) rename packages/{client => http-client}/rollup.oop.config.js (100%) rename packages/{client => http-client}/src/index.js (100%) rename packages/{client => http-client}/src/jsonql-api.js (100%) rename packages/{client => http-client}/src/jsonql-client-cls.js (100%) rename packages/{client => http-client}/src/lib/base/auth-cls.js (100%) rename packages/{client => http-client}/src/lib/base/base-cls.js (100%) rename packages/{client => http-client}/src/lib/base/contract-cls.js (100%) rename packages/{client => http-client}/src/lib/base/flyio.js (100%) rename packages/{client => http-client}/src/lib/base/http-cls.js (100%) rename packages/{client => http-client}/src/lib/base/index.js (100%) rename packages/{client => http-client}/src/lib/ee.js (100%) rename packages/{client => http-client}/src/lib/jsonql-api-generator.js (100%) rename packages/{client => http-client}/src/lib/options/base-options.js (100%) rename packages/{client => http-client}/src/lib/options/check-options.js (100%) rename packages/{client => http-client}/src/lib/options/index.js (100%) rename packages/{client => http-client}/src/lib/stores/index.js (100%) rename packages/{client => http-client}/src/lib/stores/local-store.js (100%) rename packages/{client => http-client}/src/lib/stores/session-store.js (100%) rename packages/{client => http-client}/src/lib/utils.js (100%) rename packages/{client => http-client}/src/ts/README.md (100%) rename packages/{client => http-client}/tests/contract-lock.test.js (100%) rename packages/{client => http-client}/tests/fixtures/contract.js (100%) rename packages/{client => http-client}/tests/fixtures/dev.js (100%) rename packages/{client => http-client}/tests/fixtures/favicon.ico (100%) rename packages/{client => http-client}/tests/fixtures/helpers/setup-browser.js (100%) rename packages/{client => http-client}/tests/fixtures/index.html (100%) rename packages/{client => http-client}/tests/fixtures/obj.js (100%) rename packages/{client => http-client}/tests/fixtures/options.json (100%) rename packages/{client => http-client}/tests/fixtures/resolvers/auth/issuer.js (100%) rename packages/{client => http-client}/tests/fixtures/resolvers/auth/validator.js (100%) rename packages/{client => http-client}/tests/fixtures/resolvers/mutation/plus.js (100%) rename packages/{client => http-client}/tests/fixtures/resolvers/query/get-something.js (100%) rename packages/{client => http-client}/tests/fixtures/resolvers/query/test-list.js (100%) rename packages/{client => http-client}/tests/fixtures/run.js (100%) rename packages/{client => http-client}/tests/fixtures/server.js (100%) rename packages/{client => http-client}/tests/fn.test.js (100%) rename packages/{client => http-client}/tests/main.test.js (100%) rename packages/{client => http-client}/tests/qunit/files/base-test.js (100%) rename packages/{client => http-client}/tests/qunit/files/stores-test.js (100%) rename packages/{client => http-client}/tests/qunit/run-qunit-setup.js (100%) rename packages/{client => http-client}/tests/qunit/run-qunit.js (100%) rename packages/{client => http-client}/tests/qunit/webroot/index.html (100%) rename packages/{client => http-client}/tests/qunit/webroot/nb-qunit-helper.js (100%) rename packages/{client => http-client}/tests/validation.test.js (100%) diff --git a/packages/client/package.json b/packages/client/package.json old mode 100755 new mode 100644 index 96eb533b..16cdbc9c --- a/packages/client/package.json +++ b/packages/client/package.json @@ -1,10 +1,10 @@ { - "name": "jsonql-client", - "version": "1.2.0-beta.4", - "description": "json:ql client package for javascript using superagent", + "name": "@jsonql/client", + "version": "1.0.0-alpha.1", + "description": "jsonql combine client for http / ws develop with Typescript", "main": "index.js", - "module": "src/index.js", - "browser": "lib/jsonql-client.js", + "module": "src/index.ts", + "browser": "dist/jsonql-client.js", "scripts": { "test": "npm run build && DEBUG=jsonql* node ./tests/browser/run-qunit.js", "prepare": "npm run build", @@ -37,10 +37,7 @@ "index.js", "oop.js" ], - "author": "to1source ", - "contributors": [ - "NEWBRAN LTD " - ], + "author": "Joel Chu ", "repository": { "type": "git", "url": "git+ssh://git@gitee.com:to1source/jsonql.git" diff --git a/packages/client/README.md b/packages/http-client/README.md similarity index 100% rename from packages/client/README.md rename to packages/http-client/README.md diff --git a/packages/client/dist/jsonql-client.cls.js b/packages/http-client/dist/jsonql-client.cls.js similarity index 100% rename from packages/client/dist/jsonql-client.cls.js rename to packages/http-client/dist/jsonql-client.cls.js diff --git a/packages/client/dist/jsonql-client.cls.js.map b/packages/http-client/dist/jsonql-client.cls.js.map similarity index 100% rename from packages/client/dist/jsonql-client.cls.js.map rename to packages/http-client/dist/jsonql-client.cls.js.map diff --git a/packages/client/dist/jsonql-client.umd.js b/packages/http-client/dist/jsonql-client.umd.js similarity index 100% rename from packages/client/dist/jsonql-client.umd.js rename to packages/http-client/dist/jsonql-client.umd.js diff --git a/packages/client/dist/jsonql-client.umd.js.map b/packages/http-client/dist/jsonql-client.umd.js.map similarity index 100% rename from packages/client/dist/jsonql-client.umd.js.map rename to packages/http-client/dist/jsonql-client.umd.js.map diff --git a/packages/client/dist/jsonql-stores.js b/packages/http-client/dist/jsonql-stores.js similarity index 100% rename from packages/client/dist/jsonql-stores.js rename to packages/http-client/dist/jsonql-stores.js diff --git a/packages/client/dist/jsonql-stores.js.map b/packages/http-client/dist/jsonql-stores.js.map similarity index 100% rename from packages/client/dist/jsonql-stores.js.map rename to packages/http-client/dist/jsonql-stores.js.map diff --git a/packages/client/index.js b/packages/http-client/index.js similarity index 100% rename from packages/client/index.js rename to packages/http-client/index.js diff --git a/packages/client/index.ts b/packages/http-client/index.ts similarity index 100% rename from packages/client/index.ts rename to packages/http-client/index.ts diff --git a/packages/http-client/package.json b/packages/http-client/package.json new file mode 100755 index 00000000..35484f6e --- /dev/null +++ b/packages/http-client/package.json @@ -0,0 +1,105 @@ +{ + "name": "@jsonql/http-client", + "version": "1.0.0-beta.1", + "description": "json:ql client package for javascript using superagent", + "main": "index.js", + "module": "src/index.js", + "browser": "lib/jsonql-client.js", + "scripts": { + "test": "npm run build && DEBUG=jsonql* node ./tests/browser/run-qunit.js", + "prepare": "npm run build", + "test:node": "DEBUG=jsonql* ava --verbose", + "test:contract": "DEBUG=jsonql-* ava ./tests/contract-lock.test.js", + "test:browser": "npm run build && npm run run:browser", + "test:fn": "ava ./tests/fn.test.js", + "run:browser": "DEBUG=jsonql-* node ./tests/fixtures/run.js", + "start": "rollup -c -w --environment NODE_ENV:dev", + "build": "npm run build:production && npm run build:oop", + "build:umd": "rollup -c", + "build:production": "NODE_ENV=production rollup -c", + "build:test": "rollup -c -w", + "build:oop": "rollup --config ./rollup.oop.config.js", + "build:stores": "NODE_ENV=stores rollup --config ./rollup.oop.config.js", + "contract:file": "node ./tests/fixtures/contract.js", + "contract": "npm run contract:base && npm run contract:public", + "contract:base": "DEBUG=jsonql-contract* node ../contract-cli/cmd.js ./tests/fixtures/resolvers ./tests/fixtures/contracts/tmp", + "contract:public": "DEBUG=jsonql-contract* node ../contract-cli/cmd.js ./tests/fixtures/resolvers ./tests/fixtures/contracts/tmp --public=1", + "qunit": "npm run build:umd && DEBUG=jsonql-*,server-io-core* node ./tests/qunit/run-qunit.js" + }, + "keywords": [ + "json", + "ql", + "js" + ], + "files": [ + "lib", + "src", + "index.js", + "oop.js" + ], + "author": "to1source ", + "contributors": [ + "NEWBRAN LTD " + ], + "repository": { + "type": "git", + "url": "git+ssh://git@gitee.com:to1source/jsonql.git" + }, + "license": "MIT", + "dependencies": { + "flyio": "^0.6.14", + "jsonql-constants": "^1.7.6", + "jsonql-errors": "^1.0.9", + "jsonql-jwt": "^1.2.1", + "jsonql-params-validator": "^1.4.3", + "lodash-es": "^4.17.11", + "nb-event-service": "^1.6.0", + "store": "^2.0.12" + }, + "devDependencies": { + "ava": "^2.1.0", + "browser-env": "^3.2.6", + "debug": "^4.1.1", + "esm": "^3.2.25", + "glob": "^7.1.4", + "jsonql-koa": "^1.2.4", + "koa-favicon": "^2.0.1", + "nyc": "^14.1.1", + "promise-polyfill": "8.1.3", + "qunit": "^2.9.2", + "rollup": "^1.16.3", + "rollup-plugin-alias": "^1.5.2", + "rollup-plugin-analyzer": "^3.0.1", + "rollup-plugin-async": "^1.2.0", + "rollup-plugin-buble": "^0.19.6", + "rollup-plugin-bundle-size": "^1.0.3", + "rollup-plugin-commonjs": "^10.0.1", + "rollup-plugin-copy": "^3.0.0", + "rollup-plugin-node-builtins": "^2.1.2", + "rollup-plugin-node-globals": "^1.4.0", + "rollup-plugin-node-resolve": "^5.2.0", + "rollup-plugin-replace": "^2.2.0", + "rollup-plugin-serve": "^1.0.1", + "rollup-plugin-terser": "^5.0.0", + "rollup-plugin-uglify": "^6.0.2", + "server-io-core": "^1.1.0", + "window": "^4.2.6" + }, + "ava": { + "files": [ + "tests/*.test.js", + "!tests/fixtures/*.*", + "!tests/browser/*.*", + "!tests/qunit/*.*" + ], + "require": [ + "./tests/fixtures/helpers/setup-browser.js" + ], + "cache": false, + "concurrency": 5, + "failFast": true, + "failWithoutAssertions": false, + "tap": false, + "compileEnhancements": false + } +} diff --git a/packages/client/rollup.config.js b/packages/http-client/rollup.config.js similarity index 100% rename from packages/client/rollup.config.js rename to packages/http-client/rollup.config.js diff --git a/packages/client/rollup.oop.config.js b/packages/http-client/rollup.oop.config.js similarity index 100% rename from packages/client/rollup.oop.config.js rename to packages/http-client/rollup.oop.config.js diff --git a/packages/client/src/index.js b/packages/http-client/src/index.js similarity index 100% rename from packages/client/src/index.js rename to packages/http-client/src/index.js diff --git a/packages/client/src/jsonql-api.js b/packages/http-client/src/jsonql-api.js similarity index 100% rename from packages/client/src/jsonql-api.js rename to packages/http-client/src/jsonql-api.js diff --git a/packages/client/src/jsonql-client-cls.js b/packages/http-client/src/jsonql-client-cls.js similarity index 100% rename from packages/client/src/jsonql-client-cls.js rename to packages/http-client/src/jsonql-client-cls.js diff --git a/packages/client/src/lib/base/auth-cls.js b/packages/http-client/src/lib/base/auth-cls.js similarity index 100% rename from packages/client/src/lib/base/auth-cls.js rename to packages/http-client/src/lib/base/auth-cls.js diff --git a/packages/client/src/lib/base/base-cls.js b/packages/http-client/src/lib/base/base-cls.js similarity index 100% rename from packages/client/src/lib/base/base-cls.js rename to packages/http-client/src/lib/base/base-cls.js diff --git a/packages/client/src/lib/base/contract-cls.js b/packages/http-client/src/lib/base/contract-cls.js similarity index 100% rename from packages/client/src/lib/base/contract-cls.js rename to packages/http-client/src/lib/base/contract-cls.js diff --git a/packages/client/src/lib/base/flyio.js b/packages/http-client/src/lib/base/flyio.js similarity index 100% rename from packages/client/src/lib/base/flyio.js rename to packages/http-client/src/lib/base/flyio.js diff --git a/packages/client/src/lib/base/http-cls.js b/packages/http-client/src/lib/base/http-cls.js similarity index 100% rename from packages/client/src/lib/base/http-cls.js rename to packages/http-client/src/lib/base/http-cls.js diff --git a/packages/client/src/lib/base/index.js b/packages/http-client/src/lib/base/index.js similarity index 100% rename from packages/client/src/lib/base/index.js rename to packages/http-client/src/lib/base/index.js diff --git a/packages/client/src/lib/ee.js b/packages/http-client/src/lib/ee.js similarity index 100% rename from packages/client/src/lib/ee.js rename to packages/http-client/src/lib/ee.js diff --git a/packages/client/src/lib/jsonql-api-generator.js b/packages/http-client/src/lib/jsonql-api-generator.js similarity index 100% rename from packages/client/src/lib/jsonql-api-generator.js rename to packages/http-client/src/lib/jsonql-api-generator.js diff --git a/packages/client/src/lib/options/base-options.js b/packages/http-client/src/lib/options/base-options.js similarity index 100% rename from packages/client/src/lib/options/base-options.js rename to packages/http-client/src/lib/options/base-options.js diff --git a/packages/client/src/lib/options/check-options.js b/packages/http-client/src/lib/options/check-options.js similarity index 100% rename from packages/client/src/lib/options/check-options.js rename to packages/http-client/src/lib/options/check-options.js diff --git a/packages/client/src/lib/options/index.js b/packages/http-client/src/lib/options/index.js similarity index 100% rename from packages/client/src/lib/options/index.js rename to packages/http-client/src/lib/options/index.js diff --git a/packages/client/src/lib/stores/index.js b/packages/http-client/src/lib/stores/index.js similarity index 100% rename from packages/client/src/lib/stores/index.js rename to packages/http-client/src/lib/stores/index.js diff --git a/packages/client/src/lib/stores/local-store.js b/packages/http-client/src/lib/stores/local-store.js similarity index 100% rename from packages/client/src/lib/stores/local-store.js rename to packages/http-client/src/lib/stores/local-store.js diff --git a/packages/client/src/lib/stores/session-store.js b/packages/http-client/src/lib/stores/session-store.js similarity index 100% rename from packages/client/src/lib/stores/session-store.js rename to packages/http-client/src/lib/stores/session-store.js diff --git a/packages/client/src/lib/utils.js b/packages/http-client/src/lib/utils.js similarity index 100% rename from packages/client/src/lib/utils.js rename to packages/http-client/src/lib/utils.js diff --git a/packages/client/src/ts/README.md b/packages/http-client/src/ts/README.md similarity index 100% rename from packages/client/src/ts/README.md rename to packages/http-client/src/ts/README.md diff --git a/packages/client/tests/contract-lock.test.js b/packages/http-client/tests/contract-lock.test.js similarity index 100% rename from packages/client/tests/contract-lock.test.js rename to packages/http-client/tests/contract-lock.test.js diff --git a/packages/client/tests/fixtures/contract.js b/packages/http-client/tests/fixtures/contract.js similarity index 100% rename from packages/client/tests/fixtures/contract.js rename to packages/http-client/tests/fixtures/contract.js diff --git a/packages/client/tests/fixtures/dev.js b/packages/http-client/tests/fixtures/dev.js similarity index 100% rename from packages/client/tests/fixtures/dev.js rename to packages/http-client/tests/fixtures/dev.js diff --git a/packages/client/tests/fixtures/favicon.ico b/packages/http-client/tests/fixtures/favicon.ico similarity index 100% rename from packages/client/tests/fixtures/favicon.ico rename to packages/http-client/tests/fixtures/favicon.ico diff --git a/packages/client/tests/fixtures/helpers/setup-browser.js b/packages/http-client/tests/fixtures/helpers/setup-browser.js similarity index 100% rename from packages/client/tests/fixtures/helpers/setup-browser.js rename to packages/http-client/tests/fixtures/helpers/setup-browser.js diff --git a/packages/client/tests/fixtures/index.html b/packages/http-client/tests/fixtures/index.html similarity index 100% rename from packages/client/tests/fixtures/index.html rename to packages/http-client/tests/fixtures/index.html diff --git a/packages/client/tests/fixtures/obj.js b/packages/http-client/tests/fixtures/obj.js similarity index 100% rename from packages/client/tests/fixtures/obj.js rename to packages/http-client/tests/fixtures/obj.js diff --git a/packages/client/tests/fixtures/options.json b/packages/http-client/tests/fixtures/options.json similarity index 100% rename from packages/client/tests/fixtures/options.json rename to packages/http-client/tests/fixtures/options.json diff --git a/packages/client/tests/fixtures/resolvers/auth/issuer.js b/packages/http-client/tests/fixtures/resolvers/auth/issuer.js similarity index 100% rename from packages/client/tests/fixtures/resolvers/auth/issuer.js rename to packages/http-client/tests/fixtures/resolvers/auth/issuer.js diff --git a/packages/client/tests/fixtures/resolvers/auth/validator.js b/packages/http-client/tests/fixtures/resolvers/auth/validator.js similarity index 100% rename from packages/client/tests/fixtures/resolvers/auth/validator.js rename to packages/http-client/tests/fixtures/resolvers/auth/validator.js diff --git a/packages/client/tests/fixtures/resolvers/mutation/plus.js b/packages/http-client/tests/fixtures/resolvers/mutation/plus.js similarity index 100% rename from packages/client/tests/fixtures/resolvers/mutation/plus.js rename to packages/http-client/tests/fixtures/resolvers/mutation/plus.js diff --git a/packages/client/tests/fixtures/resolvers/query/get-something.js b/packages/http-client/tests/fixtures/resolvers/query/get-something.js similarity index 100% rename from packages/client/tests/fixtures/resolvers/query/get-something.js rename to packages/http-client/tests/fixtures/resolvers/query/get-something.js diff --git a/packages/client/tests/fixtures/resolvers/query/test-list.js b/packages/http-client/tests/fixtures/resolvers/query/test-list.js similarity index 100% rename from packages/client/tests/fixtures/resolvers/query/test-list.js rename to packages/http-client/tests/fixtures/resolvers/query/test-list.js diff --git a/packages/client/tests/fixtures/run.js b/packages/http-client/tests/fixtures/run.js similarity index 100% rename from packages/client/tests/fixtures/run.js rename to packages/http-client/tests/fixtures/run.js diff --git a/packages/client/tests/fixtures/server.js b/packages/http-client/tests/fixtures/server.js similarity index 100% rename from packages/client/tests/fixtures/server.js rename to packages/http-client/tests/fixtures/server.js diff --git a/packages/client/tests/fn.test.js b/packages/http-client/tests/fn.test.js similarity index 100% rename from packages/client/tests/fn.test.js rename to packages/http-client/tests/fn.test.js diff --git a/packages/client/tests/main.test.js b/packages/http-client/tests/main.test.js similarity index 100% rename from packages/client/tests/main.test.js rename to packages/http-client/tests/main.test.js diff --git a/packages/client/tests/qunit/files/base-test.js b/packages/http-client/tests/qunit/files/base-test.js similarity index 100% rename from packages/client/tests/qunit/files/base-test.js rename to packages/http-client/tests/qunit/files/base-test.js diff --git a/packages/client/tests/qunit/files/stores-test.js b/packages/http-client/tests/qunit/files/stores-test.js similarity index 100% rename from packages/client/tests/qunit/files/stores-test.js rename to packages/http-client/tests/qunit/files/stores-test.js diff --git a/packages/client/tests/qunit/run-qunit-setup.js b/packages/http-client/tests/qunit/run-qunit-setup.js similarity index 100% rename from packages/client/tests/qunit/run-qunit-setup.js rename to packages/http-client/tests/qunit/run-qunit-setup.js diff --git a/packages/client/tests/qunit/run-qunit.js b/packages/http-client/tests/qunit/run-qunit.js similarity index 100% rename from packages/client/tests/qunit/run-qunit.js rename to packages/http-client/tests/qunit/run-qunit.js diff --git a/packages/client/tests/qunit/webroot/index.html b/packages/http-client/tests/qunit/webroot/index.html similarity index 100% rename from packages/client/tests/qunit/webroot/index.html rename to packages/http-client/tests/qunit/webroot/index.html diff --git a/packages/client/tests/qunit/webroot/nb-qunit-helper.js b/packages/http-client/tests/qunit/webroot/nb-qunit-helper.js similarity index 100% rename from packages/client/tests/qunit/webroot/nb-qunit-helper.js rename to packages/http-client/tests/qunit/webroot/nb-qunit-helper.js diff --git a/packages/client/tests/validation.test.js b/packages/http-client/tests/validation.test.js similarity index 100% rename from packages/client/tests/validation.test.js rename to packages/http-client/tests/validation.test.js -- Gitee From 0c4b50cb19d87d77d03d9788a6eb370ae08af87f Mon Sep 17 00:00:00 2001 From: Joelchu Date: Sat, 13 Jul 2019 12:32:24 +0800 Subject: [PATCH 02/23] setup utils folder --- packages/util/README.md | 11 +++++++++++ packages/util/package.json | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 packages/util/README.md create mode 100644 packages/util/package.json diff --git a/packages/util/README.md b/packages/util/README.md new file mode 100644 index 00000000..44de9a65 --- /dev/null +++ b/packages/util/README.md @@ -0,0 +1,11 @@ +# jsonql-util + +This is a dependency module for various jsonql node modules. + +Please check [jsonql.org](http://jsonql.org) for more information. + +--- + +ISC + +Joel Chu (c) 2019 diff --git a/packages/util/package.json b/packages/util/package.json new file mode 100644 index 00000000..fad2b757 --- /dev/null +++ b/packages/util/package.json @@ -0,0 +1,36 @@ +{ + "name": "jsonql-util", + "version": "1.0.0-beta", + "description": "This is not for generate use, it's for jsonql various modules", + "main": "index.js", + "scripts": { + "test": "ava" + }, + "keywords": [ + "jsonql", + "utils" + ], + "author": "Joel Chu ", + "license": "ISC", + "repository": { + "type": "git", + "url": "git+ssh://git@gitee.com:to1source/jsonql.git" + }, + "ava": { + "files": [ + "tests/*.test.js", + "!tests/fixtures/*.*", + "!tests/browser/*.*", + "!tests/qunit/*.*" + ], + "require": [ + "esm" + ], + "cache": false, + "concurrency": 5, + "failFast": true, + "failWithoutAssertions": false, + "tap": false, + "compileEnhancements": false + } +} -- Gitee From cd3e859eec50bc1fcbd94618e53db7c4f37c68fe Mon Sep 17 00:00:00 2001 From: Joelchu Date: Sat, 13 Jul 2019 20:41:54 +0800 Subject: [PATCH 03/23] init all the base options for the typescript development --- packages/event/index.ts | 1 + packages/event/package.json | 37 +++++++++++++++++++++ packages/event/tsconfig.json | 63 ++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 packages/event/index.ts create mode 100644 packages/event/package.json create mode 100644 packages/event/tsconfig.json diff --git a/packages/event/index.ts b/packages/event/index.ts new file mode 100644 index 00000000..b7a70fb6 --- /dev/null +++ b/packages/event/index.ts @@ -0,0 +1 @@ +// The top level for @jsonql/event diff --git a/packages/event/package.json b/packages/event/package.json new file mode 100644 index 00000000..298b8356 --- /dev/null +++ b/packages/event/package.json @@ -0,0 +1,37 @@ +{ + "name": "@jsonql/event", + "version": "1.0.0", + "description": "Ported from nb-event-service rewritten with Typescript", + "main": "dist/index.js", + "module": "dist/index.es.js", + "types": "dist/index.d.ts", + "files": [ + "dist", + "index.ts" + ], + "scripts": { + "test": "ava", + "build": "rollup -c", + "dev": "rollup -cw" + }, + "keywords": [ + "jsonql", + "event", + "nb-event-service", + "eventEmitter" + ], + "author": "Joel Chu ", + "repository": { + "type": "git", + "url": "git+ssh://git@gitee.com:to1source/jsonql.git" + }, + "license": "ISC", + "devDependencies": { + "ava": "^2.2.0", + "typescript": "^3.5.3" + }, + "dependencies": { + "rollup": "^1.16.7", + "rollup-plugin-typescript2": "^0.22.0" + } +} diff --git a/packages/event/tsconfig.json b/packages/event/tsconfig.json new file mode 100644 index 00000000..c93485c3 --- /dev/null +++ b/packages/event/tsconfig.json @@ -0,0 +1,63 @@ +{ + "compilerOptions": { + /* Basic Options */ + // "incremental": true, /* Enable incremental compilation */ + "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */ + "module": "umd", /* 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. */ + // "checkJs": true, /* Report errors in .js files. */ + // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ + // "declaration": true, /* Generates corresponding '.d.ts' file. */ + // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ + // "sourceMap": true, /* Generates corresponding '.map' file. */ + // "outFile": "./", /* Concatenate and emit output to single file. */ + "outDir": "./dist", /* Redirect output structure to the directory. */ + // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ + // "composite": true, /* Enable project compilation */ + // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ + // "removeComments": true, /* Do not emit comments to output. */ + // "noEmit": true, /* Do not emit outputs. */ + // "importHelpers": true, /* Import emit helpers from 'tslib'. */ + // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ + // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ + + /* Strict Type-Checking Options */ + "strict": true, /* Enable all strict type-checking options. */ + // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ + // "strictNullChecks": true, /* Enable strict null checks. */ + // "strictFunctionTypes": true, /* Enable strict checking of function types. */ + // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ + // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ + // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ + // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ + + /* Additional Checks */ + // "noUnusedLocals": true, /* Report errors on unused locals. */ + // "noUnusedParameters": true, /* Report errors on unused parameters. */ + // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ + // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ + + /* Module Resolution Options */ + // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ + // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ + // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ + // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ + // "typeRoots": [], /* List of folders to include type definitions from. */ + // "types": [], /* Type declaration files to be included in compilation. */ + // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ + "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ + // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ + // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ + + /* Source Map Options */ + // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ + // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ + // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ + + /* Experimental Options */ + // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ + // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ + } +} -- Gitee From 638ad4887a847e34c3bdeaf046117254218ec965 Mon Sep 17 00:00:00 2001 From: Joelchu Date: Sat, 13 Jul 2019 20:48:55 +0800 Subject: [PATCH 04/23] setup rollup --- packages/event/rollup.config.js | 25 +++++++++++++++++++++++++ packages/event/tsconfig.json | 8 +++++++- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 packages/event/rollup.config.js diff --git a/packages/event/rollup.config.js b/packages/event/rollup.config.js new file mode 100644 index 00000000..4ad0797a --- /dev/null +++ b/packages/event/rollup.config.js @@ -0,0 +1,25 @@ +import typescript from 'rollup-plugin-typescript2' +import pkg from './package.json' + +export default { + input: 'index.ts', + output: [ + { + file: pkg.main, + format: 'cjs' + }, + { + file: pkg.module, + format: 'es' + } + ], + external: [ + ...Object.keys(pkg.depedencies || {}), + ...Object.keys(pkg.peerDependencies || {}) + ], + plugins: [ + typescript({ + typescript: require('typescript') + }) + ] +} diff --git a/packages/event/tsconfig.json b/packages/event/tsconfig.json index c93485c3..d02e2743 100644 --- a/packages/event/tsconfig.json +++ b/packages/event/tsconfig.json @@ -59,5 +59,11 @@ /* Experimental Options */ // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ - } + }, + "include": [ + "src/**/*" + ], + "exclude": [ + "node_modules" + ] } -- Gitee From 296504db134f02835a2750d3f4003d890b71381e Mon Sep 17 00:00:00 2001 From: Joelchu Date: Sun, 14 Jul 2019 08:43:21 +0800 Subject: [PATCH 05/23] setting up ava ts test env --- packages/event/package.json | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/event/package.json b/packages/event/package.json index 298b8356..1b6c45d7 100644 --- a/packages/event/package.json +++ b/packages/event/package.json @@ -28,10 +28,31 @@ "license": "ISC", "devDependencies": { "ava": "^2.2.0", - "typescript": "^3.5.3" - }, - "dependencies": { + "ts-node": "^8.3.0", + "typescript": "^3.5.3", "rollup": "^1.16.7", "rollup-plugin-typescript2": "^0.22.0" + }, + "dependencies": { + + }, + "ava": { + "files": [ + "tests/*.test.ts", + "!tests/helpers/*.*", + "!tests/fixtures/*.*" + ], + "cache": true, + "concurrency": 5, + "failFast": true, + "failWithoutAssertions": false, + "tap": false, + "compileEnhancements": false, + "extensions": [ + "ts" + ], + "require": [ + "ts-node/register" + ] } } -- Gitee From 967b8b17e8ccad2e36b6a1c6056132f7024469e0 Mon Sep 17 00:00:00 2001 From: Joelchu Date: Sun, 14 Jul 2019 09:56:17 +0800 Subject: [PATCH 06/23] start constructing --- packages/event/src/event-service.ts | 21 +++++++++++++++++++++ packages/event/src/hash-code.ts | 9 +++++++++ 2 files changed, 30 insertions(+) create mode 100644 packages/event/src/event-service.ts create mode 100644 packages/event/src/hash-code.ts diff --git a/packages/event/src/event-service.ts b/packages/event/src/event-service.ts new file mode 100644 index 00000000..6e6ebd6e --- /dev/null +++ b/packages/event/src/event-service.ts @@ -0,0 +1,21 @@ +// The main +interface configObj { + logger?: any, // this is the shitty thing about TS + +} + + +export default class EventService { + private normalStore: new Map(); + private lazyStore: new Map(); + + + /** + * Create EventService instance + * @param {configObj} config configuration object + * @return {void} nothing + */ + constructor(config: configObj): void { + + } +} diff --git a/packages/event/src/hash-code.ts b/packages/event/src/hash-code.ts new file mode 100644 index 00000000..512e1a49 --- /dev/null +++ b/packages/event/src/hash-code.ts @@ -0,0 +1,9 @@ +/** + * generate a 32bit hash based on the function.toString() + * _from http://stackoverflow.com/questions/7616461/generate-a-hash-_from-string-in-javascript-jquery + * @param {string} s the converted to string function + * @return {string} the hashed function string + */ +export default function hashCode(s: String): String { + return s.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a},0) + "" +} -- Gitee From 85ef1ee9da4dc59a31cc29ada2af9491f1a98c9a Mon Sep 17 00:00:00 2001 From: Joelchu Date: Sun, 14 Jul 2019 14:17:21 +0800 Subject: [PATCH 07/23] struggle a bit to setup the basic --- .../6d274a1bdd6a4ae01f191f5d94ca2e2046153ffb | 1 + .../a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 | 1 + .../6d274a1bdd6a4ae01f191f5d94ca2e2046153ffb | 1 + .../a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 | 1 + .../6d274a1bdd6a4ae01f191f5d94ca2e2046153ffb | 1 + .../a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 | 1 + .../0af14034b4858555dc8d4a8ed15d7f08de40eb36 | 0 .../2c0fa7d7cfab72e02c2850c2c6402684f18bdc9d | 0 .../609dd35a6af3197577b43708e69aa1be093cdb69 | 0 .../7f715d854fbb98c5fe1dab178b872994225edc80 | 0 .../ab2c42645f134bd03c75a1cfe9a94d3dd21da9fe | 0 packages/event/dist/index.es.js | 27 +++++++++++++++++ packages/event/dist/index.js | 29 +++++++++++++++++++ packages/event/index.ts | 3 ++ packages/event/src/event-service.ts | 22 +++++++++----- packages/event/tsconfig.json | 2 +- 16 files changed, 80 insertions(+), 9 deletions(-) create mode 100644 packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/6d274a1bdd6a4ae01f191f5d94ca2e2046153ffb create mode 100644 packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 create mode 100644 packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/semanticDiagnostics/cache/6d274a1bdd6a4ae01f191f5d94ca2e2046153ffb create mode 100644 packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/semanticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 create mode 100644 packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/syntacticDiagnostics/cache/6d274a1bdd6a4ae01f191f5d94ca2e2046153ffb create mode 100644 packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/syntacticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 create mode 100644 packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/0af14034b4858555dc8d4a8ed15d7f08de40eb36 create mode 100644 packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/2c0fa7d7cfab72e02c2850c2c6402684f18bdc9d create mode 100644 packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/609dd35a6af3197577b43708e69aa1be093cdb69 create mode 100644 packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/7f715d854fbb98c5fe1dab178b872994225edc80 create mode 100644 packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/ab2c42645f134bd03c75a1cfe9a94d3dd21da9fe create mode 100644 packages/event/dist/index.es.js create mode 100644 packages/event/dist/index.js diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/6d274a1bdd6a4ae01f191f5d94ca2e2046153ffb b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/6d274a1bdd6a4ae01f191f5d94ca2e2046153ffb new file mode 100644 index 00000000..52553434 --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/6d274a1bdd6a4ae01f191f5d94ca2e2046153ffb @@ -0,0 +1 @@ +{"code":"// The main\r\n/*\r\ninterface configObj {\r\n logger?: any, // this is the shitty thing about TS\r\n keep? : Boolean,\r\n suspend?: Boolean\r\n}\r\n*/\r\nvar EventService = /** @class */ (function () {\r\n /**\r\n * Create EventService instance\r\n * @param {configObj} config configuration object\r\n * @return {void} nothing\r\n */\r\n function EventService(logger) {\r\n this.logger = logger;\r\n }\r\n return EventService;\r\n}());\r\nexport default EventService;\r\n","references":[]} diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 new file mode 100644 index 00000000..7242cbab --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 @@ -0,0 +1 @@ +{"code":"// The top level for @jsonql/event\r\nimport * as EventService from './src/event-service';\r\nexport default EventService;\r\n","references":["/home/joel/projects/open-source/jsonql/packages/event/src/event-service.ts"]} diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/semanticDiagnostics/cache/6d274a1bdd6a4ae01f191f5d94ca2e2046153ffb b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/semanticDiagnostics/cache/6d274a1bdd6a4ae01f191f5d94ca2e2046153ffb new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/semanticDiagnostics/cache/6d274a1bdd6a4ae01f191f5d94ca2e2046153ffb @@ -0,0 +1 @@ +[] diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/semanticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/semanticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/semanticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 @@ -0,0 +1 @@ +[] diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/syntacticDiagnostics/cache/6d274a1bdd6a4ae01f191f5d94ca2e2046153ffb b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/syntacticDiagnostics/cache/6d274a1bdd6a4ae01f191f5d94ca2e2046153ffb new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/syntacticDiagnostics/cache/6d274a1bdd6a4ae01f191f5d94ca2e2046153ffb @@ -0,0 +1 @@ +[] diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/syntacticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/syntacticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/syntacticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 @@ -0,0 +1 @@ +[] diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/0af14034b4858555dc8d4a8ed15d7f08de40eb36 b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/0af14034b4858555dc8d4a8ed15d7f08de40eb36 new file mode 100644 index 00000000..e69de29b diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/2c0fa7d7cfab72e02c2850c2c6402684f18bdc9d b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/2c0fa7d7cfab72e02c2850c2c6402684f18bdc9d new file mode 100644 index 00000000..e69de29b diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/609dd35a6af3197577b43708e69aa1be093cdb69 b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/609dd35a6af3197577b43708e69aa1be093cdb69 new file mode 100644 index 00000000..e69de29b diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/7f715d854fbb98c5fe1dab178b872994225edc80 b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/7f715d854fbb98c5fe1dab178b872994225edc80 new file mode 100644 index 00000000..e69de29b diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/ab2c42645f134bd03c75a1cfe9a94d3dd21da9fe b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/ab2c42645f134bd03c75a1cfe9a94d3dd21da9fe new file mode 100644 index 00000000..e69de29b diff --git a/packages/event/dist/index.es.js b/packages/event/dist/index.es.js new file mode 100644 index 00000000..3be7b0da --- /dev/null +++ b/packages/event/dist/index.es.js @@ -0,0 +1,27 @@ +// The main +/* +interface configObj { + logger?: any, // this is the shitty thing about TS + keep? : Boolean, + suspend?: Boolean +} +*/ +var EventService = /** @class */ (function () { + /** + * Create EventService instance + * @param {configObj} config configuration object + * @return {void} nothing + */ + function EventService(logger) { + this.logger = logger; + } + return EventService; +}()); + +var EventService$1 = /*#__PURE__*/Object.freeze({ + 'default': EventService +}); + +// The top level for @jsonql/event + +export default EventService$1; diff --git a/packages/event/dist/index.js b/packages/event/dist/index.js new file mode 100644 index 00000000..03623a61 --- /dev/null +++ b/packages/event/dist/index.js @@ -0,0 +1,29 @@ +'use strict'; + +// The main +/* +interface configObj { + logger?: any, // this is the shitty thing about TS + keep? : Boolean, + suspend?: Boolean +} +*/ +var EventService = /** @class */ (function () { + /** + * Create EventService instance + * @param {configObj} config configuration object + * @return {void} nothing + */ + function EventService(logger) { + this.logger = logger; + } + return EventService; +}()); + +var EventService$1 = /*#__PURE__*/Object.freeze({ + 'default': EventService +}); + +// The top level for @jsonql/event + +module.exports = EventService$1; diff --git a/packages/event/index.ts b/packages/event/index.ts index b7a70fb6..0f376a1f 100644 --- a/packages/event/index.ts +++ b/packages/event/index.ts @@ -1 +1,4 @@ // The top level for @jsonql/event +import * as EventService from './src/event-service'; + +export default EventService; diff --git a/packages/event/src/event-service.ts b/packages/event/src/event-service.ts index 6e6ebd6e..1c9e58d8 100644 --- a/packages/event/src/event-service.ts +++ b/packages/event/src/event-service.ts @@ -1,21 +1,27 @@ // The main +/* interface configObj { logger?: any, // this is the shitty thing about TS - + keep? : Boolean, + suspend?: Boolean } - - +*/ export default class EventService { - private normalStore: new Map(); - private lazyStore: new Map(); + // private normalStore: new Map(); + // private lazyStore: new Map(); + // private logger: (...args) => void; + private logger: any; /** * Create EventService instance * @param {configObj} config configuration object - * @return {void} nothing + * @return {void} nothing - don't do :void this fuck typescript up what a lot of shit */ - constructor(config: configObj): void { - + constructor(logger: any) { + this.logger = logger; } + + + } diff --git a/packages/event/tsconfig.json b/packages/event/tsconfig.json index d02e2743..5909d0cb 100644 --- a/packages/event/tsconfig.json +++ b/packages/event/tsconfig.json @@ -3,7 +3,7 @@ /* Basic Options */ // "incremental": true, /* Enable incremental compilation */ "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */ - "module": "umd", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 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. */ // "checkJs": true, /* Report errors in .js files. */ -- Gitee From dd99b3c386e9c8042f0d5869d411b98703d60e97 Mon Sep 17 00:00:00 2001 From: Joel Chu Date: Sun, 14 Jul 2019 14:53:41 +0800 Subject: [PATCH 08/23] resolve the problems with init the Map --- ...ffb => 555348b18b3b7a2564f189a577b79953ff802452} | 2 +- ...ffb => 555348b18b3b7a2564f189a577b79953ff802452} | 0 ...ffb => 555348b18b3b7a2564f189a577b79953ff802452} | 0 packages/event/dist/index.es.js | 7 +++++-- packages/event/dist/index.js | 7 +++++-- packages/event/package.json | 4 +--- packages/event/src/event-service.ts | 13 +++++++++---- 7 files changed, 21 insertions(+), 12 deletions(-) rename packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/{6d274a1bdd6a4ae01f191f5d94ca2e2046153ffb => 555348b18b3b7a2564f189a577b79953ff802452} (42%) rename packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/semanticDiagnostics/cache/{6d274a1bdd6a4ae01f191f5d94ca2e2046153ffb => 555348b18b3b7a2564f189a577b79953ff802452} (100%) rename packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/syntacticDiagnostics/cache/{6d274a1bdd6a4ae01f191f5d94ca2e2046153ffb => 555348b18b3b7a2564f189a577b79953ff802452} (100%) diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/6d274a1bdd6a4ae01f191f5d94ca2e2046153ffb b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/555348b18b3b7a2564f189a577b79953ff802452 similarity index 42% rename from packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/6d274a1bdd6a4ae01f191f5d94ca2e2046153ffb rename to packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/555348b18b3b7a2564f189a577b79953ff802452 index 52553434..d55be610 100644 --- a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/6d274a1bdd6a4ae01f191f5d94ca2e2046153ffb +++ b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/555348b18b3b7a2564f189a577b79953ff802452 @@ -1 +1 @@ -{"code":"// The main\r\n/*\r\ninterface configObj {\r\n logger?: any, // this is the shitty thing about TS\r\n keep? : Boolean,\r\n suspend?: Boolean\r\n}\r\n*/\r\nvar EventService = /** @class */ (function () {\r\n /**\r\n * Create EventService instance\r\n * @param {configObj} config configuration object\r\n * @return {void} nothing\r\n */\r\n function EventService(logger) {\r\n this.logger = logger;\r\n }\r\n return EventService;\r\n}());\r\nexport default EventService;\r\n","references":[]} +{"code":"// The main\r\n/*\r\ninterface configObj {\r\n logger?: any, // this is the shitty thing about TS\r\n keep? : Boolean,\r\n suspend?: Boolean\r\n}\r\n*/\r\nvar EventService = /** @class */ (function () {\r\n /**\r\n * Create EventService instance\r\n * @param {configObj} config configuration object\r\n * @return {void} nothing - don't do :void this fuck typescript up what a lot of shit\r\n */\r\n function EventService(logger) {\r\n // hack\r\n this.logger = typeof logger === 'function' ? logger : function () { };\r\n this.normalStore = new Map();\r\n this.lazyStore = new Map();\r\n }\r\n return EventService;\r\n}());\r\nexport default EventService;\r\n","references":[]} diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/semanticDiagnostics/cache/6d274a1bdd6a4ae01f191f5d94ca2e2046153ffb b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/semanticDiagnostics/cache/555348b18b3b7a2564f189a577b79953ff802452 similarity index 100% rename from packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/semanticDiagnostics/cache/6d274a1bdd6a4ae01f191f5d94ca2e2046153ffb rename to packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/semanticDiagnostics/cache/555348b18b3b7a2564f189a577b79953ff802452 diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/syntacticDiagnostics/cache/6d274a1bdd6a4ae01f191f5d94ca2e2046153ffb b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/syntacticDiagnostics/cache/555348b18b3b7a2564f189a577b79953ff802452 similarity index 100% rename from packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/syntacticDiagnostics/cache/6d274a1bdd6a4ae01f191f5d94ca2e2046153ffb rename to packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/syntacticDiagnostics/cache/555348b18b3b7a2564f189a577b79953ff802452 diff --git a/packages/event/dist/index.es.js b/packages/event/dist/index.es.js index 3be7b0da..cc6cf2cb 100644 --- a/packages/event/dist/index.es.js +++ b/packages/event/dist/index.es.js @@ -10,10 +10,13 @@ var EventService = /** @class */ (function () { /** * Create EventService instance * @param {configObj} config configuration object - * @return {void} nothing + * @return {void} nothing - don't do :void this fuck typescript up what a lot of shit */ function EventService(logger) { - this.logger = logger; + // hack + this.logger = typeof logger === 'function' ? logger : function () { }; + this.normalStore = new Map(); + this.lazyStore = new Map(); } return EventService; }()); diff --git a/packages/event/dist/index.js b/packages/event/dist/index.js index 03623a61..4bec91e0 100644 --- a/packages/event/dist/index.js +++ b/packages/event/dist/index.js @@ -12,10 +12,13 @@ var EventService = /** @class */ (function () { /** * Create EventService instance * @param {configObj} config configuration object - * @return {void} nothing + * @return {void} nothing - don't do :void this fuck typescript up what a lot of shit */ function EventService(logger) { - this.logger = logger; + // hack + this.logger = typeof logger === 'function' ? logger : function () { }; + this.normalStore = new Map(); + this.lazyStore = new Map(); } return EventService; }()); diff --git a/packages/event/package.json b/packages/event/package.json index 1b6c45d7..89194d1c 100644 --- a/packages/event/package.json +++ b/packages/event/package.json @@ -33,9 +33,7 @@ "rollup": "^1.16.7", "rollup-plugin-typescript2": "^0.22.0" }, - "dependencies": { - - }, + "dependencies": {}, "ava": { "files": [ "tests/*.test.ts", diff --git a/packages/event/src/event-service.ts b/packages/event/src/event-service.ts index 1c9e58d8..6e4b8793 100644 --- a/packages/event/src/event-service.ts +++ b/packages/event/src/event-service.ts @@ -8,8 +8,8 @@ interface configObj { */ export default class EventService { - // private normalStore: new Map(); - // private lazyStore: new Map(); + private normalStore: any; + private lazyStore: any; // private logger: (...args) => void; private logger: any; @@ -19,9 +19,14 @@ export default class EventService { * @return {void} nothing - don't do :void this fuck typescript up what a lot of shit */ constructor(logger: any) { - this.logger = logger; + // hack + this.logger = typeof logger === 'function' ? logger : () => {}; + + this.normalStore = new Map(); + this.lazyStore = new Map(); } - + + } -- Gitee From be2a9332c2b98f6632d6a0df813bb20252d433f3 Mon Sep 17 00:00:00 2001 From: Joel Chu Date: Sun, 14 Jul 2019 15:36:39 +0800 Subject: [PATCH 09/23] break up the declare files from the class definition file --- .../a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 | 1 + .../ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2 | 1 + .../ccc70771d4ab5581158090a0106a087a69a20bdb | 1 + .../efdd6301de655680bafd2f3af8ef7173a51d219e | 1 + .../a9b3003bafaffbecf76c95cc6906ea31baf1a3b9} | 0 .../ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2} | 0 .../ccc70771d4ab5581158090a0106a087a69a20bdb | 1 + .../efdd6301de655680bafd2f3af8ef7173a51d219e | 1 + .../a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 | 1 + .../ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2 | 1 + .../ccc70771d4ab5581158090a0106a087a69a20bdb | 1 + .../efdd6301de655680bafd2f3af8ef7173a51d219e | 1 + .../0af14034b4858555dc8d4a8ed15d7f08de40eb36 | 0 .../2c0fa7d7cfab72e02c2850c2c6402684f18bdc9d | 0 .../609dd35a6af3197577b43708e69aa1be093cdb69 | 0 .../7f715d854fbb98c5fe1dab178b872994225edc80 | 0 .../ab2c42645f134bd03c75a1cfe9a94d3dd21da9fe | 0 ... e6b6beb338c32055d84de9d86dd4a380e8952662} | 2 +- .../e6b6beb338c32055d84de9d86dd4a380e8952662 | 1 + .../e6b6beb338c32055d84de9d86dd4a380e8952662 | 1 + .../33be17634973f39fb3851a17d137ddadbbdf9bff | 1 + .../51af2df8071ce95ae1e4eb6859380cd66e17fe3b | 1 + .../a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 | 1 + .../33be17634973f39fb3851a17d137ddadbbdf9bff | 1 + .../51af2df8071ce95ae1e4eb6859380cd66e17fe3b | 1 + .../a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 | 1 + .../33be17634973f39fb3851a17d137ddadbbdf9bff | 1 + .../51af2df8071ce95ae1e4eb6859380cd66e17fe3b | 1 + .../a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 | 1 + .../0af14034b4858555dc8d4a8ed15d7f08de40eb36 | 0 .../2c0fa7d7cfab72e02c2850c2c6402684f18bdc9d | 0 .../609dd35a6af3197577b43708e69aa1be093cdb69 | 0 .../7f715d854fbb98c5fe1dab178b872994225edc80 | 0 .../ab2c42645f134bd03c75a1cfe9a94d3dd21da9fe | 0 packages/event/dist/index.es.js | 28 +- packages/event/dist/index.js | 28 +- packages/event/src/enums.ts | 8 + packages/event/src/event-service.js | 598 ++++++++++++++++++ packages/event/src/event-service.ts | 35 +- packages/event/src/interfaces.ts | 14 + 40 files changed, 704 insertions(+), 30 deletions(-) create mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 create mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2 create mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/ccc70771d4ab5581158090a0106a087a69a20bdb create mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/efdd6301de655680bafd2f3af8ef7173a51d219e rename packages/event/.rpt2_cache/{rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/semanticDiagnostics/cache/555348b18b3b7a2564f189a577b79953ff802452 => rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9} (100%) rename packages/event/.rpt2_cache/{rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/syntacticDiagnostics/cache/555348b18b3b7a2564f189a577b79953ff802452 => rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2} (100%) create mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/ccc70771d4ab5581158090a0106a087a69a20bdb create mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/efdd6301de655680bafd2f3af8ef7173a51d219e create mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 create mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2 create mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/ccc70771d4ab5581158090a0106a087a69a20bdb create mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/efdd6301de655680bafd2f3af8ef7173a51d219e create mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/0af14034b4858555dc8d4a8ed15d7f08de40eb36 create mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/2c0fa7d7cfab72e02c2850c2c6402684f18bdc9d create mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/609dd35a6af3197577b43708e69aa1be093cdb69 create mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/7f715d854fbb98c5fe1dab178b872994225edc80 create mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/ab2c42645f134bd03c75a1cfe9a94d3dd21da9fe rename packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/{555348b18b3b7a2564f189a577b79953ff802452 => e6b6beb338c32055d84de9d86dd4a380e8952662} (49%) create mode 100644 packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/semanticDiagnostics/cache/e6b6beb338c32055d84de9d86dd4a380e8952662 create mode 100644 packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/syntacticDiagnostics/cache/e6b6beb338c32055d84de9d86dd4a380e8952662 create mode 100644 packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/code/cache/33be17634973f39fb3851a17d137ddadbbdf9bff create mode 100644 packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/code/cache/51af2df8071ce95ae1e4eb6859380cd66e17fe3b create mode 100644 packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/code/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 create mode 100644 packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/semanticDiagnostics/cache/33be17634973f39fb3851a17d137ddadbbdf9bff create mode 100644 packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/semanticDiagnostics/cache/51af2df8071ce95ae1e4eb6859380cd66e17fe3b create mode 100644 packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/semanticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 create mode 100644 packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/syntacticDiagnostics/cache/33be17634973f39fb3851a17d137ddadbbdf9bff create mode 100644 packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/syntacticDiagnostics/cache/51af2df8071ce95ae1e4eb6859380cd66e17fe3b create mode 100644 packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/syntacticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 create mode 100644 packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/0af14034b4858555dc8d4a8ed15d7f08de40eb36 create mode 100644 packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/2c0fa7d7cfab72e02c2850c2c6402684f18bdc9d create mode 100644 packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/609dd35a6af3197577b43708e69aa1be093cdb69 create mode 100644 packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/7f715d854fbb98c5fe1dab178b872994225edc80 create mode 100644 packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/ab2c42645f134bd03c75a1cfe9a94d3dd21da9fe create mode 100644 packages/event/src/enums.ts create mode 100644 packages/event/src/event-service.js create mode 100644 packages/event/src/interfaces.ts diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 new file mode 100644 index 00000000..7242cbab --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 @@ -0,0 +1 @@ +{"code":"// The top level for @jsonql/event\r\nimport * as EventService from './src/event-service';\r\nexport default EventService;\r\n","references":["/home/joel/projects/open-source/jsonql/packages/event/src/event-service.ts"]} diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2 b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2 new file mode 100644 index 00000000..a375a083 --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2 @@ -0,0 +1 @@ +{"code":"\"use strict\";\r\n/*\r\n@TODO\r\ninterface configObj {\r\n logger?: any, // this is the shitty thing about TS\r\n keep? : Boolean,\r\n suspend?: Boolean\r\n}\r\n*/\r\n","references":[]} diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/ccc70771d4ab5581158090a0106a087a69a20bdb b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/ccc70771d4ab5581158090a0106a087a69a20bdb new file mode 100644 index 00000000..99aa20c5 --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/ccc70771d4ab5581158090a0106a087a69a20bdb @@ -0,0 +1 @@ +{"code":"\"use strict\";\r\n// declare all the enum(s) here then import back to the class file\r\nvar eventTypes;\r\n(function (eventTypes) {\r\n eventTypes[\"on\"] = \"on\";\r\n eventTypes[\"only\"] = \"only\";\r\n eventTypes[\"once\"] = \"once\";\r\n eventTypes[\"onlyOnce\"] = \"onlyOnce\";\r\n})(eventTypes || (eventTypes = {}));\r\n","references":[]} diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/efdd6301de655680bafd2f3af8ef7173a51d219e b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/efdd6301de655680bafd2f3af8ef7173a51d219e new file mode 100644 index 00000000..c91f7314 --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/efdd6301de655680bafd2f3af8ef7173a51d219e @@ -0,0 +1 @@ +{"code":"// The main class\r\nimport './interfaces.ts';\r\nimport './enums.ts';\r\nvar EventService = /** @class */ (function () {\r\n /**\r\n * Create EventService instance\r\n * @param {configObj} config configuration object\r\n * @return {void} nothing - don't do :void this fuck typescript up what a lot of shit\r\n */\r\n function EventService(logger) {\r\n // hack\r\n this.logger = typeof logger === 'function' ? logger : function () { };\r\n this.normalStore = new Map();\r\n this.lazyStore = new Map();\r\n }\r\n /**\r\n * Register your evt handler, note we don't check the type here,\r\n * we expect you to be sensible and know what you are doing.\r\n * @param {string} evt name of event\r\n * @param {function} callback bind method --> if it's array or not\r\n * @param {object} [context=null] to execute this call in\r\n * @return {number} the size of the store\r\n */\r\n EventService.prototype.on = function (evt, callback, context) {\r\n if (context === void 0) { context = null; }\r\n var type = 'on';\r\n };\r\n return EventService;\r\n}());\r\nexport default EventService;\r\n","references":[]} diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/semanticDiagnostics/cache/555348b18b3b7a2564f189a577b79953ff802452 b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 similarity index 100% rename from packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/semanticDiagnostics/cache/555348b18b3b7a2564f189a577b79953ff802452 rename to packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/syntacticDiagnostics/cache/555348b18b3b7a2564f189a577b79953ff802452 b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2 similarity index 100% rename from packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/syntacticDiagnostics/cache/555348b18b3b7a2564f189a577b79953ff802452 rename to packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2 diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/ccc70771d4ab5581158090a0106a087a69a20bdb b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/ccc70771d4ab5581158090a0106a087a69a20bdb new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/ccc70771d4ab5581158090a0106a087a69a20bdb @@ -0,0 +1 @@ +[] diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/efdd6301de655680bafd2f3af8ef7173a51d219e b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/efdd6301de655680bafd2f3af8ef7173a51d219e new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/efdd6301de655680bafd2f3af8ef7173a51d219e @@ -0,0 +1 @@ +[] diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 @@ -0,0 +1 @@ +[] diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2 b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2 new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2 @@ -0,0 +1 @@ +[] diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/ccc70771d4ab5581158090a0106a087a69a20bdb b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/ccc70771d4ab5581158090a0106a087a69a20bdb new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/ccc70771d4ab5581158090a0106a087a69a20bdb @@ -0,0 +1 @@ +[] diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/efdd6301de655680bafd2f3af8ef7173a51d219e b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/efdd6301de655680bafd2f3af8ef7173a51d219e new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/efdd6301de655680bafd2f3af8ef7173a51d219e @@ -0,0 +1 @@ +[] diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/0af14034b4858555dc8d4a8ed15d7f08de40eb36 b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/0af14034b4858555dc8d4a8ed15d7f08de40eb36 new file mode 100644 index 00000000..e69de29b diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/2c0fa7d7cfab72e02c2850c2c6402684f18bdc9d b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/2c0fa7d7cfab72e02c2850c2c6402684f18bdc9d new file mode 100644 index 00000000..e69de29b diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/609dd35a6af3197577b43708e69aa1be093cdb69 b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/609dd35a6af3197577b43708e69aa1be093cdb69 new file mode 100644 index 00000000..e69de29b diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/7f715d854fbb98c5fe1dab178b872994225edc80 b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/7f715d854fbb98c5fe1dab178b872994225edc80 new file mode 100644 index 00000000..e69de29b diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/ab2c42645f134bd03c75a1cfe9a94d3dd21da9fe b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/ab2c42645f134bd03c75a1cfe9a94d3dd21da9fe new file mode 100644 index 00000000..e69de29b diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/555348b18b3b7a2564f189a577b79953ff802452 b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/e6b6beb338c32055d84de9d86dd4a380e8952662 similarity index 49% rename from packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/555348b18b3b7a2564f189a577b79953ff802452 rename to packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/e6b6beb338c32055d84de9d86dd4a380e8952662 index d55be610..b1d54b4f 100644 --- a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/555348b18b3b7a2564f189a577b79953ff802452 +++ b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/e6b6beb338c32055d84de9d86dd4a380e8952662 @@ -1 +1 @@ -{"code":"// The main\r\n/*\r\ninterface configObj {\r\n logger?: any, // this is the shitty thing about TS\r\n keep? : Boolean,\r\n suspend?: Boolean\r\n}\r\n*/\r\nvar EventService = /** @class */ (function () {\r\n /**\r\n * Create EventService instance\r\n * @param {configObj} config configuration object\r\n * @return {void} nothing - don't do :void this fuck typescript up what a lot of shit\r\n */\r\n function EventService(logger) {\r\n // hack\r\n this.logger = typeof logger === 'function' ? logger : function () { };\r\n this.normalStore = new Map();\r\n this.lazyStore = new Map();\r\n }\r\n return EventService;\r\n}());\r\nexport default EventService;\r\n","references":[]} +{"code":"// The main\r\n/*\r\ninterface configObj {\r\n logger?: any, // this is the shitty thing about TS\r\n keep? : Boolean,\r\n suspend?: Boolean\r\n}\r\n*/\r\nvar EventService = /** @class */ (function () {\r\n /**\r\n * Create EventService instance\r\n * @param {configObj} config configuration object\r\n * @return {void} nothing - don't do :void this fuck typescript up what a lot of shit\r\n */\r\n function EventService(logger) {\r\n // hack\r\n this.logger = typeof logger === 'function' ? logger : function () { };\r\n this.normalStore = new Map();\r\n this.lazyStore = new Map();\r\n }\r\n /**\r\n * Register your evt handler, note we don't check the type here,\r\n * we expect you to be sensible and know what you are doing.\r\n * @param {string} evt name of event\r\n * @param {function} callback bind method --> if it's array or not\r\n * @param {object} [context=null] to execute this call in\r\n * @return {number} the size of the store\r\n */\r\n EventService.prototype.on = function (evt, callback, context) {\r\n if (context === void 0) { context = null; }\r\n var type = 'on';\r\n };\r\n return EventService;\r\n}());\r\nexport default EventService;\r\n","references":[]} diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/semanticDiagnostics/cache/e6b6beb338c32055d84de9d86dd4a380e8952662 b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/semanticDiagnostics/cache/e6b6beb338c32055d84de9d86dd4a380e8952662 new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/semanticDiagnostics/cache/e6b6beb338c32055d84de9d86dd4a380e8952662 @@ -0,0 +1 @@ +[] diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/syntacticDiagnostics/cache/e6b6beb338c32055d84de9d86dd4a380e8952662 b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/syntacticDiagnostics/cache/e6b6beb338c32055d84de9d86dd4a380e8952662 new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/syntacticDiagnostics/cache/e6b6beb338c32055d84de9d86dd4a380e8952662 @@ -0,0 +1 @@ +[] diff --git a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/code/cache/33be17634973f39fb3851a17d137ddadbbdf9bff b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/code/cache/33be17634973f39fb3851a17d137ddadbbdf9bff new file mode 100644 index 00000000..c1ba0bd8 --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/code/cache/33be17634973f39fb3851a17d137ddadbbdf9bff @@ -0,0 +1 @@ +{"code":"\"use strict\";\r\n","references":[]} diff --git a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/code/cache/51af2df8071ce95ae1e4eb6859380cd66e17fe3b b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/code/cache/51af2df8071ce95ae1e4eb6859380cd66e17fe3b new file mode 100644 index 00000000..9aff289b --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/code/cache/51af2df8071ce95ae1e4eb6859380cd66e17fe3b @@ -0,0 +1 @@ +{"code":"// The main\r\n/*\r\ninterface configObj {\r\n logger?: any, // this is the shitty thing about TS\r\n keep? : Boolean,\r\n suspend?: Boolean\r\n}\r\n*/\r\nimport './interfaces.ts';\r\nvar EventService = /** @class */ (function () {\r\n /**\r\n * Create EventService instance\r\n * @param {configObj} config configuration object\r\n * @return {void} nothing - don't do :void this fuck typescript up what a lot of shit\r\n */\r\n function EventService(logger) {\r\n // hack\r\n this.logger = typeof logger === 'function' ? logger : function () { };\r\n this.normalStore = new Map();\r\n this.lazyStore = new Map();\r\n }\r\n /**\r\n * Register your evt handler, note we don't check the type here,\r\n * we expect you to be sensible and know what you are doing.\r\n * @param {string} evt name of event\r\n * @param {function} callback bind method --> if it's array or not\r\n * @param {object} [context=null] to execute this call in\r\n * @return {number} the size of the store\r\n */\r\n EventService.prototype.on = function (evt, callback, context) {\r\n if (context === void 0) { context = null; }\r\n var type = 'on';\r\n };\r\n return EventService;\r\n}());\r\nexport default EventService;\r\n","references":[]} diff --git a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/code/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/code/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 new file mode 100644 index 00000000..7242cbab --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/code/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 @@ -0,0 +1 @@ +{"code":"// The top level for @jsonql/event\r\nimport * as EventService from './src/event-service';\r\nexport default EventService;\r\n","references":["/home/joel/projects/open-source/jsonql/packages/event/src/event-service.ts"]} diff --git a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/semanticDiagnostics/cache/33be17634973f39fb3851a17d137ddadbbdf9bff b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/semanticDiagnostics/cache/33be17634973f39fb3851a17d137ddadbbdf9bff new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/semanticDiagnostics/cache/33be17634973f39fb3851a17d137ddadbbdf9bff @@ -0,0 +1 @@ +[] diff --git a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/semanticDiagnostics/cache/51af2df8071ce95ae1e4eb6859380cd66e17fe3b b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/semanticDiagnostics/cache/51af2df8071ce95ae1e4eb6859380cd66e17fe3b new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/semanticDiagnostics/cache/51af2df8071ce95ae1e4eb6859380cd66e17fe3b @@ -0,0 +1 @@ +[] diff --git a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/semanticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/semanticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/semanticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 @@ -0,0 +1 @@ +[] diff --git a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/syntacticDiagnostics/cache/33be17634973f39fb3851a17d137ddadbbdf9bff b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/syntacticDiagnostics/cache/33be17634973f39fb3851a17d137ddadbbdf9bff new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/syntacticDiagnostics/cache/33be17634973f39fb3851a17d137ddadbbdf9bff @@ -0,0 +1 @@ +[] diff --git a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/syntacticDiagnostics/cache/51af2df8071ce95ae1e4eb6859380cd66e17fe3b b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/syntacticDiagnostics/cache/51af2df8071ce95ae1e4eb6859380cd66e17fe3b new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/syntacticDiagnostics/cache/51af2df8071ce95ae1e4eb6859380cd66e17fe3b @@ -0,0 +1 @@ +[] diff --git a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/syntacticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/syntacticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/syntacticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 @@ -0,0 +1 @@ +[] diff --git a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/0af14034b4858555dc8d4a8ed15d7f08de40eb36 b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/0af14034b4858555dc8d4a8ed15d7f08de40eb36 new file mode 100644 index 00000000..e69de29b diff --git a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/2c0fa7d7cfab72e02c2850c2c6402684f18bdc9d b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/2c0fa7d7cfab72e02c2850c2c6402684f18bdc9d new file mode 100644 index 00000000..e69de29b diff --git a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/609dd35a6af3197577b43708e69aa1be093cdb69 b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/609dd35a6af3197577b43708e69aa1be093cdb69 new file mode 100644 index 00000000..e69de29b diff --git a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/7f715d854fbb98c5fe1dab178b872994225edc80 b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/7f715d854fbb98c5fe1dab178b872994225edc80 new file mode 100644 index 00000000..e69de29b diff --git a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/ab2c42645f134bd03c75a1cfe9a94d3dd21da9fe b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/ab2c42645f134bd03c75a1cfe9a94d3dd21da9fe new file mode 100644 index 00000000..e69de29b diff --git a/packages/event/dist/index.es.js b/packages/event/dist/index.es.js index cc6cf2cb..c24efe5b 100644 --- a/packages/event/dist/index.es.js +++ b/packages/event/dist/index.es.js @@ -1,11 +1,13 @@ -// The main -/* -interface configObj { - logger?: any, // this is the shitty thing about TS - keep? : Boolean, - suspend?: Boolean -} -*/ +// declare all the enum(s) here then import back to the class file +var eventTypes; +(function (eventTypes) { + eventTypes["on"] = "on"; + eventTypes["only"] = "only"; + eventTypes["once"] = "once"; + eventTypes["onlyOnce"] = "onlyOnce"; +})(eventTypes || (eventTypes = {})); + +// The main class var EventService = /** @class */ (function () { /** * Create EventService instance @@ -18,6 +20,16 @@ var EventService = /** @class */ (function () { this.normalStore = new Map(); this.lazyStore = new Map(); } + /** + * 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 + */ + EventService.prototype.on = function (evt, callback, context) { + }; return EventService; }()); diff --git a/packages/event/dist/index.js b/packages/event/dist/index.js index 4bec91e0..fbc03ffb 100644 --- a/packages/event/dist/index.js +++ b/packages/event/dist/index.js @@ -1,13 +1,15 @@ 'use strict'; -// The main -/* -interface configObj { - logger?: any, // this is the shitty thing about TS - keep? : Boolean, - suspend?: Boolean -} -*/ +// declare all the enum(s) here then import back to the class file +var eventTypes; +(function (eventTypes) { + eventTypes["on"] = "on"; + eventTypes["only"] = "only"; + eventTypes["once"] = "once"; + eventTypes["onlyOnce"] = "onlyOnce"; +})(eventTypes || (eventTypes = {})); + +// The main class var EventService = /** @class */ (function () { /** * Create EventService instance @@ -20,6 +22,16 @@ var EventService = /** @class */ (function () { this.normalStore = new Map(); this.lazyStore = new Map(); } + /** + * 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 + */ + EventService.prototype.on = function (evt, callback, context) { + }; return EventService; }()); diff --git a/packages/event/src/enums.ts b/packages/event/src/enums.ts new file mode 100644 index 00000000..6ddb113b --- /dev/null +++ b/packages/event/src/enums.ts @@ -0,0 +1,8 @@ +// declare all the enum(s) here then import back to the class file + +enum eventTypes { + on = 'on', + only = 'only', + once = 'once', + onlyOnce = 'onlyOnce' +} diff --git a/packages/event/src/event-service.js b/packages/event/src/event-service.js new file mode 100644 index 00000000..a75d6c05 --- /dev/null +++ b/packages/event/src/event-service.js @@ -0,0 +1,598 @@ +// this is the new implementation without the hash key +// only using Map and Set instead +import { + NB_EVENT_SERVICE_PRIVATE_STORE, + NB_EVENT_SERVICE_PRIVATE_LAZY +} from './store' +import genHaskKey from './hash-code.js' +// export +export default class EventService { + /** + * class constructor + */ + constructor(config = {}) { + if (config.logger && typeof config.logger === 'function') { + this.logger = config.logger; + } + this.keep = config.keep; + this.suspend = false; + // for the $done setter + this.result = config.keep ? [] : null; + // we need to init the store first otherwise it could be a lot of checking later + this.normalStore = new Map() + this.lazyStore = new Map() + } + + /** + * logger function for overwrite + */ + logger() {} + + ////////////////////////// + // PUBLIC METHODS // + ////////////////////////// + + /** + * 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 + */ + $on(evt , callback , context = null) { + const type = 'on'; + this.validate(evt, callback) + // first need to check if this evt is in lazy store + let 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 + let size = 0; + lazyStoreContent.forEach(content => { + let [ payload, ctx, t ] = content; + 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) + size += this.addToNormalStore(evt, type, callback, context || ctx) + }) + 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 {boolean} result + */ + $once(evt , callback , context = null) { + this.validate(evt, callback) + const type = 'once'; + let lazyStoreContent = this.takeFromStore(evt) + // this is normal register before call $trigger + let 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) + const list = Array.from(lazyStoreContent) + // should never have more than 1 + const [ payload, ctx, t ] = list[0] + 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) + } + } + + /** + * 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 + */ + $only(evt, callback, context = null) { + this.validate(evt, callback) + const type = 'only'; + let added = false; + let lazyStoreContent = this.takeFromStore(evt) + // this is normal register before call $trigger + let 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`) + const list = Array.from(lazyStoreContent) + // $only allow to trigger this multiple time on the single handler + list.forEach( l => { + const [ payload, ctx, t ] = l; + 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 {void} + */ + $onlyOnce(evt, callback, context = null) { + this.validate(evt, callback) + const type = 'onlyOnce'; + let added = false; + let lazyStoreContent = this.takeFromStore(evt) + // this is normal register before call $trigger + let 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) + const list = Array.from(lazyStoreContent) + // should never have more than 1 + const [ payload, ctx, t ] = list[0] + 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 {} + */ + $replace(evt, callback, context = null, type = 'on') { + if (this.validateType(type)) { + this.$off(evt) + let method = this['$' + type] + return Reflect.apply(method, this, [evt, callback, context]) + } + throw new Error(`${type} is not supported!`) + } + + /** + * 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 + */ + $trigger(evt , payload = [] , context = null, type = false) { + this.validateEvt(evt) + let found = 0; + // first check the normal store + let nStore = this.normalStore; + this.logger('$trigger', nStore) + if (nStore.has(evt)) { + this.logger('$trigger', evt, 'found') + let nSet = Array.from(nStore.get(evt)) + let ctn = nSet.length; + let hasOnce = false; + let hasOnly = false; + for (let i=0; i < ctn; ++i) { + ++found; + // this.logger('found', found) + let [ _, callback, ctx, type ] = nSet[i] + this.run(callback, payload, context || ctx) + if (type === 'once' || type === '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 + */ + $call(evt, params, type = false, context = null) { + let 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 + */ + $off(evt) { + this.validateEvt(evt) + let stores = [ this.lazyStore, this.normalStore ] + let found = false; + stores.forEach(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 + */ + $get(evt, full = false) { + this.validateEvt(evt) + let store = this.normalStore; + if (store.has(evt)) { + return Array + .from(store.get(evt)) + .map( l => { + if (full) { + return l; + } + let [key, callback, ] = l; + return callback; + }) + } + return false; + } + + /** + * Holding off all the event firing and put them back into the lazy store + * until the suspend been lifted + * @param {string} [type=all] what type of event should be suspended + * @return {void} + */ + $suspend(type = 'all') { + this.suspend = type === 'all' ? true : this.validateType(type); + } + + /** + * Lifted the suspend + * @return {void} + */ + $resume() { + this.suspend = false; + } + + /** + * store the return result from the run + * @param {*} value whatever return from callback + */ + set $done(value) { + 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() { + if (this.keep) { + this.logger(this.result) + return this.result[this.result.length - 1] + } + return this.result; + } + + ///////////////////////////// + // PRIVATE METHODS // + ///////////////////////////// + + /** + * validate the event name + * @param {string} evt event name + * @return {boolean} true when OK + */ + validateEvt(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 + */ + validate(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 + */ + validateType(type) { + const types = ['on', 'only', 'once', 'onlyOnce'] + return !!types.filter(t => type === t).length; + } + + /** + * 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 + */ + run(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 + * @param {string} [storeName = lazyStore] name of store + * @return {object|boolean} content or false on not found + */ + takeFromStore(evt, storeName = 'lazyStore') { + let store = this[storeName]; // it could be empty at this point + if (store) { + this.logger('takeFromStore', storeName, store) + if (store.has(evt)) { + let content = store.get(evt) + this.logger('takeFromStore', content) + store.delete(evt) + return content; + } + return false; + } + throw new Error(`${storeName} is not supported!`) + } + + /** + * 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 + */ + addToStore(store, evt, ...args) { + 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 + */ + checkContentExist(args, fnSet) { + let list = Array.from(fnSet) + return !!list.filter(l => { + let [hash,] = l; + if (hash === args[0]) { + return true; + } + return false; + }).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 + */ + checkTypeInStore(evtName, type) { + this.validateEvt(evtName) + this.validateEvt(type) + 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(list => { + let [ ,,,t ] = list; + 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 + */ + checkTypeInLazyStore(evtName, type) { + this.validateEvt(evtName) + this.validateEvt(type) + let store = this.lazyStore.get(evtName) + this.logger('checkTypeInLazyStore', store) + if (store) { + return !!Array + .from(store) + .filter(l => { + let [,,t] = l; + return t !== type; + }).length + } + 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} size of the store + */ + addToNormalStore(evt, type, callback, 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`) + let key = this.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; + } + 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} [context=null] the context the callback execute in + * @param {string} [type=false] register a type so no other type can add to this evt + * @return {number} size of the store + */ + addToLazyStore(evt, payload = [], context = null, type = false) { + // 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 = [this.lazyStore, evt, this.toArray(payload), context] + if (type) { + args.push(type) + } + let [_store, size] = Reflect.apply(this.addToStore, this, args) + this.lazyStore = _store; + return size; + } + + /** + * make sure we store the argument correctly + * @param {*} arg could be array + * @return {array} make sured + */ + toArray(arg) { + return Array.isArray(arg) ? arg : [arg]; + } + + /** + * setter to store the Set in private + * @param {object} obj a Set + */ + set normalStore(obj) { + NB_EVENT_SERVICE_PRIVATE_STORE.set(this, obj) + } + + /** + * @return {object} Set object + */ + get normalStore() { + return NB_EVENT_SERVICE_PRIVATE_STORE.get(this) + } + + /** + * setter to store the Set in lazy store + * @param {object} obj a Set + */ + set lazyStore(obj) { + NB_EVENT_SERVICE_PRIVATE_LAZY.set(this , obj) + } + + /** + * @return {object} the lazy store Set + */ + get lazyStore() { + return NB_EVENT_SERVICE_PRIVATE_LAZY.get(this) + } + + /** + * generate a hashKey to identify the function call + * The build-in store some how could store the same values! + * @param {function} fn the converted to string function + * @return {string} hashKey + */ + hashFnToKey(fn) { + return genHaskKey(fn.toString()) + ''; + } + +} diff --git a/packages/event/src/event-service.ts b/packages/event/src/event-service.ts index 6e4b8793..ef169d43 100644 --- a/packages/event/src/event-service.ts +++ b/packages/event/src/event-service.ts @@ -1,16 +1,12 @@ -// The main -/* -interface configObj { - logger?: any, // this is the shitty thing about TS - keep? : Boolean, - suspend?: Boolean -} -*/ +// The main class +import './interfaces.ts'; +import './enums.ts'; + export default class EventService { - private normalStore: any; - private lazyStore: any; - // private logger: (...args) => void; + private normalStore: Map; + private lazyStore: Map; + private logger: any; /** @@ -22,8 +18,21 @@ export default class EventService { // hack this.logger = typeof logger === 'function' ? logger : () => {}; - this.normalStore = new Map(); - this.lazyStore = new Map(); + this.normalStore = new Map() + this.lazyStore = new Map() + } + + /** + * 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 + */ + on(evt: String, callback: myCallbackType, context: any = null) { + const type = 'on'; + } diff --git a/packages/event/src/interfaces.ts b/packages/event/src/interfaces.ts new file mode 100644 index 00000000..82154f01 --- /dev/null +++ b/packages/event/src/interfaces.ts @@ -0,0 +1,14 @@ +// declare all the interfaces here +// then re-use inside the class +interface myCallbackType { + (myArgument: string): void +} + +/* +@TODO +interface configObj { + logger?: any, // this is the shitty thing about TS + keep? : Boolean, + suspend?: Boolean +} +*/ -- Gitee From 7ee94dd25cd3d4aeda37e9a7af667d425aa2581c Mon Sep 17 00:00:00 2001 From: Joel Chu Date: Sun, 14 Jul 2019 18:22:27 +0800 Subject: [PATCH 10/23] finally resolve all the stupid type problem with the filter and map function --- .../efdd6301de655680bafd2f3af8ef7173a51d219e | 1 - .../efdd6301de655680bafd2f3af8ef7173a51d219e | 1 - .../efdd6301de655680bafd2f3af8ef7173a51d219e | 1 - packages/event/dist/index.es.js | 254 +++++++++++++++++- packages/event/dist/index.js | 254 +++++++++++++++++- packages/event/src/event-service.ts | 249 ++++++++++++++++- packages/event/src/hash-code.ts | 5 +- 7 files changed, 754 insertions(+), 11 deletions(-) delete mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/efdd6301de655680bafd2f3af8ef7173a51d219e delete mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/efdd6301de655680bafd2f3af8ef7173a51d219e delete mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/efdd6301de655680bafd2f3af8ef7173a51d219e diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/efdd6301de655680bafd2f3af8ef7173a51d219e b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/efdd6301de655680bafd2f3af8ef7173a51d219e deleted file mode 100644 index c91f7314..00000000 --- a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/efdd6301de655680bafd2f3af8ef7173a51d219e +++ /dev/null @@ -1 +0,0 @@ -{"code":"// The main class\r\nimport './interfaces.ts';\r\nimport './enums.ts';\r\nvar EventService = /** @class */ (function () {\r\n /**\r\n * Create EventService instance\r\n * @param {configObj} config configuration object\r\n * @return {void} nothing - don't do :void this fuck typescript up what a lot of shit\r\n */\r\n function EventService(logger) {\r\n // hack\r\n this.logger = typeof logger === 'function' ? logger : function () { };\r\n this.normalStore = new Map();\r\n this.lazyStore = new Map();\r\n }\r\n /**\r\n * Register your evt handler, note we don't check the type here,\r\n * we expect you to be sensible and know what you are doing.\r\n * @param {string} evt name of event\r\n * @param {function} callback bind method --> if it's array or not\r\n * @param {object} [context=null] to execute this call in\r\n * @return {number} the size of the store\r\n */\r\n EventService.prototype.on = function (evt, callback, context) {\r\n if (context === void 0) { context = null; }\r\n var type = 'on';\r\n };\r\n return EventService;\r\n}());\r\nexport default EventService;\r\n","references":[]} diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/efdd6301de655680bafd2f3af8ef7173a51d219e b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/efdd6301de655680bafd2f3af8ef7173a51d219e deleted file mode 100644 index fe51488c..00000000 --- a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/efdd6301de655680bafd2f3af8ef7173a51d219e +++ /dev/null @@ -1 +0,0 @@ -[] diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/efdd6301de655680bafd2f3af8ef7173a51d219e b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/efdd6301de655680bafd2f3af8ef7173a51d219e deleted file mode 100644 index fe51488c..00000000 --- a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/efdd6301de655680bafd2f3af8ef7173a51d219e +++ /dev/null @@ -1 +0,0 @@ -[] diff --git a/packages/event/dist/index.es.js b/packages/event/dist/index.es.js index c24efe5b..4d5bd3c9 100644 --- a/packages/event/dist/index.es.js +++ b/packages/event/dist/index.es.js @@ -7,7 +7,19 @@ var eventTypes; eventTypes["onlyOnce"] = "onlyOnce"; })(eventTypes || (eventTypes = {})); +/** + * 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) + ""; +} + // The main class +// main var EventService = /** @class */ (function () { /** * Create EventService instance @@ -19,6 +31,8 @@ var EventService = /** @class */ (function () { 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, @@ -28,7 +42,245 @@ var EventService = /** @class */ (function () { * @param {object} [context=null] to execute this call in * @return {number} the size of the store */ - EventService.prototype.on = function (evt, callback, context) { + EventService.prototype.$on = function (evt, callback, context) { + return 1; + }; + Object.defineProperty(EventService.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 + */ + EventService.prototype.mapToArr = function (mapObj) { + return Array.from(mapObj); + }; + /** + * make sure we store the argument correctly + * @param {*} arg could be array + * @return {array} make sured + */ + EventService.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 + */ + EventService.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 + * @param {string} [storeName = lazyStore] name of store + * @return {object|boolean} content or false on not found + */ + EventService.prototype.takeFromStore = function (evt, store) { + // 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; + } + 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) + */ + EventService.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 + */ + EventService.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; + }; + /** + * 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 + */ + EventService.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 + * @param {string} type the type to check + * @return {boolean} true you can add, false then you can't add this type + */ + EventService.prototype.checkTypeInStore = function (evtName, type) { + var all = this.getByEvt(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 + */ + EventService.prototype.checkTypeInLazyStore = function (evtName, type) { + var store = this.lazyStore.get(evtName); + this.logger('checkTypeInLazyStore', store); + if (store) { + return !!Array + .from(store) + .filter(function (value, index, array) { + var t = value[2]; + return t !== type; + }).length; + } + 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 + */ + EventService.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"); + 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; + } + 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} [type=false] register a type so no other type can add to this evt + * @return {number} size of the store + */ + EventService.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; }; return EventService; }()); diff --git a/packages/event/dist/index.js b/packages/event/dist/index.js index fbc03ffb..95471b22 100644 --- a/packages/event/dist/index.js +++ b/packages/event/dist/index.js @@ -9,7 +9,19 @@ var eventTypes; eventTypes["onlyOnce"] = "onlyOnce"; })(eventTypes || (eventTypes = {})); +/** + * 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) + ""; +} + // The main class +// main var EventService = /** @class */ (function () { /** * Create EventService instance @@ -21,6 +33,8 @@ var EventService = /** @class */ (function () { 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, @@ -30,7 +44,245 @@ var EventService = /** @class */ (function () { * @param {object} [context=null] to execute this call in * @return {number} the size of the store */ - EventService.prototype.on = function (evt, callback, context) { + EventService.prototype.$on = function (evt, callback, context) { + return 1; + }; + Object.defineProperty(EventService.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 + */ + EventService.prototype.mapToArr = function (mapObj) { + return Array.from(mapObj); + }; + /** + * make sure we store the argument correctly + * @param {*} arg could be array + * @return {array} make sured + */ + EventService.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 + */ + EventService.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 + * @param {string} [storeName = lazyStore] name of store + * @return {object|boolean} content or false on not found + */ + EventService.prototype.takeFromStore = function (evt, store) { + // 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; + } + 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) + */ + EventService.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 + */ + EventService.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; + }; + /** + * 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 + */ + EventService.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 + * @param {string} type the type to check + * @return {boolean} true you can add, false then you can't add this type + */ + EventService.prototype.checkTypeInStore = function (evtName, type) { + var all = this.getByEvt(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 + */ + EventService.prototype.checkTypeInLazyStore = function (evtName, type) { + var store = this.lazyStore.get(evtName); + this.logger('checkTypeInLazyStore', store); + if (store) { + return !!Array + .from(store) + .filter(function (value, index, array) { + var t = value[2]; + return t !== type; + }).length; + } + 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 + */ + EventService.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"); + 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; + } + 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} [type=false] register a type so no other type can add to this evt + * @return {number} size of the store + */ + EventService.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; }; return EventService; }()); diff --git a/packages/event/src/event-service.ts b/packages/event/src/event-service.ts index ef169d43..2d6148bd 100644 --- a/packages/event/src/event-service.ts +++ b/packages/event/src/event-service.ts @@ -1,13 +1,16 @@ // The main class import './interfaces.ts'; import './enums.ts'; - +import hashFnToKey from './hash-code'; +// main export default class EventService { - + // public props + keep: boolean; + // private props private normalStore: Map; private lazyStore: Map; - private logger: any; + private result: any[]; /** * Create EventService instance @@ -20,6 +23,9 @@ export default class EventService { this.normalStore = new Map() this.lazyStore = new Map() + + this.keep = false; + this.result = []; } /** @@ -30,12 +36,247 @@ export default class EventService { * @param {object} [context=null] to execute this call in * @return {number} the size of the store */ - on(evt: String, callback: myCallbackType, context: any = null) { + public $on(evt: string, callback: myCallbackType, context: any = null): number { const type = 'on'; + return 1; + } + + + //////////////////////////////// + // 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() { + if (this.keep) { + this.logger(this.result) + return this.result[this.result.length - 1] + } + 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]; + } + /** + * 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 + * @param {string} [storeName = lazyStore] name of store + * @return {object|boolean} content or false on not found + */ + private takeFromStore(evt: string, store: any) { + // 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; + } + 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; + } + + /** + * 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 + * @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 { + let all = this.getByEvt(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 { + let store = this.lazyStore.get(evtName) + this.logger('checkTypeInLazyStore', store) + if (store) { + return !!Array + .from(store) + .filter((value: any, index: number, array: any[]): boolean => { + let [,,t] = value; + return t !== type; + }).length + } + 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: string, 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`) + 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; + } + 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} [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): 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; + } } diff --git a/packages/event/src/hash-code.ts b/packages/event/src/hash-code.ts index 512e1a49..8b1cc8a8 100644 --- a/packages/event/src/hash-code.ts +++ b/packages/event/src/hash-code.ts @@ -1,9 +1,10 @@ /** * generate a 32bit hash based on the function.toString() * _from http://stackoverflow.com/questions/7616461/generate-a-hash-_from-string-in-javascript-jquery - * @param {string} s the converted to string function + * @param {function} fn the converted to string function * @return {string} the hashed function string */ -export default function hashCode(s: String): String { +export default function hashCode(fn: Function): String { + let s = fn.toString() return s.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a},0) + "" } -- Gitee From 9dce7f17d6684c8f0a12c38dfa206654f4cfbc95 Mon Sep 17 00:00:00 2001 From: Joel Chu Date: Sun, 14 Jul 2019 20:35:08 +0800 Subject: [PATCH 11/23] Should ignore that rpt2-cache folder --- packages/event/.gitignore | 1 + .../7035ba062321d6588f0b6a1ef51cc75a58981cbc | 1 + .../b08c37772ef58648f81f89ddd7be1af0fd2147b5 | 1 + .../7035ba062321d6588f0b6a1ef51cc75a58981cbc | 1 + .../b08c37772ef58648f81f89ddd7be1af0fd2147b5 | 1 + .../7035ba062321d6588f0b6a1ef51cc75a58981cbc | 1 + .../b08c37772ef58648f81f89ddd7be1af0fd2147b5 | 1 + packages/event/src/enums.ts | 10 +- packages/event/src/event-service.ts | 237 +++++++++++++++++- 9 files changed, 247 insertions(+), 7 deletions(-) create mode 100644 packages/event/.gitignore create mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/7035ba062321d6588f0b6a1ef51cc75a58981cbc create mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/b08c37772ef58648f81f89ddd7be1af0fd2147b5 create mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/7035ba062321d6588f0b6a1ef51cc75a58981cbc create mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/b08c37772ef58648f81f89ddd7be1af0fd2147b5 create mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/7035ba062321d6588f0b6a1ef51cc75a58981cbc create mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/b08c37772ef58648f81f89ddd7be1af0fd2147b5 diff --git a/packages/event/.gitignore b/packages/event/.gitignore new file mode 100644 index 00000000..d8a458d6 --- /dev/null +++ b/packages/event/.gitignore @@ -0,0 +1 @@ +./rpt2_cache diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/7035ba062321d6588f0b6a1ef51cc75a58981cbc b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/7035ba062321d6588f0b6a1ef51cc75a58981cbc new file mode 100644 index 00000000..f2232a3f --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/7035ba062321d6588f0b6a1ef51cc75a58981cbc @@ -0,0 +1 @@ +{"code":"/**\r\n * generate a 32bit hash based on the function.toString()\r\n * _from http://stackoverflow.com/questions/7616461/generate-a-hash-_from-string-in-javascript-jquery\r\n * @param {function} fn the converted to string function\r\n * @return {string} the hashed function string\r\n */\r\nexport default function hashCode(fn) {\r\n var s = fn.toString();\r\n return s.split(\"\").reduce(function (a, b) { a = ((a << 5) - a) + b.charCodeAt(0); return a & a; }, 0) + \"\";\r\n}\r\n","references":[]} diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/b08c37772ef58648f81f89ddd7be1af0fd2147b5 b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/b08c37772ef58648f81f89ddd7be1af0fd2147b5 new file mode 100644 index 00000000..bf6c4b20 --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/b08c37772ef58648f81f89ddd7be1af0fd2147b5 @@ -0,0 +1 @@ +{"code":"// The main class\r\nimport './interfaces.ts';\r\nimport './enums.ts';\r\nimport hashFnToKey from './hash-code';\r\n// main\r\nvar EventService = /** @class */ (function () {\r\n /**\r\n * Create EventService instance\r\n * @param {configObj} config configuration object\r\n * @return {void} nothing - don't do :void this fuck typescript up what a lot of shit\r\n */\r\n function EventService(logger) {\r\n // hack\r\n this.logger = typeof logger === 'function' ? logger : function () { };\r\n this.normalStore = new Map();\r\n this.lazyStore = new Map();\r\n this.keep = false;\r\n this.result = [];\r\n }\r\n /**\r\n * Register your evt handler, note we don't check the type here,\r\n * we expect you to be sensible and know what you are doing.\r\n * @param {string} evt name of event\r\n * @param {function} callback bind method --> if it's array or not\r\n * @param {object} [context=null] to execute this call in\r\n * @return {number} the size of the store\r\n */\r\n EventService.prototype.$on = function (evt, callback, context) {\r\n if (context === void 0) { context = null; }\r\n var type = 'on';\r\n return 1;\r\n };\r\n Object.defineProperty(EventService.prototype, \"$done\", {\r\n /**\r\n * @TODO is there any real use with the keep prop?\r\n * getter for $done\r\n * @return {*} whatever last store result\r\n */\r\n get: function () {\r\n if (this.keep) {\r\n this.logger(this.result);\r\n return this.result[this.result.length - 1];\r\n }\r\n return this.result;\r\n },\r\n ////////////////////////////////\r\n // SETTER / GETTER //\r\n ////////////////////////////////\r\n /**\r\n * store the return result from the run\r\n * @param {*} value whatever return from callback\r\n */\r\n set: function (value) {\r\n this.logger('set $done', value);\r\n if (this.keep) {\r\n this.result.push(value);\r\n }\r\n else {\r\n this.result = value;\r\n }\r\n },\r\n enumerable: true,\r\n configurable: true\r\n });\r\n /////////////////////////////////\r\n // PRIVATE / PROTECTED METHODS //\r\n /////////////////////////////////\r\n /**\r\n * @param {any} mapObj the Map object\r\n * @return {array} transform to array to work with\r\n */\r\n EventService.prototype.mapToArr = function (mapObj) {\r\n return Array.from(mapObj);\r\n };\r\n /**\r\n * make sure we store the argument correctly\r\n * @param {*} arg could be array\r\n * @return {array} make sured\r\n */\r\n EventService.prototype.toArray = function (arg) {\r\n return Array.isArray(arg) ? arg : [arg];\r\n };\r\n /**\r\n * Run the callback\r\n * @param {function} callback function to execute\r\n * @param {array} payload for callback\r\n * @param {object} ctx context or null\r\n * @return {void} the result store in $done\r\n */\r\n EventService.prototype.run = function (callback, payload, ctx) {\r\n this.logger('run', callback, payload, ctx);\r\n this.$done = Reflect.apply(callback, ctx, this.toArray(payload));\r\n };\r\n /**\r\n * Take the content out and remove it from store id by the name\r\n * @param {string} evt event name\r\n * @param {string} [storeName = lazyStore] name of store\r\n * @return {object|boolean} content or false on not found\r\n */\r\n EventService.prototype.takeFromStore = function (evt, store) {\r\n // let store = this[storeName]; // it could be empty at this point\r\n this.logger('takeFromStore', store);\r\n if (store.has(evt)) {\r\n var content = store.get(evt);\r\n this.logger('takeFromStore', content);\r\n store.delete(evt);\r\n return content;\r\n }\r\n return false;\r\n };\r\n /**\r\n * The add to store step is similar so make it generic for resuse\r\n * @param {object} store which store to use\r\n * @param {string} evt event name\r\n * @param {spread} args because the lazy store and normal store store different things\r\n * @return {array} store and the size of the store (cheated @TODO)\r\n */\r\n EventService.prototype.addToStore = function (store, evt) {\r\n var args = [];\r\n for (var _i = 2; _i < arguments.length; _i++) {\r\n args[_i - 2] = arguments[_i];\r\n }\r\n var fnSet;\r\n if (store.has(evt)) {\r\n this.logger('addToStore', evt + \" existed\");\r\n fnSet = store.get(evt);\r\n }\r\n else {\r\n this.logger('addToStore', \"create new Set for \" + evt);\r\n // this is new\r\n fnSet = new Set();\r\n }\r\n // lazy only store 2 items - this is not the case in V1.6.0 anymore\r\n // we need to check the first parameter is string or not\r\n if (args.length > 2) {\r\n if (Array.isArray(args[0])) { // lazy store\r\n // check if this type of this event already register in the lazy store\r\n var t = args[2];\r\n if (!this.checkTypeInLazyStore(evt, t)) {\r\n fnSet.add(args);\r\n }\r\n }\r\n else {\r\n if (!this.checkContentExist(args, fnSet)) {\r\n this.logger('addToStore', \"insert new\", args);\r\n fnSet.add(args);\r\n }\r\n }\r\n }\r\n else { // add straight to lazy store\r\n fnSet.add(args);\r\n }\r\n store.set(evt, fnSet);\r\n return [store, fnSet.size];\r\n };\r\n /**\r\n * @param {array} args for compare\r\n * @param {object} fnSet A Set to search from\r\n * @return {boolean} true on exist\r\n */\r\n EventService.prototype.checkContentExist = function (args, fnSet) {\r\n var list = this.mapToArr(fnSet);\r\n return !!list\r\n .filter(function (value, index, array) {\r\n var hash = value[0];\r\n return hash === args[0];\r\n }).length;\r\n };\r\n /**\r\n * return all the listener from the event\r\n * @param {string} evtName event name\r\n * @param {boolean} [full=false] if true then return the entire content\r\n * @return {array|boolean} listerner(s) or false when not found\r\n */\r\n EventService.prototype.getByEvt = function (evtName) {\r\n var store = this.normalStore;\r\n if (store.has(evtName)) {\r\n return Array\r\n .from(store.get(evtName))\r\n .map(function (l) { return l; });\r\n }\r\n return false;\r\n };\r\n /**\r\n * get the existing type to make sure no mix type add to the same store\r\n * @param {string} evtName event name\r\n * @param {string} type the type to check\r\n * @return {boolean} true you can add, false then you can't add this type\r\n */\r\n EventService.prototype.checkTypeInStore = function (evtName, type) {\r\n var all = this.getByEvt(evtName);\r\n if (all === false) {\r\n // pristine it means you can add\r\n return true;\r\n }\r\n // it should only have ONE type in ONE event store\r\n return !all\r\n .filter(function (value, index, array) {\r\n var t = value[3];\r\n return type !== t;\r\n }).length;\r\n };\r\n /**\r\n * This is checking just the lazy store because the structure is different\r\n * therefore we need to use a new method to check it\r\n * @param {string} evtName event name\r\n * @param {string} type the types of allow event\r\n * @return {boolean} true OK\r\n */\r\n EventService.prototype.checkTypeInLazyStore = function (evtName, type) {\r\n var store = this.lazyStore.get(evtName);\r\n this.logger('checkTypeInLazyStore', store);\r\n if (store) {\r\n return !!Array\r\n .from(store)\r\n .filter(function (value, index, array) {\r\n var t = value[2];\r\n return t !== type;\r\n }).length;\r\n }\r\n return false;\r\n };\r\n /**\r\n * wrapper to re-use the addToStore,\r\n * V1.3.0 add extra check to see if this type can add to this evt\r\n * @param {string} evt event name\r\n * @param {string} type on or once\r\n * @param {function} callback function\r\n * @param {object} context the context the function execute in or null\r\n * @return {number|boolean} size of the store, or false when the check type failed\r\n */\r\n EventService.prototype.addToNormalStore = function (evt, type, callback, context) {\r\n if (context === void 0) { context = null; }\r\n this.logger('addToNormalStore', evt, type, 'add to normal store');\r\n // @TODO we need to check the existing store for the type first!\r\n if (this.checkTypeInStore(evt, type)) {\r\n this.logger(type + \" can add to \" + evt + \" store\");\r\n var key = hashFnToKey(callback);\r\n var args = [this.normalStore, evt, key, callback, context, type];\r\n var _a = Reflect.apply(this.addToStore, this, args), _store = _a[0], size = _a[1];\r\n this.normalStore = _store;\r\n return size;\r\n }\r\n return false;\r\n };\r\n /**\r\n * Add to lazy store this get calls when the callback is not register yet\r\n * so we only get a payload object or even nothing\r\n * @param {string} evt event name\r\n * @param {array} payload of arguments or empty if there is none\r\n * @param {object} [ctx=null] the context the callback execute in\r\n * @param {string} [type=false] register a type so no other type can add to this evt\r\n * @return {number} size of the store\r\n */\r\n EventService.prototype.addToLazyStore = function (evt, payload, ctx, type) {\r\n if (payload === void 0) { payload = []; }\r\n if (ctx === void 0) { ctx = null; }\r\n // this is add in V1.6.0\r\n // when there is type then we will need to check if this already added in lazy store\r\n // and no other type can add to this lazy store\r\n var args = [];\r\n args.push(this.lazyStore, evt, this.toArray(payload), ctx);\r\n if (type) {\r\n args.push(type);\r\n }\r\n var _a = Reflect.apply(this.addToStore, this, args), _store = _a[0], size = _a[1];\r\n this.lazyStore = _store;\r\n return size;\r\n };\r\n return EventService;\r\n}());\r\nexport default EventService;\r\n","references":["/home/joel/projects/open-source/jsonql/packages/event/src/hash-code.ts"]} diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/7035ba062321d6588f0b6a1ef51cc75a58981cbc b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/7035ba062321d6588f0b6a1ef51cc75a58981cbc new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/7035ba062321d6588f0b6a1ef51cc75a58981cbc @@ -0,0 +1 @@ +[] diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/b08c37772ef58648f81f89ddd7be1af0fd2147b5 b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/b08c37772ef58648f81f89ddd7be1af0fd2147b5 new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/b08c37772ef58648f81f89ddd7be1af0fd2147b5 @@ -0,0 +1 @@ +[] diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/7035ba062321d6588f0b6a1ef51cc75a58981cbc b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/7035ba062321d6588f0b6a1ef51cc75a58981cbc new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/7035ba062321d6588f0b6a1ef51cc75a58981cbc @@ -0,0 +1 @@ +[] diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/b08c37772ef58648f81f89ddd7be1af0fd2147b5 b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/b08c37772ef58648f81f89ddd7be1af0fd2147b5 new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/b08c37772ef58648f81f89ddd7be1af0fd2147b5 @@ -0,0 +1 @@ +[] diff --git a/packages/event/src/enums.ts b/packages/event/src/enums.ts index 6ddb113b..e07ebf82 100644 --- a/packages/event/src/enums.ts +++ b/packages/event/src/enums.ts @@ -1,8 +1,8 @@ // declare all the enum(s) here then import back to the class file - +// it has to be numeric value ... what a pain enum eventTypes { - on = 'on', - only = 'only', - once = 'once', - onlyOnce = 'onlyOnce' + on = 1, + only = 2, + once = 3, + onlyOnce = 4 } diff --git a/packages/event/src/event-service.ts b/packages/event/src/event-service.ts index 2d6148bd..07975286 100644 --- a/packages/event/src/event-service.ts +++ b/packages/event/src/event-service.ts @@ -38,8 +38,221 @@ export default class EventService { */ public $on(evt: string, callback: myCallbackType, context: any = null): number { const type = 'on'; + // first need to check if this evt is in lazy store + let 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 1; + 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 + let size = 0; + lazyStoreContent.forEach((content: any[]): void => { + let [ payload, ctx, t ] = content; + 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) + size += this.addToNormalStore(evt, type, callback, context || ctx) + }) + 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 + */ + $once(evt: string , callback: myCallbackType , context: any = null): any { + const type = 'once'; + let lazyStoreContent = this.takeFromStore(evt) + // this is normal register before call $trigger + let 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) + const list = this.mapToArr(lazyStoreContent) + // should never have more than 1 + const [ payload, ctx, t ] = list[0] + 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 + */ + $only(evt: string, callback: myCallbackType, context: any = null): boolean { + const type = 'only'; + let added = false; + let lazyStoreContent = this.takeFromStore(evt) + // this is normal register before call $trigger + let 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`) + const list = this.mapToArr(lazyStoreContent) + // $only allow to trigger this multiple time on the single handler + list.forEach( l => { + const [ payload, ctx, t ] = l; + 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 + */ + $onlyOnce(evt: string, callback: myCallbackType, context: any = null): boolean { + this.validate(evt, callback) + const type = 'onlyOnce'; + let added = false; + let lazyStoreContent = this.takeFromStore(evt) + // this is normal register before call $trigger + let 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) + const list = this.mapToArr(lazyStoreContent) + // should never have more than 1 + const [ payload, ctx, t ] = list[0] + 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 + */ + $replace(evt: string, callback: myCallbackType, context: any = null, type: string = 'on'): any { + this.$off(evt) + // this will become a problem + let 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 + */ + $trigger(evt: string , payload: any[] = [] , context: any = null, type: string|boolean = false): number { + let found = 0; + // first check the normal store + let nStore = this.normalStore; + this.logger('$trigger', nStore) + if (nStore.has(evt)) { + this.logger('$trigger', evt, 'found') + let nSet = this.mapToArr(nStore.get(evt)) + let ctn = nSet.length; + let hasOnce = false; + let hasOnly = false; + for (let i=0; i < ctn; ++i) { + ++found; + // this.logger('found', found) + let [ _, callback, ctx, type ] = nSet[i] + this.run(callback, payload, context || ctx) + if (type === 'once' || type === '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 + */ + $call(evt: string, params: any[], type: string|boolean = false, context: any = null): any { + let 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 + */ + $off(evt: string): boolean { + this.validateEvt(evt) + let stores = [ this.lazyStore, this.normalStore ] + let found = false; + stores.forEach(store => { + if (store.has(evt)) { + found = true; + store.delete(evt) + } + }) + return found; } @@ -65,7 +278,7 @@ export default class EventService { * getter for $done * @return {*} whatever last store result */ - get $done() { + get $done(): any { if (this.keep) { this.logger(this.result) return this.result[this.result.length - 1] @@ -94,6 +307,26 @@ export default class EventService { 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 -- Gitee From 77dec1ee11401f5a7747f737df9d5e6033c58cf4 Mon Sep 17 00:00:00 2001 From: Joel Chu Date: Sun, 14 Jul 2019 20:35:55 +0800 Subject: [PATCH 12/23] drop the unused --- .../code/cache/7035ba062321d6588f0b6a1ef51cc75a58981cbc | 1 - .../code/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 | 1 - .../code/cache/ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2 | 1 - .../code/cache/b08c37772ef58648f81f89ddd7be1af0fd2147b5 | 1 - .../code/cache/ccc70771d4ab5581158090a0106a087a69a20bdb | 1 - .../cache/7035ba062321d6588f0b6a1ef51cc75a58981cbc | 1 - .../cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 | 1 - .../cache/ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2 | 1 - .../cache/b08c37772ef58648f81f89ddd7be1af0fd2147b5 | 1 - .../cache/ccc70771d4ab5581158090a0106a087a69a20bdb | 1 - .../cache/7035ba062321d6588f0b6a1ef51cc75a58981cbc | 1 - .../cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 | 1 - .../cache/ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2 | 1 - .../cache/b08c37772ef58648f81f89ddd7be1af0fd2147b5 | 1 - .../cache/ccc70771d4ab5581158090a0106a087a69a20bdb | 1 - .../types/cache/0af14034b4858555dc8d4a8ed15d7f08de40eb36 | 0 .../types/cache/2c0fa7d7cfab72e02c2850c2c6402684f18bdc9d | 0 .../types/cache/609dd35a6af3197577b43708e69aa1be093cdb69 | 0 .../types/cache/7f715d854fbb98c5fe1dab178b872994225edc80 | 0 .../types/cache/ab2c42645f134bd03c75a1cfe9a94d3dd21da9fe | 0 .../code/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 | 1 - .../code/cache/e6b6beb338c32055d84de9d86dd4a380e8952662 | 1 - .../cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 | 1 - .../cache/e6b6beb338c32055d84de9d86dd4a380e8952662 | 1 - .../cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 | 1 - .../cache/e6b6beb338c32055d84de9d86dd4a380e8952662 | 1 - .../types/cache/0af14034b4858555dc8d4a8ed15d7f08de40eb36 | 0 .../types/cache/2c0fa7d7cfab72e02c2850c2c6402684f18bdc9d | 0 .../types/cache/609dd35a6af3197577b43708e69aa1be093cdb69 | 0 .../types/cache/7f715d854fbb98c5fe1dab178b872994225edc80 | 0 .../types/cache/ab2c42645f134bd03c75a1cfe9a94d3dd21da9fe | 0 .../code/cache/33be17634973f39fb3851a17d137ddadbbdf9bff | 1 - .../code/cache/51af2df8071ce95ae1e4eb6859380cd66e17fe3b | 1 - .../code/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 | 1 - .../cache/33be17634973f39fb3851a17d137ddadbbdf9bff | 1 - .../cache/51af2df8071ce95ae1e4eb6859380cd66e17fe3b | 1 - .../cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 | 1 - .../cache/33be17634973f39fb3851a17d137ddadbbdf9bff | 1 - .../cache/51af2df8071ce95ae1e4eb6859380cd66e17fe3b | 1 - .../cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 | 1 - .../types/cache/0af14034b4858555dc8d4a8ed15d7f08de40eb36 | 0 .../types/cache/2c0fa7d7cfab72e02c2850c2c6402684f18bdc9d | 0 .../types/cache/609dd35a6af3197577b43708e69aa1be093cdb69 | 0 .../types/cache/7f715d854fbb98c5fe1dab178b872994225edc80 | 0 .../types/cache/ab2c42645f134bd03c75a1cfe9a94d3dd21da9fe | 0 packages/event/src/enums.ts | 8 -------- packages/event/src/event-service.ts | 1 - 47 files changed, 39 deletions(-) delete mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/7035ba062321d6588f0b6a1ef51cc75a58981cbc delete mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 delete mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2 delete mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/b08c37772ef58648f81f89ddd7be1af0fd2147b5 delete mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/ccc70771d4ab5581158090a0106a087a69a20bdb delete mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/7035ba062321d6588f0b6a1ef51cc75a58981cbc delete mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 delete mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2 delete mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/b08c37772ef58648f81f89ddd7be1af0fd2147b5 delete mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/ccc70771d4ab5581158090a0106a087a69a20bdb delete mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/7035ba062321d6588f0b6a1ef51cc75a58981cbc delete mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 delete mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2 delete mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/b08c37772ef58648f81f89ddd7be1af0fd2147b5 delete mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/ccc70771d4ab5581158090a0106a087a69a20bdb delete mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/0af14034b4858555dc8d4a8ed15d7f08de40eb36 delete mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/2c0fa7d7cfab72e02c2850c2c6402684f18bdc9d delete mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/609dd35a6af3197577b43708e69aa1be093cdb69 delete mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/7f715d854fbb98c5fe1dab178b872994225edc80 delete mode 100644 packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/ab2c42645f134bd03c75a1cfe9a94d3dd21da9fe delete mode 100644 packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 delete mode 100644 packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/e6b6beb338c32055d84de9d86dd4a380e8952662 delete mode 100644 packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/semanticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 delete mode 100644 packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/semanticDiagnostics/cache/e6b6beb338c32055d84de9d86dd4a380e8952662 delete mode 100644 packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/syntacticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 delete mode 100644 packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/syntacticDiagnostics/cache/e6b6beb338c32055d84de9d86dd4a380e8952662 delete mode 100644 packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/0af14034b4858555dc8d4a8ed15d7f08de40eb36 delete mode 100644 packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/2c0fa7d7cfab72e02c2850c2c6402684f18bdc9d delete mode 100644 packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/609dd35a6af3197577b43708e69aa1be093cdb69 delete mode 100644 packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/7f715d854fbb98c5fe1dab178b872994225edc80 delete mode 100644 packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/ab2c42645f134bd03c75a1cfe9a94d3dd21da9fe delete mode 100644 packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/code/cache/33be17634973f39fb3851a17d137ddadbbdf9bff delete mode 100644 packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/code/cache/51af2df8071ce95ae1e4eb6859380cd66e17fe3b delete mode 100644 packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/code/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 delete mode 100644 packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/semanticDiagnostics/cache/33be17634973f39fb3851a17d137ddadbbdf9bff delete mode 100644 packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/semanticDiagnostics/cache/51af2df8071ce95ae1e4eb6859380cd66e17fe3b delete mode 100644 packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/semanticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 delete mode 100644 packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/syntacticDiagnostics/cache/33be17634973f39fb3851a17d137ddadbbdf9bff delete mode 100644 packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/syntacticDiagnostics/cache/51af2df8071ce95ae1e4eb6859380cd66e17fe3b delete mode 100644 packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/syntacticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 delete mode 100644 packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/0af14034b4858555dc8d4a8ed15d7f08de40eb36 delete mode 100644 packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/2c0fa7d7cfab72e02c2850c2c6402684f18bdc9d delete mode 100644 packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/609dd35a6af3197577b43708e69aa1be093cdb69 delete mode 100644 packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/7f715d854fbb98c5fe1dab178b872994225edc80 delete mode 100644 packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/ab2c42645f134bd03c75a1cfe9a94d3dd21da9fe delete mode 100644 packages/event/src/enums.ts diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/7035ba062321d6588f0b6a1ef51cc75a58981cbc b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/7035ba062321d6588f0b6a1ef51cc75a58981cbc deleted file mode 100644 index f2232a3f..00000000 --- a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/7035ba062321d6588f0b6a1ef51cc75a58981cbc +++ /dev/null @@ -1 +0,0 @@ -{"code":"/**\r\n * generate a 32bit hash based on the function.toString()\r\n * _from http://stackoverflow.com/questions/7616461/generate-a-hash-_from-string-in-javascript-jquery\r\n * @param {function} fn the converted to string function\r\n * @return {string} the hashed function string\r\n */\r\nexport default function hashCode(fn) {\r\n var s = fn.toString();\r\n return s.split(\"\").reduce(function (a, b) { a = ((a << 5) - a) + b.charCodeAt(0); return a & a; }, 0) + \"\";\r\n}\r\n","references":[]} diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 deleted file mode 100644 index 7242cbab..00000000 --- a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 +++ /dev/null @@ -1 +0,0 @@ -{"code":"// The top level for @jsonql/event\r\nimport * as EventService from './src/event-service';\r\nexport default EventService;\r\n","references":["/home/joel/projects/open-source/jsonql/packages/event/src/event-service.ts"]} diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2 b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2 deleted file mode 100644 index a375a083..00000000 --- a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2 +++ /dev/null @@ -1 +0,0 @@ -{"code":"\"use strict\";\r\n/*\r\n@TODO\r\ninterface configObj {\r\n logger?: any, // this is the shitty thing about TS\r\n keep? : Boolean,\r\n suspend?: Boolean\r\n}\r\n*/\r\n","references":[]} diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/b08c37772ef58648f81f89ddd7be1af0fd2147b5 b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/b08c37772ef58648f81f89ddd7be1af0fd2147b5 deleted file mode 100644 index bf6c4b20..00000000 --- a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/b08c37772ef58648f81f89ddd7be1af0fd2147b5 +++ /dev/null @@ -1 +0,0 @@ -{"code":"// The main class\r\nimport './interfaces.ts';\r\nimport './enums.ts';\r\nimport hashFnToKey from './hash-code';\r\n// main\r\nvar EventService = /** @class */ (function () {\r\n /**\r\n * Create EventService instance\r\n * @param {configObj} config configuration object\r\n * @return {void} nothing - don't do :void this fuck typescript up what a lot of shit\r\n */\r\n function EventService(logger) {\r\n // hack\r\n this.logger = typeof logger === 'function' ? logger : function () { };\r\n this.normalStore = new Map();\r\n this.lazyStore = new Map();\r\n this.keep = false;\r\n this.result = [];\r\n }\r\n /**\r\n * Register your evt handler, note we don't check the type here,\r\n * we expect you to be sensible and know what you are doing.\r\n * @param {string} evt name of event\r\n * @param {function} callback bind method --> if it's array or not\r\n * @param {object} [context=null] to execute this call in\r\n * @return {number} the size of the store\r\n */\r\n EventService.prototype.$on = function (evt, callback, context) {\r\n if (context === void 0) { context = null; }\r\n var type = 'on';\r\n return 1;\r\n };\r\n Object.defineProperty(EventService.prototype, \"$done\", {\r\n /**\r\n * @TODO is there any real use with the keep prop?\r\n * getter for $done\r\n * @return {*} whatever last store result\r\n */\r\n get: function () {\r\n if (this.keep) {\r\n this.logger(this.result);\r\n return this.result[this.result.length - 1];\r\n }\r\n return this.result;\r\n },\r\n ////////////////////////////////\r\n // SETTER / GETTER //\r\n ////////////////////////////////\r\n /**\r\n * store the return result from the run\r\n * @param {*} value whatever return from callback\r\n */\r\n set: function (value) {\r\n this.logger('set $done', value);\r\n if (this.keep) {\r\n this.result.push(value);\r\n }\r\n else {\r\n this.result = value;\r\n }\r\n },\r\n enumerable: true,\r\n configurable: true\r\n });\r\n /////////////////////////////////\r\n // PRIVATE / PROTECTED METHODS //\r\n /////////////////////////////////\r\n /**\r\n * @param {any} mapObj the Map object\r\n * @return {array} transform to array to work with\r\n */\r\n EventService.prototype.mapToArr = function (mapObj) {\r\n return Array.from(mapObj);\r\n };\r\n /**\r\n * make sure we store the argument correctly\r\n * @param {*} arg could be array\r\n * @return {array} make sured\r\n */\r\n EventService.prototype.toArray = function (arg) {\r\n return Array.isArray(arg) ? arg : [arg];\r\n };\r\n /**\r\n * Run the callback\r\n * @param {function} callback function to execute\r\n * @param {array} payload for callback\r\n * @param {object} ctx context or null\r\n * @return {void} the result store in $done\r\n */\r\n EventService.prototype.run = function (callback, payload, ctx) {\r\n this.logger('run', callback, payload, ctx);\r\n this.$done = Reflect.apply(callback, ctx, this.toArray(payload));\r\n };\r\n /**\r\n * Take the content out and remove it from store id by the name\r\n * @param {string} evt event name\r\n * @param {string} [storeName = lazyStore] name of store\r\n * @return {object|boolean} content or false on not found\r\n */\r\n EventService.prototype.takeFromStore = function (evt, store) {\r\n // let store = this[storeName]; // it could be empty at this point\r\n this.logger('takeFromStore', store);\r\n if (store.has(evt)) {\r\n var content = store.get(evt);\r\n this.logger('takeFromStore', content);\r\n store.delete(evt);\r\n return content;\r\n }\r\n return false;\r\n };\r\n /**\r\n * The add to store step is similar so make it generic for resuse\r\n * @param {object} store which store to use\r\n * @param {string} evt event name\r\n * @param {spread} args because the lazy store and normal store store different things\r\n * @return {array} store and the size of the store (cheated @TODO)\r\n */\r\n EventService.prototype.addToStore = function (store, evt) {\r\n var args = [];\r\n for (var _i = 2; _i < arguments.length; _i++) {\r\n args[_i - 2] = arguments[_i];\r\n }\r\n var fnSet;\r\n if (store.has(evt)) {\r\n this.logger('addToStore', evt + \" existed\");\r\n fnSet = store.get(evt);\r\n }\r\n else {\r\n this.logger('addToStore', \"create new Set for \" + evt);\r\n // this is new\r\n fnSet = new Set();\r\n }\r\n // lazy only store 2 items - this is not the case in V1.6.0 anymore\r\n // we need to check the first parameter is string or not\r\n if (args.length > 2) {\r\n if (Array.isArray(args[0])) { // lazy store\r\n // check if this type of this event already register in the lazy store\r\n var t = args[2];\r\n if (!this.checkTypeInLazyStore(evt, t)) {\r\n fnSet.add(args);\r\n }\r\n }\r\n else {\r\n if (!this.checkContentExist(args, fnSet)) {\r\n this.logger('addToStore', \"insert new\", args);\r\n fnSet.add(args);\r\n }\r\n }\r\n }\r\n else { // add straight to lazy store\r\n fnSet.add(args);\r\n }\r\n store.set(evt, fnSet);\r\n return [store, fnSet.size];\r\n };\r\n /**\r\n * @param {array} args for compare\r\n * @param {object} fnSet A Set to search from\r\n * @return {boolean} true on exist\r\n */\r\n EventService.prototype.checkContentExist = function (args, fnSet) {\r\n var list = this.mapToArr(fnSet);\r\n return !!list\r\n .filter(function (value, index, array) {\r\n var hash = value[0];\r\n return hash === args[0];\r\n }).length;\r\n };\r\n /**\r\n * return all the listener from the event\r\n * @param {string} evtName event name\r\n * @param {boolean} [full=false] if true then return the entire content\r\n * @return {array|boolean} listerner(s) or false when not found\r\n */\r\n EventService.prototype.getByEvt = function (evtName) {\r\n var store = this.normalStore;\r\n if (store.has(evtName)) {\r\n return Array\r\n .from(store.get(evtName))\r\n .map(function (l) { return l; });\r\n }\r\n return false;\r\n };\r\n /**\r\n * get the existing type to make sure no mix type add to the same store\r\n * @param {string} evtName event name\r\n * @param {string} type the type to check\r\n * @return {boolean} true you can add, false then you can't add this type\r\n */\r\n EventService.prototype.checkTypeInStore = function (evtName, type) {\r\n var all = this.getByEvt(evtName);\r\n if (all === false) {\r\n // pristine it means you can add\r\n return true;\r\n }\r\n // it should only have ONE type in ONE event store\r\n return !all\r\n .filter(function (value, index, array) {\r\n var t = value[3];\r\n return type !== t;\r\n }).length;\r\n };\r\n /**\r\n * This is checking just the lazy store because the structure is different\r\n * therefore we need to use a new method to check it\r\n * @param {string} evtName event name\r\n * @param {string} type the types of allow event\r\n * @return {boolean} true OK\r\n */\r\n EventService.prototype.checkTypeInLazyStore = function (evtName, type) {\r\n var store = this.lazyStore.get(evtName);\r\n this.logger('checkTypeInLazyStore', store);\r\n if (store) {\r\n return !!Array\r\n .from(store)\r\n .filter(function (value, index, array) {\r\n var t = value[2];\r\n return t !== type;\r\n }).length;\r\n }\r\n return false;\r\n };\r\n /**\r\n * wrapper to re-use the addToStore,\r\n * V1.3.0 add extra check to see if this type can add to this evt\r\n * @param {string} evt event name\r\n * @param {string} type on or once\r\n * @param {function} callback function\r\n * @param {object} context the context the function execute in or null\r\n * @return {number|boolean} size of the store, or false when the check type failed\r\n */\r\n EventService.prototype.addToNormalStore = function (evt, type, callback, context) {\r\n if (context === void 0) { context = null; }\r\n this.logger('addToNormalStore', evt, type, 'add to normal store');\r\n // @TODO we need to check the existing store for the type first!\r\n if (this.checkTypeInStore(evt, type)) {\r\n this.logger(type + \" can add to \" + evt + \" store\");\r\n var key = hashFnToKey(callback);\r\n var args = [this.normalStore, evt, key, callback, context, type];\r\n var _a = Reflect.apply(this.addToStore, this, args), _store = _a[0], size = _a[1];\r\n this.normalStore = _store;\r\n return size;\r\n }\r\n return false;\r\n };\r\n /**\r\n * Add to lazy store this get calls when the callback is not register yet\r\n * so we only get a payload object or even nothing\r\n * @param {string} evt event name\r\n * @param {array} payload of arguments or empty if there is none\r\n * @param {object} [ctx=null] the context the callback execute in\r\n * @param {string} [type=false] register a type so no other type can add to this evt\r\n * @return {number} size of the store\r\n */\r\n EventService.prototype.addToLazyStore = function (evt, payload, ctx, type) {\r\n if (payload === void 0) { payload = []; }\r\n if (ctx === void 0) { ctx = null; }\r\n // this is add in V1.6.0\r\n // when there is type then we will need to check if this already added in lazy store\r\n // and no other type can add to this lazy store\r\n var args = [];\r\n args.push(this.lazyStore, evt, this.toArray(payload), ctx);\r\n if (type) {\r\n args.push(type);\r\n }\r\n var _a = Reflect.apply(this.addToStore, this, args), _store = _a[0], size = _a[1];\r\n this.lazyStore = _store;\r\n return size;\r\n };\r\n return EventService;\r\n}());\r\nexport default EventService;\r\n","references":["/home/joel/projects/open-source/jsonql/packages/event/src/hash-code.ts"]} diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/ccc70771d4ab5581158090a0106a087a69a20bdb b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/ccc70771d4ab5581158090a0106a087a69a20bdb deleted file mode 100644 index 99aa20c5..00000000 --- a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/code/cache/ccc70771d4ab5581158090a0106a087a69a20bdb +++ /dev/null @@ -1 +0,0 @@ -{"code":"\"use strict\";\r\n// declare all the enum(s) here then import back to the class file\r\nvar eventTypes;\r\n(function (eventTypes) {\r\n eventTypes[\"on\"] = \"on\";\r\n eventTypes[\"only\"] = \"only\";\r\n eventTypes[\"once\"] = \"once\";\r\n eventTypes[\"onlyOnce\"] = \"onlyOnce\";\r\n})(eventTypes || (eventTypes = {}));\r\n","references":[]} diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/7035ba062321d6588f0b6a1ef51cc75a58981cbc b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/7035ba062321d6588f0b6a1ef51cc75a58981cbc deleted file mode 100644 index fe51488c..00000000 --- a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/7035ba062321d6588f0b6a1ef51cc75a58981cbc +++ /dev/null @@ -1 +0,0 @@ -[] diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 deleted file mode 100644 index fe51488c..00000000 --- a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 +++ /dev/null @@ -1 +0,0 @@ -[] diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2 b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2 deleted file mode 100644 index fe51488c..00000000 --- a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2 +++ /dev/null @@ -1 +0,0 @@ -[] diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/b08c37772ef58648f81f89ddd7be1af0fd2147b5 b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/b08c37772ef58648f81f89ddd7be1af0fd2147b5 deleted file mode 100644 index fe51488c..00000000 --- a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/b08c37772ef58648f81f89ddd7be1af0fd2147b5 +++ /dev/null @@ -1 +0,0 @@ -[] diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/ccc70771d4ab5581158090a0106a087a69a20bdb b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/ccc70771d4ab5581158090a0106a087a69a20bdb deleted file mode 100644 index fe51488c..00000000 --- a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/semanticDiagnostics/cache/ccc70771d4ab5581158090a0106a087a69a20bdb +++ /dev/null @@ -1 +0,0 @@ -[] diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/7035ba062321d6588f0b6a1ef51cc75a58981cbc b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/7035ba062321d6588f0b6a1ef51cc75a58981cbc deleted file mode 100644 index fe51488c..00000000 --- a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/7035ba062321d6588f0b6a1ef51cc75a58981cbc +++ /dev/null @@ -1 +0,0 @@ -[] diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 deleted file mode 100644 index fe51488c..00000000 --- a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 +++ /dev/null @@ -1 +0,0 @@ -[] diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2 b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2 deleted file mode 100644 index fe51488c..00000000 --- a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/ab9be8fe85fb075b1f93d8de0623ff9f02a3e5a2 +++ /dev/null @@ -1 +0,0 @@ -[] diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/b08c37772ef58648f81f89ddd7be1af0fd2147b5 b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/b08c37772ef58648f81f89ddd7be1af0fd2147b5 deleted file mode 100644 index fe51488c..00000000 --- a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/b08c37772ef58648f81f89ddd7be1af0fd2147b5 +++ /dev/null @@ -1 +0,0 @@ -[] diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/ccc70771d4ab5581158090a0106a087a69a20bdb b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/ccc70771d4ab5581158090a0106a087a69a20bdb deleted file mode 100644 index fe51488c..00000000 --- a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/syntacticDiagnostics/cache/ccc70771d4ab5581158090a0106a087a69a20bdb +++ /dev/null @@ -1 +0,0 @@ -[] diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/0af14034b4858555dc8d4a8ed15d7f08de40eb36 b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/0af14034b4858555dc8d4a8ed15d7f08de40eb36 deleted file mode 100644 index e69de29b..00000000 diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/2c0fa7d7cfab72e02c2850c2c6402684f18bdc9d b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/2c0fa7d7cfab72e02c2850c2c6402684f18bdc9d deleted file mode 100644 index e69de29b..00000000 diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/609dd35a6af3197577b43708e69aa1be093cdb69 b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/609dd35a6af3197577b43708e69aa1be093cdb69 deleted file mode 100644 index e69de29b..00000000 diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/7f715d854fbb98c5fe1dab178b872994225edc80 b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/7f715d854fbb98c5fe1dab178b872994225edc80 deleted file mode 100644 index e69de29b..00000000 diff --git a/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/ab2c42645f134bd03c75a1cfe9a94d3dd21da9fe b/packages/event/.rpt2_cache/rpt2_23a1d50f95d314c8923b5551a9500bf2cecc70d5/types/cache/ab2c42645f134bd03c75a1cfe9a94d3dd21da9fe deleted file mode 100644 index e69de29b..00000000 diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 deleted file mode 100644 index 7242cbab..00000000 --- a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 +++ /dev/null @@ -1 +0,0 @@ -{"code":"// The top level for @jsonql/event\r\nimport * as EventService from './src/event-service';\r\nexport default EventService;\r\n","references":["/home/joel/projects/open-source/jsonql/packages/event/src/event-service.ts"]} diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/e6b6beb338c32055d84de9d86dd4a380e8952662 b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/e6b6beb338c32055d84de9d86dd4a380e8952662 deleted file mode 100644 index b1d54b4f..00000000 --- a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/code/cache/e6b6beb338c32055d84de9d86dd4a380e8952662 +++ /dev/null @@ -1 +0,0 @@ -{"code":"// The main\r\n/*\r\ninterface configObj {\r\n logger?: any, // this is the shitty thing about TS\r\n keep? : Boolean,\r\n suspend?: Boolean\r\n}\r\n*/\r\nvar EventService = /** @class */ (function () {\r\n /**\r\n * Create EventService instance\r\n * @param {configObj} config configuration object\r\n * @return {void} nothing - don't do :void this fuck typescript up what a lot of shit\r\n */\r\n function EventService(logger) {\r\n // hack\r\n this.logger = typeof logger === 'function' ? logger : function () { };\r\n this.normalStore = new Map();\r\n this.lazyStore = new Map();\r\n }\r\n /**\r\n * Register your evt handler, note we don't check the type here,\r\n * we expect you to be sensible and know what you are doing.\r\n * @param {string} evt name of event\r\n * @param {function} callback bind method --> if it's array or not\r\n * @param {object} [context=null] to execute this call in\r\n * @return {number} the size of the store\r\n */\r\n EventService.prototype.on = function (evt, callback, context) {\r\n if (context === void 0) { context = null; }\r\n var type = 'on';\r\n };\r\n return EventService;\r\n}());\r\nexport default EventService;\r\n","references":[]} diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/semanticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/semanticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 deleted file mode 100644 index fe51488c..00000000 --- a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/semanticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 +++ /dev/null @@ -1 +0,0 @@ -[] diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/semanticDiagnostics/cache/e6b6beb338c32055d84de9d86dd4a380e8952662 b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/semanticDiagnostics/cache/e6b6beb338c32055d84de9d86dd4a380e8952662 deleted file mode 100644 index fe51488c..00000000 --- a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/semanticDiagnostics/cache/e6b6beb338c32055d84de9d86dd4a380e8952662 +++ /dev/null @@ -1 +0,0 @@ -[] diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/syntacticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/syntacticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 deleted file mode 100644 index fe51488c..00000000 --- a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/syntacticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 +++ /dev/null @@ -1 +0,0 @@ -[] diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/syntacticDiagnostics/cache/e6b6beb338c32055d84de9d86dd4a380e8952662 b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/syntacticDiagnostics/cache/e6b6beb338c32055d84de9d86dd4a380e8952662 deleted file mode 100644 index fe51488c..00000000 --- a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/syntacticDiagnostics/cache/e6b6beb338c32055d84de9d86dd4a380e8952662 +++ /dev/null @@ -1 +0,0 @@ -[] diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/0af14034b4858555dc8d4a8ed15d7f08de40eb36 b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/0af14034b4858555dc8d4a8ed15d7f08de40eb36 deleted file mode 100644 index e69de29b..00000000 diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/2c0fa7d7cfab72e02c2850c2c6402684f18bdc9d b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/2c0fa7d7cfab72e02c2850c2c6402684f18bdc9d deleted file mode 100644 index e69de29b..00000000 diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/609dd35a6af3197577b43708e69aa1be093cdb69 b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/609dd35a6af3197577b43708e69aa1be093cdb69 deleted file mode 100644 index e69de29b..00000000 diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/7f715d854fbb98c5fe1dab178b872994225edc80 b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/7f715d854fbb98c5fe1dab178b872994225edc80 deleted file mode 100644 index e69de29b..00000000 diff --git a/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/ab2c42645f134bd03c75a1cfe9a94d3dd21da9fe b/packages/event/.rpt2_cache/rpt2_57fce54fe0abf45f6dd5e7a4f7bd29ec7def635e/types/cache/ab2c42645f134bd03c75a1cfe9a94d3dd21da9fe deleted file mode 100644 index e69de29b..00000000 diff --git a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/code/cache/33be17634973f39fb3851a17d137ddadbbdf9bff b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/code/cache/33be17634973f39fb3851a17d137ddadbbdf9bff deleted file mode 100644 index c1ba0bd8..00000000 --- a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/code/cache/33be17634973f39fb3851a17d137ddadbbdf9bff +++ /dev/null @@ -1 +0,0 @@ -{"code":"\"use strict\";\r\n","references":[]} diff --git a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/code/cache/51af2df8071ce95ae1e4eb6859380cd66e17fe3b b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/code/cache/51af2df8071ce95ae1e4eb6859380cd66e17fe3b deleted file mode 100644 index 9aff289b..00000000 --- a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/code/cache/51af2df8071ce95ae1e4eb6859380cd66e17fe3b +++ /dev/null @@ -1 +0,0 @@ -{"code":"// The main\r\n/*\r\ninterface configObj {\r\n logger?: any, // this is the shitty thing about TS\r\n keep? : Boolean,\r\n suspend?: Boolean\r\n}\r\n*/\r\nimport './interfaces.ts';\r\nvar EventService = /** @class */ (function () {\r\n /**\r\n * Create EventService instance\r\n * @param {configObj} config configuration object\r\n * @return {void} nothing - don't do :void this fuck typescript up what a lot of shit\r\n */\r\n function EventService(logger) {\r\n // hack\r\n this.logger = typeof logger === 'function' ? logger : function () { };\r\n this.normalStore = new Map();\r\n this.lazyStore = new Map();\r\n }\r\n /**\r\n * Register your evt handler, note we don't check the type here,\r\n * we expect you to be sensible and know what you are doing.\r\n * @param {string} evt name of event\r\n * @param {function} callback bind method --> if it's array or not\r\n * @param {object} [context=null] to execute this call in\r\n * @return {number} the size of the store\r\n */\r\n EventService.prototype.on = function (evt, callback, context) {\r\n if (context === void 0) { context = null; }\r\n var type = 'on';\r\n };\r\n return EventService;\r\n}());\r\nexport default EventService;\r\n","references":[]} diff --git a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/code/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/code/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 deleted file mode 100644 index 7242cbab..00000000 --- a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/code/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 +++ /dev/null @@ -1 +0,0 @@ -{"code":"// The top level for @jsonql/event\r\nimport * as EventService from './src/event-service';\r\nexport default EventService;\r\n","references":["/home/joel/projects/open-source/jsonql/packages/event/src/event-service.ts"]} diff --git a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/semanticDiagnostics/cache/33be17634973f39fb3851a17d137ddadbbdf9bff b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/semanticDiagnostics/cache/33be17634973f39fb3851a17d137ddadbbdf9bff deleted file mode 100644 index fe51488c..00000000 --- a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/semanticDiagnostics/cache/33be17634973f39fb3851a17d137ddadbbdf9bff +++ /dev/null @@ -1 +0,0 @@ -[] diff --git a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/semanticDiagnostics/cache/51af2df8071ce95ae1e4eb6859380cd66e17fe3b b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/semanticDiagnostics/cache/51af2df8071ce95ae1e4eb6859380cd66e17fe3b deleted file mode 100644 index fe51488c..00000000 --- a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/semanticDiagnostics/cache/51af2df8071ce95ae1e4eb6859380cd66e17fe3b +++ /dev/null @@ -1 +0,0 @@ -[] diff --git a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/semanticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/semanticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 deleted file mode 100644 index fe51488c..00000000 --- a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/semanticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 +++ /dev/null @@ -1 +0,0 @@ -[] diff --git a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/syntacticDiagnostics/cache/33be17634973f39fb3851a17d137ddadbbdf9bff b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/syntacticDiagnostics/cache/33be17634973f39fb3851a17d137ddadbbdf9bff deleted file mode 100644 index fe51488c..00000000 --- a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/syntacticDiagnostics/cache/33be17634973f39fb3851a17d137ddadbbdf9bff +++ /dev/null @@ -1 +0,0 @@ -[] diff --git a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/syntacticDiagnostics/cache/51af2df8071ce95ae1e4eb6859380cd66e17fe3b b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/syntacticDiagnostics/cache/51af2df8071ce95ae1e4eb6859380cd66e17fe3b deleted file mode 100644 index fe51488c..00000000 --- a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/syntacticDiagnostics/cache/51af2df8071ce95ae1e4eb6859380cd66e17fe3b +++ /dev/null @@ -1 +0,0 @@ -[] diff --git a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/syntacticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/syntacticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 deleted file mode 100644 index fe51488c..00000000 --- a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/syntacticDiagnostics/cache/a9b3003bafaffbecf76c95cc6906ea31baf1a3b9 +++ /dev/null @@ -1 +0,0 @@ -[] diff --git a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/0af14034b4858555dc8d4a8ed15d7f08de40eb36 b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/0af14034b4858555dc8d4a8ed15d7f08de40eb36 deleted file mode 100644 index e69de29b..00000000 diff --git a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/2c0fa7d7cfab72e02c2850c2c6402684f18bdc9d b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/2c0fa7d7cfab72e02c2850c2c6402684f18bdc9d deleted file mode 100644 index e69de29b..00000000 diff --git a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/609dd35a6af3197577b43708e69aa1be093cdb69 b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/609dd35a6af3197577b43708e69aa1be093cdb69 deleted file mode 100644 index e69de29b..00000000 diff --git a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/7f715d854fbb98c5fe1dab178b872994225edc80 b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/7f715d854fbb98c5fe1dab178b872994225edc80 deleted file mode 100644 index e69de29b..00000000 diff --git a/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/ab2c42645f134bd03c75a1cfe9a94d3dd21da9fe b/packages/event/.rpt2_cache/rpt2_ce6b7734c97367a72ffc6cf3a7c8d10508f1f3f3/types/cache/ab2c42645f134bd03c75a1cfe9a94d3dd21da9fe deleted file mode 100644 index e69de29b..00000000 diff --git a/packages/event/src/enums.ts b/packages/event/src/enums.ts deleted file mode 100644 index e07ebf82..00000000 --- a/packages/event/src/enums.ts +++ /dev/null @@ -1,8 +0,0 @@ -// declare all the enum(s) here then import back to the class file -// it has to be numeric value ... what a pain -enum eventTypes { - on = 1, - only = 2, - once = 3, - onlyOnce = 4 -} diff --git a/packages/event/src/event-service.ts b/packages/event/src/event-service.ts index 07975286..9245c736 100644 --- a/packages/event/src/event-service.ts +++ b/packages/event/src/event-service.ts @@ -1,6 +1,5 @@ // The main class import './interfaces.ts'; -import './enums.ts'; import hashFnToKey from './hash-code'; // main export default class EventService { -- Gitee From d1750c69f494df95f7d41efd76d4daf9f9dd72a4 Mon Sep 17 00:00:00 2001 From: Joel Chu Date: Sun, 14 Jul 2019 21:46:16 +0800 Subject: [PATCH 13/23] Finally fix all the Typing errors --- packages/event/.gitignore | 2 +- packages/event/dist/index.es.js | 257 ++++++++++++++++++++++++++-- packages/event/dist/index.js | 257 ++++++++++++++++++++++++++-- packages/event/src/event-service.ts | 27 +-- 4 files changed, 506 insertions(+), 37 deletions(-) diff --git a/packages/event/.gitignore b/packages/event/.gitignore index d8a458d6..74163bb1 100644 --- a/packages/event/.gitignore +++ b/packages/event/.gitignore @@ -1 +1 @@ -./rpt2_cache +./.rpt2_cache diff --git a/packages/event/dist/index.es.js b/packages/event/dist/index.es.js index 4d5bd3c9..e3ad2cab 100644 --- a/packages/event/dist/index.es.js +++ b/packages/event/dist/index.es.js @@ -1,12 +1,3 @@ -// declare all the enum(s) here then import back to the class file -var eventTypes; -(function (eventTypes) { - eventTypes["on"] = "on"; - eventTypes["only"] = "only"; - eventTypes["once"] = "once"; - eventTypes["onlyOnce"] = "onlyOnce"; -})(eventTypes || (eventTypes = {})); - /** * generate a 32bit hash based on the function.toString() * _from http://stackoverflow.com/questions/7616461/generate-a-hash-_from-string-in-javascript-jquery @@ -43,7 +34,230 @@ var EventService = /** @class */ (function () { * @return {number} the size of the store */ EventService.prototype.$on = function (evt, callback, context) { - return 1; + var _this = this; + if (context === void 0) { context = null; } + 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 + */ + EventService.prototype.$once = function (evt, callback, context) { + if (context === void 0) { context = null; } + 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 + */ + EventService.prototype.$only = function (evt, callback, context) { + var _this = this; + if (context === void 0) { context = null; } + 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 + */ + EventService.prototype.$onlyOnce = function (evt, callback, context) { + if (context === void 0) { context = null; } + 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 + */ + EventService.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 + */ + EventService.prototype.$trigger = function (evt, payload, context, type) { + if (payload === void 0) { payload = []; } + if (context === void 0) { context = null; } + if (type === void 0) { type = false; } + 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 + */ + EventService.prototype.$call = function (evt, params, type, context) { + if (type === void 0) { type = false; } + if (context === void 0) { context = null; } + 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 + */ + EventService.prototype.$off = function (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; }; Object.defineProperty(EventService.prototype, "$done", { /** @@ -95,6 +309,25 @@ var EventService = /** @class */ (function () { EventService.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 + */ + EventService.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 @@ -109,10 +342,10 @@ var EventService = /** @class */ (function () { /** * Take the content out and remove it from store id by the name * @param {string} evt event name - * @param {string} [storeName = lazyStore] name of store * @return {object|boolean} content or false on not found */ - EventService.prototype.takeFromStore = function (evt, store) { + EventService.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)) { diff --git a/packages/event/dist/index.js b/packages/event/dist/index.js index 95471b22..c7b28bd8 100644 --- a/packages/event/dist/index.js +++ b/packages/event/dist/index.js @@ -1,14 +1,5 @@ 'use strict'; -// declare all the enum(s) here then import back to the class file -var eventTypes; -(function (eventTypes) { - eventTypes["on"] = "on"; - eventTypes["only"] = "only"; - eventTypes["once"] = "once"; - eventTypes["onlyOnce"] = "onlyOnce"; -})(eventTypes || (eventTypes = {})); - /** * generate a 32bit hash based on the function.toString() * _from http://stackoverflow.com/questions/7616461/generate-a-hash-_from-string-in-javascript-jquery @@ -45,7 +36,230 @@ var EventService = /** @class */ (function () { * @return {number} the size of the store */ EventService.prototype.$on = function (evt, callback, context) { - return 1; + var _this = this; + if (context === void 0) { context = null; } + 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 + */ + EventService.prototype.$once = function (evt, callback, context) { + if (context === void 0) { context = null; } + 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 + */ + EventService.prototype.$only = function (evt, callback, context) { + var _this = this; + if (context === void 0) { context = null; } + 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 + */ + EventService.prototype.$onlyOnce = function (evt, callback, context) { + if (context === void 0) { context = null; } + 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 + */ + EventService.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 + */ + EventService.prototype.$trigger = function (evt, payload, context, type) { + if (payload === void 0) { payload = []; } + if (context === void 0) { context = null; } + if (type === void 0) { type = false; } + 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 + */ + EventService.prototype.$call = function (evt, params, type, context) { + if (type === void 0) { type = false; } + if (context === void 0) { context = null; } + 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 + */ + EventService.prototype.$off = function (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; }; Object.defineProperty(EventService.prototype, "$done", { /** @@ -97,6 +311,25 @@ var EventService = /** @class */ (function () { EventService.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 + */ + EventService.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 @@ -111,10 +344,10 @@ var EventService = /** @class */ (function () { /** * Take the content out and remove it from store id by the name * @param {string} evt event name - * @param {string} [storeName = lazyStore] name of store * @return {object|boolean} content or false on not found */ - EventService.prototype.takeFromStore = function (evt, store) { + EventService.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)) { diff --git a/packages/event/src/event-service.ts b/packages/event/src/event-service.ts index 9245c736..e37f9f3f 100644 --- a/packages/event/src/event-service.ts +++ b/packages/event/src/event-service.ts @@ -35,7 +35,7 @@ export default class EventService { * @param {object} [context=null] to execute this call in * @return {number} the size of the store */ - public $on(evt: string, callback: myCallbackType, context: any = null): number { + public $on(evt: string, callback: myCallbackType, context: any = null): any { const type = 'on'; // first need to check if this evt is in lazy store let lazyStoreContent = this.takeFromStore(evt) @@ -57,7 +57,10 @@ export default class EventService { throw new Error(`You are trying to register an event already been taken by other type: ${t}`) } this.run(callback, payload, context || ctx) - size += this.addToNormalStore(evt, type, callback, context || ctx) + let result = this.addToNormalStore(evt, type, callback, context || ctx) + if (typeof result !== 'boolean') { + size += result; + } }) return size; } @@ -109,7 +112,9 @@ export default class EventService { */ $only(evt: string, callback: myCallbackType, context: any = null): boolean { const type = 'only'; - let added = false; + // 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 + let added: any = false; let lazyStoreContent = this.takeFromStore(evt) // this is normal register before call $trigger let nStore = this.normalStore; @@ -142,9 +147,8 @@ export default class EventService { * @return {boolean} same as above */ $onlyOnce(evt: string, callback: myCallbackType, context: any = null): boolean { - this.validate(evt, callback) const type = 'onlyOnce'; - let added = false; + let added: any = false; let lazyStoreContent = this.takeFromStore(evt) // this is normal register before call $trigger let nStore = this.normalStore; @@ -230,7 +234,7 @@ export default class EventService { * @param {object} context what context callback execute in * @return {*} from $trigger */ - $call(evt: string, params: any[], type: string|boolean = false, context: any = null): any { + $call(evt: string, params: any[], type: any = false, context: any = null): any { let args = [evt, params] args.push(context, type) return Reflect.apply(this.$trigger, this, args) @@ -242,7 +246,6 @@ export default class EventService { * @return {boolean} true actually delete something */ $off(evt: string): boolean { - this.validateEvt(evt) let stores = [ this.lazyStore, this.normalStore ] let found = false; stores.forEach(store => { @@ -313,7 +316,7 @@ export default class EventService { */ private getMethodBy(type: string): any { switch(type) { - case 'on' + case 'on': return this.$on; case 'only': return this.$only; @@ -341,10 +344,10 @@ export default class EventService { /** * Take the content out and remove it from store id by the name * @param {string} evt event name - * @param {string} [storeName = lazyStore] name of store * @return {object|boolean} content or false on not found */ - private takeFromStore(evt: string, store: any) { + 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)) { @@ -475,7 +478,7 @@ export default class EventService { * @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: string, callback: myCallbackType, context: any = null): number | boolean { + 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)) { @@ -498,7 +501,7 @@ export default class EventService { * @param {string} [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): number { + private addToLazyStore(evt: string, payload: any[] = [], ctx: any = null, type: any): 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 -- Gitee From 890e1a94e3af9a884b91a6a0e60bbbea2ae0ddc3 Mon Sep 17 00:00:00 2001 From: Joel Chu Date: Sun, 14 Jul 2019 21:46:52 +0800 Subject: [PATCH 14/23] ignore the typescript generate rubbish --- packages/event/.gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/event/.gitignore b/packages/event/.gitignore index 74163bb1..a761871b 100644 --- a/packages/event/.gitignore +++ b/packages/event/.gitignore @@ -1 +1 @@ -./.rpt2_cache +.rpt2_cache/ -- Gitee From 7288c6ad98afc0dd25dbe2296f98b2d3d761aa43 Mon Sep 17 00:00:00 2001 From: Joel Chu Date: Sun, 14 Jul 2019 22:53:12 +0800 Subject: [PATCH 15/23] start retesting the code --- packages/event/dist/index.es.js | 78 +++++++---- packages/event/dist/index.js | 78 +++++++---- packages/event/index.ts | 4 +- packages/event/package.json | 10 +- packages/event/src/event-service.ts | 43 ++++-- packages/event/tests/basic.test.js | 154 ++++++++++++++++++++++ packages/event/tests/fixtures/browser.js | 9 ++ packages/event/tests/fixtures/index.html | 25 ++++ packages/event/tests/lazy.test.js | 55 ++++++++ packages/event/tests/map-set.test.js | 108 +++++++++++++++ packages/event/tests/once-problem.test.js | 86 ++++++++++++ packages/event/tests/only-once.test.js | 62 +++++++++ packages/event/tests/only.test.js | 72 ++++++++++ packages/event/tests/replace.test.js | 41 ++++++ 14 files changed, 753 insertions(+), 72 deletions(-) create mode 100644 packages/event/tests/basic.test.js create mode 100644 packages/event/tests/fixtures/browser.js create mode 100644 packages/event/tests/fixtures/index.html create mode 100644 packages/event/tests/lazy.test.js create mode 100644 packages/event/tests/map-set.test.js create mode 100644 packages/event/tests/once-problem.test.js create mode 100644 packages/event/tests/only-once.test.js create mode 100644 packages/event/tests/only.test.js create mode 100644 packages/event/tests/replace.test.js diff --git a/packages/event/dist/index.es.js b/packages/event/dist/index.es.js index e3ad2cab..b983a1e1 100644 --- a/packages/event/dist/index.es.js +++ b/packages/event/dist/index.es.js @@ -11,13 +11,13 @@ function hashCode(fn) { // The main class // main -var EventService = /** @class */ (function () { +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 EventService(logger) { + function JsonqlEventService(logger) { // hack this.logger = typeof logger === 'function' ? logger : function () { }; this.normalStore = new Map(); @@ -33,7 +33,7 @@ var EventService = /** @class */ (function () { * @param {object} [context=null] to execute this call in * @return {number} the size of the store */ - EventService.prototype.$on = function (evt, callback, context) { + JsonqlEventService.prototype.$on = function (evt, callback, context) { var _this = this; if (context === void 0) { context = null; } var type = 'on'; @@ -72,7 +72,7 @@ var EventService = /** @class */ (function () { * @param {object} [context=null] the handler execute in * @return {number|boolean} result */ - EventService.prototype.$once = function (evt, callback, context) { + JsonqlEventService.prototype.$once = function (evt, callback, context) { if (context === void 0) { context = null; } var type = 'once'; var lazyStoreContent = this.takeFromStore(evt); @@ -109,7 +109,7 @@ var EventService = /** @class */ (function () { * @param {object} [context=null] the context the event handler execute in * @return {boolean} true bind for first time, false already existed */ - EventService.prototype.$only = function (evt, callback, context) { + JsonqlEventService.prototype.$only = function (evt, callback, context) { var _this = this; if (context === void 0) { context = null; } var type = 'only'; @@ -146,7 +146,7 @@ var EventService = /** @class */ (function () { * @param {object} [context=null] exeucte context * @return {boolean} same as above */ - EventService.prototype.$onlyOnce = function (evt, callback, context) { + JsonqlEventService.prototype.$onlyOnce = function (evt, callback, context) { if (context === void 0) { context = null; } var type = 'onlyOnce'; var added = false; @@ -180,7 +180,7 @@ var EventService = /** @class */ (function () { * @param {string} [type=on] what type of method to replace * @return {any} whatever the call method return */ - EventService.prototype.$replace = function (evt, callback, context, type) { + JsonqlEventService.prototype.$replace = function (evt, callback, context, type) { if (context === void 0) { context = null; } if (type === void 0) { type = 'on'; } this.$off(evt); @@ -196,7 +196,7 @@ var EventService = /** @class */ (function () { * @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 */ - EventService.prototype.$trigger = function (evt, payload, context, type) { + 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; } @@ -236,7 +236,7 @@ var EventService = /** @class */ (function () { * @param {object} context what context callback execute in * @return {*} from $trigger */ - EventService.prototype.$call = function (evt, params, type, context) { + JsonqlEventService.prototype.$call = function (evt, params, type, context) { if (type === void 0) { type = false; } if (context === void 0) { context = null; } var args = [evt, params]; @@ -248,7 +248,7 @@ var EventService = /** @class */ (function () { * @param {string} evt name * @return {boolean} true actually delete something */ - EventService.prototype.$off = function (evt) { + JsonqlEventService.prototype.$off = function (evt) { var stores = [this.lazyStore, this.normalStore]; var found = false; stores.forEach(function (store) { @@ -259,7 +259,29 @@ var EventService = /** @class */ (function () { }); return found; }; - Object.defineProperty(EventService.prototype, "$done", { + /** + * 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; } + 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 @@ -298,7 +320,7 @@ var EventService = /** @class */ (function () { * @param {any} mapObj the Map object * @return {array} transform to array to work with */ - EventService.prototype.mapToArr = function (mapObj) { + JsonqlEventService.prototype.mapToArr = function (mapObj) { return Array.from(mapObj); }; /** @@ -306,7 +328,7 @@ var EventService = /** @class */ (function () { * @param {*} arg could be array * @return {array} make sured */ - EventService.prototype.toArray = function (arg) { + JsonqlEventService.prototype.toArray = function (arg) { return Array.isArray(arg) ? arg : [arg]; }; /** @@ -314,7 +336,7 @@ var EventService = /** @class */ (function () { * @param {string} type the 4 types * @return {*} the method */ - EventService.prototype.getMethodBy = function (type) { + JsonqlEventService.prototype.getMethodBy = function (type) { switch (type) { case 'on': return this.$on; @@ -335,7 +357,7 @@ var EventService = /** @class */ (function () { * @param {object} ctx context or null * @return {void} the result store in $done */ - EventService.prototype.run = function (callback, payload, ctx) { + JsonqlEventService.prototype.run = function (callback, payload, ctx) { this.logger('run', callback, payload, ctx); this.$done = Reflect.apply(callback, ctx, this.toArray(payload)); }; @@ -344,7 +366,7 @@ var EventService = /** @class */ (function () { * @param {string} evt event name * @return {object|boolean} content or false on not found */ - EventService.prototype.takeFromStore = function (evt) { + JsonqlEventService.prototype.takeFromStore = function (evt) { var store = this.lazyStore; // let store = this[storeName]; // it could be empty at this point this.logger('takeFromStore', store); @@ -363,7 +385,7 @@ var EventService = /** @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) */ - EventService.prototype.addToStore = function (store, evt) { + JsonqlEventService.prototype.addToStore = function (store, evt) { var args = []; for (var _i = 2; _i < arguments.length; _i++) { args[_i - 2] = arguments[_i]; @@ -406,7 +428,7 @@ var EventService = /** @class */ (function () { * @param {object} fnSet A Set to search from * @return {boolean} true on exist */ - EventService.prototype.checkContentExist = function (args, fnSet) { + JsonqlEventService.prototype.checkContentExist = function (args, fnSet) { var list = this.mapToArr(fnSet); return !!list .filter(function (value, index, array) { @@ -420,7 +442,7 @@ var EventService = /** @class */ (function () { * @param {boolean} [full=false] if true then return the entire content * @return {array|boolean} listerner(s) or false when not found */ - EventService.prototype.getByEvt = function (evtName) { + JsonqlEventService.prototype.getByEvt = function (evtName) { var store = this.normalStore; if (store.has(evtName)) { return Array @@ -435,7 +457,7 @@ var EventService = /** @class */ (function () { * @param {string} type the type to check * @return {boolean} true you can add, false then you can't add this type */ - EventService.prototype.checkTypeInStore = function (evtName, type) { + JsonqlEventService.prototype.checkTypeInStore = function (evtName, type) { var all = this.getByEvt(evtName); if (all === false) { // pristine it means you can add @@ -455,7 +477,7 @@ var EventService = /** @class */ (function () { * @param {string} type the types of allow event * @return {boolean} true OK */ - EventService.prototype.checkTypeInLazyStore = function (evtName, type) { + JsonqlEventService.prototype.checkTypeInLazyStore = function (evtName, type) { var store = this.lazyStore.get(evtName); this.logger('checkTypeInLazyStore', store); if (store) { @@ -477,7 +499,7 @@ var EventService = /** @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 */ - EventService.prototype.addToNormalStore = function (evt, type, callback, context) { + 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! @@ -497,10 +519,10 @@ var EventService = /** @class */ (function () { * @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} [type=false] register a type so no other type can add to this evt + * @param {string|boolean} [type=false] register a type so no other type can add to this evt * @return {number} size of the store */ - EventService.prototype.addToLazyStore = function (evt, payload, ctx, type) { + 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 @@ -515,13 +537,13 @@ var EventService = /** @class */ (function () { this.lazyStore = _store; return size; }; - return EventService; + return JsonqlEventService; }()); -var EventService$1 = /*#__PURE__*/Object.freeze({ - 'default': EventService +var JsonqlEventService$1 = /*#__PURE__*/Object.freeze({ + 'default': JsonqlEventService }); // The top level for @jsonql/event -export default EventService$1; +export default JsonqlEventService$1; diff --git a/packages/event/dist/index.js b/packages/event/dist/index.js index c7b28bd8..083ade3d 100644 --- a/packages/event/dist/index.js +++ b/packages/event/dist/index.js @@ -13,13 +13,13 @@ function hashCode(fn) { // The main class // main -var EventService = /** @class */ (function () { +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 EventService(logger) { + function JsonqlEventService(logger) { // hack this.logger = typeof logger === 'function' ? logger : function () { }; this.normalStore = new Map(); @@ -35,7 +35,7 @@ var EventService = /** @class */ (function () { * @param {object} [context=null] to execute this call in * @return {number} the size of the store */ - EventService.prototype.$on = function (evt, callback, context) { + JsonqlEventService.prototype.$on = function (evt, callback, context) { var _this = this; if (context === void 0) { context = null; } var type = 'on'; @@ -74,7 +74,7 @@ var EventService = /** @class */ (function () { * @param {object} [context=null] the handler execute in * @return {number|boolean} result */ - EventService.prototype.$once = function (evt, callback, context) { + JsonqlEventService.prototype.$once = function (evt, callback, context) { if (context === void 0) { context = null; } var type = 'once'; var lazyStoreContent = this.takeFromStore(evt); @@ -111,7 +111,7 @@ var EventService = /** @class */ (function () { * @param {object} [context=null] the context the event handler execute in * @return {boolean} true bind for first time, false already existed */ - EventService.prototype.$only = function (evt, callback, context) { + JsonqlEventService.prototype.$only = function (evt, callback, context) { var _this = this; if (context === void 0) { context = null; } var type = 'only'; @@ -148,7 +148,7 @@ var EventService = /** @class */ (function () { * @param {object} [context=null] exeucte context * @return {boolean} same as above */ - EventService.prototype.$onlyOnce = function (evt, callback, context) { + JsonqlEventService.prototype.$onlyOnce = function (evt, callback, context) { if (context === void 0) { context = null; } var type = 'onlyOnce'; var added = false; @@ -182,7 +182,7 @@ var EventService = /** @class */ (function () { * @param {string} [type=on] what type of method to replace * @return {any} whatever the call method return */ - EventService.prototype.$replace = function (evt, callback, context, type) { + JsonqlEventService.prototype.$replace = function (evt, callback, context, type) { if (context === void 0) { context = null; } if (type === void 0) { type = 'on'; } this.$off(evt); @@ -198,7 +198,7 @@ var EventService = /** @class */ (function () { * @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 */ - EventService.prototype.$trigger = function (evt, payload, context, type) { + 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; } @@ -238,7 +238,7 @@ var EventService = /** @class */ (function () { * @param {object} context what context callback execute in * @return {*} from $trigger */ - EventService.prototype.$call = function (evt, params, type, context) { + JsonqlEventService.prototype.$call = function (evt, params, type, context) { if (type === void 0) { type = false; } if (context === void 0) { context = null; } var args = [evt, params]; @@ -250,7 +250,7 @@ var EventService = /** @class */ (function () { * @param {string} evt name * @return {boolean} true actually delete something */ - EventService.prototype.$off = function (evt) { + JsonqlEventService.prototype.$off = function (evt) { var stores = [this.lazyStore, this.normalStore]; var found = false; stores.forEach(function (store) { @@ -261,7 +261,29 @@ var EventService = /** @class */ (function () { }); return found; }; - Object.defineProperty(EventService.prototype, "$done", { + /** + * 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; } + 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 @@ -300,7 +322,7 @@ var EventService = /** @class */ (function () { * @param {any} mapObj the Map object * @return {array} transform to array to work with */ - EventService.prototype.mapToArr = function (mapObj) { + JsonqlEventService.prototype.mapToArr = function (mapObj) { return Array.from(mapObj); }; /** @@ -308,7 +330,7 @@ var EventService = /** @class */ (function () { * @param {*} arg could be array * @return {array} make sured */ - EventService.prototype.toArray = function (arg) { + JsonqlEventService.prototype.toArray = function (arg) { return Array.isArray(arg) ? arg : [arg]; }; /** @@ -316,7 +338,7 @@ var EventService = /** @class */ (function () { * @param {string} type the 4 types * @return {*} the method */ - EventService.prototype.getMethodBy = function (type) { + JsonqlEventService.prototype.getMethodBy = function (type) { switch (type) { case 'on': return this.$on; @@ -337,7 +359,7 @@ var EventService = /** @class */ (function () { * @param {object} ctx context or null * @return {void} the result store in $done */ - EventService.prototype.run = function (callback, payload, ctx) { + JsonqlEventService.prototype.run = function (callback, payload, ctx) { this.logger('run', callback, payload, ctx); this.$done = Reflect.apply(callback, ctx, this.toArray(payload)); }; @@ -346,7 +368,7 @@ var EventService = /** @class */ (function () { * @param {string} evt event name * @return {object|boolean} content or false on not found */ - EventService.prototype.takeFromStore = function (evt) { + JsonqlEventService.prototype.takeFromStore = function (evt) { var store = this.lazyStore; // let store = this[storeName]; // it could be empty at this point this.logger('takeFromStore', store); @@ -365,7 +387,7 @@ var EventService = /** @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) */ - EventService.prototype.addToStore = function (store, evt) { + JsonqlEventService.prototype.addToStore = function (store, evt) { var args = []; for (var _i = 2; _i < arguments.length; _i++) { args[_i - 2] = arguments[_i]; @@ -408,7 +430,7 @@ var EventService = /** @class */ (function () { * @param {object} fnSet A Set to search from * @return {boolean} true on exist */ - EventService.prototype.checkContentExist = function (args, fnSet) { + JsonqlEventService.prototype.checkContentExist = function (args, fnSet) { var list = this.mapToArr(fnSet); return !!list .filter(function (value, index, array) { @@ -422,7 +444,7 @@ var EventService = /** @class */ (function () { * @param {boolean} [full=false] if true then return the entire content * @return {array|boolean} listerner(s) or false when not found */ - EventService.prototype.getByEvt = function (evtName) { + JsonqlEventService.prototype.getByEvt = function (evtName) { var store = this.normalStore; if (store.has(evtName)) { return Array @@ -437,7 +459,7 @@ var EventService = /** @class */ (function () { * @param {string} type the type to check * @return {boolean} true you can add, false then you can't add this type */ - EventService.prototype.checkTypeInStore = function (evtName, type) { + JsonqlEventService.prototype.checkTypeInStore = function (evtName, type) { var all = this.getByEvt(evtName); if (all === false) { // pristine it means you can add @@ -457,7 +479,7 @@ var EventService = /** @class */ (function () { * @param {string} type the types of allow event * @return {boolean} true OK */ - EventService.prototype.checkTypeInLazyStore = function (evtName, type) { + JsonqlEventService.prototype.checkTypeInLazyStore = function (evtName, type) { var store = this.lazyStore.get(evtName); this.logger('checkTypeInLazyStore', store); if (store) { @@ -479,7 +501,7 @@ var EventService = /** @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 */ - EventService.prototype.addToNormalStore = function (evt, type, callback, context) { + 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! @@ -499,10 +521,10 @@ var EventService = /** @class */ (function () { * @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} [type=false] register a type so no other type can add to this evt + * @param {string|boolean} [type=false] register a type so no other type can add to this evt * @return {number} size of the store */ - EventService.prototype.addToLazyStore = function (evt, payload, ctx, type) { + 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 @@ -517,13 +539,13 @@ var EventService = /** @class */ (function () { this.lazyStore = _store; return size; }; - return EventService; + return JsonqlEventService; }()); -var EventService$1 = /*#__PURE__*/Object.freeze({ - 'default': EventService +var JsonqlEventService$1 = /*#__PURE__*/Object.freeze({ + 'default': JsonqlEventService }); // The top level for @jsonql/event -module.exports = EventService$1; +module.exports = JsonqlEventService$1; diff --git a/packages/event/index.ts b/packages/event/index.ts index 0f376a1f..dd4076cc 100644 --- a/packages/event/index.ts +++ b/packages/event/index.ts @@ -1,4 +1,4 @@ // The top level for @jsonql/event -import * as EventService from './src/event-service'; +import * as JsonqlEventService from './src/event-service'; -export default EventService; +export default JsonqlEventService; diff --git a/packages/event/package.json b/packages/event/package.json index 89194d1c..c70ed61f 100644 --- a/packages/event/package.json +++ b/packages/event/package.json @@ -12,7 +12,8 @@ "scripts": { "test": "ava", "build": "rollup -c", - "dev": "rollup -cw" + "dev": "rollup -cw", + "test:basic": "DEBUG=nb-event-service* ava ./tests/basic.test.js" }, "keywords": [ "jsonql", @@ -28,10 +29,11 @@ "license": "ISC", "devDependencies": { "ava": "^2.2.0", - "ts-node": "^8.3.0", - "typescript": "^3.5.3", + "debug": "^4.1.1", "rollup": "^1.16.7", - "rollup-plugin-typescript2": "^0.22.0" + "rollup-plugin-typescript2": "^0.22.0", + "ts-node": "^8.3.0", + "typescript": "^3.5.3" }, "dependencies": {}, "ava": { diff --git a/packages/event/src/event-service.ts b/packages/event/src/event-service.ts index e37f9f3f..75c7ca4b 100644 --- a/packages/event/src/event-service.ts +++ b/packages/event/src/event-service.ts @@ -2,7 +2,7 @@ import './interfaces.ts'; import hashFnToKey from './hash-code'; // main -export default class EventService { +export default class JsonqlEventService { // public props keep: boolean; // private props @@ -74,7 +74,7 @@ export default class EventService { * @param {object} [context=null] the handler execute in * @return {number|boolean} result */ - $once(evt: string , callback: myCallbackType , context: any = null): any { + public $once(evt: string , callback: myCallbackType , context: any = null): any { const type = 'once'; let lazyStoreContent = this.takeFromStore(evt) // this is normal register before call $trigger @@ -110,7 +110,7 @@ export default class EventService { * @param {object} [context=null] the context the event handler execute in * @return {boolean} true bind for first time, false already existed */ - $only(evt: string, callback: myCallbackType, context: any = null): boolean { + public $only(evt: string, callback: myCallbackType, context: any = null): boolean { 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 @@ -146,7 +146,7 @@ export default class EventService { * @param {object} [context=null] exeucte context * @return {boolean} same as above */ - $onlyOnce(evt: string, callback: myCallbackType, context: any = null): boolean { + public $onlyOnce(evt: string, callback: myCallbackType, context: any = null): boolean { const type = 'onlyOnce'; let added: any = false; let lazyStoreContent = this.takeFromStore(evt) @@ -180,7 +180,7 @@ export default class EventService { * @param {string} [type=on] what type of method to replace * @return {any} whatever the call method return */ - $replace(evt: string, callback: myCallbackType, context: any = null, type: string = 'on'): any { + public $replace(evt: string, callback: myCallbackType, context: any = null, type: string = 'on'): any { this.$off(evt) // this will become a problem let method = this.getMethodBy(type) @@ -195,7 +195,7 @@ export default class EventService { * @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 */ - $trigger(evt: string , payload: any[] = [] , context: any = null, type: string|boolean = false): number { + public $trigger(evt: string , payload: any[] = [] , context: any = null, type: string|boolean = false): number { let found = 0; // first check the normal store let nStore = this.normalStore; @@ -234,7 +234,7 @@ export default class EventService { * @param {object} context what context callback execute in * @return {*} from $trigger */ - $call(evt: string, params: any[], type: any = false, context: any = null): any { + public $call(evt: string, params: any[], type: any = false, context: any = null): any { let args = [evt, params] args.push(context, type) return Reflect.apply(this.$trigger, this, args) @@ -245,7 +245,7 @@ export default class EventService { * @param {string} evt name * @return {boolean} true actually delete something */ - $off(evt: string): boolean { + public $off(evt: string): boolean { let stores = [ this.lazyStore, this.normalStore ] let found = false; stores.forEach(store => { @@ -257,6 +257,29 @@ export default class EventService { 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 { + 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 // @@ -498,10 +521,10 @@ export default class EventService { * @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} [type=false] register a type so no other type can add to this evt + * @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: any): number { + 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 diff --git a/packages/event/tests/basic.test.js b/packages/event/tests/basic.test.js new file mode 100644 index 00000000..ded66522 --- /dev/null +++ b/packages/event/tests/basic.test.js @@ -0,0 +1,154 @@ +const test = require('ava') +// import the cjs version for testing +const EventService = require('../dist/index') +const logger = require('debug')('nb-event-service') +const debug = require('debug')('nb-event-service:test:basic') +let value = 1000; + +const NBEventService = EventService.default; + +// debug(NBEventService) + +test.before( t => { + t.context.evtSrv = new NBEventService({ + logger + }) +}) + +test('It should able to validate the evt', t => { + let evtSrv = t.context.evtSrv; + let fn = (...args) => Reflect.apply(evtSrv.$on, evtSrv, args) + + t.throws(() => fn('some', false) , Error, 'Should throw error because callback is not a function') +}) + +test.cb('It should able to bind a simple test and callback', t => { + t.plan(1) + let evtName = 'simple' + t.context.evtSrv.$on(evtName, function(num) { + t.is(num, value) + t.end() + }) + t.context.evtSrv.$trigger(evtName, value) +}) + +test.cb('It should able to emit the event before register the listener', t => { + t.plan(1) + let evtName = 'simple-reverse' + + t.context.evtSrv.$trigger(evtName, value) + + t.context.evtSrv.$on(evtName, function(num) { + t.is(num, value) + t.end() + }) +}) + +test.cb('It should able to add more than one listerner to the same event', t => { + t.plan(2) + + let evtName = 'multiple' + let letter = 'again' + + t.context.evtSrv.$on(evtName, function(a) { + t.is(a , letter) + }) + + t.context.evtSrv.$on(evtName, function(b) { + t.is(b, letter) + t.end() + }) + + t.context.evtSrv.$trigger(evtName, letter) + +}) + +test('It should not allow to add the same function again', t => { + let evtName = 'add-once' + const callback = (x) => { + debug(x) + } + + t.context.evtSrv.$on(evtName, callback) + t.context.evtSrv.$on(evtName, callback) + + let ctn = t.context.evtSrv.$get(evtName) + + t.is(ctn.length, 1) +}) + +test('It should only call once if we use the $once option', t => { + let evtName = 'once-call' + let ctn = 0; + + const callback = () => { + ++ctn; + debug(ctn) + } + + const callback2 = () => { + ++ctn; + debug(ctn) + } + + t.context.evtSrv.$once(evtName, callback) + t.context.evtSrv.$once(evtName, callback2) + + t.context.evtSrv.$trigger(evtName) + t.context.evtSrv.$trigger(evtName) + + t.is(ctn, 1) + +}) + +test('Using the $call alias to $trigger should do the same thing', t => { + let evtName = 'alias' + let ctn = 0; + + const callback = () => { + ++ctn; + debug(ctn) + } + t.context.evtSrv.$once(evtName, callback) + t.context.evtSrv.$trigger(evtName) + t.context.evtSrv.$call(evtName) + + t.is(ctn, 1) +}) + +test('Using $trigger and $call should make the callback run again', t => { + let evtName = 'alias-two' + let ctn = 0; + + const callback = () => { + ++ctn; + debug(ctn) + } + + t.context.evtSrv.$trigger(evtName) + t.context.evtSrv.$call(evtName) + + t.context.evtSrv.$on(evtName, callback) + + t.is(ctn, 2) + + t.pass() +}) + +test('Should not able to call the method once the $off is called', t => { + + let evtName = 'off-event' + + const callback = (l) => { + debug(`${l}`) + } + + t.context.evtSrv.$on(evtName, callback) + + t.context.evtSrv.$off(evtName) + + t.context.evtSrv.$trigger(evtName) + + t.false(t.context.evtSrv.$get(evtName)) + +}) diff --git a/packages/event/tests/fixtures/browser.js b/packages/event/tests/fixtures/browser.js new file mode 100644 index 00000000..5eaf0477 --- /dev/null +++ b/packages/event/tests/fixtures/browser.js @@ -0,0 +1,9 @@ +const serverIoCore = require('server-io-core') +const { join } = require('path') + +serverIoCore({ + webroot: [ + join(__dirname), + join(__dirname, '..', '..', 'dist') + ] +}) diff --git a/packages/event/tests/fixtures/index.html b/packages/event/tests/fixtures/index.html new file mode 100644 index 00000000..e794df2a --- /dev/null +++ b/packages/event/tests/fixtures/index.html @@ -0,0 +1,25 @@ + + + + NBEventService test in browser + + +

NBEventService test in browser

+

+ Just look at the console log +

+ + + + diff --git a/packages/event/tests/lazy.test.js b/packages/event/tests/lazy.test.js new file mode 100644 index 00000000..94fb7e25 --- /dev/null +++ b/packages/event/tests/lazy.test.js @@ -0,0 +1,55 @@ +// testing the lazy store with a type using the context parameter +const test = require('ava') + +const NBEventService = require('../main') +const logger = require('debug')('nb-event-service') +const debug = require('debug')('nb-event-service:test:lazy') + +test.before(t => { + t.context.evtSrv = new NBEventService({ + logger + }) +}) + +test('when using the context as type in lazy store, other type should not able to add to lazystore', t => { + let es = t.context.evtSrv; + let evt = 'not-here-yet' + // first we trigger an non-exist event + + es.$trigger(evt, 1, null, 'on') + es.$trigger(evt, 2, null, 'only') + + // now the lazy store should have only one item + let content = es.takeFromStore(evt) + + debug(content) + + t.is(content.size, 1) +}) + +test('It should throw an error if the event been trigger with one type but try to register with another', t => { + let es = t.context.evtSrv; + let evt = 'some-event' + + es.$trigger(evt, 1, null, 'on') + + const fn = (num) => es.$only(evt, num => { + debug(num) + }) + + t.throws(() => fn(), Error, 'It should throw error because its different type') +}) + +test('using $call should able to pass the type without passing the context', t => { + let es = t.context.evtSrv; + let evt = 'just-calling' + + es.$call(evt, 100, 'only') + + const fn = (num) => es.$on(evt, num => { + debug(num) + }) + + t.throws(() => fn(), Error, 'It should throw error because already register with $only') + +}) diff --git a/packages/event/tests/map-set.test.js b/packages/event/tests/map-set.test.js new file mode 100644 index 00000000..fcadf1c4 --- /dev/null +++ b/packages/event/tests/map-set.test.js @@ -0,0 +1,108 @@ +// testing the using Map and Set +const test = require('ava') +const debug = require('debug')('nb-event-service:test:map-set') + +test('A Set should not allow the same thing add twice', t => { + let ctn = 0; + let testSet = new Set() + const testFn = () => { + debug(`call me ${++ctn}`) + } + // problem - can only store primitive type that id is the same + testSet.add(testFn) + testSet.add(testFn) + t.is(testSet.size, 1) +}) + +test('It should able to store a Set inside a Map', t => { + let ctn = 0; + const testFnA = () => { + debug(`call A ${++ctn}`) + } + + const testFnB = () => { + debug(`call B ${++ctn}`) + } + + let testMap = new Map() + let testSet = new Set() + + testMap.set('key-1', testSet) + let ts1 = testMap.get('key-1') + ts1.add(testFnA) + ts1.add(testFnB) + + t.is(testMap.get('key-1').size, 2) + + for (let fn of ts1) { + fn() + } + t.pass() +}) + +test('It should able to store multiple level within a class structure and act like a private property', t => { + const testFnB = () => { + debug(`call B ${++ctn}`) + } + + let PRIVATE_STORE = new Map() + class Dummy { + + add(item) { + PRIVATE_STORE.set(this, item) + } + + get() { + return PRIVATE_STORE.get(this) + } + } + + let dummyIns = new Dummy() + + let mapA = new Map() + let toAdd1 = new Set() + mapA.set('a', toAdd1) + let toAdd1a = mapA.get('a') + toAdd1a.add(testFnB) + toAdd1a.add(testFnB) + dummyIns.add(mapA) + + let dummyValue1 = dummyIns.get() + + t.is( dummyValue1.get('a').size, 1 ) + + t.pass() +}) + +test('It should store one item if its adding different thing to the same key', t => { + let ctn = 0; + const testFnA = () => { + debug(`call A ${++ctn}`) + } + const testFnB = () => { + debug(`call B ${++ctn}`) + } + let PRIVATE_STORE = new Map() + class Dummy { + add(item) { + PRIVATE_STORE.set(this, item) + } + get() { + return PRIVATE_STORE.get(this) + } + } + + let dummyInstance = new Dummy() + + let mapA = new Map(); + mapA.set('key-1', testFnA) + let mapB = new Map(); + mapB.set('key-2', testFnB) + + dummyInstance.add(mapA) + dummyInstance.add(mapB) + + t.truthy( dummyInstance.get().has('key-2') ) + + +}) diff --git a/packages/event/tests/once-problem.test.js b/packages/event/tests/once-problem.test.js new file mode 100644 index 00000000..1623500e --- /dev/null +++ b/packages/event/tests/once-problem.test.js @@ -0,0 +1,86 @@ +const test = require('ava') + +const NBEventService = require('../main') +const logger = require('debug')('nb-event-service') +const debug = require('debug')('nb-event-service:test:only-problem') +let value = 1000; + +test.before( t => { + t.context.evtSrv = new NBEventService({ + logger + }) +}) + +test('This is how $once normally works', t => { + // problem is when you call $trigger before you register it with $once + let evtName = 'once-normal'; + let evtSrv = t.context.evtSrv; + + evtSrv.$trigger(evtName, 1000) + + evtSrv.$once(evtName, function(val) { + debug(val) + return (val*0.1) + val; + }) + + t.is(evtSrv.$done, 1100, 'The first time call $done getter') + + evtSrv.$trigger(evtName) + // it should be the same because it never get call again + t.is(evtSrv.$done, 1100, 'The second time call $done getter') + +}) + +test.cb('$once should allow to add more than one listner', t => { + t.plan(3) + let evtName = 'more-once'; + let evtSrv = t.context.evtSrv; + + evtSrv.$once(evtName, function() { + debug('$once', 'First listener') + t.pass() + return 1; + }) + + evtSrv.$once(evtName, function() { + debug('$once', 'Second listener') + t.pass() + t.end() + return 2; + }) + + evtSrv.$call(evtName) + + t.is(evtSrv.$done, 2) + +}) + +test.only('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; + + evtSrv.$trigger(evtName, 1000) + + evtSrv.$on(evtName, function(val) { + return (val*0.2) + val; + }) + // this should not add + evtSrv.$once(evtName, function(val) { + return (val*0.1) + val; + }) + + evtSrv.$on(evtName, function(val) { + return (val*0.3) + val; + }) + + // now the first $on call hijacked the evt + // I thought it will be 1300 but its 1200 + t.is(evtSrv.$done, 1200) + + evtSrv.$trigger(evtName, 2000) + // but this work as expecte because the order of adding to it + // where the last one was taken out from the lazyStore + t.is(evtSrv.$done, 2600) + +}) diff --git a/packages/event/tests/only-once.test.js b/packages/event/tests/only-once.test.js new file mode 100644 index 00000000..1fd346b3 --- /dev/null +++ b/packages/event/tests/only-once.test.js @@ -0,0 +1,62 @@ +const test = require('ava') + +const NBEventService = require('../main') +const logger = require('debug')('nb-event-service') +const debug = require('debug')('nb-event-service:test:only-once') +let value = 2000; + +test.before(t => { + t.context.evtSrv = new NBEventService({ + logger + }) +}) + +test('It should only add one callback and fire once with $onlyOnce', t => { + + let evtSrv = t.context.evtSrv; + let evt = 'only-once-evt' + evtSrv.$onlyOnce(evt, function() { + debug('call me first') + return 'first' + }) + evtSrv.$onlyOnce(evt, function() { + debug('call me again') + return 'again' + }) + + evtSrv.$trigger(evt) + + t.is(evtSrv.$done, 'first') + + t.is(evtSrv.$trigger(evt), 0) + +}) + +test('It should only get trigger once with only callback', t => { + + let evtSrv = t.context.evtSrv; + let evt = 'only-once-evt-call' + + evtSrv.$trigger(evt) + + evtSrv.$onlyOnce(evt, function() { + debug('call me call me') + return 'me' + }) + + let list = evtSrv.$get(evt) + + t.falsy(list.length) + + evtSrv.$onlyOnce(evt, function() { + debug('call me too') + return 'too' + }) + + //evtSrv.$trigger(evt) + + t.is(evtSrv.$trigger(evt), 1) + + t.is(evtSrv.$done, 'too') + +}) diff --git a/packages/event/tests/only.test.js b/packages/event/tests/only.test.js new file mode 100644 index 00000000..58f69b0e --- /dev/null +++ b/packages/event/tests/only.test.js @@ -0,0 +1,72 @@ +const test = require('ava') + +const NBEventService = require('../main') +const logger = require('debug')('nb-event-service') +const debug = require('debug')('nb-event-service:test:only-problem') +let value = 1000; + +test.before( t => { + t.context.evtSrv = new NBEventService({ + logger + }) +}) + +test('Test the check type method', t => { + + let evt = 'test-evt' + let evtSrv = t.context.evtSrv; + + t.is(evtSrv.checkTypeInStore(evt, 'on'), true) + + evtSrv.$on(evt, function() { + debug('call me') + }) + + t.is(evtSrv.checkTypeInStore(evt, 'on'), true) + + evtSrv.$on(evt, function() { + debug('call me again') + }) + + t.is(evtSrv.checkTypeInStore(evt, 'only'), false) +}) + +test('only should only allow to add one listner', t => { + let evt = 'test-only-evt' + let evtSrv = t.context.evtSrv; + evtSrv.$only(evt, function(num) { + return num + 10; + }) + evtSrv.$only(evt, function(num) { + return num * 10; + }) + evtSrv.$call(evt, 100) + t.is(evtSrv.$done, 110) +}) + +test('Test the trigger before call $only and see if that works the same', t => { + let ctn = 0; + let evt = 'test-only-reverse' + let evtSrv = t.context.evtSrv; + + evtSrv.$call(evt, 'x') + + let result1 = evtSrv.$only(evt, function(A) { + debug(A, ctn) + return ++ctn; + }) + + t.truthy(result1) + + evtSrv.$call(evt, 'y') + + let result2 = evtSrv.$only(evt, function(B) { + debug(B, ctn) + return --ctn; + }) + + t.false(result2) + + t.is(evtSrv.$done , 2) + +}) diff --git a/packages/event/tests/replace.test.js b/packages/event/tests/replace.test.js new file mode 100644 index 00000000..29f71634 --- /dev/null +++ b/packages/event/tests/replace.test.js @@ -0,0 +1,41 @@ +const test = require('ava') + +const NBEventService = require('../main') +const logger = require('debug')('nb-event-service') +const debug = require('debug')('nb-event-service:test:replace') + + +test.before(t => { + t.context.evtSrv = new NBEventService({ + logger + }) +}) + +test('It should able to validate against the type', t => { + let evtSrv = t.context.evtSrv; + let wrongType = 'whatever' + let fn = (type) => evtSrv.$replace('some-event', () => {}, null, type) + + t.throws(() => fn(wrongType), Error, 'It should throw if we pass the wrong type') + +}) + + +test.cb('It should able to replace the event callback', t => { + let evtSrv = t.context.evtSrv; + let evt = 'same-event' + + evtSrv.$on(evt, (n) => { + debug('first callback', n) + }) + + evtSrv.$trigger(evt, 0) + + evtSrv.$replace(evt, (n) => { + t.is(n, 1) + t.end() + }) + + evtSrv.$trigger(evt, 1) + +}) -- Gitee From 737d7f5d74ff90250782d597bf0c9390fc1ad7cf Mon Sep 17 00:00:00 2001 From: Joel Chu Date: Sun, 14 Jul 2019 22:56:19 +0800 Subject: [PATCH 16/23] skip the validate test because its no longer exist --- packages/event/tests/basic.test.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/event/tests/basic.test.js b/packages/event/tests/basic.test.js index ded66522..505f85da 100644 --- a/packages/event/tests/basic.test.js +++ b/packages/event/tests/basic.test.js @@ -10,12 +10,10 @@ const NBEventService = EventService.default; // debug(NBEventService) test.before( t => { - t.context.evtSrv = new NBEventService({ - logger - }) + t.context.evtSrv = new NBEventService(logger) }) -test('It should able to validate the evt', t => { +test.skip('It should able to validate the evt', t => { let evtSrv = t.context.evtSrv; let fn = (...args) => Reflect.apply(evtSrv.$on, evtSrv, args) -- Gitee From 19e45842505213c95a587bebda45292c730fdfc5 Mon Sep 17 00:00:00 2001 From: Joel Chu Date: Mon, 15 Jul 2019 09:38:47 +0800 Subject: [PATCH 17/23] rename the export files also create a browser version --- ...ndex.es.js => jsonql-event-service.cjs.js} | 10 +- .../{index.js => jsonql-event-service.es.js} | 10 - .../event/dist/jsonql-event-service.umd.js | 548 ++++++++++++++++++ packages/event/index.ts | 4 +- packages/event/package.json | 5 +- packages/event/rollup.config.js | 5 + 6 files changed, 559 insertions(+), 23 deletions(-) rename packages/event/dist/{index.es.js => jsonql-event-service.cjs.js} (96%) rename packages/event/dist/{index.js => jsonql-event-service.es.js} (96%) create mode 100644 packages/event/dist/jsonql-event-service.umd.js diff --git a/packages/event/dist/index.es.js b/packages/event/dist/jsonql-event-service.cjs.js similarity index 96% rename from packages/event/dist/index.es.js rename to packages/event/dist/jsonql-event-service.cjs.js index b983a1e1..fb68bc14 100644 --- a/packages/event/dist/index.es.js +++ b/packages/event/dist/jsonql-event-service.cjs.js @@ -1,3 +1,5 @@ +'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 @@ -539,11 +541,3 @@ var JsonqlEventService = /** @class */ (function () { }; return JsonqlEventService; }()); - -var JsonqlEventService$1 = /*#__PURE__*/Object.freeze({ - 'default': JsonqlEventService -}); - -// The top level for @jsonql/event - -export default JsonqlEventService$1; diff --git a/packages/event/dist/index.js b/packages/event/dist/jsonql-event-service.es.js similarity index 96% rename from packages/event/dist/index.js rename to packages/event/dist/jsonql-event-service.es.js index 083ade3d..34b59b79 100644 --- a/packages/event/dist/index.js +++ b/packages/event/dist/jsonql-event-service.es.js @@ -1,5 +1,3 @@ -'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 @@ -541,11 +539,3 @@ var JsonqlEventService = /** @class */ (function () { }; return JsonqlEventService; }()); - -var JsonqlEventService$1 = /*#__PURE__*/Object.freeze({ - 'default': JsonqlEventService -}); - -// The top level for @jsonql/event - -module.exports = JsonqlEventService$1; diff --git a/packages/event/dist/jsonql-event-service.umd.js b/packages/event/dist/jsonql-event-service.umd.js new file mode 100644 index 00000000..48720813 --- /dev/null +++ b/packages/event/dist/jsonql-event-service.umd.js @@ -0,0 +1,548 @@ +(function (factory) { + typeof define === 'function' && define.amd ? define(factory) : + factory(); +}(function () { '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) + ""; + } + + // 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; } + 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; } + 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; } + 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; } + 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; } + 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; } + 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) { + 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; } + 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; + // 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; + } + 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; + }; + /** + * 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 + * @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) { + var all = this.getByEvt(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) { + var store = this.lazyStore.get(evtName); + this.logger('checkTypeInLazyStore', store); + if (store) { + return !!Array + .from(store) + .filter(function (value, index, array) { + var t = value[2]; + return t !== type; + }).length; + } + 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; } + 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"); + 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; + } + 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; + }; + return JsonqlEventService; + }()); + +})); diff --git a/packages/event/index.ts b/packages/event/index.ts index dd4076cc..13318338 100644 --- a/packages/event/index.ts +++ b/packages/event/index.ts @@ -1,4 +1,2 @@ // The top level for @jsonql/event -import * as JsonqlEventService from './src/event-service'; - -export default JsonqlEventService; +export * from './src/event-service'; diff --git a/packages/event/package.json b/packages/event/package.json index c70ed61f..5870ba3f 100644 --- a/packages/event/package.json +++ b/packages/event/package.json @@ -2,8 +2,9 @@ "name": "@jsonql/event", "version": "1.0.0", "description": "Ported from nb-event-service rewritten with Typescript", - "main": "dist/index.js", - "module": "dist/index.es.js", + "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", "files": [ "dist", diff --git a/packages/event/rollup.config.js b/packages/event/rollup.config.js index 4ad0797a..7220fd53 100644 --- a/packages/event/rollup.config.js +++ b/packages/event/rollup.config.js @@ -11,6 +11,11 @@ export default { { file: pkg.module, format: 'es' + }, + { + file: pkg.browser, + format: 'umd', + name: 'JsonqlEventService' } ], external: [ -- Gitee From 6e9e96a2b8a3e6650c72b00e58cb82ada712c240 Mon Sep 17 00:00:00 2001 From: Joel Chu Date: Mon, 15 Jul 2019 09:43:39 +0800 Subject: [PATCH 18/23] Add README --- packages/event/README.md | 27 +++++++++++++++++++++++++++ packages/event/tests/basic.test.js | 8 ++------ 2 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 packages/event/README.md diff --git a/packages/event/README.md b/packages/event/README.md new file mode 100644 index 00000000..8e9410a9 --- /dev/null +++ b/packages/event/README.md @@ -0,0 +1,27 @@ +# @jsonql/event + +This is ported from [nb-event-service]() and rewritten with Typescript + +The different is - no alias options + +## Installation + +You don't usually use this directly, this is part of the `@jsonql` modules. + +But it's design to work standalone: + +```sh +$ npm i @jsonql/event +``` + +For directly use in browser, you can use the `dist/jsonql-event-service.umd.js` + +## API + + + +--- + +ISC + +[Joel Chu](https://joelchu.com) (c) 2019 - [NEWBRAN LTD UK](https://newbran.ch) / [TO1SOURCE CN](https://to1source.cn) diff --git a/packages/event/tests/basic.test.js b/packages/event/tests/basic.test.js index 505f85da..c2c56bcd 100644 --- a/packages/event/tests/basic.test.js +++ b/packages/event/tests/basic.test.js @@ -1,16 +1,12 @@ const test = require('ava') // import the cjs version for testing -const EventService = require('../dist/index') +const { JsonqlEventService } = require('../dist/index') const logger = require('debug')('nb-event-service') const debug = require('debug')('nb-event-service:test:basic') let value = 1000; -const NBEventService = EventService.default; - -// debug(NBEventService) - test.before( t => { - t.context.evtSrv = new NBEventService(logger) + t.context.evtSrv = new JsonqlEventService(logger) }) test.skip('It should able to validate the evt', t => { -- Gitee From f80e330a9b8b5112e58b5b2b316807eefd75ff08 Mon Sep 17 00:00:00 2001 From: Joel Chu Date: Mon, 15 Jul 2019 09:59:05 +0800 Subject: [PATCH 19/23] Finally got the import export right with a names JsonqlEventService --- packages/event/dist/jsonql-event-service.cjs.js | 4 ++++ packages/event/dist/jsonql-event-service.es.js | 2 ++ packages/event/dist/jsonql-event-service.umd.js | 13 +++++++++---- packages/event/index.ts | 5 ++++- packages/event/src/event-service.ts | 2 +- packages/event/tests/basic.test.js | 4 +++- 6 files changed, 23 insertions(+), 7 deletions(-) diff --git a/packages/event/dist/jsonql-event-service.cjs.js b/packages/event/dist/jsonql-event-service.cjs.js index fb68bc14..65d55bfd 100644 --- a/packages/event/dist/jsonql-event-service.cjs.js +++ b/packages/event/dist/jsonql-event-service.cjs.js @@ -1,5 +1,7 @@ 'use strict'; +Object.defineProperty(exports, '__esModule', { value: true }); + /** * generate a 32bit hash based on the function.toString() * _from http://stackoverflow.com/questions/7616461/generate-a-hash-_from-string-in-javascript-jquery @@ -541,3 +543,5 @@ var JsonqlEventService = /** @class */ (function () { }; return JsonqlEventService; }()); + +exports.JsonqlEventService = JsonqlEventService; diff --git a/packages/event/dist/jsonql-event-service.es.js b/packages/event/dist/jsonql-event-service.es.js index 34b59b79..9c96af46 100644 --- a/packages/event/dist/jsonql-event-service.es.js +++ b/packages/event/dist/jsonql-event-service.es.js @@ -539,3 +539,5 @@ var JsonqlEventService = /** @class */ (function () { }; return JsonqlEventService; }()); + +export { JsonqlEventService }; diff --git a/packages/event/dist/jsonql-event-service.umd.js b/packages/event/dist/jsonql-event-service.umd.js index 48720813..f6e0a473 100644 --- a/packages/event/dist/jsonql-event-service.umd.js +++ b/packages/event/dist/jsonql-event-service.umd.js @@ -1,7 +1,8 @@ -(function (factory) { - typeof define === 'function' && define.amd ? define(factory) : - factory(); -}(function () { 'use strict'; +(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'; /** * generate a 32bit hash based on the function.toString() @@ -545,4 +546,8 @@ return JsonqlEventService; }()); + exports.JsonqlEventService = JsonqlEventService; + + Object.defineProperty(exports, '__esModule', { value: true }); + })); diff --git a/packages/event/index.ts b/packages/event/index.ts index 13318338..879d21b2 100644 --- a/packages/event/index.ts +++ b/packages/event/index.ts @@ -1,2 +1,5 @@ // The top level for @jsonql/event -export * from './src/event-service'; + +// 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 75c7ca4b..679ec3f7 100644 --- a/packages/event/src/event-service.ts +++ b/packages/event/src/event-service.ts @@ -2,7 +2,7 @@ import './interfaces.ts'; import hashFnToKey from './hash-code'; // main -export default class JsonqlEventService { +export class JsonqlEventService { // public props keep: boolean; // private props diff --git a/packages/event/tests/basic.test.js b/packages/event/tests/basic.test.js index c2c56bcd..d7f37a60 100644 --- a/packages/event/tests/basic.test.js +++ b/packages/event/tests/basic.test.js @@ -1,10 +1,12 @@ const test = require('ava') // import the cjs version for testing -const { JsonqlEventService } = require('../dist/index') +const { JsonqlEventService } = require('../dist/jsonql-event-service.cjs') const logger = require('debug')('nb-event-service') const debug = require('debug')('nb-event-service:test:basic') let value = 1000; +debug(JsonqlEventService) + test.before( t => { t.context.evtSrv = new JsonqlEventService(logger) }) -- Gitee From e55c90b8a96465f817b161dbaf37be6cc9dfdf18 Mon Sep 17 00:00:00 2001 From: Joel Chu Date: Mon, 15 Jul 2019 10:14:01 +0800 Subject: [PATCH 20/23] rename all the import --- packages/event/tests/fixtures/index.html | 8 ++++---- packages/event/tests/lazy.test.js | 4 ++-- packages/event/tests/once-problem.test.js | 8 +++----- packages/event/tests/only-once.test.js | 6 ++---- packages/event/tests/only.test.js | 6 ++---- packages/event/tests/replace.test.js | 6 ++---- 6 files changed, 15 insertions(+), 23 deletions(-) diff --git a/packages/event/tests/fixtures/index.html b/packages/event/tests/fixtures/index.html index e794df2a..2886d642 100644 --- a/packages/event/tests/fixtures/index.html +++ b/packages/event/tests/fixtures/index.html @@ -1,17 +1,17 @@ - NBEventService test in browser + JsonqlEventService test in browser -

NBEventService test in browser

+

JsonqlEventService test in browser

Just look at the console log

+