diff --git a/package.json b/package.json index 8fed9eef..d42e5296 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "zeed", "type": "module", - "version": "0.13.21", + "version": "0.13.22", "description": "🌱 Simple foundation library", "author": { "name": "Dirk Holtwick", diff --git a/src/common/data/array.spec.ts b/src/common/data/array.spec.ts index 580ee3d0..7db44331 100644 --- a/src/common/data/array.spec.ts +++ b/src/common/data/array.spec.ts @@ -11,6 +11,7 @@ import { arrayMin, arrayMinus, arrayRemoveElement, + arraySetArrayInPlace, arrayShuffleForce, arraySorted, arraySortedNumbers, @@ -27,6 +28,12 @@ describe('Array', () => { expect(r).toEqual([1, 3, 4]) }) + it('should set in place items', () => { + const r = [1, 2, 3] + arraySetArrayInPlace(r, [9, 8, 7]) + expect(r).toEqual([9, 8, 7]) + }) + it('should intersect', () => { expect(arrayIntersection([1, 1, 2, 2, 3], [2, 2, 3, 5, 6])).toEqual([2, 3]) expect(arraySymmetricDifference([1, 1, 2, 2, 3], [2, 2, 3, 5, 6])).toEqual([ diff --git a/src/common/data/array.ts b/src/common/data/array.ts index 13b5d430..92a176f0 100644 --- a/src/common/data/array.ts +++ b/src/common/data/array.ts @@ -73,6 +73,11 @@ export function arrayEmptyInPlace(array: T[]): T[] { return array } +export function arraySetArrayInPlace(array: T[], newContent: T[]): T[] { + array.splice(0, array.length, ...newContent) + return array +} + export function arraySorted( arr: Iterable | ArrayLike, cond: ((a: T, b: T) => number) | undefined = cmp, diff --git a/src/common/msg/rpc.ts b/src/common/msg/rpc.ts index 71d63a41..c191204b 100644 --- a/src/common/msg/rpc.ts +++ b/src/common/msg/rpc.ts @@ -1,6 +1,6 @@ // From https://github.com/antfu/birpc/blob/main/src/index.ts MIT -import { LoggerInterface } from "../log/log-base" +import type { LoggerInterface } from '../log/log-base' export type ArgumentsType = T extends (...args: infer A) => any ? A : never export type ReturnType = T extends (...args: any) => infer R ? R : never diff --git a/src/common/pipes/types.spec.ts b/src/common/pipes/types.spec.ts new file mode 100644 index 00000000..d6d9a38d --- /dev/null +++ b/src/common/pipes/types.spec.ts @@ -0,0 +1,26 @@ +import { Pipe } from "./types" + +describe("types.spec", () => { + it.skip("should pipe", async () => { + const p1: Pipe = { + post(s) { }, // todo + on: (fn) => { }, // todo + serialize(o) { + return JSON.stringify(o) + }, + deserialize(s) { + return JSON.parse(s) + }, + } + + async function echo(pipe: Pipe, o: object) { + let resolve:any + pipe.on(fn => resolve = fn) + pipe.post(o) + return new Promise(resolve) + } + + let x = await echo(p1, { a: 1 }) + expect(x).toMatchInlineSnapshot() + }) +}) \ No newline at end of file diff --git a/src/common/pipes/types.ts b/src/common/pipes/types.ts new file mode 100644 index 00000000..80128956 --- /dev/null +++ b/src/common/pipes/types.ts @@ -0,0 +1,31 @@ +// Experiment to replace "Channel" by "Pipe". +// Different naming just to avoid confusion. +// Goal: Simplify things by removing event handling etc. + +export interface Pipe { + /** Function to post raw message */ + post: (data: PipeObjectData) => void + + /** Listener to receive raw message */ + on: (fn: (data: PipeObjectData) => void) => void + + /** Custom function to serialize data */ + serialize?: (data: PipeObjectData) => PipeRawData + + /** Custom function to deserialize data */ + deserialize?: (data: PipeRawData) => PipeObjectData +} + +export interface PipeAsync { + /** Function to post raw message */ + post: (data: PipeObjectData) => Promise + + /** Listener to receive raw message */ + on: (fn: (data: PipeObjectData) => Promise) => void + + /** Custom function to serialize data */ + serialize?: (data: PipeObjectData) => Promise + + /** Custom function to deserialize data */ + deserialize?: (data: PipeRawData) => Promise +}