From ef423a947aaadb311efe5bfb67a22ebe78fec722 Mon Sep 17 00:00:00 2001 From: Fabian Stamm Date: Thu, 15 Oct 2020 01:48:52 +0200 Subject: [PATCH] Add Deno support --- meta.json | 13 +++++++++ package-lock.json | 8 +++--- package.json | 7 +++-- pre.js | 15 ++++++++++ src/asynciter.ts | 71 +++++++++++++++++++++++++++++++++++++++++++++++ src/index.ts | 7 ++--- src/signal.ts | 12 ++++++++ tsconfig.json | 12 ++++---- 8 files changed, 128 insertions(+), 17 deletions(-) create mode 100644 meta.json create mode 100644 pre.js create mode 100644 src/asynciter.ts create mode 100644 src/signal.ts diff --git a/meta.json b/meta.json new file mode 100644 index 0000000..02ef033 --- /dev/null +++ b/meta.json @@ -0,0 +1,13 @@ +{ + "name": "utils", + "version": "2.2.11", + "description": "Some helpful utility classes and functions", + "author": "Fabian Stamm ", + "contributors": [], + "root": "./esm/", + "files": ["**/*.ts", "**/*.js", "README.md"], + "hooks": { + "prepublish": "pre.js" + }, + "readme": "readme.md" +} diff --git a/package-lock.json b/package-lock.json index fe023cf..4edc3cf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,13 +1,13 @@ { "name": "@hibas123/utils", - "version": "2.2.4", + "version": "2.2.10", "lockfileVersion": 1, "requires": true, "dependencies": { "typescript": { - "version": "3.4.5", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.4.5.tgz", - "integrity": "sha512-YycBxUb49UUhdNMU5aJ7z5Ej2XGmaIBL0x34vZ82fn3hGvD+bgrMrVDpatgz2f7YxUMJxMkbWxJZeAvDxVe7Vw==", + "version": "4.0.3", + "resolved": "https://npm.hibas123.de/typescript/-/typescript-4.0.3.tgz", + "integrity": "sha512-tEu6DGxGgRJPb/mVPIZ48e69xCn2yRmCgYmDugAVwmJ6o+0u1RI18eO7E7WBTLYLaEVVOhwQmcdhQHweux/WPg==", "dev": true } } diff --git a/package.json b/package.json index 0871f83..4362850 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hibas123/utils", - "version": "2.2.10", + "version": "2.2.11", "description": "Different Utilities, that are not worth own packages", "main": "lib/index.js", "types": "lib/index.d.ts", @@ -8,7 +8,8 @@ "scripts": { "prepublishOnly": "npm run build", "build": "tsc && tsc -p tsconfig.esm.json", - "watch-ts": "tsc -w" + "watch-ts": "tsc -w", + "postpublish": "denreg publish" }, "author": "Fabian Stamm ", "license": "MIT", @@ -17,7 +18,7 @@ "type": "git" }, "devDependencies": { - "typescript": "^3.4.5" + "typescript": "^4.0.3" }, "files": [ "src/", diff --git a/pre.js b/pre.js new file mode 100644 index 0000000..e227355 --- /dev/null +++ b/pre.js @@ -0,0 +1,15 @@ +const rjson = async (file) => JSON.parse(await Deno.readTextFile(file)); +const wjson = (file, data) => + Deno.writeTextFile(file, JSON.stringify(data, undefined, 3)); + +const pkg = await rjson("package.json"); +const meta = await rjson("meta.json"); + +console.log("Changing meta.version from", meta.version, "to", pkg.version); +meta.version = pkg.version || meta.version; + +// await wjson("meta.json", meta); + +await Deno.copyFile("esm/index.js", "esm/mod.js"); +await Deno.copyFile("esm/index.d.ts", "esm/mod.d.ts"); +await Deno.copyFile("readme.md", "esm/readme.md"); diff --git a/src/asynciter.ts b/src/asynciter.ts new file mode 100644 index 0000000..931f67a --- /dev/null +++ b/src/asynciter.ts @@ -0,0 +1,71 @@ +import Observable from "./observable.js"; +import Signal from "./signal.js"; + +interface IAsyncIteratorMessage { + error: Error | undefined; + data: T | undefined; + close: true | undefined; +} +export default class AsyncIteratorFromCB implements AsyncIterable { + private onData = new Observable>(); + constructor() {} + + public getCallback() { + return (error: Error | undefined, data: T | undefined) => { + this.onData.send({ error, data, close: undefined }); + }; + } + + public send(data: T) { + this.onData.send({ + error: undefined, + data, + close: undefined, + }); + } + + public close() { + this.onData.send({ + close: true, + data: undefined, + error: undefined, + }); + this.onData.close(); + } + + [Symbol.asyncIterator]() { + const queue: IAsyncIteratorMessage[] = []; + const signal = new Signal(); + this.onData.subscribe((data) => { + queue.push(data); + signal.sendSignal(); + }); + return { + async next() { + const send = () => { + const value = queue.shift(); + if (!value) throw new Error("Error in AsyncIter"); + if (value.close) { + return { + done: true, + value: undefined as any, + }; + } else if (value.error) { + throw value.error; + } else { + return { + done: false, + value: value.data as T, + }; + } + }; + if (queue.length > 0) { + return send(); + } else { + await signal.awaitSignal(); + return send(); + } + }, + }; + } +} diff --git a/src/index.ts b/src/index.ts index de7691c..5f6e18e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,12 +6,11 @@ import Observable, { } from "./observable.js"; import AwaitStore from "./awaiter.js"; -export { - Lock, +export type { Release, - Observable, ObserverCallback, ObserverCallbackCollect, ObservableInterface, - AwaitStore, }; + +export { Lock, Observable, AwaitStore }; diff --git a/src/signal.ts b/src/signal.ts new file mode 100644 index 0000000..50ca389 --- /dev/null +++ b/src/signal.ts @@ -0,0 +1,12 @@ +export default class Signal { + awaiter: (() => void)[] = []; + sendSignal(): void { + this.awaiter.forEach((a) => a()); + this.awaiter = []; + } + awaitSignal(): Promise { + return new Promise((resolve) => { + this.awaiter.push(resolve); + }); + } +} diff --git a/tsconfig.json b/tsconfig.json index 4ded17b..67d1912 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,15 +1,15 @@ { "compilerOptions": { - "target": "es2015", + "target": "esnext", "module": "commonjs", "moduleResolution": "node", "outDir": "lib", "preserveWatchOutput": true, "declaration": true, "sourceMap": true, - "strict": true + "strict": true, + "importsNotUsedAsValues": "error", + "isolatedModules": true }, - "include": [ - "./src" - ] -} \ No newline at end of file + "include": ["./src"] +}