Skip to content

Commit

Permalink
Adding in the ability to set a custom context on the pullstate instan…
Browse files Browse the repository at this point in the history
…ce, for flexible SSR scenarios
  • Loading branch information
lostpebble committed Dec 3, 2020
1 parent 0cd8792 commit e639d3e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 24 deletions.
18 changes: 13 additions & 5 deletions src/PullstateCore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ export class PullstateSingleton<S extends IPullstateAllStores = IPullstateAllSto
instantiate(
{
hydrateSnapshot,
ssr = false
}: { hydrateSnapshot?: IPullstateSnapshot; ssr?: boolean } = {}): PullstateInstance<S> {
ssr = false,
customContext
}: { hydrateSnapshot?: IPullstateSnapshot; ssr?: boolean, customContext?: any } = {}): PullstateInstance<S> {
if (!ssr) {
const instantiated = new PullstateInstance(clientStores.stores, false);
const instantiated = new PullstateInstance(clientStores.stores, false, customContext);

if (hydrateSnapshot != null) {
instantiated.hydrateFromSnapshot(hydrateSnapshot);
Expand Down Expand Up @@ -106,7 +107,7 @@ export class PullstateSingleton<S extends IPullstateAllStores = IPullstateAllSto
});
}

return new PullstateInstance(newStores as S, true);
return new PullstateInstance(newStores as S, true, customContext);
}

