Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: City #17

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/event/disposable/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# @x-oasis/disposable

## Installation

```bash
$ npm i @x-oasis/disposable
```

## How to use

```typescript
import disposable from '@x-oasis/disposable'
```
25 changes: 25 additions & 0 deletions packages/event/disposable/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "@x-oasis/disposable",
"version": "0.1.34",
"description": "disposable function",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"module": "dist/disposable.esm.js",
"files": [
"dist",
"index.ts",
"src"
],
"scripts": {
"build": "tsdx build --tsconfig tsconfig.build.json",
"clean": "rimraf ./dist",
"test": "vitest",
"compile": "tsc -p tsconfig.build.json"
},
"author": "",
"license": "ISC",
"devDependencies": {
"tsdx": "^0.14.1"
},
"dependencies": {}
}
34 changes: 34 additions & 0 deletions packages/event/disposable/src/Disposable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import isObject from '@x-oasis/is-object';
import { IDisposable } from './types/disposable';
import DisposableStore from './DisposableStore';

const isFunction = (o) => typeof o === 'function';

export function isDisposable<T extends object>(
thing: T
): thing is T & IDisposable {
return isObject(thing) && isFunction((thing as IDisposable).dispose);
}

export function toDisposable(fn: Function): IDisposable {
return {
dispose: () => fn(),
};
}

export class Disposable implements IDisposable {
static readonly None = Object.freeze<IDisposable>({ dispose() {} });

private readonly _store = new DisposableStore();

public dispose(): void {
this._store.dispose();
}

registerDisposable<T extends IDisposable>(disposable: T) {
if ((disposable as any) === this) {
throw new Error('Can not register itself');
}
this._store.add(disposable);
}
}
99 changes: 99 additions & 0 deletions packages/event/disposable/src/DisposableStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { isArray } from '@redcity/core/common/assertion/types';
import { Iterable } from '@redcity/core/common/assertion/iterable';
import { IDisposable } from './types/disposable';
import { isDisposable } from './Disposable';

export function dispose<T extends IDisposable>(disposable: T): T;
export function dispose<T extends IDisposable>(
disposable: T | undefined
): T | undefined;
export function dispose<T extends IDisposable>(
arg: T | Iterable<T> | undefined
): any;
export function dispose<T extends IDisposable>(
disposables: T[]
): T | T[] | undefined {
const errors = [];
if (isArray(disposables)) {
disposables.forEach((disposable) => {
try {
disposable?.dispose();
} catch (err) {
errors.push(err);
}
});
return [];
}
if (isDisposable<IDisposable>(disposables)) {
try {
disposables.dispose();
} catch (err) {
errors.push(err);
}
return disposables;
}
if (Iterable.is<IDisposable>(disposables)) {
for (const disposable of disposables) {
try {
disposable?.dispose();
} catch (err) {
errors.push(err);
}
}
}
}

export default class DisposableStore implements IDisposable {
private readonly _toDispose = new Set<IDisposable>();

private _isDisposed = false;

public dispose(): void {
if (this._isDisposed) {
return;
}

this._isDisposed = true;
this.clear();
}

public get isDisposed(): boolean {
return this._isDisposed;
}

public clear(): void {
if (this._toDispose.size === 0) {
return;
}

dispose(this._toDispose);

this._toDispose.clear();
}

public add<T extends IDisposable>(thing: T): T {
if (!thing) return thing;
if ((thing as unknown as DisposableStore) === this) {
throw new Error('Cannot register a disposable on itself!');
}

if (this._isDisposed) {
// ...
} else {
this._toDispose.add(thing);
}

return thing;
}

public delete<T extends IDisposable>(thing: T): void {
if (!thing) {
return;
}
if ((thing as unknown as DisposableStore) === this) {
throw new Error('Cannot dispose a disposable on itself!');
}
this._toDispose.delete(thing);
thing.dispose();
}
}
3 changes: 3 additions & 0 deletions packages/event/disposable/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { Disposable } from './Disposable';
export { default as DisposableStore } from './DisposableStore';
export * from './types';
3 changes: 3 additions & 0 deletions packages/event/disposable/src/types/disposable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface IDisposable {
dispose(): void;
}
1 change: 1 addition & 0 deletions packages/event/disposable/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './disposable';
7 changes: 7 additions & 0 deletions packages/event/disposable/test/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { expect, test } from 'vitest';
import { Disposable } from '../src';

test('disposable', async () => {
const disposable = new Disposable();
expect(disposable).toBe(Disposable);
});
12 changes: 12 additions & 0 deletions packages/event/disposable/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "../../../tsconfig.build.json",
"compilerOptions": {
"outDir": "./dist",
"esModuleInterop": true
},

