Skip to content

Commit 05e71fd

Browse files
committed
#15 - Wire in destroy flow
1 parent 1f814da commit 05e71fd

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

src/scripts/core/Mediator.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ const Mediator = function (opts={}) {
122122
if (requestHandler) {
123123
return requestHandler(options);
124124
}
125+
},
126+
127+
destroy () {
128+
requests.handlers = {};
125129
}
126130
};
127131

@@ -152,6 +156,10 @@ const Mediator = function (opts={}) {
152156
if (commandHandler) {
153157
commandHandler(options);
154158
}
159+
},
160+
161+
destroy () {
162+
commands.handlers = {};
155163
}
156164
};
157165

@@ -176,9 +184,18 @@ const Mediator = function (opts={}) {
176184

177185
emit (eventKey, options) {
178186
const eventHandlers = events.getHandlers(eventKey);
187+
179188
if (eventHandlers.length) {
180189
eventHandlers.forEach((eventHandler) => eventHandler(options));
181190
}
191+
192+
if (eventKey === 'app:destroy') {
193+
fn.destroy();
194+
}
195+
},
196+
197+
destroy () {
198+
events.handlers = {};
182199
}
183200
};
184201

@@ -370,6 +387,12 @@ const Mediator = function (opts={}) {
370387

371388
getId () {
372389
return internal.id;
390+
},
391+
392+
destroy () {
393+
requests.destroy();
394+
commands.destroy();
395+
events.destroy();
373396
}
374397
};
375398

src/scripts/core/Module.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ const Module = function (moduleObj) {
112112
moduleUtils.bindDomEvents(handlerMethods, context);
113113
},
114114

115+
deregisterDomHandlers (domHandlersMap, context) {
116+
let handlerMethods = moduleUtils.getHandlerMethods(domHandlersMap, context);
117+
moduleUtils.unbindDomEvents(handlerMethods, context);
118+
},
119+
115120
getHandlerMethods (handlerMap, context) {
116121
let routedHandlers = {};
117122

@@ -171,6 +176,22 @@ const Module = function (moduleObj) {
171176
});
172177
},
173178

179+
unbindDomEvents (handlers, context) {
180+
const { dom } = context;
181+
182+
if (!dom) {
183+
return;
184+
}
185+
186+
Object.keys(handlers).forEach((eventElKey) => {
187+
const [eventKey, elemKey] = eventElKey.split(' @');
188+
const elem = elemKey ? dom[elemKey][0] : dom.el[0];
189+
const eventHandler = handlers[eventElKey];
190+
191+
elem.removeEventListener(eventKey, eventHandler);
192+
});
193+
},
194+
174195
mergeProps (defaultProps, props={}) {
175196
let mergedProps = {};
176197

@@ -248,6 +269,10 @@ const Module = function (moduleObj) {
248269
context.setup();
249270
}
250271

272+
context.mediator.registerHandler('event', 'app:destroy', function () {
273+
moduleProto.destroyModule(opts);
274+
});
275+
251276
if (moduleHandlers) {
252277
moduleUtils.registerHandlers(opts.mediator, moduleHandlers, context);
253278
}
@@ -280,8 +305,12 @@ const Module = function (moduleObj) {
280305
}
281306
},
282307

283-
destroyModule () {
308+
destroyModule (opts) {
309+
const { context } = opts;
284310

311+
if (moduleHandlers.domEvents) {
312+
moduleUtils.deregisterDomHandlers(moduleHandlers.domEvents, context);
313+
}
285314
}
286315
};
287316

src/scripts/modules/Styles.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ const Styles = Module({
1616
stylesheet: null,
1717
},
1818

19-
handlers: {},
19+
handlers: {
20+
events: {
21+
'app:destroy': 'destroy'
22+
}
23+
},
2024

2125
methods: {
2226
setup () {
@@ -85,6 +89,14 @@ const Styles = Module({
8589

8690
updateStylesheet (stylesheetContent) {
8791
this.stylesheet.textContent = stylesheetContent;
92+
},
93+
94+
removeStylesheet () {
95+
document.head.removeChild(this.stylesheet);
96+
},
97+
98+
destroy () {
99+
this.removeStylesheet();
88100
}
89101
}
90102
});

test/server/html/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ <h1>
133133
}
134134
}
135135
});
136-
console.log(typesterInstance);
137136
</script>
138137

139138
<script>

0 commit comments

Comments
 (0)