Skip to content

Commit

Permalink
feat: back event name
Browse files Browse the repository at this point in the history
  • Loading branch information
betula committed Dec 23, 2023
1 parent 2c0324a commit 9992b37
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 25 deletions.
12 changes: 6 additions & 6 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ npm install ya-signals
- [`untracked(fn)`](#untrackedfn)
- [`transaction(fn)`](#transactionfn)
- [Extra API](#extra-api)
- [Simple and fast actions abstraction](#simple-and-fast-actions-abstraction)
- [Simple and fast events abstraction](#simple-and-fast-events-abstraction)
- [Automatic unsubscription control](#automatic-unsubscription-control)
- [On demand services](#on-demand-services)
- [Isolated services scope for SSR support](#isolated-services-scope-for-ssr-support)
Expand Down Expand Up @@ -306,17 +306,17 @@ transaction(() => {

## Extra API

### Simple and fast actions abstraction
### Simple and fast events abstraction

```typescript
import { action } from "ya-signals";
import { event } from "ya-signals"

const userLoggedIn = action()
const userLoggedIn = event()

// subscribe to the action
// subscribe to the event
userLoggedIn.subscribe(listener)

// call the action
// call the event
userLoggedIn()
```

Expand Down
32 changes: 27 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

_React application architecture on MobX._

[![npm version](https://img.shields.io/npm/v/ya-signals?style=flat-square)](https://www.npmjs.com/package/ya-signals)
[![npm bundle size](https://img.shields.io/bundlephobia/minzip/ya-signals?style=flat-square)](https://bundlephobia.com/result?p=ya-signals)
[![npm version](https://img.shields.io/npm/v/ya-signals?style=flat-square)](https://www.npmjs.com/package/ya-signals) [![npm bundle size](https://img.shields.io/bundlephobia/minzip/ya-signals?style=flat-square)](https://bundlephobia.com/result?p=ya-signals)

## Installation

Expand Down Expand Up @@ -51,11 +50,33 @@ service.destroy(userService);

### Describe component logic in OOP-style

```typescript
import { hook, un } from "ya-signals";

class RecipeForm {
constructor() {
un(() => {
// destroy
})
}
}

export const useRecipeForm = hook(RecipeForm)

// Somewhere in React component
const form = useRecipeForm()
```

**And it can be with params of course**

```typescript
import { hook, un, type SignalReadonly } from "ya-signals";

export class RecipeForm {
constructor(signalParams: SignalReadonly<[number, string]>) {
// Can be object struct with named fields
type Params = [number, string];

class RecipeForm {
constructor(signalParams: SignalReadonly<Params>) {
un(() => {
// destroy
})
Expand All @@ -75,7 +96,8 @@ Somewhere inside React component function
import { useRecipeForm } from './recipe-form.ts';

function Form() {
const form = useRecipeForm([10, 'hello']); // params available here
// Params available here
const form = useRecipeForm([10, 'hello']);

return <>
// ...
Expand Down
10 changes: 5 additions & 5 deletions src/action.ts → src/event.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { event, listen } from 'evemin';
import { event as originEvent, listen } from 'evemin';
import { un } from 'unsubscriber';

export interface Action<T> {
export interface Event<T> {
<T>(value: T): void;
subscribe(listener: (value: T) => void): (() => void);
}

export interface LightAction extends Action<void> {
export interface LightEvent extends Event<void> {
(): void;
subscribe(listener: () => void): (() => void);
}

export const action = <T = void>(): T extends void ? LightAction : Action<T> => {
const fn = event() as any;
export const event = <T = void>(): T extends void ? LightEvent : Event<T> => {
const fn = originEvent() as any;
fn.subscribe = (listener) => un(listen(fn, listener));
return fn;
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ export { un } from 'unsubscriber';

export { autorun, reaction, sync, when } from './reaction';

export { action, type Action, type LightAction } from './action';
export { event, type Event, type LightEvent } from './event';
export { service } from './service';
export { hook } from './hook';
4 changes: 2 additions & 2 deletions src/reaction.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { un } from "unsubscriber";
import { un } from 'unsubscriber';
import {
autorun as autorunOrigin,
reaction as reactionOrigin,
when as whenOrigin
} from "mobx";
} from 'mobx';

export const autorun = (expression: () => void) => (
un(autorunOrigin(expression))
Expand Down
2 changes: 1 addition & 1 deletion src/service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { provide, destroy } from "provi/client"
import { provide, destroy } from 'provi/client'

const INSTANTIATE_KEY = Symbol('instantiate');
const DESTROY_KEY = Symbol('destroy');
Expand Down
10 changes: 5 additions & 5 deletions test/action.test.ts → test/event.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { action } from "../src";
import { event } from "../src";

it('light action works', () => {
it('light event works', () => {
const spy = jest.fn();

const a = action();
const a = event();
a.subscribe(spy);

expect(spy).not.toBeCalled();
Expand All @@ -13,10 +13,10 @@ it('light action works', () => {
expect(spy).toBeCalledTimes(2);
});

it('params action works', () => {
it('params event works', () => {
const spy = jest.fn();

const a = action<number>();
const a = event<number>();
a.subscribe(spy);

expect(spy).not.toBeCalled();
Expand Down

0 comments on commit 9992b37

Please sign in to comment.