Skip to content

Commit

Permalink
Merge pull request #12 from Ayc0/Ayc0/names
Browse files Browse the repository at this point in the history
Change names
  • Loading branch information
Ayc0 authored May 8, 2021
2 parents d73571e + 149b145 commit 0aae8f2
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 70 deletions.
2 changes: 1 addition & 1 deletion docs/code.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
.replace(/¤/g, '\n');
const code = document.getElementById('code');
const source = document.getElementById('source');
const text = `const { createCup, store } = window['manatea'];
const text = `const { orderCup } = window['manatea'];
const { Infuser, infuse } = window['react-manatea'];
${rawText}
Expand Down
10 changes: 5 additions & 5 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
- [react-manatea](react.html)

```jsx
const { createCup } = manatea;
const { orderCup } = manatea;
const { Infuser, infuse } = ReactManatea;

const add = tea => Math.min(50, tea + 1);
Expand All @@ -13,7 +13,7 @@ const subtract = tea => Math.max(0, tea - 1);
// Use lib
const UnnamedCup = (() => {
// Define store
const counter = createCup(0);
const counter = orderCup(0);

const increment = () => counter(add);
const decrement = () => counter(subtract);
Expand All @@ -30,7 +30,7 @@ const UnnamedCup = (() => {

const NamedCup = (() => {
// Define store
const counter = createCup(0, 'counter');
const counter = orderCup(0, 'counter');

const increment = () => counter(add);
const decrement = () => counter(subtract);
Expand All @@ -47,7 +47,7 @@ const NamedCup = (() => {

const ConnectedUnnamedCup = (() => {
// Define store
const counter = createCup(0);
const counter = orderCup(0);

const increment = () => counter(add);
const decrement = () => counter(subtract);
Expand All @@ -65,7 +65,7 @@ const ConnectedUnnamedCup = (() => {

const ConnectedNamedCup = (() => {
// Define store
const counter = createCup(0, 'connect');
const counter = orderCup(0, 'connect');

const increment = () => counter(add);
const decrement = () => counter(subtract);
Expand Down
4 changes: 2 additions & 2 deletions docs/manatea.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
const subtract = tea => Math.max(0, tea - 1);

// Define store
const counter = createCup(0);
const counter = orderCup(0);

const increment = () => counter(add);
const decrement = () => counter(subtract);
Expand All @@ -32,7 +32,7 @@
const add = tea => Math.min(50, tea + 1);
const subtract = tea => Math.max(0, tea - 1);

const counter = createCup(0, 'counter');
const counter = orderCup(0, 'counter');

const increment = () => counter(add);
const decrement = () => counter(subtract);
Expand Down
22 changes: 11 additions & 11 deletions packages/manatea/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ This package has no dependencies and weights less than 1kB (and less than 500B g
### Create cup

```js
import { createCup } from 'manatea';
import { orderCup } from 'manatea';

// Defining a cup
const counter = createCup(0);
const counter = orderCup(0);
```

### Read cup's tea
Expand Down Expand Up @@ -44,23 +44,23 @@ counter(tea => {
counter(tea => tea + 1).then(tea => console.log(tea));
```

### Store's listeners
### Cup's servers

```js
// Add listener
const listener = counter.on(tea => console.log(tea));
// Add server
const server = counter.on(tea => console.log(tea));
```

### Remove cup's listener
### Dismiss cup's server

```js
listener.listening; // true
listener();
listener.listening; // false
server.listening; // true
server();
server.listening; // false

// OR

listener.listening; // true
server.listening; // true
counter.clear();
listener.listening; // false
server.listening; // false
```
32 changes: 16 additions & 16 deletions packages/manatea/__tests__/manatea.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// @ts-ignore
import { createCup } from '../src/index';
import { orderCup } from '../src/index';

