Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prettier so folks' editors (maybe just me? 😅 ) don't reformat things differently on save -- should help focus changes in PRs #163

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ jobs:
- uses: actions/setup-node@v4
- run: npm install
- run: npm test
- run: |
npm install
npm run format:check
working-directory: packages/signal-polyfill
6 changes: 6 additions & 0 deletions packages/signal-polyfill/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import("prettier").Config} */
const config = {
singleQuote: true,
};

export default config;
3 changes: 3 additions & 0 deletions packages/signal-polyfill/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
"types": "dist/index.d.ts",
"scripts": {
"dev": "vite",
"format:check": "prettier --check .",
"format": "prettier --write .",
"build": "tsc && vite build",
"test": "vitest"
},
"author": "EisenbergEffect",
"license": "Apache-2.0",
"devDependencies": {
"@types/node": "^20.11.25",
"prettier": "^3.2.5",
"typescript": "latest",
"vite": "^5.2.6",
"vite-plugin-dts": "^3.7.3",
Expand Down
40 changes: 20 additions & 20 deletions packages/signal-polyfill/readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Signal Polyfill

## ⚠️ This polyfill is a preview of an in-progress proposal and could change at any time. Do not use this in production. ⚠️
## ⚠️ This polyfill is a preview of an in-progress proposal and could change at any time. Do not use this in production. ⚠️

A "signal" is [a proposed first-class JavaScript data type](../../README.md) that enables one-way data flow through cells of state or computations derived from other state/computations.

Expand All @@ -10,16 +10,16 @@ This is a polyfill for the `Signal` API.

### Using signals

* Use `Signal.State(value)` to create a single "cell" of data that can flow through the unidirectional state graph.
* Use `Signal.Computed(callback)` to define a computation based on state or other computations flowing through the graph.
- Use `Signal.State(value)` to create a single "cell" of data that can flow through the unidirectional state graph.
- Use `Signal.Computed(callback)` to define a computation based on state or other computations flowing through the graph.

```js
import { Signal } from "signal-polyfill";
import { effect } from "./effect.js";
import { Signal } from 'signal-polyfill';
import { effect } from './effect.js';

const counter = new Signal.State(0);
const isEven = new Signal.Computed(() => (counter.get() & 1) == 0);
const parity = new Signal.Computed(() => isEven.get() ? "even" : "odd");
const parity = new Signal.Computed(() => (isEven.get() ? 'even' : 'odd'));

effect(() => console.log(parity.get())); // Console logs "even" immediately.
setInterval(() => counter.set(counter.get() + 1), 1000); // Changes the counter every 1000ms.
Expand All @@ -44,14 +44,14 @@ Depending on how the effect is implemented, the above code could result in an in

### Creating a simple effect

* You can use `Signal.subtle.Watch(callback)` combined with `Signal.Computed(callback)` to create a simple _effect_ implementation.
* The `Signal.subtle.Watch` `callback` is invoked synchronously when a watched signal becomes dirty.
* To batch effect updates, library authors are expected to implement their own schedulers.
* Use `Signal.subtle.Watch#getPending()` to retrieve an array of dirty signals.
* Calling `Signal.subtle.Watch#watch()` with no arguments will re-watch the list of tracked signals again.
- You can use `Signal.subtle.Watch(callback)` combined with `Signal.Computed(callback)` to create a simple _effect_ implementation.
- The `Signal.subtle.Watch` `callback` is invoked synchronously when a watched signal becomes dirty.
- To batch effect updates, library authors are expected to implement their own schedulers.
- Use `Signal.subtle.Watch#getPending()` to retrieve an array of dirty signals.
- Calling `Signal.subtle.Watch#watch()` with no arguments will re-watch the list of tracked signals again.

```js
import { Signal } from "signal-polyfill";
import { Signal } from 'signal-polyfill';

let needsEnqueue = true;

Expand All @@ -64,7 +64,7 @@ const w = new Signal.subtle.Watcher(() => {

function processPending() {
needsEnqueue = true;

for (const s of w.getPending()) {
s.get();
}
Expand All @@ -74,18 +74,18 @@ function processPending() {

export function effect(callback) {
let cleanup;

const computed = new Signal.Computed(() => {
typeof cleanup === "function" && cleanup();
typeof cleanup === 'function' && cleanup();
cleanup = callback();
});

w.watch(computed);
computed.get();

return () => {
w.unwatch(computed);
typeof cleanup === "function" && cleanup();
typeof cleanup === 'function' && cleanup();
};
}
```
Expand All @@ -98,7 +98,7 @@ export function effect(callback) {
A class accessor decorator can be combined with the `Signal.State()` API to enable improved DX.

```js
import { Signal } from "signal-polyfill";
import { Signal } from 'signal-polyfill';

export function signal(target) {
const { get } = target;
Expand All @@ -111,7 +111,7 @@ export function signal(target) {
set(value) {
get.call(this).set(value);
},

init(value) {
return new Signal.State(value);
},
Expand Down
15 changes: 11 additions & 4 deletions packages/signal-polyfill/src/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@
* found in the LICENSE file at https://angular.io/license
*/

import {defaultEquals, ValueEqualityFn} from './equality.js';
import {consumerAfterComputation, consumerBeforeComputation, producerAccessed, producerUpdateValueVersion, REACTIVE_NODE, ReactiveNode, SIGNAL} from './graph.js';

import { defaultEquals, ValueEqualityFn } from './equality.js';
import {
consumerAfterComputation,
consumerBeforeComputation,
producerAccessed,
producerUpdateValueVersion,
REACTIVE_NODE,
ReactiveNode,
SIGNAL,
} from './graph.js';

/**
* A computation, which derives a value from a declarative reactive expression.
Expand Down Expand Up @@ -36,7 +43,7 @@ export interface ComputedNode<T> extends ReactiveNode {
equal: ValueEqualityFn<T>;
}

export type ComputedGetter<T> = (() => T)&{
export type ComputedGetter<T> = (() => T) & {
[SIGNAL]: ComputedNode<T>;
};

Expand Down
2 changes: 1 addition & 1 deletion packages/signal-polyfill/src/equality.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ export type ValueEqualityFn<T> = (a: T, b: T) => boolean;
*/
export function defaultEquals<T>(a: T, b: T) {
return Object.is(a, b);
}
}
2 changes: 1 addition & 1 deletion packages/signal-polyfill/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ export function throwInvalidWriteToSignalError() {

export function setThrowInvalidWriteToSignalError(fn: () => never): void {
throwInvalidWriteToSignalErrorFn = fn;
}
}
Loading
Loading