Skip to content

Commit

Permalink
fix Non-blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
dineug committed Apr 13, 2023
1 parent 0549ccb commit 8296b76
Show file tree
Hide file tree
Showing 13 changed files with 26 additions and 103 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ like [co](https://github.com/tj/co), or [redux-saga](https://redux-saga.js.org)
- [API](https://github.com/dineug/go/tree/main/packages/go#api)
- [go](https://github.com/dineug/go/tree/main/packages/go#go)
- [all](https://github.com/dineug/go/tree/main/packages/go#all)
- [call](https://github.com/dineug/go/tree/main/packages/go#call)
- [cancel](https://github.com/dineug/go/tree/main/packages/go#cancel)
- [debounce](https://github.com/dineug/go/tree/main/packages/go#debounce)
- [delay](https://github.com/dineug/go/tree/main/packages/go#delay)
- [flush](https://github.com/dineug/go/tree/main/packages/go#flush)
- [fork](https://github.com/dineug/go/tree/main/packages/go#fork)
- [kill](https://github.com/dineug/go/tree/main/packages/go#kill)
- [put](https://github.com/dineug/go/tree/main/packages/go#put)
- [race](https://github.com/dineug/go/tree/main/packages/go#race)
Expand Down
68 changes: 12 additions & 56 deletions packages/go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ like [co](https://github.com/tj/co), or [redux-saga](https://redux-saga.js.org)
- [API](#api)
- [go](#go)
- [all](#all)
- [call](#call)
- [cancel](#cancel)
- [debounce](#debounce)
- [delay](#delay)
- [flush](#flush)
- [fork](#fork)
- [kill](#kill)
- [put](#put)
- [race](#race)
Expand All @@ -36,12 +34,10 @@ npm install @dineug/go
```ts
import {
all,
call,
channel,
CO,
delay,
flush,
fork,
go,
put,
race,
Expand All @@ -52,15 +48,15 @@ const ch = channel();

const foo: CO = function* () {
const value = yield take(ch);
const value2 = yield call(v => v, value);
const value2 = yield go(v => v, value);

yield fork(function* () {
go(function* () {
const value = yield take(ch);
});

const values = yield all([
call(() => 1),
fork(() => 2),
go(() => 1),
go(() => 2),
Promise.resolve(3),
Promise.resolve(4),
5,
Expand Down Expand Up @@ -154,8 +150,7 @@ Same as Promise.all

```js
all([
call(() => 1),
fork(() => 2),
go(() => 1),
Promise.resolve(3),
Promise.resolve(4),
5,
Expand All @@ -175,22 +170,6 @@ const all = values =>
});
```

### call

Same as go

#### Example

```js
call(function* () {});
```

#### low-level operator

```js
const call = go;
```

### cancel

Cancel the promise
Expand Down Expand Up @@ -241,7 +220,7 @@ debounce(ch, function* () {}, 1000);
#### low-level operator

```js
const debounce = (channel, callback, ms) => {
const debounce = (channel, callback, ms) =>
go(function* () {
let timerId = -1;

Expand All @@ -252,7 +231,6 @@ const debounce = (channel, callback, ms) => {
timerId = window.setTimeout(go, ms, callback, value);
}
});
};
```

### delay
Expand Down Expand Up @@ -289,24 +267,6 @@ const flush = channel =>
new Promise((resolve, reject) => channel.flush(resolve, reject));
```

### fork

#### Example

```js
go(function* () {
yield fork(function* () {});
});
```

#### low-level operator

```js
const fork = (callback, ...args) => {
go(callback, ...args);
};
```

### kill

Exit All
Expand All @@ -315,8 +275,8 @@ Exit All

```js
go(function* () {
yield call(function* () {
yield call(function* () {
yield go(function* () {
yield go(function* () {
yield kill();
});
});
Expand Down Expand Up @@ -426,14 +386,13 @@ takeEvery(ch, function* () {});
#### low-level operator

```js
const takeEvery = (channel, callback) => {
const takeEvery = (channel, callback) =>
go(function* () {
while (true) {
const value = yield take(channel);
go(callback, value);
}
});
};
```

### takeLatest
Expand All @@ -447,7 +406,7 @@ takeLatest(ch, function* () {});
#### low-level operator

```js
const takeLatest = (channel, callback) => {
const takeLatest = (channel, callback) =>
go(function* () {
let lastTask;

Expand All @@ -457,7 +416,6 @@ const takeLatest = (channel, callback) => {
lastTask = go(callback, value);
}
});
};
```

### takeLeading
Expand All @@ -471,7 +429,7 @@ takeLeading(ch, function* () {});
#### low-level operator

```js
const takeLeading = (channel, callback) => {
const takeLeading = (channel, callback) =>
go(function* () {
let executable = true;

Expand All @@ -486,7 +444,6 @@ const takeLeading = (channel, callback) => {
}
}
});
};
```

### throttle
Expand All @@ -510,7 +467,7 @@ const defaultConfig: Required<ThrottleConfig> = {
trailing: false,
};

const throttle = (channel, callback, ms, config) => {
const throttle = (channel, callback, ms, config) =>
go(function* () {
const options = Object.assign({}, defaultConfig, {
...config,
Expand Down Expand Up @@ -541,5 +498,4 @@ const throttle = (channel, callback, ms, config) => {
}, ms);
}
});
};
```
2 changes: 1 addition & 1 deletion packages/go/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dineug/go",
"version": "0.1.4",
"version": "0.1.5",
"description": "Promise Extension Library",
"main": "dist/go.js",
"module": "dist/go.esm.js",
Expand Down
22 changes: 5 additions & 17 deletions packages/go/src/index.dev.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
import {
all,
call,
channel,
CO,
delay,
flush,
fork,
go,
put,
race,
take,
} from '@/index';
import { all, channel, CO, delay, flush, go, put, race, take } from '@/index';

const inputChannel = channel<number>();
const outputChannel = channel<number>();
Expand All @@ -19,18 +7,18 @@ const foo: CO = function* () {
console.log('start');
const value = yield take(inputChannel);
console.log('take:value', value);
const value2 = yield call((v: any) => v, value);
const value2 = yield go((v: any) => v, value);
console.log('call:value', value2);
yield put(outputChannel, 1234);

yield fork(function* () {
go(function* () {
const value = yield take(inputChannel);
console.log('fork:take:value', value);
});

const values = yield all([
call(() => 1),
fork(() => 2),
go(() => 1),
go(() => 2),
Promise.resolve(3),
Promise.resolve(4),
5,
Expand Down
3 changes: 0 additions & 3 deletions packages/go/src/operators/call.ts

This file was deleted.

3 changes: 1 addition & 2 deletions packages/go/src/operators/debounce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const debounce = <
channel: Channel<T>,
callback: F,
ms: number
) => {
) =>
go(function* () {
let timerId = -1;

Expand All @@ -20,4 +20,3 @@ export const debounce = <
timerId = window.setTimeout(go, ms, callback, value);
}
});
};
9 changes: 0 additions & 9 deletions packages/go/src/operators/fork.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/go/src/operators/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
export * from '@/operators/all';
export * from '@/operators/call';
export * from '@/operators/cancel';
export * from '@/operators/debounce';
export * from '@/operators/delay';
export * from '@/operators/flush';
export * from '@/operators/fork';
export * from '@/operators/kill';
export * from '@/operators/put';
export * from '@/operators/race';
Expand Down
2 changes: 1 addition & 1 deletion packages/go/src/operators/take.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Channel } from '@/channel';
import { type PromiseWithCancel, go } from '@/go';
import { go, type PromiseWithCancel } from '@/go';

export const take = <T = any>(channel: Channel<T>) =>
go(function* () {
Expand Down
5 changes: 2 additions & 3 deletions packages/go/src/operators/takeEvery.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { type TakeCallback, Channel } from '@/channel';
import { Channel, type TakeCallback } from '@/channel';
import { go } from '@/go';
import { take } from '@/operators/take';

export const takeEvery = <T = any, F extends TakeCallback<T> = TakeCallback<T>>(
channel: Channel<T>,
callback: F
) => {
) =>
go(function* () {
while (true) {
const value = yield take(channel);
// @ts-ignore
go(callback, value);
}
});
};
5 changes: 2 additions & 3 deletions packages/go/src/operators/takeLatest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Channel } from '@/channel';
import { type PromiseWithCancel, go } from '@/go';
import { go, type PromiseWithCancel } from '@/go';
import { cancel } from '@/operators/cancel';
import { take } from '@/operators/take';

Expand All @@ -9,7 +9,7 @@ export const takeLatest = <
>(
channel: Channel<T>,
callback: F
) => {
) =>
go(function* () {
let lastTask: PromiseWithCancel | undefined;

Expand All @@ -20,4 +20,3 @@ export const takeLatest = <
lastTask = go(callback, value);
}
});
};
3 changes: 1 addition & 2 deletions packages/go/src/operators/takeLeading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const takeLeading = <
>(
channel: Channel<T>,
callback: F
) => {
) =>
go(function* () {
let executable = true;

Expand All @@ -24,4 +24,3 @@ export const takeLeading = <
}
}
});
};
3 changes: 1 addition & 2 deletions packages/go/src/operators/throttle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const throttle = <
callback: F,
ms: number,
config?: ThrottleConfig
) => {
) =>
go(function* () {
const options = Object.assign({}, defaultConfig, {
...config,
Expand Down Expand Up @@ -53,4 +53,3 @@ export const throttle = <
}, ms);
}
});
};

0 comments on commit 8296b76

Please sign in to comment.