Skip to content

Commit

Permalink
fix(core)!: delegate overrideInitialEvents to makeCoreStore (#445)
Browse files Browse the repository at this point in the history
  • Loading branch information
orionmiz authored Jan 30, 2024
1 parent bda5803 commit a32a7e0
Show file tree
Hide file tree
Showing 39 changed files with 444 additions and 206 deletions.
19 changes: 19 additions & 0 deletions .changeset/funny-needles-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
"@stackflow/plugin-map-initial-activity": patch
"@stackflow/plugin-google-analytics-4": patch
"@stackflow/plugin-stack-depth-change": patch
"@stackflow/plugin-renderer-basic": patch
"@stackflow/plugin-history-sync": patch
"@stackflow/plugin-renderer-web": patch
"@stackflow/compat-await-push": patch
"@stackflow/plugin-basic-ui": patch
"@stackflow/plugin-devtools": patch
"@stackflow/plugin-preload": patch
"@stackflow/react": patch
"@stackflow/link": patch
"@stackflow/core": patch
"@stackflow/demo": patch
"@stackflow/docs": patch
---

fix(core)!: delegate overrideInitialEvents to makeCoreStore
26 changes: 26 additions & 0 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"mode": "exit",
"tag": "canary",
"initialVersions": {
"@stackflow/core": "1.0.9",
"@stackflow/demo": "1.2.19",
"@stackflow/docs": "1.2.20",
"@stackflow/compat-await-push": "1.1.6",
"@stackflow/link": "1.3.15",
"@stackflow/plugin-basic-ui": "1.5.1",
"@stackflow/plugin-devtools": "0.1.7",
"@stackflow/plugin-google-analytics-4": "1.1.8",
"@stackflow/plugin-history-sync": "1.3.17",
"@stackflow/plugin-map-initial-activity": "1.0.4",
"@stackflow/plugin-preload": "1.2.14",
"@stackflow/plugin-renderer-basic": "1.1.6",
"@stackflow/plugin-renderer-web": "1.1.6",
"@stackflow/plugin-stack-depth-change": "1.1.1",
"@stackflow/react": "1.1.6",
"@stackflow/esbuild-config": "1.0.1",
"@stackflow/eslint-config": "1.0.2"
},
"changesets": [
"red-lies-poke"
]
}
6 changes: 6 additions & 0 deletions .changeset/red-lies-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@stackflow/react": patch
"@stackflow/core": patch
---

fix(core)!: delegate overrideInitialEvents to makeCoreStore
6 changes: 6 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @stackflow/core

## 1.0.10-canary.0

### Patch Changes

- fix(core)!: delegate overrideInitialEvents to makeCoreStore

