Skip to content

Commit

Permalink
Fix importing the services in SSR context
Browse files Browse the repository at this point in the history
  • Loading branch information
titouanmathis committed Nov 18, 2024
1 parent f4eccd3 commit 69c8d2b
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 10 deletions.
6 changes: 2 additions & 4 deletions packages/js-toolkit/services/AbstractService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { isFunction } from '../utils/index.js';

export interface ServiceInterface<T> {
/**
* Remove a function from the resize service by its key.
Expand All @@ -23,7 +21,7 @@ export interface ServiceInterface<T> {
* Service configuration of events to be attached to targets.
*/
export type ServiceConfig = [
EventTarget | ((instance: AbstractService) => EventTarget),
((instance: AbstractService) => EventTarget),
[string, AddEventListenerOptions?][],
][];

Expand Down Expand Up @@ -147,7 +145,7 @@ export class AbstractService<PropsType = any> {
*/
__manageEvents(mode: 'add' | 'remove') {
for (const [target, events] of this.constructor.config) {
const resolvedTarget = isFunction(target) ? target(this) : target;
const resolvedTarget = target(this);
for (const [type, options] of events) {
resolvedTarget[`${mode}EventListener`](type, this, options);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/js-toolkit/services/DragService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class DragService extends AbstractService<DragServiceProps> {
],
],
[
window,
() => window,
[
['pointerup', PASSIVE_EVENT_OPTIONS],
['touchend', PASSIVE_EVENT_OPTIONS],
Expand Down
2 changes: 1 addition & 1 deletion packages/js-toolkit/services/KeyService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface KeyServiceProps {
export type KeyServiceInterface = ServiceInterface<KeyServiceProps>;

export class KeyService extends AbstractService<KeyServiceProps> {
static config: ServiceConfig = [[document, [['keydown'], ['keyup']]]];
static config: ServiceConfig = [[() => document, [['keydown'], ['keyup']]]];

previousEvent: Event | null = null;

Expand Down
2 changes: 1 addition & 1 deletion packages/js-toolkit/services/LoadService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface LoadServiceProps {
export type LoadServiceInterface = ServiceInterface<LoadServiceProps>;

export class LoadService extends AbstractService<LoadServiceProps> {
static config: ServiceConfig = [[window, [['load']]]];
static config: ServiceConfig = [[() => window, [['load']]]];

props: LoadServiceProps = {
time: performance.now(),
Expand Down
2 changes: 1 addition & 1 deletion packages/js-toolkit/services/PointerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type PointerServiceInterface = ServiceInterface<PointerServiceProps>;
export class PointerService extends AbstractService<PointerServiceProps> {
static config: ServiceConfig = [
[
document,
() => document,
[
['mouseenter', ONCE_CAPTURE_EVENT_OPTIONS],
['mousemove', PASSIVE_CAPTURE_EVENT_OPTIONS],
Expand Down
2 changes: 1 addition & 1 deletion packages/js-toolkit/services/ResizeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type ResizeServiceInterface<U extends Features['breakpoints'] = Features[
export class ResizeService<
T extends Features['breakpoints'] = Features['breakpoints'],
> extends AbstractService<ResizeServiceProps> {
static config: ServiceConfig = [[window, [['resize']]]];
static config: ServiceConfig = [[() => window, [['resize']]]];

breakpoints: T;

Expand Down
2 changes: 1 addition & 1 deletion packages/js-toolkit/services/ScrollService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface ScrollServiceProps {
export type ScrollServiceInterface = ServiceInterface<ScrollServiceProps>;

export class ScrollService extends AbstractService<ScrollServiceProps> {
static config: ServiceConfig = [[document, [['scroll', PASSIVE_CAPTURE_EVENT_OPTIONS]]]];
static config: ServiceConfig = [[() => document, [['scroll', PASSIVE_CAPTURE_EVENT_OPTIONS]]]];

props: ScrollServiceProps = {
x: window.scrollX,
Expand Down

0 comments on commit 69c8d2b

Please sign in to comment.