-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathmakeCoreStore.ts
More file actions
142 lines (124 loc) · 4.05 KB
/
makeCoreStore.ts
File metadata and controls
142 lines (124 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import type { Aggregator } from "./aggregators/Aggregator";
import { SyncAggregator } from "./aggregators/SyncAggregator";
import type { DomainEvent, PushedEvent, StepPushedEvent } from "./event-types";
import { makeEvent } from "./event-utils";
import type { StackflowActions, StackflowPlugin } from "./interfaces";
import { divideBy, once } from "./utils";
import { makeActions } from "./utils/makeActions";
import { ScheduledPublisher } from "./utils/publishers/ScheduledPublisher";
import { SequentialScheduler } from "./utils/schedulers/SequentialScheduler";
import { SwitchScheduler } from "./utils/schedulers/SwitchScheduler";
import { triggerPostEffectHooks } from "./utils/triggerPostEffectHooks";
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 storeListeners: Array<() => void> = [];
const defaultPlugin: StackflowPlugin = () => ({
key: "@stackflow/core",
onChanged() {
storeListeners.forEach((listener) => listener());
},
});
const pluginInstances: ReturnType<StackflowPlugin>[] = [
defaultPlugin(),
...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,
);
const isInitialActivityIgnored =
initialPushedEvents.length > 0 &&
initialPushedEventsByOption.length > 0 &&
initialPushedEvents !== initialPushedEventsByOption;
if (isInitialActivityIgnored) {
options.handlers?.onInitialActivityIgnored?.(initialPushedEvents);
}
if (initialPushedEvents.length === 0) {
options.handlers?.onInitialActivityNotFound?.();
}
const aggregator: Aggregator = new SyncAggregator({
changePublisher: new ScheduledPublisher(new SequentialScheduler()),
updateScheduler: new SwitchScheduler({
scheduler: new SequentialScheduler(),
}),
updateErrorReporter: (error) => {
console.error(error);
},
initialEvents: [...initialRemainingEvents, ...initialPushedEvents],
});
aggregator.subscribeChanges((effects) => {
triggerPostEffectHooks(effects, pluginInstances, actions);
});
const actions: StackflowActions = {
getStack() {
return aggregator.getStack();
},
dispatchEvent(name, params) {
const newEvent = makeEvent(name, params);
aggregator.dispatchEvent(newEvent);
},
push: () => {},
replace: () => {},
pop: () => {},
stepPush: () => {},
stepReplace: () => {},
stepPop: () => {},
pause: () => {},
resume: () => {},
};
// Initialize action methods after actions object is fully created
Object.assign(
actions,
makeActions({
dispatchEvent: actions.dispatchEvent,
pluginInstances,
actions,
}),
);
return {
actions,
init: once(() => {
pluginInstances.forEach((pluginInstance) => {
pluginInstance.onInit?.({
actions,
});
});
}),
pullEvents: () => aggregator.getStack().events,
subscribe(listener) {
storeListeners.push(listener);
return function dispose() {
const listenerIndex = storeListeners.findIndex((l) => l === listener);
if (listenerIndex > -1) {
storeListeners.splice(listenerIndex, 1);
}
};
},
pluginInstances,
};
}