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

chore(*): update internal deps #112

Merged
merged 2 commits into from
May 25, 2024
Merged
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
2 changes: 1 addition & 1 deletion packages/logger/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class GecutLogger {
timeEnd?: (label: string) => void;

sub(domain: string, _devMode = this.devMode) {
return new GecutLogger(`${this.domain} ⬅ ${domain}`, _devMode);
return new GecutLogger(`${this.domain} ⬅ ${domain}`, _devMode);
}

private static stabilizeDomain(domain: string): string {
Expand Down
3 changes: 2 additions & 1 deletion packages/signal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"directory": "packages/signal"
},
"dependencies": {
"@gecut/logger": "workspace:^"
"@gecut/logger": "workspace:^",
"@gecut/types": "workspace:^"
}
}
32 changes: 11 additions & 21 deletions packages/signal/src/libraries/_signal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GecutLogger } from '@gecut/logger';
import {GecutLogger} from '@gecut/logger';

import type { SubscribeOptions, Subscriber } from '../type.js';
import type {SubscribeOptions, Subscriber} from '../type.js';

/**
* A Signal object allows for subscribing to notifications with callbacks.
Expand All @@ -17,7 +17,7 @@ export abstract class Signal<T> {
options: Required<SubscribeOptions>;
}[] = [];

protected value?: T;
protected value: T | undefined;
protected hasDispatched = false;
protected log: GecutLogger;

Expand All @@ -26,11 +26,9 @@ export abstract class Signal<T> {
* @param {Subscriber<T>} callback - The callback to unsubscribe.
*/
unsubscribe(callback: Subscriber<T>): void {
this.log.methodArgs?.('unsubscribe', { callback });
this.log.methodArgs?.('unsubscribe', {callback});

this.subscribers = this.subscribers.filter(
(subscriber) => subscriber.callback !== callback
);
this.subscribers = this.subscribers.filter((subscriber) => subscriber.callback !== callback);
}

/**
Expand All @@ -40,36 +38,28 @@ export abstract class Signal<T> {
* @return {unknown} The `subscribe` method returns an object with a single property `unsubscribe`, which is a
* function that can be called to unsubscribe from the subscription.
*/
subscribe(
callback: Subscriber<T>,
options: SubscribeOptions = {}
): { unsubscribe: () => void } {
subscribe(callback: Subscriber<T>, options: SubscribeOptions = {}): {unsubscribe: () => void} {
const resolvedOptions: Required<SubscribeOptions> = {
once: false,
priority: 0,
disabled: false,
receivePrevious: false,
...options
...options,
};

const newSubscriber = { callback, options: resolvedOptions };
const newSubscriber = {callback, options: resolvedOptions};

this.log.methodArgs?.('subscribe', newSubscriber);

this.subscribers.push(newSubscriber);
this.subscribers.sort((a, b) => b.options.priority - a.options.priority);

if (
resolvedOptions.receivePrevious &&
this.hasDispatched &&
this.value &&
!resolvedOptions.disabled
) {
if (resolvedOptions.receivePrevious && this.hasDispatched && this.value && !resolvedOptions.disabled) {
callback(this.value);
}

return {
unsubscribe: this.unsubscribe.bind(this, callback)
unsubscribe: this.unsubscribe.bind(this, callback),
};
}

Expand Down Expand Up @@ -103,7 +93,7 @@ export abstract class Signal<T> {
this.subscribe(resolve, {
once: true,
priority: 1_000,
receivePrevious: false
receivePrevious: false,
});
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/signal/src/libraries/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export class ContextSignal<T> extends Signal<T> {

const value = this.getValue();

if (value) {
this.setValue(value);
if (this.hasDispatched) {
this.setValue(value as T);
}
}
}
2 changes: 1 addition & 1 deletion packages/signal/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

"include": ["src/**/*.ts"],
"exclude": [],
"references": [{"path": "../logger"}]
"references": [{"path": "../logger"}, {"path": "../types"}]
}
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ __metadata:
resolution: "@gecut/signal@workspace:packages/signal"
dependencies:
"@gecut/logger": "workspace:^"
"@gecut/types": "workspace:^"
languageName: unknown
linkType: soft

Expand Down