-
Notifications
You must be signed in to change notification settings - Fork 0
/
poly.ts
205 lines (179 loc) · 5.67 KB
/
poly.ts
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import morphdom from "morphdom";
import { Browser, RealBrowser } from "./browser";
import { SubscriptionManager } from "./subscription";
import {
BrowserLogger,
Logger,
Config as LoggerConfig,
defaultLoggerConfig,
Domain,
Verbosity,
} from "./logger";
import { Effect, Model, Msg, Page, JsMsg } from "./rust_types";
import { EffectHandler } from "./effect";
import { JsonHelper } from "./json";
import { BrowserLocalStorage, LocalStorage } from "./browser/local_storage";
import { BrowserWindow, Window } from "./browser/window";
import { BrowserHistory, History } from "./browser/history";
import {
Config as CustomEffectConfig,
defaultCustomEffectConfig,
} from "./effect/custom";
import { BrowserDate, Date } from "./browser/date";
import { BrowserConsole, ConsoleInterface } from "./browser/console";
import { BrowserLocation, LocationInterface } from "./browser/location";
import { replacePlaceholder } from "./msg";
import { isObject } from "./util";
import { BrowserClipboard, ClipboardInterface } from "./browser/clipboard";
import { BrowserSessionStorage, SessionStorage } from "./browser/session_storage";
interface Config {
loggerConfig?: LoggerConfig;
customEffectConfig?: CustomEffectConfig;
}
interface State {
model: Model;
}
class Poly {
private readonly appElem: HTMLElement;
private readonly browser: Browser;
private readonly console: ConsoleInterface;
private readonly clipboard: ClipboardInterface;
private readonly window: Window;
private readonly date: Date;
private readonly localStorage: LocalStorage;
private readonly sessionStorage: SessionStorage;
private readonly logger: Logger;
private readonly jsonHelper: JsonHelper;
private readonly history: History;
private readonly location: LocationInterface;
private readonly subscriptionManager: SubscriptionManager;
private readonly effectHandler: EffectHandler;
private readonly state: State = {
model: null,
};
constructor(private readonly page: Page, private readonly config?: Config) {
this.browser = new RealBrowser();
this.console = new BrowserConsole();
this.clipboard = new BrowserClipboard();
const appId = page.id();
const appElem = this.browser.getElementById(appId);
if (!appElem) {
throw new Error(`Could not find element with id '${appId}'`);
}
this.appElem = appElem;
this.window = new BrowserWindow();
this.date = new BrowserDate();
this.localStorage = new BrowserLocalStorage();
this.sessionStorage = new BrowserSessionStorage();
this.logger = new BrowserLogger(
config?.loggerConfig ?? defaultLoggerConfig()
);
this.jsonHelper = new JsonHelper(this.logger);
this.history = new BrowserHistory();
this.location = new BrowserLocation();
this.subscriptionManager = new SubscriptionManager(
this.browser,
this.logger,
(msg: Msg) => {
this.update(msg);
}
);
this.effectHandler = new EffectHandler(
this.config?.customEffectConfig ?? defaultCustomEffectConfig(),
this.browser,
this.console,
this.clipboard,
this.window,
this.date,
this.history,
this.location,
this.localStorage,
this.sessionStorage,
this.jsonHelper,
this.logger,
(msg: Msg) => {
this.update(msg);
},
);
}
public init() {
const { model, effects } = this.page.init();
this.handleModelAndEffects(model, effects);
}
public sendMessage(type: string, data: any) {
this.updateFromJs({ type, data });
}
public onCustomEffect(handler: (effect: any) => void) {
this.effectHandler.setCustomEffectHandler(handler);
}
private updateDom(markup: string) {
morphdom(this.appElem, markup, {
onBeforeElUpdated(fromElem, toElem) {
// Skip elements which has the unmanaged attribute
const isUnmanaged = fromElem.hasAttribute("unmanaged");
if (isUnmanaged) {
return false;
}
return true;
},
});
this.logger.debug({
domain: Domain.Core,
verbosity: Verbosity.Verbose,
message: "Updated DOM with new markup",
context: {
markup: markup,
},
});
}
private async prepareMsg(msg: Msg): Promise<any> {
if (!("effect" in msg)) {
return msg.msg;
}
const effectValue = await this.effectHandler.run(msg.effect, msg.sourceEvent);
if (!isObject(msg.msg)) {
return msg.msg;
}
return replacePlaceholder(msg.msg, effectValue);
}
private async update(msg: Msg) {
const realMsg = await this.prepareMsg(msg);
this.logger.debug({
domain: Domain.Core,
verbosity: Verbosity.Normal,
message: "Sending msg to rust",
context: {
msg: realMsg,
},
});
const { model, effects } = this.page.update(realMsg, this.state.model);
this.handleModelAndEffects(model, effects);
}
private updateFromJs(msg: JsMsg) {
this.logger.debug({
domain: Domain.Core,
verbosity: Verbosity.Normal,
message: "Sending js msg to rust",
context: {
msg,
},
});
const { model, effects } = this.page.updateFromJs(msg, this.state.model);
this.handleModelAndEffects(model, effects);
}
private handleModelAndEffects(model: Model, effects: Effect[]) {
this.logger.debug({
domain: Domain.Core,
verbosity: Verbosity.Normal,
message: "Updating model",
context: { model },
});
this.state.model = model;
const markup = this.page.viewBody(this.state.model);
this.updateDom(markup);
const newSubscriptions = this.page.getSubscriptions(this.state.model);
this.subscriptionManager.handle(newSubscriptions);
this.effectHandler.handle(effects);
}
}
export { Poly, Config };