Skip to content

Commit

Permalink
Merge pull request #12 from p2panda/clean-and-improve-api
Browse files Browse the repository at this point in the history
Refactor code base and build pipeline
  • Loading branch information
adzialocha authored Feb 27, 2023
2 parents 0fd59b0 + 7accede commit 129f850
Show file tree
Hide file tree
Showing 31 changed files with 2,004 additions and 2,415 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* Updates for breaking `p2panda-js` API changes [#4](https://github.com/p2panda/shirokuma/pull/4)
* Update dependencies [#5](https://github.com/p2panda/shirokuma/pull/5)
* Refactor code-base, remove caching layer, add "slim" and "inlined" builds [#12](https://github.com/p2panda/shirokuma/pull/12)

[unreleased]: https://github.com/p2panda/shirokuma/compare/...HEAD
67 changes: 19 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,9 @@ To install `shirokuma` run:
npm i shirokuma
```

## Usage

`shirokuma` runs in web browsers and can be integrated in a bundle for example
via Webpack or Rollup.
## Example

```js
```typescript
import { KeyPair, Session, initWebAssembly } from 'shirokuma';

// This method needs to be run once before to initialise the embedded
Expand All @@ -75,40 +72,28 @@ const keyPair = new KeyPair();
const session = new Session('https://welle.liebechaos.org').setKeyPair(keyPair);

// Compose your operation payload, according to chosen schema
const payload = {
const fields = {
message: 'Hi there',
};

// Send new chat operation to the node
await session.create(payload, { schema: CHAT_SCHEMA });

// Query instances from the p2panda node
import { gql, useQuery } from '@apollo/client';

const GET_CHAT_MESSAGES = gql`
all_${CHAT_SCHEMA} {
fields {
message
}
}
`;

const Chat = () => {
const { loading, error, data } = useQuery(GET_CHAT_MESSAGES);

if (loading) return 'Loading...';
if (error) return `Error! ${error.message}`;

return (
<ul>
{data[`all_${CHAT_SCHEMA}`].map((doc) => (
<li key={doc.id}>{doc.fields.message}</li>
))}
</ul>
);
};
// Send new chat message to the node
await session.create(fields, { schemaId: CHAT_SCHEMA });
```

## Usage

`shirokuma` runs both in NodeJS and web browsers and comes as a ES, CommonJS
or UMD module. It can easily be integrated into Webpack, Rollup or other tools.

Since `shirokuma` contains WebAssembly code, it is necessary to initialise it
before using the methods in the Browser. This initialisation step is not
required in NodeJS contexts.

To make this step a little bit easier `shirokuma` inlines the WebAssembly code
as a base64 string which gets decoded automatically during initialisation. For
manual initialisation the package also comes with "slim" versions where you
need to provide a path to the ".wasm" file yourself.

## Development

```bash
Expand All @@ -125,20 +110,6 @@ npm test
npm run build
```

### Debug logging

Enable debug logging for node environments by setting an environment variable:

```bash
export DEBUG='shirokuma*'
```

Enable debug logging from a browser console by storing a key `debug` in local storage:

```js
localStorage.debug = 'shirokuma*';
```

### Documentation

```bash
Expand Down
Loading

0 comments on commit 129f850

Please sign in to comment.