"include": [
"index.ts",
"src/**/*"
]
}
7 changes: 7 additions & 0 deletions packages/event/disposable/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"jsx": "react",
"esModuleInterop": true
}
}
26 changes: 26 additions & 0 deletions packages/event/disposable/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// import path from 'path';
// import tsPath from 'vite-tsconfig-paths';
import { defineConfig } from 'vitest/config';

// const __dirname = import.meta.url.slice(7, import.meta.url.lastIndexOf('/'));

export default defineConfig({
test: {
globals: true,
include: ['test/**/*.(spec|test).ts'],
exclude: ['node_modules/**'],
threads: false,

coverage: {
provider: 'istanbul', // or 'c8'
},
},

// plugins: [tsPath()],
resolve: {
alias: {},
},
define: {
__DEV__: false,
},
});
13 changes: 13 additions & 0 deletions packages/event/emitter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# @x-oasis/each

## Installation

```bash
$ npm i @x-oasis/each
```

## How to use

```typescript
import each from '@x-oasis/each'
```
27 changes: 27 additions & 0 deletions packages/event/emitter/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@x-oasis/each",
"version": "0.1.34",
"description": "each function",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"module": "dist/each.esm.js",
"files": [
"dist",
"index.ts",
"src"
],
"scripts": {
"build": "tsdx build --tsconfig tsconfig.build.json",
"clean": "rimraf ./dist",
"test": "vitest",
"compile": "tsc -p tsconfig.build.json"
},
"author": "",
"license": "ISC",
"devDependencies": {
"tsdx": "^0.14.1"
},
"dependencies": {
"@x-oasis/is-object": "workspace:*"
}
}
20 changes: 20 additions & 0 deletions packages/event/emitter/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import isObject from '@x-oasis/is-object';

type EachArray<T> = (index: number, entry: any, obj: T) => void;
type EachObject<T> = <K extends keyof T>(key: K, entry: T[K], obj: T) => number;
type Iter<T extends Array<any> | { [key: string]: any }> = T extends Array<any>
? EachArray<T>
: T extends { [key: string]: any }
? EachObject<T>
: never;

export default function each<T>(obj: T, iter: Iter<T>) {
if (Array.isArray(obj)) {
(obj as Array<any>).forEach((entry, index) =>
(iter as EachArray<T>)(index, entry, obj)
);
} else if (isObject(obj)) {
// @ts-ignore
ownKeys(obj).forEach((key) => (iter as EachObject<T>)(key, obj[key], obj));
}
}
9 changes: 9 additions & 0 deletions packages/event/emitter/test/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { expect, test } from 'vitest';
import each from '../';

test('each', async () => {
const array = [6.1, 4.2, 6.3];
const actual = each(array, Math.floor);

expect(actual).toEqual({ '4': [4.2], '6': [6.1, 6.3] });
});
12 changes: 12 additions & 0 deletions packages/event/emitter/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "../../../tsconfig.build.json",
"compilerOptions": {
"outDir": "./dist",
"esModuleInterop": true
},

"include": [
"index.ts",
"src/**/*"
]
}
7 changes: 7 additions & 0 deletions packages/event/emitter/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"jsx": "react",
"esModuleInterop": true
}
}
26 changes: 26 additions & 0 deletions packages/event/emitter/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// import path from 'path';
// import tsPath from 'vite-tsconfig-paths';
import { defineConfig } from 'vitest/config';

// const __dirname = import.meta.url.slice(7, import.meta.url.lastIndexOf('/'));

export default defineConfig({
test: {
globals: true,
include: ['test/**/*.(spec|test).ts'],
exclude: ['node_modules/**'],
threads: false,

coverage: {
provider: 'istanbul', // or 'c8'
},
},

// plugins: [tsPath()],
resolve: {
alias: {},
},
define: {
__DEV__: false,
},
});
2 changes: 1 addition & 1 deletion packages/functional/each/test/test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from 'vitest';
import each from '../';
import each from '../src';

test('each', async () => {
const array = [6.1, 4.2, 6.3];
Expand Down
2 changes: 2 additions & 0 deletions packages/hypertext/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# @x-oasis/hypertext

19 changes: 19 additions & 0 deletions packages/promise/deferred/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# @x-oasis/deferred

## Installation

```bash
$ npm i @x-oasis/deferred
```

## How to use

```typescript
import deferred from '@x-oasis/deferred'
```

## How to run test

```bash
$ pnpm test
```
Loading
Loading