Skip to content

Commit

Permalink
chore: docs up
Browse files Browse the repository at this point in the history
  • Loading branch information
betula committed Dec 24, 2023
1 parent 35e95aa commit 2a21461
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 20 deletions.
44 changes: 33 additions & 11 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,34 +330,56 @@ un(() => {

### On demand services

[![Edit example in Codesandbox](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/p/sandbox/sleepy-field-79wqfh?file=%2Fsrc%2FApp.tsx%3A25%2C1-25%2C11)

```typescript
import { service, un } from "ya-signals";
import { service, makeObservable, observable } from "ya-signals";

// AppService.ts

class AppService {
public lang: string;

class UserServer {
constructor() {
un(() => {
// destroy
})
this.lang = "ru";

makeObservable(this, {
lang: observable.ref, // immutable value
});
}
}

// On demand service abstraction
export const userService = service(UserServer)
// Only Proxy for create class on demand in future
export const appService = service(AppService);
```

If you run `appService.user` in your code anywhere it's get app property for **on demand** created service

// If you run `userService.user` it's get user property for on demand created service
const user = userService.user
```typescript
import { observer } from "ya-signals";
import { appService } from "./AppService.ts"

// App.tsx

export const App = observer(() => {
return (
<div className="App">
<h1>App lang {appService.lang}</h1>
</div>
);
});
```

In rare cases when it's necessary to initialize a service without invoking any method.

```typescript
service.instantiate(userService)
service.instantiate(appService);
```

In rare case when it's necessary to destroy a service manually.

```typescript
service.destroy(userService);
service.destroy(appService);
```

### Isolated services scope for SSR support
Expand Down
44 changes: 35 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,56 @@ npm install ya-signals

### On demand services

[![Edit example in Codesandbox](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/p/sandbox/sleepy-field-79wqfh?file=%2Fsrc%2FApp.tsx%3A25%2C1-25%2C11)

```typescript
import { service } from "ya-signals";
import { service, makeObservable, observable } from "ya-signals";

// AppService.ts

class AppService {
public lang: string;

constructor() {
this.lang = "ru";

class UserServer {
constructor() {}
makeObservable(this, {
lang: observable.ref, // immutable value
});
}
}

// On demand service abstraction
export const userService = service(UserServer)
// Only Proxy for create class on demand in future
export const appService = service(AppService);
```

// If you run `userService.user` in your code anywhere it's get user property for on demand created service
const user = userService.user
If you run `appService.user` in your code anywhere it's get app property for **on demand** created service

```typescript
import { observer } from "ya-signals";
import { appService } from "./AppService.ts"

// App.tsx

export const App = observer(() => {
return (
<div className="App">
<h1>App lang {appService.lang}</h1>
</div>
);
});
```

In rare cases when it's necessary to initialize a service without invoking any method.

```typescript
service.instantiate(userService)
service.instantiate(appService);
```

In rare case when it's necessary to destroy a service manually.

```typescript
service.destroy(userService);
service.destroy(appService);
```

### Describe component logic in OOP-style
Expand Down

0 comments on commit 2a21461

Please sign in to comment.