Socket
Socket
Sign inDemoInstall

@stackflow/plugin-history-sync

Package Overview
Dependencies
13
Maintainers
3
Versions
110
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.3.10-alpha.4 to 1.3.10-alpha.17

1

dist/historyState.d.ts

@@ -6,2 +6,3 @@ import type { Activity, ActivityStep } from "@stackflow/core";

step?: ActivityStep;
silent?: boolean;
}

@@ -8,0 +9,0 @@ export declare function safeParseState(state: unknown): State | null;

@@ -0,1 +1,6 @@

declare global {
interface ProxyConstructor {
new <TSource extends object, TTarget extends object>(target: TSource, handler: ProxyHandler<TSource>): TTarget;
}
}
export {};

75

dist/index.js

@@ -83,3 +83,4 @@ "use strict";

activity: serializeActivity(state.activity),
step: state.step ? serializeStep(state.step) : void 0
step: state.step ? serializeStep(state.step) : void 0,
silent: state.silent
};

@@ -185,20 +186,13 @@ }

var makeQueue = (history) => {
let pending = false;
let previousTask = Promise.resolve();
const queue = (cb) => {
const start = () => {
pending = true;
const clean = history.listen(() => {
clean();
pending = false;
});
cb();
};
if (pending) {
const clean = history.listen(() => {
clean();
start();
});
} else {
start();
}
previousTask = previousTask.then(
() => new Promise((resolve) => {
const clean = history.listen(() => {
clean();
resolve();
});
cb();
})
);
};

@@ -339,2 +333,7 @@ return queue;

const targetStep = historyState.step;
const { silent } = historyState;
if (silent) {
historyState.silent = false;
return;
}
const { activities } = getStack();

@@ -387,4 +386,5 @@ const currentActivity = activities.find(

if ((targetStep == null ? void 0 : targetStep.enteredBy.name) === "StepPushed" || (targetStep == null ? void 0 : targetStep.enteredBy.name) === "StepReplaced") {
const { enteredBy } = targetStep;
pushFlag += 1;
stepPush(__spreadValues({}, targetStep.enteredBy));
stepPush(__spreadValues({}, enteredBy));
}

@@ -395,4 +395,7 @@ }

if (!nextStep && targetStep && ((targetStep == null ? void 0 : targetStep.enteredBy.name) === "StepPushed" || (targetStep == null ? void 0 : targetStep.enteredBy.name) === "StepReplaced")) {
pushFlag += 1;
stepPush(__spreadValues({}, targetStep.enteredBy));
const { enteredBy } = targetStep;
queue(() => {
pushFlag += 1;
stepPush(__spreadValues({}, enteredBy));
});
}

@@ -436,3 +439,4 @@ dispatchEvent("StepPopped", {});

state: {
activity
activity,
silent: true
},

@@ -458,3 +462,4 @@ useHash: options.useHash

activity,
step
step,
silent: true
},

@@ -478,3 +483,4 @@ useHash: options.useHash

state: {
activity
activity,
silent: true
},

@@ -499,3 +505,4 @@ useHash: options.useHash

activity,
step
step,
silent: true
},

@@ -538,6 +545,8 @@ useHash: options.useHash

if (previousActivity) {
popFlag += previousActivity.steps.length - 1;
do {
for (let i = 0; i < previousActivity.steps.length - 1; i += 1) {
queue(history.back);
queue(() => {
popFlag += 1;
history.back();
});
}

@@ -554,4 +563,6 @@ } while (!safeParseState(getCurrentState({ history })));

if (((_a2 = currentActivity == null ? void 0 : currentActivity.steps.length) != null ? _a2 : 0) > 1) {
popFlag += 1;
queue(history.back);
queue(() => {
popFlag += 1;
history.back();
});
}

@@ -567,6 +578,8 @@ },