describe('Manatea', () => {
it('should be updatable', async () => {
const cup = createCup<number>(1);
const cup = orderCup<number>(1);
expect(cup()).toBe(1);
await cup(2);
expect(cup()).toBe(2);
Expand All @@ -14,7 +14,7 @@ describe('Manatea', () => {
});

it('should be listenable', async () => {
const cup = createCup<number>(1);
const cup = orderCup<number>(1);
const fn = jest.fn();
cup.on(tea => fn(tea));
await cup(2);
Expand All @@ -23,32 +23,32 @@ describe('Manatea', () => {
expect(fn).toHaveBeenCalledTimes(1);
});

it('should have clearable listeners', async () => {
it('should have clearable servers', async () => {
{
const cup = createCup<number>(1);
const cup = orderCup<number>(1);
const fn = jest.fn();
const listener = cup.on(fn);
expect(listener.listening).toBe(true);
listener();
expect(listener.listening).toBe(false);
const server = cup.on(fn);
expect(server.listening).toBe(true);
server();
expect(server.listening).toBe(false);
await cup(2);
expect(fn).not.toHaveBeenCalled();
}
{
const cup = createCup<number>(1);
const cup = orderCup<number>(1);
const fn = jest.fn();
const listener = cup.on(fn);
expect(listener.listening).toBe(true);
const server = cup.on(fn);
expect(server.listening).toBe(true);
cup.clear();
expect(listener.listening).toBe(false);
expect(server.listening).toBe(false);
await cup(2);
expect(fn).not.toHaveBeenCalled();
}
});

it('shouldn’t create infinite loops', async () => {
const cup1 = createCup<number>(0);
const cup2 = createCup<number>(0);
const cup1 = orderCup<number>(0);
const cup2 = orderCup<number>(0);
let i = 0;
cup1.on((_tea, context) => {
if (i++ > 0) {
Expand All @@ -65,7 +65,7 @@ describe('Manatea', () => {
});

it('shouldn’t update with NaN', async () => {
const cup = createCup(NaN);
const cup = orderCup(NaN);
const fn = jest.fn();
cup.on(fn);
await cup(NaN);
Expand Down
51 changes: 26 additions & 25 deletions packages/manatea/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,75 +12,76 @@ export type Tea =
| Set<any>;

type Handler<T extends Tea> = (tea: T, context: Context) => void;
export interface Listener {
export interface Server {
(): boolean;
listening: boolean;
}

type Change<T extends Tea> = ((tea: T) => T | Promise<T>) | T;
type Order<T extends Tea> = ((tea: T) => T | Promise<T>) | T;

export interface Cup<T extends Tea> {
(): T;
(change: Change<T>, context?: Context): Promise<T>;
on: (fn: Handler<T>) => Listener;
(order: Order<T>, context?: Context): Promise<T>;
on: (fn: Handler<T>) => Server;
clear: () => void;
}
type Context = WeakSet<Cup<any>>;

export function createCup<T extends Tea>(
initialTea: T,
fixation: (tea: T) => T = t => t,
export function orderCup<T extends Tea>(
firstTea: T,
flavoring: (tea: T) => T = t => t,
): Cup<T> {
let handlers = new Set<Handler<T>>();
let fixedTea = fixation(initialTea);
let flavoredTea = flavoring(firstTea);

let isPreviousCancelled = { cancelled: false };

const setTea = (newTea: T, context: Context) => {
const fixedNewTea = fixation(newTea);
const setTea = (teaRefill: T, context: Context) => {
const flavoredTeaRefill = flavoring(teaRefill);
if (
fixedTea === fixedNewTea ||
(Number.isNaN(fixedTea as any) && Number.isNaN(fixedNewTea as any))
flavoredTea === flavoredTeaRefill ||
(Number.isNaN(flavoredTea as any) &&
Number.isNaN(flavoredTeaRefill as any))
) {
return;
}
isPreviousCancelled.cancelled = true;
const isCancelled = { cancelled: false };
isPreviousCancelled = isCancelled;
fixedTea = fixedNewTea;
flavoredTea = flavoredTeaRefill;
handlers.forEach(handler => {
if (isCancelled.cancelled) {
return;
}
handler(fixedTea, context);
handler(flavoredTea, context);
});
};

function cup(): T;
function cup(change: Change<T>, context?: Context): Promise<T>;
function cup(change?: Change<T>, context: Context = new WeakSet()) {
function cup(order: Order<T>, context?: Context): Promise<T>;
function cup(order?: Order<T>, context: Context = new WeakSet()) {
if (arguments.length === 0) {
return fixedTea;
return flavoredTea;
}
return Promise.resolve(
typeof change === 'function' ? change(fixedTea) : change,
).then(newTea => {
typeof order === 'function' ? order(flavoredTea) : order,
).then(teaRefill => {
if (context.has(cup)) {
return fixedTea;
return flavoredTea;
}
context.add(cup);
setTea(newTea, context);
return fixedTea;
setTea(teaRefill, context);
return flavoredTea;
});
}

cup.on = (fn: Handler<T>) => {
handlers.add(fn);
const listener = () => handlers.delete(fn);
Object.defineProperty(listener, 'listening', {
const server = () => handlers.delete(fn);
Object.defineProperty(server, 'listening', {
get: () => handlers.has(fn),
});
return listener as Listener;
return server as Server;
};

cup.clear = () => handlers.clear();
Expand Down
12 changes: 6 additions & 6 deletions packages/react-manatea/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ Bindings of [manatea](<[https://npmjs.](https://www.npmjs.com/package/manatea)>)

```js
import React from 'react';
import { createCup } from 'manatea';
import { orderCup } from 'manatea';
import { infuse } from 'react-manatea';

// Defining a cup
const timer = createCup(0);
const timer = orderCup(0);

const Timer = infuse(timer)(({ tea: time }) => <div>Time: {time}</div>);
```
Expand All @@ -27,11 +27,11 @@ const Timer = infuse(timer)(({ tea: time }) => <div>Time: {time}</div>);

```js
import React from 'react';
import { createCup } from 'manatea';
import { orderCup } from 'manatea';
import { Infuser } from 'react-manatea';

// Defining a cup
const timer = createCup(0);
const timer = orderCup(0);

const Timer = () => (
<Infuser cup={timer}>{({ tea: time }) => <div>Time: {time}</div>}</Infuser>
Expand All @@ -42,11 +42,11 @@ const Timer = () => (

```js
import React, { useInfuser } from 'react';
import { createCup } from 'manatea';
import { orderCup } from 'manatea';
import { useInfuser } from 'react-manatea';

// Defining a cup
const timer = createCup(0);
const timer = orderCup(0);

const Timer = () => {
const [time, setTime] = useInfuser(timer);
Expand Down
8 changes: 4 additions & 4 deletions packages/react-manatea/src/useInfuser.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import * as React from 'react';
import { Cup, Tea, Listener } from 'manatea';
import { Cup, Tea, Server } from 'manatea';

export const useInfuser = <T extends Tea>(cup: Cup<T>) => {
const [tea, setTea] = React.useState(() => cup());

React.useEffect(() => {
const listener: Listener = cup.on((tea: T) => setTea(tea));
const server: Server = cup.on((tea: T) => setTea(tea));
setTea(cup());
return () => {
if (listener.listening) {
listener();
if (server.listening) {
server();
}
};
}, [cup]);
Expand Down

0 comments on commit 0aae8f2

Please sign in to comment.