Skip to content

Commit 1f96b19

Browse files
feat: add update methods to SharedFunctionsManager, SharedStatesManager, and SharedSubscriptionsManager for modifying shared data
1 parent 5d0da4f commit 1f96b19

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/hooks/use-shared-function.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ export class SharedFunctionsApi extends SharedValuesApi<SharedFunction<unknown>,
5555
}
5656
super.set(key, value, scopeName);
5757
}
58+
update<T, Args extends unknown[], S extends string = string>(key: S, updater: (prev: SharedFunctionValue<T>) => { fnState: SharedFunctionValue<T> }, scopeName?: Prefix): void;
59+
update<T, Args extends unknown[]>(sharedFunctionCreated: SharedFunctionCreated<T, Args>, updater: (prev: SharedFunctionValue<T>) => { fnState: SharedFunctionValue<T> }): void;
60+
update<T, Args extends unknown[], S extends string = string>(key: S | SharedFunctionCreated<T, Args>, updater: (prev: SharedFunctionValue<T>) => { fnState: SharedFunctionValue<T> }, scopeName: Prefix = "_global") {
61+
let prev: SharedFunctionValue<T>;
62+
if (typeof key === "string") {
63+
prev = this.get(key, scopeName);
64+
} else {
65+
prev = this.get(key);
66+
}
67+
const newValue = updater(prev);
68+
if (typeof key === "string") {
69+
this.set(key, newValue, scopeName);
70+
} else {
71+
this.set(key, newValue);
72+
}
73+
}
5874
}
5975

6076
const sharedFunctionsManager = new SharedFunctionsManager();

src/hooks/use-shared-state.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ export class SharedStatesApi extends SharedValuesApi<SharedState<unknown>, { val
4747
}
4848
super.set(key, {value}, scopeName);
4949
}
50+
update<T, S extends string = string>(key: S, updater: (prev: T) => T, scopeName?: Prefix): void;
51+
update<T>(sharedStateCreated: SharedStateCreated<T>, updater: (prev: T) => T): void;
52+
update<T, S extends string = string>(key: S | SharedStateCreated<T>, updater: (prev: T) => T, scopeName: Prefix = "_global") {
53+
let prev: T;
54+
if (typeof key === "string") {
55+
prev = this.get(key, scopeName);
56+
} else {
57+
prev = this.get(key);
58+
}
59+
const newValue = updater(prev);
60+
if (typeof key === "string") {
61+
this.set(key, newValue, scopeName);
62+
} else {
63+
this.set(key, newValue);
64+
}
65+
}
5066
}
5167

5268
const sharedStatesManager = new SharedStatesManager();

src/hooks/use-shared-subscription.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,22 @@ export class SharedSubscriptionsApi extends SharedValuesApi<SharedSubscription<u
9292
}
9393
super.set(key, value, scopeName);
9494
}
95+
update<T, S extends string = string>(key: S, updater: (prev: SharedSubscriptionValue<T>) => { fnState: SharedSubscriptionValue<T> }, scopeName?: Prefix): void;
96+
update<T>(sharedSubscriptionCreated: SharedSubscriptionCreated<T>, updater: (prev: SharedSubscriptionValue<T>) => { fnState: SharedSubscriptionValue<T> }): void;
97+
update<T, S extends string = string>(key: S | SharedSubscriptionCreated<T>, updater: (prev: SharedSubscriptionValue<T>) => { fnState: SharedSubscriptionValue<T> }, scopeName: Prefix = "_global") {
98+
let prev: SharedSubscriptionValue<T>;
99+
if (typeof key === "string") {
100+
prev = this.get(key, scopeName);
101+
} else {
102+
prev = this.get(key);
103+
}
104+
const newValue = updater(prev);
105+
if (typeof key === "string") {
106+
this.set(key, newValue, scopeName);
107+
} else {
108+
this.set(key, newValue);
109+
}
110+
}
95111
}
96112

97113
const sharedSubscriptionsManager = new SharedSubscriptionsManager();

0 commit comments

Comments
 (0)