Skip to content

Commit

Permalink
feat(release): v0.9.16
Browse files Browse the repository at this point in the history
  • Loading branch information
kodemon committed Aug 31, 2022
1 parent c256543 commit 3b62dde
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
17 changes: 16 additions & 1 deletion packages/mvc/src/Controller.ts
Expand Up @@ -2,7 +2,7 @@ import type { Subscription } from "rxjs";

import { Debounce } from "./Debounce";

export abstract class Controller<State extends {} = {}, Props extends {} = {}> {
export abstract class Controller<State extends JsonLike = {}, Props extends JsonLike = {}> {
static readonly state = {};

/**
Expand Down Expand Up @@ -74,6 +74,19 @@ export abstract class Controller<State extends {} = {}, Props extends {} = {}> {
|--------------------------------------------------------------------------------
*/

subscribe<K extends keyof State>(
name: K,
subscriber: (next: (value: State[K]) => void) => Subscription
): Promise<State[K]> {
this.subscriptions[name as string]?.unsubscribe();
return new Promise<State[K]>((resolve) => {
this.subscriptions[name as string] = subscriber((value) => {
this.setState(name, value);
resolve(value);
});
});
}

/**
* Wrapper method for controller setState. Enables the ability to predefine a
* state update function with a static supported key. Handy for creating lean
Expand Down Expand Up @@ -124,3 +137,5 @@ export abstract class Controller<State extends {} = {}, Props extends {} = {}> {
return actions;
}
}

type JsonLike = Record<string, any>;
53 changes: 27 additions & 26 deletions sandbox/react/src/Modules/Realms/Controllers/RealmsController.ts
Expand Up @@ -12,6 +12,31 @@ export class RealmsController extends Controller<State, Props> {
#filter: Filter = {};
#sort: -1 | 1 = 1;

/*
|--------------------------------------------------------------------------------
| Resolvers
|--------------------------------------------------------------------------------
*/

async resolve({ name }: Props = { name: this.#name }) {
this.#name = name; // set name provided through external properties
await this.#subscribeToRealms();
}

async #subscribeToRealms() {
await this.subscribe("realms", (resolve) =>
Realm.subscribe(
this.#filter,
{
sort: {
name: this.#sort
}
},
resolve
)
);
}

/*
|--------------------------------------------------------------------------------
| Options
Expand All @@ -28,36 +53,12 @@ export class RealmsController extends Controller<State, Props> {
}
};
}
this.subscribeToRealms();
this.#subscribeToRealms();
}

async toggle() {
this.#sort = this.#sort === 1 ? -1 : 1;
this.subscribeToRealms();
}

/*
|--------------------------------------------------------------------------------
| Resolvers
|--------------------------------------------------------------------------------
*/

async resolve({ name }: Props = { name: this.#name }) {
this.#name = name; // set name provided through external properties
await this.subscribeToRealms();
}

async subscribeToRealms() {
this.subscriptions.realms?.unsubscribe();
this.subscriptions.realms = Realm.subscribe(
this.#filter,
{
sort: {
name: this.#sort
}
},
this.setNext("realms")
);
this.#subscribeToRealms();
}

/*
Expand Down

0 comments on commit 3b62dde

Please sign in to comment.