Skip to content

Commit

Permalink
BREAKING: upgrade @kt3k/signal to 0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k committed Sep 20, 2024
1 parent dacb0de commit 8733687
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 21 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,12 @@ communicating with them.
the values are changed.

```ts
import { signal } from "@kt3k/cell"
import { Signal } from "@kt3k/cell"

const sig = signal(0)
const sig = new Signal(0)

const stop = sig.onChange((v) => {
alert(`The value changed to: ${v}!`)
const stop = sig.subscribe((v) => {
console.log(`The value is ${v}!`)
})

sig.update(1)
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"imports": {
"@b-fuze/deno-dom": "jsr:@b-fuze/deno-dom@^0.1.47",
"@kt3k/signal": "jsr:@kt3k/signal@^0.2.0",
"@kt3k/signal": "jsr:@kt3k/signal@^0.3.0",
"@std/assert": "jsr:@std/assert@^1.0.0",
"@std/async": "jsr:@std/async@^1.0.0",
"@std/testing": "jsr:@std/testing@^1.0.0"
Expand Down
8 changes: 4 additions & 4 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 67 additions & 9 deletions docs/dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ function logEvent({
console.groupEnd();
}

// https://jsr.io/@kt3k/signal/0.1.6/mod.ts
var Signal = class {
// https://jsr.io/@kt3k/signal/0.3.0/mod.ts
var Signal = class _Signal {
#val;
#handlers = [];
constructor(value) {
Expand Down Expand Up @@ -69,11 +69,55 @@ var Signal = class {
}
}
/**
* Update the signal value by comparing the fields of the new value.
* Subscribe to the signal.
*
* @param cb The callback function to be called when the signal is updated
* @returns A function to stop the subscription
*/
onChange(cb) {
this.#handlers.push(cb);
return () => {
this.#handlers.splice(this.#handlers.indexOf(cb) >>> 0, 1);
};
}
/**
* Subscribe to the signal.
*
* @param cb The callback function to be called when the signal is updated and also called immediately
* @returns A function to stop the subscription
*/
subscribe(cb) {
cb(this.#val);
return this.onChange(cb);
}
/** Maps the signal to a different signal */
map(fn) {
const signal = new _Signal(fn(this.#val));
this.onChange((val) => signal.update(fn(val)));
return signal;
}
};
var GroupSignal = class _GroupSignal {
#val;
#handlers = [];
constructor(value) {
this.#val = value;
}
/**
* Get the current value of the signal.
*
* @returns The current value of the signal
*/
get() {
return this.#val;
}
/**
* Update the signal value.
* The signal event is only emitted when the fields of the new value are different from the current value.
*
* @param value The new value of the signal
*/
updateByFields(value) {
update(value) {
if (typeof value !== "object" || value === null) {
throw new Error("value must be an object");
}
Expand All @@ -99,10 +143,23 @@ var Signal = class {
this.#handlers.splice(this.#handlers.indexOf(cb) >>> 0, 1);
};
}
/**
* Subscribe to the signal.
*
* @param cb The callback function to be called when the signal is updated and also called immediately
* @returns A function to stop the subscription
*/
subscribe(cb) {
cb(this.#val);
return this.onChange(cb);
}
/** Maps the signal to a different signal */
map(fn) {
const signal = new _GroupSignal(fn(this.#val));
this.onChange((val) => signal.update(fn(val)));
return signal;
}
};
function signal(value) {
return new Signal(value);
}

// mod.ts
var registry = {};
Expand Down Expand Up @@ -261,9 +318,10 @@ function unmount(name, el) {
el.dispatchEvent(new CustomEvent(`__unmount__:${name}`));
}
export {
GroupSignal,
Signal,
mount,
register,
signal,
unmount
};
/*! Cell v0.3.6 | Copyright 2024 Yoshiya Hinosawa and Capsule contributors | MIT license */
/*! Cell v0.4.4 | Copyright 2024 Yoshiya Hinosawa and Capsule contributors | MIT license */
4 changes: 2 additions & 2 deletions docs/dist.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*! Cell v0.4.4 | Copyright 2024 Yoshiya Hinosawa and Capsule contributors | MIT license */
import { documentReady, logEvent } from "./util.ts"
export { groupSignal, signal } from "@kt3k/signal"
export { GroupSignal, Signal } from "@kt3k/signal"

interface Initializer {
// deno-lint-ignore no-explicit-any
Expand Down

0 comments on commit 8733687

Please sign in to comment.