## 1.0.9

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stackflow/core",
"version": "1.0.9",
"version": "1.0.10-canary.0",
"license": "MIT",
"exports": {
".": {
Expand Down
59 changes: 48 additions & 11 deletions core/src/makeCoreStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import isEqual from "react-fast-compare";

import { aggregate } from "./aggregate";
import type { Effect } from "./Effect";
import type { DomainEvent } from "./event-types";
import type { DomainEvent, PushedEvent, StepPushedEvent } from "./event-types";
import type { BaseDomainEvent } from "./event-types/_base";
import { makeEvent } from "./event-utils";
import type { StackflowActions, StackflowPlugin } from "./interfaces";
import { produceEffects } from "./produceEffects";
import type { Stack } from "./Stack";
import { once } from "./utils";
import { divideBy, once } from "./utils";

const SECOND = 1000;

Expand All @@ -19,26 +19,25 @@ const INTERVAL_MS = SECOND / 60;

export type MakeCoreStoreOptions = {
initialEvents: DomainEvent[];
initialContext?: any;
plugins: StackflowPlugin[];
handlers?: {
onInitialActivityIgnored?: (
initialPushedEvents: (PushedEvent | StepPushedEvent)[],
) => void;
onInitialActivityNotFound?: () => void;
};
};

export type CoreStore = {
actions: StackflowActions;
init: () => void;
pullEvents: () => DomainEvent[];
subscribe: (listener: () => void) => () => void;
pluginInstances: ReturnType<StackflowPlugin>[];
};

export function makeCoreStore(options: MakeCoreStoreOptions): CoreStore {
const events: {
value: DomainEvent[];
} = {
value: [...options.initialEvents],
};
const stack = {
value: aggregate(events.value, new Date().getTime()),
};

const storeListeners: Array<() => void> = [];

const defaultPlugin: StackflowPlugin = () => ({
Expand All @@ -53,6 +52,43 @@ export function makeCoreStore(options: MakeCoreStoreOptions): CoreStore {
...options.plugins.map((plugin) => plugin()),
];

const [initialPushedEventsByOption, initialRemainingEvents] = divideBy(
options.initialEvents,
(e) => e.name === "Pushed" || e.name === "StepPushed",
);

const initialPushedEvents = pluginInstances.reduce(
(initialEvents, pluginInstance) =>
pluginInstance.overrideInitialEvents?.({
initialEvents,
initialContext: options.initialContext ?? {},
}) ?? initialEvents,
initialPushedEventsByOption as (PushedEvent | StepPushedEvent)[],
);

const isInitialActivityIgnored =
initialPushedEvents.length > 0 &&
initialPushedEventsByOption.length > 0 &&
initialPushedEvents !== initialPushedEventsByOption;

if (isInitialActivityIgnored) {
options.handlers?.onInitialActivityIgnored?.(initialPushedEvents);
}

if (initialPushedEvents.length === 0) {
options.handlers?.onInitialActivityNotFound?.();
}

const events: {
value: DomainEvent[];
} = {
value: [...initialRemainingEvents, ...initialPushedEvents],
};

const stack = {
value: aggregate(events.value, new Date().getTime()),
};

const setStackValue = (nextStackValue: Stack) => {
const effects = produceEffects(stack.value, nextStackValue);

Expand Down Expand Up @@ -358,5 +394,6 @@ export function makeCoreStore(options: MakeCoreStoreOptions): CoreStore {
}
};
},
pluginInstances,
};
}
15 changes: 15 additions & 0 deletions core/src/utils/divideBy.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { makeEvent } from "../event-utils";
import { divideBy } from "./divideBy";

test("divideBy", () => {
const firstEvent = makeEvent("Popped", {});
const secondEvent = makeEvent("StepPopped", {});
const thirdEvent = makeEvent("StepPopped", {});

expect(
divideBy(
[firstEvent, secondEvent, thirdEvent],
(e) => e.name === "StepPopped",
),
).toEqual([[secondEvent, thirdEvent], [firstEvent]]);
});
17 changes: 17 additions & 0 deletions core/src/utils/divideBy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const divideBy = <T>(
arr: T[],
predicate: (t: T) => boolean,
): [T[], T[]] => {
const satisfied: T[] = [];
const unsatisfied: T[] = [];

arr.forEach((element) => {
if (predicate(element)) {
satisfied.push(element);
} else {
unsatisfied.push(element);
}
});

return [satisfied, unsatisfied];
};
1 change: 1 addition & 0 deletions core/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./compareBy";
export * from "./divideBy";
export * from "./findIndices";
export * from "./id";
export * from "./last";
Expand Down
17 changes: 17 additions & 0 deletions demo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# @stackflow/demo

## 1.2.20-canary.0

### Patch Changes

- Updated dependencies
- @stackflow/react@1.1.7-canary.0
- @stackflow/core@1.0.10-canary.0
- @stackflow/compat-await-push@1.1.7-canary.0
- @stackflow/link@1.3.16-canary.0
- @stackflow/plugin-basic-ui@1.5.2-canary.0
- @stackflow/plugin-history-sync@1.3.18-canary.0
- @stackflow/plugin-map-initial-activity@1.0.5-canary.0
- @stackflow/plugin-preload@1.2.15-canary.0
- @stackflow/plugin-renderer-basic@1.1.7-canary.0
- @stackflow/plugin-devtools@0.1.8-canary.0
- @stackflow/plugin-stack-depth-change@1.1.2-canary.0

## 1.2.19

### Patch Changes
Expand Down
24 changes: 12 additions & 12 deletions demo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stackflow/demo",
"version": "1.2.19",
"version": "1.2.20-canary.0",
"private": true,
"license": "MIT",
"exports": {
Expand Down Expand Up @@ -31,18 +31,18 @@
"dependencies": {
"@seed-design/design-token": "^1.0.0-alpha.0",
"@seed-design/stylesheet": "^1.0.0-alpha.0",
"@stackflow/compat-await-push": "^1.1.6",
"@stackflow/core": "^1.0.9",
"@stackflow/compat-await-push": "^1.1.7-canary.0",
"@stackflow/core": "^1.0.10-canary.0",
"@stackflow/eslint-config": "^1.0.2",
"@stackflow/link": "^1.3.15",
"@stackflow/plugin-basic-ui": "^1.4.2",
"@stackflow/plugin-devtools": "^0.1.7",
"@stackflow/plugin-history-sync": "^1.3.14",
"@stackflow/plugin-map-initial-activity": "^1.0.4",
"@stackflow/plugin-preload": "^1.2.14",
"@stackflow/plugin-renderer-basic": "^1.1.6",
"@stackflow/plugin-stack-depth-change": "^1.1.1",
"@stackflow/react": "^1.1.6",
"@stackflow/link": "^1.3.16-canary.0",
"@stackflow/plugin-basic-ui": "^1.5.2-canary.0",
"@stackflow/plugin-devtools": "^0.1.8-canary.0",
"@stackflow/plugin-history-sync": "^1.3.18-canary.0",
"@stackflow/plugin-map-initial-activity": "^1.0.5-canary.0",
"@stackflow/plugin-preload": "^1.2.15-canary.0",
"@stackflow/plugin-renderer-basic": "^1.1.7-canary.0",
"@stackflow/plugin-stack-depth-change": "^1.1.2-canary.0",
"@stackflow/react": "^1.1.7-canary.0",
"@typescript-eslint/eslint-plugin": "^5.32.0",
"@typescript-eslint/parser": "^5.20.0",
"eslint": "^8.13.0",
Expand Down
12 changes: 12 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# @stackflow/docs

## 1.2.21-canary.0

### Patch Changes

- Updated dependencies
- @stackflow/react@1.1.7-canary.0
- @stackflow/core@1.0.10-canary.0
- @stackflow/demo@1.2.20-canary.0
- @stackflow/plugin-basic-ui@1.5.2-canary.0
- @stackflow/plugin-history-sync@1.3.18-canary.0
- @stackflow/plugin-renderer-basic@1.1.7-canary.0

## 1.2.20

### Patch Changes
Expand Down
14 changes: 7 additions & 7 deletions docs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stackflow/docs",
"version": "1.2.20",
"version": "1.2.21-canary.0",
"private": true,
"description": "Mobile-first stack navigator framework with Composable Plugin System",
"license": "MIT",
Expand All @@ -11,13 +11,13 @@
},
"dependencies": {
"@mdx-js/react": "^2.1.1",
"@stackflow/core": "^1.0.9",
"@stackflow/demo": "^1.2.19",
"@stackflow/core": "^1.0.10-canary.0",
"@stackflow/demo": "^1.2.20-canary.0",
"@stackflow/eslint-config": "^1.0.2",
"@stackflow/plugin-basic-ui": "^1.4.2",
"@stackflow/plugin-history-sync": "^1.3.14",
"@stackflow/plugin-renderer-basic": "^1.1.6",
"@stackflow/react": "^1.1.6",
"@stackflow/plugin-basic-ui": "^1.5.2-canary.0",
"@stackflow/plugin-history-sync": "^1.3.18-canary.0",
"@stackflow/plugin-renderer-basic": "^1.1.7-canary.0",
"@stackflow/react": "^1.1.7-canary.0",
"@types/react": "^18.0.15",
"@typescript-eslint/eslint-plugin": "^5.32.0",
"@typescript-eslint/parser": "^5.20.0",
Expand Down
8 changes: 8 additions & 0 deletions extensions/compat-await-push/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @stackflow/compat-await-push

## 1.1.7-canary.0

### Patch Changes

- Updated dependencies
- @stackflow/react@1.1.7-canary.0
- @stackflow/core@1.0.10-canary.0

## 1.1.6

### Patch Changes
Expand Down
10 changes: 5 additions & 5 deletions extensions/compat-await-push/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stackflow/compat-await-push",
"version": "1.1.6",
"version": "1.1.7-canary.0",
"license": "MIT",
"exports": {
".": {
Expand All @@ -26,10 +26,10 @@
"typecheck": "tsc --noEmit"
},
"devDependencies": {
"@stackflow/core": "^1.0.9",
"@stackflow/core": "^1.0.10-canary.0",
"@stackflow/esbuild-config": "^1.0.1",
"@stackflow/eslint-config": "^1.0.2",
"@stackflow/react": "^1.1.6",
"@stackflow/react": "^1.1.7-canary.0",
"@types/react": "^18.0.10",
"@typescript-eslint/eslint-plugin": "^5.32.0",
"@typescript-eslint/parser": "^5.20.0",
Expand All @@ -45,8 +45,8 @@
"typescript": "^4.7.4"
},
"peerDependencies": {
"@stackflow/core": "^1.0.9",
"@stackflow/react": "^1.1.6",
"@stackflow/core": "^1.0.10-canary.0",
"@stackflow/react": "^1.1.7-canary.0",
"@types/react": ">=16.8.0",
"react": ">=16.8.0"
},
Expand Down
10 changes: 10 additions & 0 deletions extensions/link/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# @stackflow/link

## 1.3.16-canary.0

### Patch Changes

- Updated dependencies
- @stackflow/react@1.1.7-canary.0
- @stackflow/core@1.0.10-canary.0
- @stackflow/plugin-history-sync@1.3.18-canary.0
- @stackflow/plugin-preload@1.2.15-canary.0

## 1.3.15

### Patch Changes
Expand Down
Loading

0 comments on commit a32a7e0

Please sign in to comment.