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

Allow options to be passed to the @signal decorator #55

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
45 changes: 40 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Signal } from "signal-polyfill";

/**
* Define the options to pass to the underling Signal
*/
export function signal<Value = any>(
options: Signal.Options<Value>,
): typeof stateDecorator<Value>;

/**
* Usage:
* ```js
Expand Down Expand Up @@ -52,16 +59,22 @@

export function signal<Value = any>(
...args:
| [Signal.Options<Value>]
| Parameters<typeof stateDecorator<Value>>
| Parameters<typeof computedDecorator<Value>>
) {
if (args[1].kind === "accessor") {
// Options
if (args.length === 1) {
return optionsBuilder(args[0]);
}

if (args[2].kind === "accessor") {

Check failure on line 71 in src/index.ts

View workflow job for this annotation

GitHub Actions / Test Node

tests/@localCopy.test.ts

TypeError: Cannot read properties of undefined (reading 'kind') ❯ signal src/index.ts:71:15 ❯ applyDec src/local-copy.ts:3:2198 ❯ p src/local-copy.ts:3:3199 ❯ _applyDecs src/local-copy.ts:3:3406 ❯ Function.<static_initializer> src/local-copy.ts:3:19 ❯ src/local-copy.ts:2:18

Check failure on line 71 in src/index.ts

View workflow job for this annotation

GitHub Actions / Test Node

tests/@signal.test.ts > @signal (accessor) > works

TypeError: Cannot read properties of undefined (reading 'kind') ❯ signal src/index.ts:71:15 ❯ applyDec tests/@signal.test.ts:4:2198 ❯ p tests/@signal.test.ts:4:3199 ❯ _applyDecs tests/@signal.test.ts:4:3406 ❯ Function._init_extra_value tests/@signal.test.ts:8:17 ❯ tests/@signal.test.ts:7:21

Check failure on line 71 in src/index.ts

View workflow job for this annotation

GitHub Actions / Test Node

tests/@signal.test.ts > @signal (getter) > works

TypeError: Cannot read properties of undefined (reading 'kind') ❯ signal src/index.ts:71:15 ❯ applyDec tests/@signal.test.ts:4:2198 ❯ p tests/@signal.test.ts:4:3199 ❯ _applyDecs tests/@signal.test.ts:4:3406 ❯ Function.<static_initializer> tests/@signal.test.ts:33:17 ❯ tests/@signal.test.ts:31:23

Check failure on line 71 in src/index.ts

View workflow job for this annotation

GitHub Actions / Test Node

tests/deep.test.ts > deepSignal > Objects > object access

TypeError: Cannot read properties of undefined (reading 'kind') ❯ signal src/index.ts:71:15 ❯ applyDec tests/deep.test.ts:6:2198 ❯ p tests/deep.test.ts:6:3199 ❯ _applyDecs tests/deep.test.ts:6:3406 ❯ Function._init_extra_obj tests/deep.test.ts:79:17 ❯ tests/deep.test.ts:78:45

Check failure on line 71 in src/index.ts

View workflow job for this annotation

GitHub Actions / Test Node

tests/deep.test.ts > deepSignal > Objects > object access in an array

TypeError: Cannot read properties of undefined (reading 'kind') ❯ signal src/index.ts:71:15 ❯ applyDec tests/deep.test.ts:6:2198 ❯ p tests/deep.test.ts:6:3199 ❯ _applyDecs tests/deep.test.ts:6:3406 ❯ Function._init_extra_arr tests/deep.test.ts:106:17 ❯ tests/deep.test.ts:105:57

Check failure on line 71 in src/index.ts

View workflow job for this annotation

GitHub Actions / Test Node

tests/deep.test.ts > deepSignal > Arrays > #splice > it works

TypeError: Cannot read properties of undefined (reading 'kind') ❯ signal src/index.ts:71:15 ❯ applyDec tests/deep.test.ts:6:2198 ❯ p tests/deep.test.ts:6:3199 ❯ _applyDecs tests/deep.test.ts:6:3406 ❯ Function._init_extra_arr2 tests/deep.test.ts:156:19 ❯ tests/deep.test.ts:155:42

Check failure on line 71 in src/index.ts

View workflow job for this annotation

GitHub Actions / Test Node

tests/deep.test.ts > deepSignal > Arrays > #splice > it works on deeply nested arrays

TypeError: Cannot read properties of undefined (reading 'kind') ❯ signal src/index.ts:71:15 ❯ applyDec tests/deep.test.ts:6:2198 ❯ p tests/deep.test.ts:6:3199 ❯ _applyDecs tests/deep.test.ts:6:3406 ❯ Function._init_extra_obj4 tests/deep.test.ts:173:19 ❯ tests/deep.test.ts:172:66

Check failure on line 71 in src/index.ts

View workflow job for this annotation

GitHub Actions / Test Node

tests/deep.test.ts > deepSignal > array data can be re-set

TypeError: Cannot read properties of undefined (reading 'kind') ❯ signal src/index.ts:71:15 ❯ applyDec tests/deep.test.ts:6:2198 ❯ p tests/deep.test.ts:6:3199 ❯ _applyDecs tests/deep.test.ts:6:3406 ❯ Function._init_extra_arr5 tests/deep.test.ts:250:15 ❯ tests/deep.test.ts:249:54

Check failure on line 71 in src/index.ts

View workflow job for this annotation

GitHub Actions / Test Node

tests/local-copy.test.ts

TypeError: Cannot read properties of undefined (reading 'kind') ❯ signal src/index.ts:71:15 ❯ applyDec src/local-copy.ts:3:2198 ❯ p src/local-copy.ts:3:3199 ❯ _applyDecs src/local-copy.ts:3:3406 ❯ Function.<static_initializer> src/local-copy.ts:3:19 ❯ src/local-copy.ts:2:18
return stateDecorator(
...(args as Parameters<typeof stateDecorator<Value>>),
);
}

if (args[1].kind === "getter") {
if (args[2].kind === "getter") {
return computedDecorator(
...(args as Parameters<typeof computedDecorator<Value>>),
);
Expand All @@ -70,7 +83,28 @@
throw new Error(`@signal can only be used on accessors or getters`);
}

function optionsBuilder<Value = any>(options: Signal.Options<Value> = {}) {

return (...args: StateArgs<Value> | ComputedArgs<Value>) => {
if (args[1].kind === "accessor") {
return signal(options, args[0], args[1]);
}

if (args[1].kind === "getter") {
return signal(options, args[0], args[1]);
}

throw new Error(`@signal can only be used on accessors or getters`);
}
}

type FullStateArgs<Value> = Parameters<typeof stateDecorator<Value>>
type FullComputedArgs<Value> = Parameters<typeof computedDecorator<Value>>
type StateArgs<Value> = [FullStateArgs<Value>[1], FullStateArgs<Value>[2]];
type ComputedArgs<Value> = [FullComputedArgs<Value>[1], FullComputedArgs<Value>[2]];

function stateDecorator<Value = any>(
options: Signal.Options<Value> = {},
target: ClassAccessorDecoratorTarget<unknown, Value>,
context: ClassAccessorDecoratorContext,
): ClassAccessorDecoratorResult<unknown, Value> {
Expand All @@ -96,12 +130,13 @@
init(value: Value) {
// SAFETY: does TS not allow us to have a different type internally?
// maybe I did something goofy.
return new Signal.State(value) as unknown as Value;
return new Signal.State(value, options) as unknown as Value;
},
};
}

function computedDecorator<Value = any>(
options: Signal.Options<Value> = {},
target: () => Value,
context: ClassGetterDecoratorContext,
): () => Value {
Expand All @@ -113,10 +148,10 @@

let caches = new WeakMap<typeof target, Signal.Computed<Value>>();

return function (this: unknown) {
return function(this: unknown) {
let cache = caches.get(target);
if (!cache) {
cache = new Signal.Computed(() => target.call(this));
cache = new Signal.Computed(() => target.call(this), options);
caches.set(target, cache);
}

Expand Down
Loading