useStores(): S {
Expand Down Expand Up @@ -206,6 +207,7 @@ export interface IPullstateInstanceConsumable<T extends IPullstateAllStores = IP
class PullstateInstance<T extends IPullstateAllStores = IPullstateAllStores>
implements IPullstateInstanceConsumable<T> {
private _ssr: boolean = false;
private _customContext: any;
private readonly _stores: T = {} as T;
_asyncCache: IPullstateAsyncCache = {
listeners: {},
Expand All @@ -214,9 +216,10 @@ class PullstateInstance<T extends IPullstateAllStores = IPullstateAllStores>
actionOrd: {}
};

constructor(allStores: T, ssr: boolean) {
constructor(allStores: T, ssr: boolean, customContext: any) {
this._stores = allStores;
this._ssr = ssr;
this._customContext = customContext;
/*if (!ssr) {
// console.log(`Instantiating Stores`, allStores);
clientStores.stores = allStores;
Expand Down Expand Up @@ -257,6 +260,10 @@ class PullstateInstance<T extends IPullstateAllStores = IPullstateAllStores>
return this._stores;
}

get customContext(): any {
return this._customContext;
}

async runAsyncAction<A, R, X extends string, N>(
asyncAction: IOCreateAsyncActionOutput<A, R, X, N>,
args: A = {} as A,
Expand All @@ -265,6 +272,7 @@ class PullstateInstance<T extends IPullstateAllStores = IPullstateAllStores>
if (this._ssr) {
(runOptions as IAsyncActionRunOptions)._asyncCache = this._asyncCache;
(runOptions as IAsyncActionRunOptions)._stores = this._stores;
(runOptions as IAsyncActionRunOptions)._customContext = this._customContext;
}

return await asyncAction.run(args, runOptions);
Expand Down
4 changes: 3 additions & 1 deletion src/async-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export interface IAsyncActionRunOptions<S extends IPullstateAllStores = any> {
key?: string;
_asyncCache?: IPullstateAsyncCache;
_stores?: S;
_customContext?: any;
}

export interface IAsyncActionGetCachedOptions {
Expand Down Expand Up @@ -254,7 +255,8 @@ export interface IPullstateAsyncCache {

export type TPullstateAsyncAction<A, R, T extends string, N, S extends IPullstateAllStores> = (
args: A,
stores: S
stores: S,
customContext: any
) => Promise<TAsyncActionResult<R, T, N>>;

export interface ICreateAsyncActionOptions<A, R, T extends string, N, S extends IPullstateAllStores> {
Expand Down
72 changes: 54 additions & 18 deletions src/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,11 @@ export function createAsyncActionDirect<A extends any = any,
R extends any = any,
N extends any = any,
S extends IPullstateAllStores = IPullstateAllStores>(
action: (args: A) => Promise<R>,
action: (args: A, stores: S, customContext: any) => Promise<R>,
options: ICreateAsyncActionOptions<A, R, string, N, S> = {}
): IOCreateAsyncActionOutput<A, R, string, N> {
return createAsyncAction<A, R, string, N, S>(async (args: A) => {
return successResult(await action(args));
return createAsyncAction<A, R, string, N, S>(async (args: A, stores: S, customContext: any) => {
return successResult(await action(args, stores, customContext));
}, options);
}

Expand Down Expand Up @@ -300,14 +300,15 @@ further looping. Fix in your cacheBreakHook() is needed.`);
stores: S,
currentActionOrd: number,
postActionEnabled: boolean,
context: EPostActionContext
executionContext: EPostActionContext,
customContext: any
): () => Promise<TAsyncActionResult<R, T, N>> {
return () =>
action(args, stores)
action(args, stores, customContext)
.then((resp) => {
if (currentActionOrd === cache.actionOrd[key]) {
if (postActionEnabled) {
runPostActionHook(resp as TAsyncActionResult<R, T, N>, args, stores, context);
runPostActionHook(resp as TAsyncActionResult<R, T, N>, args, stores, executionContext);
}
cache.results[key] = [true, true, resp, false, Date.now()] as TPullstateAsyncWatchResponse<R, T>;
}
Expand All @@ -327,7 +328,7 @@ further looping. Fix in your cacheBreakHook() is needed.`);

if (currentActionOrd === cache.actionOrd[key]) {
if (postActionEnabled) {
runPostActionHook(result, args, stores, context);
runPostActionHook(result, args, stores, executionContext);
}
cache.results[key] = [true, true, result, false, Date.now()] as TPullstateAsyncWatchResponse<R, T>;
}
Expand Down Expand Up @@ -355,7 +356,8 @@ further looping. Fix in your cacheBreakHook() is needed.`);
fromListener = false,
postActionEnabled = true,
cacheBreakEnabled = true,
holdingResult: TPullstateAsyncWatchResponse<R, T, N> | undefined = undefined
holdingResult: TPullstateAsyncWatchResponse<R, T, N> | undefined,
customContext: any
): TPullstateAsyncWatchResponse<R, T, N> {
const cached = getCachedResult(
key,
Expand Down Expand Up @@ -398,7 +400,8 @@ further looping. Fix in your cacheBreakHook() is needed.`);
stores,
currentActionOrd,
postActionEnabled,
EPostActionContext.BECKON_RUN
EPostActionContext.BECKON_RUN,
customContext
);
}

Expand Down Expand Up @@ -466,12 +469,26 @@ further looping. Fix in your cacheBreakHook() is needed.`);
const key = _createKey(args, customKey);

const cache: IPullstateAsyncCache = onServer ? useContext(PullstateContext)!._asyncCache : clientAsyncCache;
const stores =

let stores: S;
let customContext: any;

if (onServer || forceContext) {
const pullstateContext = useContext(PullstateContext)!;
stores = pullstateContext.stores as S;
customContext = pullstateContext.customContext;
} else if (clientStores.loaded) {
stores = clientStores.stores as S;
} else {
stores = storeErrorProxy as S;
}

/*const stores =
onServer || forceContext
? (useContext(PullstateContext)!.stores as S)
: clientStores.loaded
? (clientStores.stores as S)
: (storeErrorProxy as S);
: (storeErrorProxy as S);*/

const cached = getCachedResult(
key,
Expand Down Expand Up @@ -515,7 +532,8 @@ further looping. Fix in your cacheBreakHook() is needed.`);
stores,
currentActionOrd,
postActionEnabled,
EPostActionContext.READ_RUN
EPostActionContext.READ_RUN,
customContext
);

if (onServer) {
Expand Down Expand Up @@ -580,12 +598,25 @@ further looping. Fix in your cacheBreakHook() is needed.`);
// console.log(`[${key}][${watchId.current}] Starting useWatch()`);

const cache: IPullstateAsyncCache = onServer ? useContext(PullstateContext)!._asyncCache : clientAsyncCache;
const stores =

let stores: S;
let customContext: any;

if (onServer || forceContext) {
const pullstateContext = useContext(PullstateContext)!;
stores = pullstateContext.stores as S;
customContext = pullstateContext.customContext;
} else if (clientStores.loaded) {
stores = clientStores.stores as S;
} else {
stores = storeErrorProxy as S;
}
/*const stores =
onServer || forceContext
? (useContext(PullstateContext)!.stores as S)
: clientStores.loaded
? (clientStores.stores as S)
: (storeErrorProxy as S);
: (storeErrorProxy as S);*/

// only listen for updates when on client
if (!onServer) {
Expand All @@ -609,7 +640,9 @@ further looping. Fix in your cacheBreakHook() is needed.`);
stores,
true,
postActionEnabled,
cacheBreakEnabled
cacheBreakEnabled,
undefined,
customContext
);

setWatchUpdate((prev) => {
Expand Down Expand Up @@ -700,7 +733,8 @@ further looping. Fix in your cacheBreakHook() is needed.`);
cacheBreakEnabled,
// If we want to hold previous and the previous result was finished -
// keep showing that until this new one resolves
holdPrevious && responseRef.current && responseRef.current[1] ? responseRef.current : undefined
holdPrevious && responseRef.current && responseRef.current[1] ? responseRef.current : undefined,
customContext
);
}

Expand Down Expand Up @@ -744,7 +778,8 @@ further looping. Fix in your cacheBreakHook() is needed.`);
respectCache = false,
key: customKey,
_asyncCache = clientAsyncCache,
_stores = clientStores.loaded ? clientStores.stores : (storeErrorProxy as S)
_stores = clientStores.loaded ? clientStores.stores : (storeErrorProxy as S),
_customContext
}: IAsyncActionRunOptions = {}
) => {
const key = _createKey(args, customKey);
Expand Down Expand Up @@ -836,7 +871,8 @@ further looping. Fix in your cacheBreakHook() is needed.`);
_stores,
currentActionOrd,
true,
EPostActionContext.DIRECT_RUN
EPostActionContext.DIRECT_RUN,
_customContext
);

notifyListeners(key);
Expand Down

0 comments on commit e639d3e

Please sign in to comment.