-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsm-element.js
335 lines (335 loc) · 12.4 KB
/
sm-element.js
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import { html, render } from 'lit-html/lit-html';
/**
* @description serializes property to a valid attribute
*/
function serializeAttribute(prop) {
if (typeof prop === 'boolean' || prop === undefined) {
return (prop === true) ? '' : null;
}
else if (typeof prop === 'object') {
throw new Error('cannot serialize object to attribute');
}
return String(prop);
}
class SMElement extends HTMLElement {
constructor() {
super();
this.__data = {};
this.currentState = null;
this.__state = '';
this.states = Object.getPrototypeOf(this).constructor.machine.states;
// setup render target
this.createRenderRoot();
// setup property getter/setters
this.initializeProps_(Object.getPrototypeOf(this).constructor.properties);
// initialze data
this.initializeData_(Object.getPrototypeOf(this).constructor.properties);
}
static get machine() {
// return a basic, single-state machine by default
return {
initial: 'initial',
states: {
initial: {
name: 'initial',
transitions: []
}
}
};
}
static get properties() {
return {};
}
static get observedAttributes() {
const attributes = [];
if (!this.__propNamesAndAttributeNames) {
this.__propNamesAndAttributeNames = new Map();
}
for (let key in this.properties) {
// every String, Number or Boolean property is observed
const type = this.properties[key].type;
if (type === String || type === Number || type === Boolean) {
const aName = key.toLowerCase();
this.__propNamesAndAttributeNames.set(aName, key);
attributes.push(aName);
}
}
return attributes;
}
get state() {
return this.__state;
}
set state(state) {
this.__state = state;
// cannot set attributes unless connected
if (this.isConnected) {
this.setAttribute('state', this.__state);
}
// dispatch state-changed event
this.dispatchEvent(new CustomEvent('state-changed', {
detail: {
state: this.state
}
}));
}
get data() {
return this.__data;
}
set data(newData) {
const props = Object.getPrototypeOf(this).constructor.properties;
// only set props that are defined in 'properties'
const keys = Object.keys(newData);
const update = keys.reduce((acc, k) => {
if (props[k]) {
acc[k] = newData[k];
}
return acc;
}, {});
// hold onto old values for a minute
const oldData = Object.assign({}, this.__data);
// update internal data allowing for partial updates
this.__data = Object.assign({}, this.__data, update);
// reflect any attributes that need to be reflected (reflect === true)
// and dispatch any events (notify === true)
for (let key in update) {
const cprop = props[key];
if (cprop.reflect) {
const attribute = serializeAttribute(update[key]);
if (attribute === null) {
this.removeAttribute(key);
}
else {
this.setAttribute(key.toLowerCase(), attribute);
}
}
if (cprop.notify) {
this.dispatchEvent(new CustomEvent(`${key}-changed`, {
detail: {
value: newData[key],
oldValue: oldData[key]
}
}));
}
// if prop provides an event, send it along with the new value
if (cprop.event && this.currentState) {
this.send(cprop.event, {
value: newData[key],
oldValue: oldData[key]
});
}
// if prop provides an observer, call it
if (cprop.observer && typeof cprop.observer === 'function') {
cprop.observer.call(this, newData[key], oldData[key]);
}
}
this.requestRender();
}
connectedCallback() {
// ensure state attribute is in sync
if (this.getAttribute('state') !== this.__state) {
this.setAttribute('state', this.__state);
}
// set initial state
this.transitionTo_(this.getStateByName_(Object.getPrototypeOf(this).constructor.machine.initial));
// render immediately the first time, so that elements
// can be accessed in connectedCallback()
this.render(this.data);
}
disconnectedCallback() {
// nothing to do here. provided for subclasses calling super.disconnectedCallback
}
createRenderRoot() {
this.root = this.attachShadow({ mode: 'open' });
}
attributeChangedCallback(name, oldVal, newVal) {
const propName = Object.getPrototypeOf(this).constructor.__propNamesAndAttributeNames.get(name);
const type = Object.getPrototypeOf(this).constructor.properties[propName].type || String;
let value;
if (type === Boolean) {
if (newVal === '') {
value = true;
}
else if (newVal === 'false') {
value = false;
}
else {
value = Boolean(newVal);
}
}
else {
value = type(newVal);
}
// only update property if it's changed to prevent infinite loops
// with reflected properties.
const self = this;
if (value !== oldVal && propName && value !== self[propName]) {
self[propName] = value;
}
}
isState(current, desired) {
return Boolean(current && desired && current.name === desired.name);
}
/**
* @description return true if the current state is one of the supplied states
*/
oneOfState(current, ...states) {
return Boolean(states && states.includes(current));
}
/**
* @description reflects the render(data) function of the current state.
*/
currentStateRender(_data) {
return html ``;
}
/**
* @description override in sub classes, defaults to calling the currentStateRender
*/
render(data) {
return this.currentStateRender(data);
}
/**
* @param {!string} eventName
* @param {object=} detail
*/
send(eventName, detail = {}) {
if (!eventName) {
throw new Error('an event name is required to send!');
}
if (!this.currentState) {
throw new Error(`cannot send with no state: ${eventName}`);
}
// find the appropriate transitions in the current state
const transitions = this.currentState.transitions.filter((t) => t.event === eventName);
// no matching transitions in this state
if (transitions.length === 0) {
console.warn(`no transitions found in current state: "${this.state}" for event: "${eventName}"`);
return;
}
// with multiple transitions handling the same event,
// check each transition for conditions and throw an error,
// for now, if any transition does not have a condition.
if (transitions.length > 1 && transitions.filter((t) => !t.condition).length > 0) {
throw new Error(`multiple transitions found without a condition for event: ${eventName} in state: ${this.state}`);
}
// if multiple transitions, run the first one that has a condition that returns true.
if (transitions.length > 1) {
transitions.some((t) => {
const passed = t.condition.call(this, detail);
if (passed) {
const nextState = this.getStateByName_(t.target);
// before running the transition, run it's effect
if (t.effect) {
// update data with return from effect
const newData = t.effect.call(this, detail);
this.data = newData ? newData : this.data;
}
// if there is a nextState, transition to it.
if (nextState) {
// run the first passing transition
this.transitionTo_(nextState);
}
}
return passed; // break out of loop if true, before testing more conditions
});
}
else {
// only one transition, check for condition first
const transition = transitions[0];
const targetState = this.getStateByName_(transition.target);
// no condition, or condition returns true
if (!transition.condition || (transition.condition && transition.condition.call(this, detail))) {
if (transition.effect) {
// update data with return from effect
const newData = transition.effect.call(this, detail);
this.data = newData ? newData : this.data;
}
// if there is a targetState, transition to it.
if (targetState) {
this.transitionTo_(targetState);
}
}
}
}
/**
* @description convenience for setting event listeners that call send
*/
listenAndSend(eventName, detail = {}) {
return () => this.send(eventName, detail);
}
transitionTo_(newState) {
if (!newState) {
throw new Error('transitionTo_ called without a State');
}
// call onExit if exists
if (this.currentState && this.currentState.onExit) {
this.currentState.onExit.call(this, this.data);
}
this.currentState = newState;
this.currentStateRender = newState.render || function () { return html ``; };
// udpate state property
this.state = newState.name;
// call onEntry if it exists
if (newState.onEntry) {
newState.onEntry.call(this, this.data);
}
this.requestRender();
}
getStateByName_(name) {
// using Object.keys.map instead of Object.values, because not every browser
// supports Object.values
return Object.keys(this.states).map((k) => this.states[k]).find(s => s.name === name) || null;
}
initializeData_(properties) {
// flatten properties and assign
this.data = Object.keys(properties).reduce((acc, k) => {
// this happens AFTER props MAY have been set, so use local prop if exists
const self = this;
const local = self[k];
const def = typeof properties[k].value === 'function' ? properties[k].value() : properties[k].value;
acc[k] = local !== undefined ? local : def;
return acc;
}, {});
}
initializeProps_(properties) {
// create getter/setter pairs for each property
const init = (key) => {
Object.defineProperty(this, key, {
get() {
return this.data[key];
},
set(newVal) {
const update = {};
update[key] = newVal;
this.data = update;
},
enumerable: true,
});
};
for (let key in properties) {
init(key);
}
}
/**
* @description request a render on the next animation frame
*/
requestRender() {
if (this.__renderRequest) {
cancelAnimationFrame(this.__renderRequest);
}
this.__renderRequest = requestAnimationFrame(() => {
if (this.root) {
render(this.render(this.data), this.root);
// should send 'rendered'. how would this work if someone wanted to override requestRender?
// can it be a requirement that requestRender return a promise that resolves
// after render is called? probably not, since multiple things call requestRender..?
// ...
}
else {
throw new Error('attempted to render while "this.root" is undefined');
}
});
}
}
;
export default SMElement;
export { html };