const popCount = isRoot ? 0 : steps.length;
popFlag += popCount;
do {
for (let i = 0; i < popCount; i += 1) {
queue(history.back);
queue(() => {
popFlag += 1;
history.back();
});
}

@@ -573,0 +586,0 @@ } while (!safeParseState(getCurrentState({ history })));

{
"name": "@stackflow/plugin-history-sync",
"version": "1.3.10-alpha.4+b798553f",
"version": "1.3.10-alpha.17+2141839d",
"license": "MIT",

@@ -84,3 +84,3 @@ "exports": {

},
"gitHead": "b798553f3b1b67004528d6e130abd2201979a367"
"gitHead": "2141839d6514b62dd73723645b7e4684eb84d415"
}

@@ -9,2 +9,3 @@ import type { Activity, ActivityStep } from "@stackflow/core";

step?: ActivityStep;
silent?: boolean;
}

@@ -48,2 +49,3 @@

step: state.step ? serializeStep(state.step) : undefined,
silent: state.silent,
};

@@ -50,0 +52,0 @@ }

@@ -17,5 +17,35 @@ import type {

// 2프레임 + 1프레임
const ENOUGH_DELAY_TIME = 32 + 16;
declare global {
interface ProxyConstructor {
new <TSource extends object, TTarget extends object>(
target: TSource,
handler: ProxyHandler<TSource>,
): TTarget;
}
}
type PromiseProxy<T extends Record<string, (...args: any[]) => any>> = {
[K in keyof T]: (...args: Parameters<T[K]>) => Promise<ReturnType<T[K]>>;
};
const makeActionsProxy = <T extends CoreStore["actions"]>({
actions,
}: {
actions: T;
}): PromiseProxy<T> =>
new Proxy(actions, {
get<K extends keyof CoreStore["actions"]>(target: typeof actions, p: K) {
return (...args: Parameters<typeof target[K]>) =>
new Promise<ReturnType<typeof target[K]>>((resolve) => {
// @ts-ignore
const ret: ReturnType<typeof target[K]> = target[p](...args);
setTimeout(() => {
// @ts-ignore
resolve(p === "getStack" ? target[p](...args) : ret);
}, 16 + 32);
});
},
});
let dt = 0;

@@ -28,8 +58,2 @@

async function delay(ms: number) {
return new Promise<void>((res) => {
setTimeout(res, ms);
});
}
const path = (location: Location) =>

@@ -93,3 +117,3 @@ location.pathname + location.search + location.hash;

let history: MemoryHistory;
let actions: CoreStore["actions"];
let actions: PromiseProxy<CoreStore["actions"]>;

@@ -118,10 +142,12 @@ /**

actions = coreStore.actions;
actions = makeActionsProxy({
actions: coreStore.actions,
});
});
test("historySyncPlugin - 초기에 매칭하는 라우트가 없는 경우 fallbackActivity에 설정한 액티비티의 URL로 이동합니다", () => {
test("historySyncPlugin - 초기에 매칭하는 라우트가 없는 경우 fallbackActivity에 설정한 액티비티의 URL로 이동합니다", async () => {
expect(path(history.location)).toEqual("/home/");
});
test("historySyncPlugin - 초기에 매칭하는 라우트가 있는 경우 해당 액티비티의 URL로 이동합니다", () => {
test("historySyncPlugin - 초기에 매칭하는 라우트가 있는 경우 해당 액티비티의 URL로 이동합니다", async () => {
history = createMemoryHistory({

@@ -149,4 +175,4 @@ initialEntries: ["/articles/123/?title=hello"],

test("historySyncPlugin - actions.push() 후에, URL 상태가 알맞게 바뀝니다", () => {
actions.push({
test("historySyncPlugin - actions.push() 후에, URL 상태가 알맞게 바뀝니다", async () => {
await actions.push({
activityId: "a1",

@@ -163,3 +189,3 @@ activityName: "Article",

test("historySyncPlugin - useHash: true이더라도, actions.push() 후에, URL 상태가 알맞게 바뀝니다", () => {
test("historySyncPlugin - useHash: true이더라도, actions.push() 후에, URL 상태가 알맞게 바뀝니다", async () => {
history = createMemoryHistory();

@@ -182,5 +208,7 @@

actions = coreStore.actions;
actions = makeActionsProxy({
actions: coreStore.actions,
});
actions.push({
await actions.push({
activityId: "a1",

@@ -197,4 +225,4 @@ activityName: "Article",

test("historySyncPlugin - actions.replace() 후에, URL 상태가 알맞게 바뀝니다", () => {
actions.replace({
test("historySyncPlugin - actions.replace() 후에, URL 상태가 알맞게 바뀝니다", async () => {
await actions.replace({
activityId: "a1",

@@ -212,4 +240,4 @@ activityName: "Article",

test("historySyncPlugin - actions.push(), actions.pop()을 여러번 하더라도, URL 상태가 알맞게 바뀝니다", () => {
actions.push({
test("historySyncPlugin - actions.push(), actions.pop()을 여러번 하더라도, URL 상태가 알맞게 바뀝니다", async () => {
await actions.push({
activityId: "a1",

@@ -224,3 +252,3 @@ activityName: "Article",

actions.push({
await actions.push({
activityId: "a2",

@@ -235,3 +263,3 @@ activityName: "Article",

actions.push({
await actions.push({
activityId: "a3",

@@ -247,11 +275,11 @@ activityName: "Article",

actions.pop();
await actions.pop();
expect(path(history.location)).toEqual("/articles/2/?title=hello");
expect(history.index).toEqual(2);
actions.pop();
await actions.pop();
expect(path(history.location)).toEqual("/articles/1/?title=hello");
expect(history.index).toEqual(1);
actions.pop();
await actions.pop();
expect(path(history.location)).toEqual("/home/");

@@ -261,4 +289,4 @@ expect(history.index).toEqual(0);

test("historySyncPlugin - 히스토리를 back하는 경우, 스택 상태가 알맞게 바뀝니다", () => {
actions.push({
test("historySyncPlugin - 히스토리를 back하는 경우, 스택 상태가 알맞게 바뀝니다", async () => {
await actions.push({
activityId: "a1",

@@ -273,3 +301,3 @@ activityName: "Article",

history.back();
expect(activeActivity(actions.getStack())?.name).toEqual("Home");
expect(activeActivity(await actions.getStack())?.name).toEqual("Home");
});

@@ -294,3 +322,3 @@

});
actions.push({
await actions.push({
activityId: "a3",

@@ -305,11 +333,15 @@ activityName: "Article",

history.back();
expect(activeActivity(actions.getStack())?.name).toEqual("Article");
expect(activeActivity(actions.getStack())?.params?.articleId).toEqual("2");
expect(activeActivity(await actions.getStack())?.name).toEqual("Article");
expect(activeActivity(await actions.getStack())?.params?.articleId).toEqual(
"2",
);
history.back();
expect(activeActivity(actions.getStack())?.name).toEqual("Article");
expect(activeActivity(actions.getStack())?.params?.articleId).toEqual("1");
expect(activeActivity(await actions.getStack())?.name).toEqual("Article");
expect(activeActivity(await actions.getStack())?.params?.articleId).toEqual(
"1",
);
history.back();
expect(activeActivity(actions.getStack())?.name).toEqual("Home");
expect(activeActivity(await actions.getStack())?.name).toEqual("Home");
});

@@ -334,3 +366,3 @@

});
actions.push({
await actions.push({
activityId: "a3",

@@ -349,12 +381,18 @@ activityName: "Article",

history.go(1);
expect(activeActivity(actions.getStack())?.name).toEqual("Article");
expect(activeActivity(actions.getStack())?.params?.articleId).toEqual("1");
expect(activeActivity(await actions.getStack())?.name).toEqual("Article");
expect(activeActivity(await actions.getStack())?.params?.articleId).toEqual(
"1",
);
history.go(1);
expect(activeActivity(actions.getStack())?.name).toEqual("Article");
expect(activeActivity(actions.getStack())?.params?.articleId).toEqual("2");
expect(activeActivity(await actions.getStack())?.name).toEqual("Article");
expect(activeActivity(await actions.getStack())?.params?.articleId).toEqual(
"2",
);
history.go(1);
expect(activeActivity(actions.getStack())?.name).toEqual("Article");
expect(activeActivity(actions.getStack())?.params?.articleId).toEqual("3");
expect(activeActivity(await actions.getStack())?.name).toEqual("Article");
expect(activeActivity(await actions.getStack())?.params?.articleId).toEqual(
"3",
);
});

@@ -371,3 +409,3 @@

});
actions.stepPush({
await actions.stepPush({
stepId: "s1",

@@ -382,3 +420,3 @@ stepParams: {

actions.stepPush({
await actions.stepPush({
stepId: "s2",

@@ -393,3 +431,3 @@ stepParams: {

actions.push({
await actions.push({
activityId: "a2",

@@ -405,7 +443,8 @@ activityName: "Article",

actions.pop();
await actions.pop();
expect(path(history.location)).toEqual("/articles/12/?title=hello");
expect(history.index).toEqual(3);
actions.pop();
await actions.pop();
expect(path(history.location)).toEqual("/home/");

@@ -431,3 +470,3 @@ expect(history.index).toEqual(0);

});
actions.stepPush({
await actions.stepPush({
stepId: "s2",

@@ -443,11 +482,11 @@ stepParams: {

actions.stepPop();
await actions.stepPop();
expect(path(history.location)).toEqual("/articles/10/?title=hello");
expect(history.index).toEqual(1);
actions.stepPop();
await actions.stepPop();
expect(path(history.location)).toEqual("/articles/10/?title=hello");
expect(history.index).toEqual(1);
actions.pop();
await actions.pop();
expect(path(history.location)).toEqual("/home/");

@@ -474,3 +513,3 @@ expect(history.index).toEqual(0);

actions.stepReplace({
await actions.stepReplace({
stepId: "s2",

@@ -485,7 +524,7 @@ stepParams: {

actions.stepPop();
await actions.stepPop();
expect(path(history.location)).toEqual("/articles/10/?title=hello");
expect(history.index).toEqual(1);
actions.pop();
await actions.pop();
expect(path(history.location)).toEqual("/home/");

@@ -518,3 +557,3 @@ expect(history.index).toEqual(0);

});
actions.push({
await actions.push({
activityId: "a2",

@@ -529,26 +568,38 @@ activityName: "Article",

history.back();
expect(activeActivity(actions.getStack())?.params.articleId).toEqual("12");
expect(activeActivity(await actions.getStack())?.params.articleId).toEqual(
"12",
);
history.back();
expect(activeActivity(actions.getStack())?.params.articleId).toEqual("11");
expect(activeActivity(await actions.getStack())?.params.articleId).toEqual(
"11",
);
history.back();
expect(activeActivity(actions.getStack())?.params.articleId).toEqual("10");
expect(activeActivity(await actions.getStack())?.params.articleId).toEqual(
"10",
);
history.go(1);
expect(activeActivity(actions.getStack())?.params.articleId).toEqual("11");
expect(activeActivity(await actions.getStack())?.params.articleId).toEqual(
"11",
);
history.go(1);
expect(activeActivity(actions.getStack())?.params.articleId).toEqual("12");
expect(activeActivity(await actions.getStack())?.params.articleId).toEqual(
"12",
);
history.go(1);
expect(activeActivity(actions.getStack())?.params.articleId).toEqual("20");
expect(activeActivity(await actions.getStack())?.params.articleId).toEqual(
"20",
);
actions.pop();
await actions.pop();
expect(path(history.location)).toEqual("/articles/12/?title=hello");
actions.stepPop();
await actions.stepPop();
expect(path(history.location)).toEqual("/articles/11/?title=hello");
actions.pop();
await actions.pop();
expect(path(history.location)).toEqual("/home/");

@@ -558,3 +609,3 @@ expect(history.index).toEqual(0);

test("historySyncPlugin - 여러 행동 후에 새로고침을 하고 히스토리 조작을 하더라도, 스택 상태가 알맞게 바뀝니다", () => {
test("historySyncPlugin - 여러 행동 후에 새로고침을 하고 히스토리 조작을 하더라도, 스택 상태가 알맞게 바뀝니다", async () => {
actions.push({

@@ -604,3 +655,3 @@ activityId: "a1",

});
actions.push({
await actions.push({
activityId: "a3",

@@ -615,3 +666,3 @@ activityName: "Article",

// 새로고침 후
(() => {
await (async () => {
const { actions } = stackflow({

@@ -631,34 +682,48 @@ activityNames: ["Home", "Article"],

const proxyActions = makeActionsProxy({
actions,
});
await proxyActions.getStack();
history.back();
expect(activeActivity(actions.getStack())?.params.articleId).toEqual(
"24",
);
expect(
activeActivity(await proxyActions.getStack())?.params.articleId,
).toEqual("24");
expect(history.index).toEqual(5);
history.back();
expect(activeActivity(actions.getStack())?.params.articleId).toEqual(
"23",
);
expect(
activeActivity(await proxyActions.getStack())?.params.articleId,
).toEqual("23");
expect(history.index).toEqual(4);
history.back();
expect(activeActivity(actions.getStack())?.params.articleId).toEqual(
"21",
);
expect(
activeActivity(await proxyActions.getStack())?.params.articleId,
).toEqual("21");
expect(history.index).toEqual(3);
history.back();
expect(activeActivity(actions.getStack())?.params.articleId).toEqual(
"20",
);
expect(
activeActivity(await proxyActions.getStack())?.params.articleId,
).toEqual("20");
expect(history.index).toEqual(2);
history.back();
expect(activeActivity(actions.getStack())?.params.articleId).toEqual(
"10",
);
expect(
activeActivity(await proxyActions.getStack())?.params.articleId,
).toEqual("10");
expect(history.index).toEqual(1);
history.back();
expect(activeActivity(actions.getStack())?.name).toEqual("Home");
expect(activeActivity(await proxyActions.getStack())?.name).toEqual(
"Home",
);
expect(history.index).toEqual(0);

@@ -698,3 +763,3 @@ })();

actions.replace({
await actions.replace({
activityId: "a3",

@@ -706,12 +771,7 @@ activityName: "ThirdActivity",

});
await delay(ENOUGH_DELAY_TIME);
actions.pop();
await actions.pop();
// 전환이 끝나기까지 충분한 시간
// 코어쪽 추가된 테스트 코드가 Resolve 되면 삭제 가능
await delay(32 + 16);
expect(path(history.location)).toEqual("/home/");
expect(activeActivity(actions.getStack())?.name).toEqual("Home");
expect(activeActivity(await actions.getStack())?.name).toEqual("Home");
});

@@ -763,12 +823,6 @@

});
await delay(ENOUGH_DELAY_TIME);
actions.pop();
// 전환이 끝나기까지 충분한 시간
// 코어쪽 추가된 테스트 코드가 Resolve 되면 삭제 가능
await delay(32 + 16);
await actions.pop();
expect(path(history.location)).toEqual("/articles/1/");
expect(activeActivity(actions.getStack())?.name).toEqual("Article");
expect(activeActivity(await actions.getStack())?.name).toEqual("Article");
});

@@ -823,12 +877,7 @@

});
await delay(ENOUGH_DELAY_TIME);
actions.pop();
await actions.pop();
// 전환이 끝나기까지 충분한 시간
// 코어쪽 추가된 테스트 코드가 Resolve 되면 삭제 가능
await delay(16 * 5);
expect(path(history.location)).toEqual("/home/");
expect(activeActivity(actions.getStack())?.name).toEqual("Home");
expect(activeActivity(await actions.getStack())?.name).toEqual("Home");
});

@@ -873,3 +922,2 @@

});
await delay(ENOUGH_DELAY_TIME);

@@ -897,10 +945,6 @@ actions.stepPush({

actions.pop();
await actions.pop();
// 전환이 끝나기까지 충분한 시간
// 코어쪽 추가된 테스트 코드가 Resolve 되면 삭제 가능
await delay(ENOUGH_DELAY_TIME);
expect(path(history.location)).toEqual("/home/");
expect(activeActivity(actions.getStack())?.name).toEqual("Home");
expect(activeActivity(await actions.getStack())?.name).toEqual("Home");
});

@@ -945,3 +989,2 @@

});
await delay(ENOUGH_DELAY_TIME);

@@ -976,11 +1019,5 @@ actions.stepPush({

});
await delay(ENOUGH_DELAY_TIME);
actions.pop();
// 전환이 끝나기까지 충분한 시간
// 코어쪽 추가된 테스트 코드가 Resolve 되면 삭제 가능
await delay(ENOUGH_DELAY_TIME);
await actions.pop();
expect(path(history.location)).toEqual("/home/");
expect(activeActivity(actions.getStack())?.name).toEqual("Home");
expect(activeActivity(await actions.getStack())?.name).toEqual("Home");
});

@@ -1055,9 +1092,6 @@

});
await delay(ENOUGH_DELAY_TIME);
actions.pop();
await actions.pop();
await delay(ENOUGH_DELAY_TIME);
expect(path(history.location)).toEqual("/home/");
expect(activeActivity(actions.getStack())?.name).toEqual("Home");
expect(activeActivity(await actions.getStack())?.name).toEqual("Home");
});

@@ -1132,6 +1166,4 @@

});
await delay(ENOUGH_DELAY_TIME);
actions.pop();
await delay(ENOUGH_DELAY_TIME);
actions.push({

@@ -1203,9 +1235,6 @@ activityId: "a5",

});
await delay(ENOUGH_DELAY_TIME);
actions.pop();
await actions.pop();
await delay(ENOUGH_DELAY_TIME);
expect(path(history.location)).toEqual("/home/");
expect(activeActivity(actions.getStack())?.name).toEqual("Home");
expect(activeActivity(await actions.getStack())?.name).toEqual("Home");
});

@@ -1266,8 +1295,6 @@

actions.pop();
await actions.pop();
await delay(ENOUGH_DELAY_TIME);
expect(path(history.location)).toEqual("/articles/2/");
expect(activeActivity(actions.getStack())?.name).toEqual("Article");
expect(activeActivity(await actions.getStack())?.name).toEqual("Article");
expect(history.index).toEqual(2);

@@ -1299,3 +1326,3 @@ });

actions.replace({
await actions.replace({
activityId: "a3",

@@ -1308,8 +1335,8 @@ activityName: "ThirdActivity",

await delay(ENOUGH_DELAY_TIME);
expect(path(history.location)).toEqual("/third/1/");
expect(activeActivity(actions.getStack())?.name).toEqual("ThirdActivity");
expect(activeActivity(await actions.getStack())?.name).toEqual(
"ThirdActivity",
);
expect(history.index).toEqual(1);
});
});

@@ -7,23 +7,16 @@ import type { History } from "history";

export const makeQueue = (history: History) => {
let pending = false;
let previousTask = Promise.resolve();
const queue = (cb: () => void) => {
const start = () => {
pending = true;
const clean = history.listen(() => {
clean();
pending = false;
});
previousTask = previousTask.then(
() =>
new Promise<void>((resolve) => {
const clean = history.listen(() => {
clean();
resolve();
});
cb();
};
if (pending) {
const clean = history.listen(() => {
clean();
start();
});
} else {
start();
}
cb();
}),
);
};

@@ -30,0 +23,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc