Add Deno support

This commit is contained in:
Fabian Stamm 2020-10-15 01:48:52 +02:00
parent 6000e69dcd
commit ef423a947a
8 changed files with 128 additions and 17 deletions

13
meta.json Normal file
View File

@ -0,0 +1,13 @@
{
"name": "utils",
"version": "2.2.11",
"description": "Some helpful utility classes and functions",
"author": "Fabian Stamm <dev@fabianstamm.de>",
"contributors": [],
"root": "./esm/",
"files": ["**/*.ts", "**/*.js", "README.md"],
"hooks": {
"prepublish": "pre.js"
},
"readme": "readme.md"
}

8
package-lock.json generated
View File

@ -1,13 +1,13 @@
{ {
"name": "@hibas123/utils", "name": "@hibas123/utils",
"version": "2.2.4", "version": "2.2.10",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
"typescript": { "typescript": {
"version": "3.4.5", "version": "4.0.3",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-3.4.5.tgz", "resolved": "https://npm.hibas123.de/typescript/-/typescript-4.0.3.tgz",
"integrity": "sha512-YycBxUb49UUhdNMU5aJ7z5Ej2XGmaIBL0x34vZ82fn3hGvD+bgrMrVDpatgz2f7YxUMJxMkbWxJZeAvDxVe7Vw==", "integrity": "sha512-tEu6DGxGgRJPb/mVPIZ48e69xCn2yRmCgYmDugAVwmJ6o+0u1RI18eO7E7WBTLYLaEVVOhwQmcdhQHweux/WPg==",
"dev": true "dev": true
} }
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "@hibas123/utils", "name": "@hibas123/utils",
"version": "2.2.10", "version": "2.2.11",
"description": "Different Utilities, that are not worth own packages", "description": "Different Utilities, that are not worth own packages",
"main": "lib/index.js", "main": "lib/index.js",
"types": "lib/index.d.ts", "types": "lib/index.d.ts",
@ -8,7 +8,8 @@
"scripts": { "scripts": {
"prepublishOnly": "npm run build", "prepublishOnly": "npm run build",
"build": "tsc && tsc -p tsconfig.esm.json", "build": "tsc && tsc -p tsconfig.esm.json",
"watch-ts": "tsc -w" "watch-ts": "tsc -w",
"postpublish": "denreg publish"
}, },
"author": "Fabian Stamm <dev@fabianstamm.de>", "author": "Fabian Stamm <dev@fabianstamm.de>",
"license": "MIT", "license": "MIT",
@ -17,7 +18,7 @@
"type": "git" "type": "git"
}, },
"devDependencies": { "devDependencies": {
"typescript": "^3.4.5" "typescript": "^4.0.3"
}, },
"files": [ "files": [
"src/", "src/",

15
pre.js Normal file
View File

@ -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");

71
src/asynciter.ts Normal file
View File

@ -0,0 +1,71 @@
import Observable from "./observable.js";
import Signal from "./signal.js";
interface IAsyncIteratorMessage<T> {
error: Error | undefined;
data: T | undefined;
close: true | undefined;
}
export default class AsyncIteratorFromCB<T> implements AsyncIterable<T> {
private onData = new Observable<IAsyncIteratorMessage<T>>();
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<T>[] = [];
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();
}
},
};
}
}

View File

@ -6,12 +6,11 @@ import Observable, {
} from "./observable.js"; } from "./observable.js";
import AwaitStore from "./awaiter.js"; import AwaitStore from "./awaiter.js";
export { export type {
Lock,
Release, Release,
Observable,
ObserverCallback, ObserverCallback,
ObserverCallbackCollect, ObserverCallbackCollect,
ObservableInterface, ObservableInterface,
AwaitStore,
}; };
export { Lock, Observable, AwaitStore };

12
src/signal.ts Normal file
View File

@ -0,0 +1,12 @@
export default class Signal {
awaiter: (() => void)[] = [];
sendSignal(): void {
this.awaiter.forEach((a) => a());
this.awaiter = [];
}
awaitSignal(): Promise<void> {
return new Promise((resolve) => {
this.awaiter.push(resolve);
});
}
}

View File

@ -1,15 +1,15 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "es2015", "target": "esnext",
"module": "commonjs", "module": "commonjs",
"moduleResolution": "node", "moduleResolution": "node",
"outDir": "lib", "outDir": "lib",
"preserveWatchOutput": true, "preserveWatchOutput": true,
"declaration": true, "declaration": true,
"sourceMap": true, "sourceMap": true,
"strict": true "strict": true,
"importsNotUsedAsValues": "error",
"isolatedModules": true
}, },
"include": [ "include": ["./src"]
"./src"
]
} }