Skip to content

Commit

Permalink
chore: release action, type decs & README
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisergeX committed Sep 7, 2024
1 parent d702924 commit f59e118
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 121 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
name: Release
permissions:
contents: write
id-token: write
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
Expand All @@ -27,7 +28,7 @@ jobs:
with:
node-version: 20
cache: 'pnpm'
registry-url: "https://registry.npmjs.org"
registry-url: 'https://registry.npmjs.org'

- name: Install Dependencies
run: pnpm install
Expand All @@ -44,9 +45,12 @@ jobs:
id: changesets
uses: changesets/action@v1
with:
publish: pnpm publish-signal
publish: pnpm publish
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
TURBO_TELEMETRY_DISABLED: 1

- name: Publish to JSR
run: pnpm publish:jsr
109 changes: 3 additions & 106 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[![npm](https://img.shields.io/npm/v/@kaiverse/signal)](https://www.npmjs.com/package/@kaiverse/signal)
[![JSR](https://jsr.io/badges/@kaiverse/signal)](https://jsr.io/@kaiverse/signal)
[![.github/workflows/ci.yml](https://github.com/kaisergeX/signal-proxy/actions/workflows/ci.yml/badge.svg)](https://github.com/kaisergeX/signal-proxy/actions/workflows/ci.yml)

<div align="center">
Expand All @@ -9,113 +10,9 @@

[JS Signals proposal](https://github.com/tc39/proposal-signals) is currently in Stage 1. This package draws strong inspiration from [KnockoutJS](https://github.com/knockout/knockout)'s concepts and [SolidJS](https://github.com/solidjs)'s Signal, enabling us to use Signals in vanilla JavaScript.

## Installation
## Docs

Via `npmjs`

```
npm i @kaiverse/signal
```

or

```
pnpm add @kaiverse/signal
```

Via `jsr`

<details>
<summary> (Not available yet)</summary>

```
deno add @kaiverse/signal
```
or
```
npx jsr add @kaiverse/signal
```
or
```
pnpm dlx jsr add @kaiverse/signal
```
</details><br/>

Via `unpkg` CDN:

```
unpkg.com/@kaiverse/signal
```

## Compatibility

Signal is a [**Proxy**](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Proxy) object at its core, please check [compatibility section](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Proxy#browser_compatibility).

## Example

[Playground source code](packages/signal/src/playground/index.ts)

### Use Signal Proxy

````js
/**
* ```html
* <p id="fetch-result"></p>
* <button type="button" onclick="fetchNextUser()">Get next user</button>
* ```
*/

import {signalProxy} from '@kaiverse/signal';

const resultElement = document.getElementById('fetch-result');

const userSignal = signalProxy({userId: 123, reqCount: 0}, async (key, newValue) => {
// Do something when any userSignal's prop value changes
console.log(`[userSignal updated] key: ${key}, value: ${newValue}`);

if (key === 'userId') {
// Do something on `userId` changes only
const userId = newValue;
const response = await fetch(`${basePath}/user/${userId}`);
const userData = await response.json();
const totalReqCount = userSignal.reqCount + 1;
userSignal.reqCount = totalReqCount;

if (resultElement)
resultElement.innerHTML = `Name: ${userData.name}<br/>Total requests: ${totalReqCount}`;
}
});

function fetchNextUser() {
userSignal.userId++;
}
````

### Signal utilities examples

```js
import {createComputed, createEffect, createSignal} from '@kaiverse/signal';

const [count, setCount] = createSignal(0);

setInterval(() => {
setCount((c) => c + 1); // or setCount(count() + 1)
}, 1000);

createEffect(() => {
console.log('count =', count());
});

const doubled = createComputed(() => count() * 2);

createEffect(() => {
console.log('[computed] doubled =', doubled());
});
```

## Framework ports?

- [Experimental] [React signal hooks](apps/playground/src/hooks/react-signal.ts) implementation ([React playground source code](apps/playground/src/routes/signal/route.lazy.tsx)). **DO NOT** use in production.<br/><small>Those hooks work, but its lack of testing and seems that the usage of memory is inefficient. An alternative approach may be better. Please feel free to open PRs. Your contributions are welcomed and appreciated.</small>
[Documentation page](packages/signal/README.md)

## Run this monorepo locally

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"lint": "turbo lint",
"format": "prettier --write \"**/*.{ts,tsx,md}\"",
"clean": "turbo run clean && rm -rf node_modules",
"publish-signal": "turbo run build lint --filter=./packages/signal && pnpm publish --filter './packages/signal' --access public && changeset tag"
"publish": "turbo run build lint --filter=./packages/signal && pnpm publish --filter './packages/signal' --access public && changeset tag",
"publish:jsr": "cd ./packages/signal && pnpm dlx jsr publish"
},
"type": "module",
"devDependencies": {
Expand All @@ -29,4 +30,4 @@
"node": ">=18",
"pnpm": ">=9"
}
}
}
18 changes: 10 additions & 8 deletions packages/signal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

## Installation

Via `npmjs`
### Via `npmjs`

```
npm i @kaiverse/signal
Expand All @@ -22,25 +22,25 @@ or
pnpm add @kaiverse/signal
```

Via `jsr`
### Via `jsr`

<details>
<summary> (Not available yet)</summary>

```
deno add @kaiverse/signal
```

or

```
npx jsr add @kaiverse/signal
```

or

```
pnpm dlx jsr add @kaiverse/signal
```
</details><br/>

Via `unpkg` CDN:
### Via `unpkg` CDN:

```
unpkg.com/@kaiverse/signal
Expand Down Expand Up @@ -114,4 +114,6 @@ createEffect(() => {

## Framework ports?

- [Experimental] [React signal hooks](../../apps/playground/src/hooks/react-signal.ts) implementation ([React playground source code](../../apps/playground/src/routes/signal/route.lazy.tsx)). **DO NOT** use in production.<br/><small>Those hooks work, but its lack of testing and seems that the usage of memory is inefficient. An alternative approach may be better. Please feel free to open PRs. Your contributions are welcomed and appreciated.</small>
### React

[Experimental] [React signal hooks](../../apps/playground/src/hooks/react-signal.ts) implementation ([React playground source code](../../apps/playground/src/routes/signal/route.lazy.tsx)). **DO NOT** use in production.<br/><small>Those hooks work, but its lack of testing and seems that the usage of memory is inefficient. An alternative approach may be better. Please feel free to open PRs. Your contributions are welcomed and appreciated.</small>
10 changes: 9 additions & 1 deletion packages/signal/jsr.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
{
"name": "@kaiverse/signal",
"version": "0.2.1",
"exports": "./src/index.ts"
"exports": "./src/index.ts",
"publish": {
"exclude": [
"vite.config.ts",
"tsconfig.json",
"index.html",
"./src/vite-env.d.ts"
]
}
}
5 changes: 3 additions & 2 deletions packages/signal/src/core.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type {ObjectAny, SignalCompareEqual, SignalUpdateCallback} from './types';

/** Create a Signal Proxy. It track a `initialValue` object that changes over time. */
export function signalProxy<T extends ObjectAny = ObjectAny>(
target: T,
initialValue: T,
callback: SignalUpdateCallback<T>,
equals: SignalCompareEqual<T> = Object.is,
): T {
Expand All @@ -20,5 +21,5 @@ export function signalProxy<T extends ObjectAny = ObjectAny>(
},
};

return new Proxy(target, handler);
return new Proxy(initialValue, handler);
}
5 changes: 5 additions & 0 deletions packages/signal/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export type SignalSetter<in out T> = {
): undefined extends T ? undefined : U;
(value: T | SignalSetterCb<T>): T;
};

/** Signals value getter. */
export type Signal<T> = () => T;
/** Signal options */
export type SignalOptions<T> = {
/**
* Customize Signal comparison
Expand All @@ -31,7 +34,9 @@ export type SignalOptions<T> = {
/** Runs whenever Signal value changes */
onChange?: (newValue: T) => void;
};
/** Return type of a Signal */
export type SignalFactoryReturnType<T> = Readonly<[get: Signal<T>, set: SignalSetter<T>]>;
/** An Effect that runs whenever its dependencies change. */
export type SignalEffect = () => void;
export type EffectTracking = {execute: SignalEffect; deps: Set<Set<EffectTracking>>};
export type CleanupEffectFn = () => void;

0 comments on commit f59e118

Please sign in to comment.